@mastra/inngest 0.0.0-roamin-openaivoice-speak-options-passing-20250926163614 → 0.0.0-salesman-20260127182805
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 +1869 -3
- package/dist/__tests__/adapters/_utils.d.ts +18 -0
- package/dist/__tests__/adapters/_utils.d.ts.map +1 -0
- package/dist/execution-engine.d.ts +206 -0
- package/dist/execution-engine.d.ts.map +1 -0
- package/dist/index.cjs +2072 -1214
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +82 -285
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2110 -1255
- package/dist/index.js.map +1 -1
- package/dist/pubsub.d.ts +56 -0
- package/dist/pubsub.d.ts.map +1 -0
- package/dist/run.d.ts +175 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/serve.d.ts +76 -0
- package/dist/serve.d.ts.map +1 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/workflow.d.ts +52 -0
- package/dist/workflow.d.ts.map +1 -0
- package/package.json +35 -19
package/dist/index.cjs
CHANGED
|
@@ -1,287 +1,1278 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
3
|
+
var agent = require('@mastra/core/agent');
|
|
4
|
+
var error = require('@mastra/core/error');
|
|
5
|
+
var observability = require('@mastra/core/observability');
|
|
6
|
+
var processors = require('@mastra/core/processors');
|
|
7
7
|
var tools = require('@mastra/core/tools');
|
|
8
8
|
var workflows = require('@mastra/core/workflows');
|
|
9
9
|
var _constants = require('@mastra/core/workflows/_constants');
|
|
10
|
-
var hono = require('inngest/hono');
|
|
11
10
|
var zod = require('zod');
|
|
11
|
+
var crypto$1 = require('crypto');
|
|
12
|
+
var di = require('@mastra/core/di');
|
|
13
|
+
var inngest = require('inngest');
|
|
14
|
+
var realtime = require('@inngest/realtime');
|
|
15
|
+
var events = require('@mastra/core/events');
|
|
16
|
+
var web = require('stream/web');
|
|
17
|
+
var stream = require('@mastra/core/stream');
|
|
18
|
+
var hono = require('inngest/hono');
|
|
12
19
|
|
|
13
20
|
// src/index.ts
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const workflowFunctions = Array.from(
|
|
22
|
-
new Set(
|
|
23
|
-
Object.values(wfs).flatMap((wf) => {
|
|
24
|
-
if (wf instanceof InngestWorkflow) {
|
|
25
|
-
wf.__registerMastra(mastra);
|
|
26
|
-
return wf.getFunctions();
|
|
27
|
-
}
|
|
28
|
-
return [];
|
|
29
|
-
})
|
|
30
|
-
)
|
|
31
|
-
);
|
|
32
|
-
return hono.serve({
|
|
33
|
-
...registerOptions,
|
|
34
|
-
client: inngest,
|
|
35
|
-
functions: [...workflowFunctions, ...userFunctions]
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
var InngestRun = class extends workflows.Run {
|
|
39
|
-
inngest;
|
|
40
|
-
serializedStepGraph;
|
|
41
|
-
#mastra;
|
|
42
|
-
constructor(params, inngest) {
|
|
43
|
-
super(params);
|
|
44
|
-
this.inngest = inngest;
|
|
45
|
-
this.serializedStepGraph = params.serializedStepGraph;
|
|
46
|
-
this.#mastra = params.mastra;
|
|
21
|
+
var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
22
|
+
inngestStep;
|
|
23
|
+
inngestAttempts;
|
|
24
|
+
constructor(mastra, inngestStep, inngestAttempts = 0, options) {
|
|
25
|
+
super({ mastra, options });
|
|
26
|
+
this.inngestStep = inngestStep;
|
|
27
|
+
this.inngestAttempts = inngestAttempts;
|
|
47
28
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
29
|
+
// =============================================================================
|
|
30
|
+
// Hook Overrides
|
|
31
|
+
// =============================================================================
|
|
32
|
+
/**
|
|
33
|
+
* Format errors while preserving Error instances and their custom properties.
|
|
34
|
+
* Uses getErrorFromUnknown to ensure all error properties are preserved.
|
|
35
|
+
*/
|
|
36
|
+
formatResultError(error$1, lastOutput) {
|
|
37
|
+
const outputError = lastOutput?.error;
|
|
38
|
+
const errorSource = error$1 || outputError;
|
|
39
|
+
const errorInstance = error.getErrorFromUnknown(errorSource, {
|
|
40
|
+
serializeStack: true,
|
|
41
|
+
// Include stack in JSON for better debugging in Inngest
|
|
42
|
+
fallbackMessage: "Unknown workflow error"
|
|
53
43
|
});
|
|
54
|
-
|
|
55
|
-
return json.data;
|
|
44
|
+
return errorInstance.toJSON();
|
|
56
45
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (runs?.[0]?.status === "Failed") {
|
|
63
|
-
throw new Error(`Function run ${runs?.[0]?.status}`);
|
|
64
|
-
} else if (runs?.[0]?.status === "Cancelled") {
|
|
65
|
-
const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
|
|
66
|
-
workflowName: this.workflowId,
|
|
67
|
-
runId: this.runId
|
|
68
|
-
});
|
|
69
|
-
return { output: { result: { steps: snapshot?.context, status: "canceled" } } };
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return runs?.[0];
|
|
46
|
+
/**
|
|
47
|
+
* Detect InngestWorkflow instances for special nested workflow handling
|
|
48
|
+
*/
|
|
49
|
+
isNestedWorkflowStep(step) {
|
|
50
|
+
return step instanceof InngestWorkflow;
|
|
73
51
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Inngest requires requestContext serialization for memoization.
|
|
54
|
+
* When steps are replayed, the original function doesn't re-execute,
|
|
55
|
+
* so requestContext modifications must be captured and restored.
|
|
56
|
+
*/
|
|
57
|
+
requiresDurableContextSerialization() {
|
|
58
|
+
return true;
|
|
79
59
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Execute a step with retry logic for Inngest.
|
|
62
|
+
* Retries are handled via step-level retry (RetryAfterError thrown INSIDE step.run()).
|
|
63
|
+
* After retries exhausted, error propagates here and we return a failed result.
|
|
64
|
+
*/
|
|
65
|
+
async executeStepWithRetry(stepId, runStep, params) {
|
|
66
|
+
for (let i = 0; i < params.retries + 1; i++) {
|
|
67
|
+
if (i > 0 && params.delay) {
|
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, params.delay));
|
|
85
69
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
70
|
+
try {
|
|
71
|
+
const result = await this.wrapDurableOperation(stepId, runStep);
|
|
72
|
+
return { ok: true, result };
|
|
73
|
+
} catch (e) {
|
|
74
|
+
if (i === params.retries) {
|
|
75
|
+
const cause = e?.cause;
|
|
76
|
+
if (cause?.status === "failed") {
|
|
77
|
+
params.stepSpan?.error({
|
|
78
|
+
error: e,
|
|
79
|
+
attributes: { status: "failed" }
|
|
80
|
+
});
|
|
81
|
+
if (cause.error && !(cause.error instanceof Error)) {
|
|
82
|
+
cause.error = error.getErrorFromUnknown(cause.error, { serializeStack: false });
|
|
83
|
+
}
|
|
84
|
+
return { ok: false, error: cause };
|
|
85
|
+
}
|
|
86
|
+
const errorInstance = error.getErrorFromUnknown(e, {
|
|
87
|
+
serializeStack: false,
|
|
88
|
+
fallbackMessage: "Unknown step execution error"
|
|
89
|
+
});
|
|
90
|
+
params.stepSpan?.error({
|
|
91
|
+
error: errorInstance,
|
|
92
|
+
attributes: { status: "failed" }
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
ok: false,
|
|
96
|
+
error: {
|
|
97
|
+
status: "failed",
|
|
98
|
+
error: errorInstance,
|
|
99
|
+
endedAt: Date.now()
|
|
100
|
+
}
|
|
101
|
+
};
|
|
99
102
|
}
|
|
100
|
-
}
|
|
103
|
+
}
|
|
101
104
|
}
|
|
105
|
+
return { ok: false, error: { status: "failed", error: new Error("Unknown error"), endedAt: Date.now() } };
|
|
102
106
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Use Inngest's sleep primitive for durability
|
|
109
|
+
*/
|
|
110
|
+
async executeSleepDuration(duration, sleepId, workflowId) {
|
|
111
|
+
await this.inngestStep.sleep(`workflow.${workflowId}.sleep.${sleepId}`, duration < 0 ? 0 : duration);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Use Inngest's sleepUntil primitive for durability
|
|
115
|
+
*/
|
|
116
|
+
async executeSleepUntilDate(date, sleepUntilId, workflowId) {
|
|
117
|
+
await this.inngestStep.sleepUntil(`workflow.${workflowId}.sleepUntil.${sleepUntilId}`, date);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Wrap durable operations in Inngest step.run() for durability.
|
|
121
|
+
*
|
|
122
|
+
* IMPORTANT: Errors are wrapped with a cause structure before throwing.
|
|
123
|
+
* This is necessary because Inngest's error serialization (serialize-error-cjs)
|
|
124
|
+
* only captures standard Error properties (message, name, stack, code, cause).
|
|
125
|
+
* Custom properties like statusCode, responseHeaders from AI SDK errors would
|
|
126
|
+
* be lost. By putting our serialized error (via getErrorFromUnknown with toJSON())
|
|
127
|
+
* in the cause property, we ensure custom properties survive serialization.
|
|
128
|
+
* The cause property is in serialize-error-cjs's allowlist, and when the cause
|
|
129
|
+
* object is finally JSON.stringify'd, our error's toJSON() is called.
|
|
130
|
+
*/
|
|
131
|
+
async wrapDurableOperation(operationId, operationFn) {
|
|
132
|
+
return this.inngestStep.run(operationId, async () => {
|
|
133
|
+
try {
|
|
134
|
+
return await operationFn();
|
|
135
|
+
} catch (e) {
|
|
136
|
+
const errorInstance = error.getErrorFromUnknown(e, {
|
|
137
|
+
serializeStack: false,
|
|
138
|
+
fallbackMessage: "Unknown step execution error"
|
|
139
|
+
});
|
|
140
|
+
throw new Error(errorInstance.message, {
|
|
141
|
+
cause: {
|
|
142
|
+
status: "failed",
|
|
143
|
+
error: errorInstance,
|
|
144
|
+
endedAt: Date.now()
|
|
145
|
+
}
|
|
146
|
+
});
|
|
120
147
|
}
|
|
121
148
|
});
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Provide Inngest step primitive in engine context
|
|
152
|
+
*/
|
|
153
|
+
getEngineContext() {
|
|
154
|
+
return { step: this.inngestStep };
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* For Inngest, lifecycle callbacks are invoked in the workflow's finalize step
|
|
158
|
+
* (wrapped in step.run for durability), not in execute(). Override to skip.
|
|
159
|
+
*/
|
|
160
|
+
async invokeLifecycleCallbacks(_result) {
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Actually invoke the lifecycle callbacks. Called from workflow.ts finalize step.
|
|
164
|
+
*/
|
|
165
|
+
async invokeLifecycleCallbacksInternal(result) {
|
|
166
|
+
return super.invokeLifecycleCallbacks(result);
|
|
167
|
+
}
|
|
168
|
+
// =============================================================================
|
|
169
|
+
// Durable Span Lifecycle Hooks
|
|
170
|
+
// =============================================================================
|
|
171
|
+
/**
|
|
172
|
+
* Create a step span durably - on first execution, creates and exports span.
|
|
173
|
+
* On replay, returns cached span data without re-creating.
|
|
174
|
+
*/
|
|
175
|
+
async createStepSpan(params) {
|
|
176
|
+
const { executionContext, operationId, options, parentSpan } = params;
|
|
177
|
+
const parentSpanId = parentSpan?.id ?? executionContext.tracingIds?.workflowSpanId;
|
|
178
|
+
const exportedSpan = await this.wrapDurableOperation(operationId, async () => {
|
|
179
|
+
const observability = this.mastra?.observability?.getSelectedInstance({});
|
|
180
|
+
if (!observability) return void 0;
|
|
181
|
+
const span = observability.startSpan({
|
|
182
|
+
...options,
|
|
183
|
+
entityType: options.entityType,
|
|
184
|
+
traceId: executionContext.tracingIds?.traceId,
|
|
185
|
+
parentSpanId
|
|
186
|
+
});
|
|
187
|
+
return span?.exportSpan();
|
|
130
188
|
});
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
const runOutput = await this.getRunOutput(eventId);
|
|
136
|
-
const result = runOutput?.output?.result;
|
|
137
|
-
if (result.status === "failed") {
|
|
138
|
-
result.error = new Error(result.error);
|
|
139
|
-
}
|
|
140
|
-
if (result.status !== "suspended") {
|
|
141
|
-
this.cleanup?.();
|
|
189
|
+
if (exportedSpan) {
|
|
190
|
+
const observability = this.mastra?.observability?.getSelectedInstance({});
|
|
191
|
+
return observability?.rebuildSpan(exportedSpan);
|
|
142
192
|
}
|
|
143
|
-
return
|
|
193
|
+
return void 0;
|
|
144
194
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
195
|
+
/**
|
|
196
|
+
* End a step span durably.
|
|
197
|
+
*/
|
|
198
|
+
async endStepSpan(params) {
|
|
199
|
+
const { span, operationId, endOptions } = params;
|
|
200
|
+
if (!span) return;
|
|
201
|
+
await this.wrapDurableOperation(operationId, async () => {
|
|
202
|
+
span.end(endOptions);
|
|
152
203
|
});
|
|
153
|
-
this.executionResults = p;
|
|
154
|
-
return p;
|
|
155
204
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const suspendedStep = this.workflowSteps[steps?.[0] ?? ""];
|
|
165
|
-
const resumeDataToUse = await this._validateResumeData(params.resumeData, suspendedStep);
|
|
166
|
-
const eventOutput = await this.inngest.send({
|
|
167
|
-
name: `workflow.${this.workflowId}`,
|
|
168
|
-
data: {
|
|
169
|
-
inputData: resumeDataToUse,
|
|
170
|
-
runId: this.runId,
|
|
171
|
-
workflowId: this.workflowId,
|
|
172
|
-
stepResults: snapshot?.context,
|
|
173
|
-
resume: {
|
|
174
|
-
steps,
|
|
175
|
-
stepResults: snapshot?.context,
|
|
176
|
-
resumePayload: resumeDataToUse,
|
|
177
|
-
// @ts-ignore
|
|
178
|
-
resumePath: snapshot?.suspendedPaths?.[steps?.[0]]
|
|
179
|
-
}
|
|
180
|
-
}
|
|
205
|
+
/**
|
|
206
|
+
* Record error on step span durably.
|
|
207
|
+
*/
|
|
208
|
+
async errorStepSpan(params) {
|
|
209
|
+
const { span, operationId, errorOptions } = params;
|
|
210
|
+
if (!span) return;
|
|
211
|
+
await this.wrapDurableOperation(operationId, async () => {
|
|
212
|
+
span.error(errorOptions);
|
|
181
213
|
});
|
|
182
|
-
const eventId = eventOutput.ids[0];
|
|
183
|
-
if (!eventId) {
|
|
184
|
-
throw new Error("Event ID is not set");
|
|
185
|
-
}
|
|
186
|
-
const runOutput = await this.getRunOutput(eventId);
|
|
187
|
-
const result = runOutput?.output?.result;
|
|
188
|
-
if (result.status === "failed") {
|
|
189
|
-
result.error = new Error(result.error);
|
|
190
|
-
}
|
|
191
|
-
return result;
|
|
192
214
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
return () => {
|
|
208
|
-
active = false;
|
|
209
|
-
streamPromise.then(async (stream) => {
|
|
210
|
-
return stream.cancel();
|
|
211
|
-
}).catch((err) => {
|
|
212
|
-
console.error(err);
|
|
215
|
+
/**
|
|
216
|
+
* Create a generic child span durably (for control-flow operations).
|
|
217
|
+
* On first execution, creates and exports span. On replay, returns cached span data.
|
|
218
|
+
*/
|
|
219
|
+
async createChildSpan(params) {
|
|
220
|
+
const { executionContext, operationId, options, parentSpan } = params;
|
|
221
|
+
const parentSpanId = parentSpan?.id ?? executionContext.tracingIds?.workflowSpanId;
|
|
222
|
+
const exportedSpan = await this.wrapDurableOperation(operationId, async () => {
|
|
223
|
+
const observability = this.mastra?.observability?.getSelectedInstance({});
|
|
224
|
+
if (!observability) return void 0;
|
|
225
|
+
const span = observability.startSpan({
|
|
226
|
+
...options,
|
|
227
|
+
traceId: executionContext.tracingIds?.traceId,
|
|
228
|
+
parentSpanId
|
|
213
229
|
});
|
|
214
|
-
|
|
230
|
+
return span?.exportSpan();
|
|
231
|
+
});
|
|
232
|
+
if (exportedSpan) {
|
|
233
|
+
const observability = this.mastra?.observability?.getSelectedInstance({});
|
|
234
|
+
return observability?.rebuildSpan(exportedSpan);
|
|
235
|
+
}
|
|
236
|
+
return void 0;
|
|
215
237
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
};
|
|
225
|
-
await writer.write(e);
|
|
226
|
-
} catch {
|
|
227
|
-
}
|
|
228
|
-
}, "watch-v2");
|
|
229
|
-
this.closeStreamAction = async () => {
|
|
230
|
-
unwatch();
|
|
231
|
-
try {
|
|
232
|
-
await writer.close();
|
|
233
|
-
} catch (err) {
|
|
234
|
-
console.error("Error closing stream:", err);
|
|
235
|
-
} finally {
|
|
236
|
-
writer.releaseLock();
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
this.executionResults = this.start({ inputData, runtimeContext }).then((result) => {
|
|
240
|
-
if (result.status !== "suspended") {
|
|
241
|
-
this.closeStreamAction?.().catch(() => {
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
return result;
|
|
238
|
+
/**
|
|
239
|
+
* End a generic child span durably (for control-flow operations).
|
|
240
|
+
*/
|
|
241
|
+
async endChildSpan(params) {
|
|
242
|
+
const { span, operationId, endOptions } = params;
|
|
243
|
+
if (!span) return;
|
|
244
|
+
await this.wrapDurableOperation(operationId, async () => {
|
|
245
|
+
span.end(endOptions);
|
|
245
246
|
});
|
|
246
|
-
return {
|
|
247
|
-
stream: readable,
|
|
248
|
-
getWorkflowState: () => this.executionResults
|
|
249
|
-
};
|
|
250
247
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
const flowControlEntries = Object.entries({ concurrency, rateLimit, throttle, debounce, priority }).filter(
|
|
261
|
-
([_, value]) => value !== void 0
|
|
262
|
-
);
|
|
263
|
-
this.flowControlConfig = flowControlEntries.length > 0 ? Object.fromEntries(flowControlEntries) : void 0;
|
|
264
|
-
this.#mastra = params.mastra;
|
|
265
|
-
this.inngest = inngest;
|
|
248
|
+
/**
|
|
249
|
+
* Record error on a generic child span durably (for control-flow operations).
|
|
250
|
+
*/
|
|
251
|
+
async errorChildSpan(params) {
|
|
252
|
+
const { span, operationId, errorOptions } = params;
|
|
253
|
+
if (!span) return;
|
|
254
|
+
await this.wrapDurableOperation(operationId, async () => {
|
|
255
|
+
span.error(errorOptions);
|
|
256
|
+
});
|
|
266
257
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
258
|
+
/**
|
|
259
|
+
* Execute nested InngestWorkflow using inngestStep.invoke() for durability.
|
|
260
|
+
* This MUST be called directly (not inside step.run()) due to Inngest constraints.
|
|
261
|
+
*/
|
|
262
|
+
async executeWorkflowStep(params) {
|
|
263
|
+
if (!(params.step instanceof InngestWorkflow)) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
const {
|
|
267
|
+
step,
|
|
268
|
+
stepResults,
|
|
269
|
+
executionContext,
|
|
270
|
+
resume,
|
|
271
|
+
timeTravel,
|
|
272
|
+
prevOutput,
|
|
273
|
+
inputData,
|
|
274
|
+
pubsub,
|
|
275
|
+
startedAt,
|
|
276
|
+
perStep,
|
|
277
|
+
stepSpan
|
|
278
|
+
} = params;
|
|
279
|
+
const nestedTracingContext = executionContext.tracingIds?.traceId ? {
|
|
280
|
+
traceId: executionContext.tracingIds.traceId,
|
|
281
|
+
parentSpanId: stepSpan?.id
|
|
282
|
+
} : void 0;
|
|
283
|
+
const isResume = !!resume?.steps?.length;
|
|
284
|
+
let result;
|
|
285
|
+
let runId;
|
|
286
|
+
const isTimeTravel = !!(timeTravel && timeTravel.steps?.length > 1 && timeTravel.steps[0] === step.id);
|
|
287
|
+
try {
|
|
288
|
+
if (isResume) {
|
|
289
|
+
runId = stepResults[resume?.steps?.[0] ?? ""]?.suspendPayload?.__workflow_meta?.runId ?? crypto$1.randomUUID();
|
|
290
|
+
const workflowsStore = await this.mastra?.getStorage()?.getStore("workflows");
|
|
291
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
292
|
+
workflowName: step.id,
|
|
293
|
+
runId
|
|
294
|
+
});
|
|
295
|
+
const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
|
|
296
|
+
function: step.getFunction(),
|
|
297
|
+
data: {
|
|
298
|
+
inputData,
|
|
299
|
+
initialState: executionContext.state ?? snapshot?.value ?? {},
|
|
300
|
+
runId,
|
|
301
|
+
resume: {
|
|
302
|
+
runId,
|
|
303
|
+
steps: resume.steps.slice(1),
|
|
304
|
+
stepResults: snapshot?.context,
|
|
305
|
+
resumePayload: resume.resumePayload,
|
|
306
|
+
resumePath: resume.steps?.[1] ? snapshot?.suspendedPaths?.[resume.steps?.[1]] : void 0
|
|
307
|
+
},
|
|
308
|
+
outputOptions: { includeState: true },
|
|
309
|
+
perStep,
|
|
310
|
+
tracingOptions: nestedTracingContext
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
result = invokeResp.result;
|
|
314
|
+
runId = invokeResp.runId;
|
|
315
|
+
executionContext.state = invokeResp.result.state;
|
|
316
|
+
} else if (isTimeTravel) {
|
|
317
|
+
const workflowsStoreForTimeTravel = await this.mastra?.getStorage()?.getStore("workflows");
|
|
318
|
+
const snapshot = await workflowsStoreForTimeTravel?.loadWorkflowSnapshot({
|
|
319
|
+
workflowName: step.id,
|
|
320
|
+
runId: executionContext.runId
|
|
321
|
+
}) ?? { context: {} };
|
|
322
|
+
const timeTravelParams = workflows.createTimeTravelExecutionParams({
|
|
323
|
+
steps: timeTravel.steps.slice(1),
|
|
324
|
+
inputData: timeTravel.inputData,
|
|
325
|
+
resumeData: timeTravel.resumeData,
|
|
326
|
+
context: timeTravel.nestedStepResults?.[step.id] ?? {},
|
|
327
|
+
nestedStepsContext: timeTravel.nestedStepResults ?? {},
|
|
328
|
+
snapshot,
|
|
329
|
+
graph: step.buildExecutionGraph()
|
|
330
|
+
});
|
|
331
|
+
const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
|
|
332
|
+
function: step.getFunction(),
|
|
333
|
+
data: {
|
|
334
|
+
timeTravel: timeTravelParams,
|
|
335
|
+
initialState: executionContext.state ?? {},
|
|
336
|
+
runId: executionContext.runId,
|
|
337
|
+
outputOptions: { includeState: true },
|
|
338
|
+
perStep,
|
|
339
|
+
tracingOptions: nestedTracingContext
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
result = invokeResp.result;
|
|
343
|
+
runId = invokeResp.runId;
|
|
344
|
+
executionContext.state = invokeResp.result.state;
|
|
345
|
+
} else {
|
|
346
|
+
const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
|
|
347
|
+
function: step.getFunction(),
|
|
348
|
+
data: {
|
|
349
|
+
inputData,
|
|
350
|
+
initialState: executionContext.state ?? {},
|
|
351
|
+
outputOptions: { includeState: true },
|
|
352
|
+
perStep,
|
|
353
|
+
tracingOptions: nestedTracingContext
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
result = invokeResp.result;
|
|
357
|
+
runId = invokeResp.runId;
|
|
358
|
+
executionContext.state = invokeResp.result.state;
|
|
359
|
+
}
|
|
360
|
+
} catch (e) {
|
|
361
|
+
const errorCause = e?.cause;
|
|
362
|
+
if (errorCause && typeof errorCause === "object") {
|
|
363
|
+
result = errorCause;
|
|
364
|
+
runId = errorCause.runId || crypto$1.randomUUID();
|
|
365
|
+
} else {
|
|
366
|
+
runId = crypto$1.randomUUID();
|
|
367
|
+
result = {
|
|
368
|
+
status: "failed",
|
|
369
|
+
error: e instanceof Error ? e : new Error(String(e)),
|
|
370
|
+
steps: {},
|
|
371
|
+
input: inputData
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
const res = await this.inngestStep.run(
|
|
376
|
+
`workflow.${executionContext.workflowId}.step.${step.id}.nestedwf-results`,
|
|
377
|
+
async () => {
|
|
378
|
+
if (result.status === "failed") {
|
|
379
|
+
await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
|
|
380
|
+
type: "watch",
|
|
381
|
+
runId: executionContext.runId,
|
|
382
|
+
data: {
|
|
383
|
+
type: "workflow-step-result",
|
|
384
|
+
payload: {
|
|
385
|
+
id: step.id,
|
|
386
|
+
status: "failed",
|
|
387
|
+
error: result?.error,
|
|
388
|
+
payload: prevOutput
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
return { executionContext, result: { status: "failed", error: result?.error, endedAt: Date.now() } };
|
|
393
|
+
} else if (result.status === "suspended") {
|
|
394
|
+
const suspendedSteps = Object.entries(result.steps).filter(([_stepName, stepResult]) => {
|
|
395
|
+
const stepRes = stepResult;
|
|
396
|
+
return stepRes?.status === "suspended";
|
|
397
|
+
});
|
|
398
|
+
for (const [stepName, stepResult] of suspendedSteps) {
|
|
399
|
+
const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
|
|
400
|
+
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
401
|
+
await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
|
|
402
|
+
type: "watch",
|
|
403
|
+
runId: executionContext.runId,
|
|
404
|
+
data: {
|
|
405
|
+
type: "workflow-step-suspended",
|
|
406
|
+
payload: {
|
|
407
|
+
id: step.id,
|
|
408
|
+
status: "suspended"
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
return {
|
|
413
|
+
executionContext,
|
|
414
|
+
result: {
|
|
415
|
+
status: "suspended",
|
|
416
|
+
suspendedAt: Date.now(),
|
|
417
|
+
payload: stepResult.payload,
|
|
418
|
+
suspendPayload: {
|
|
419
|
+
...stepResult?.suspendPayload,
|
|
420
|
+
__workflow_meta: { runId, path: suspendPath }
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
return {
|
|
426
|
+
executionContext,
|
|
427
|
+
result: {
|
|
428
|
+
status: "suspended",
|
|
429
|
+
suspendedAt: Date.now(),
|
|
430
|
+
payload: {}
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
} else if (result.status === "tripwire") {
|
|
434
|
+
await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
|
|
435
|
+
type: "watch",
|
|
436
|
+
runId: executionContext.runId,
|
|
437
|
+
data: {
|
|
438
|
+
type: "workflow-step-result",
|
|
439
|
+
payload: {
|
|
440
|
+
id: step.id,
|
|
441
|
+
status: "tripwire",
|
|
442
|
+
error: result?.tripwire?.reason,
|
|
443
|
+
payload: prevOutput
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
return {
|
|
448
|
+
executionContext,
|
|
449
|
+
result: {
|
|
450
|
+
status: "tripwire",
|
|
451
|
+
tripwire: result?.tripwire,
|
|
452
|
+
endedAt: Date.now()
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
} else if (perStep || result.status === "paused") {
|
|
456
|
+
await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
|
|
457
|
+
type: "watch",
|
|
458
|
+
runId: executionContext.runId,
|
|
459
|
+
data: {
|
|
460
|
+
type: "workflow-step-result",
|
|
461
|
+
payload: {
|
|
462
|
+
id: step.id,
|
|
463
|
+
status: "paused"
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
|
|
468
|
+
type: "watch",
|
|
469
|
+
runId: executionContext.runId,
|
|
470
|
+
data: {
|
|
471
|
+
type: "workflow-step-finish",
|
|
472
|
+
payload: {
|
|
473
|
+
id: step.id,
|
|
474
|
+
metadata: {}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
return { executionContext, result: { status: "paused" } };
|
|
479
|
+
}
|
|
480
|
+
await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
|
|
481
|
+
type: "watch",
|
|
482
|
+
runId: executionContext.runId,
|
|
483
|
+
data: {
|
|
484
|
+
type: "workflow-step-result",
|
|
485
|
+
payload: {
|
|
486
|
+
id: step.id,
|
|
487
|
+
status: "success",
|
|
488
|
+
output: result?.result
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
|
|
493
|
+
type: "watch",
|
|
494
|
+
runId: executionContext.runId,
|
|
495
|
+
data: {
|
|
496
|
+
type: "workflow-step-finish",
|
|
497
|
+
payload: {
|
|
498
|
+
id: step.id,
|
|
499
|
+
metadata: {}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
return { executionContext, result: { status: "success", output: result?.result, endedAt: Date.now() } };
|
|
504
|
+
}
|
|
505
|
+
);
|
|
506
|
+
Object.assign(executionContext, res.executionContext);
|
|
507
|
+
return {
|
|
508
|
+
...res.result,
|
|
509
|
+
startedAt,
|
|
510
|
+
payload: inputData,
|
|
511
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
512
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
var InngestPubSub = class extends events.PubSub {
|
|
517
|
+
inngest;
|
|
518
|
+
workflowId;
|
|
519
|
+
publishFn;
|
|
520
|
+
subscriptions = /* @__PURE__ */ new Map();
|
|
521
|
+
constructor(inngest, workflowId, publishFn) {
|
|
522
|
+
super();
|
|
523
|
+
this.inngest = inngest;
|
|
524
|
+
this.workflowId = workflowId;
|
|
525
|
+
this.publishFn = publishFn;
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Publish an event to Inngest's realtime system.
|
|
529
|
+
*
|
|
530
|
+
* Topic format: "workflow.events.v2.{runId}"
|
|
531
|
+
* Maps to Inngest channel: "workflow:{workflowId}:{runId}"
|
|
532
|
+
*/
|
|
533
|
+
async publish(topic, event) {
|
|
534
|
+
if (!this.publishFn) {
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const match = topic.match(/^workflow\.events\.v2\.(.+)$/);
|
|
538
|
+
if (!match) {
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
const runId = match[1];
|
|
542
|
+
try {
|
|
543
|
+
await this.publishFn({
|
|
544
|
+
channel: `workflow:${this.workflowId}:${runId}`,
|
|
545
|
+
topic: "watch",
|
|
546
|
+
data: event.data
|
|
547
|
+
});
|
|
548
|
+
} catch (err) {
|
|
549
|
+
console.error("InngestPubSub publish error:", err?.message ?? err);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Subscribe to events from Inngest's realtime system.
|
|
554
|
+
*
|
|
555
|
+
* Topic format: "workflow.events.v2.{runId}"
|
|
556
|
+
* Maps to Inngest channel: "workflow:{workflowId}:{runId}"
|
|
557
|
+
*/
|
|
558
|
+
async subscribe(topic, cb) {
|
|
559
|
+
const match = topic.match(/^workflow\.events\.v2\.(.+)$/);
|
|
560
|
+
if (!match || !match[1]) {
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
const runId = match[1];
|
|
564
|
+
if (this.subscriptions.has(topic)) {
|
|
565
|
+
this.subscriptions.get(topic).callbacks.add(cb);
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
const callbacks = /* @__PURE__ */ new Set([cb]);
|
|
569
|
+
const channel = `workflow:${this.workflowId}:${runId}`;
|
|
570
|
+
const streamPromise = realtime.subscribe(
|
|
571
|
+
{
|
|
572
|
+
channel,
|
|
573
|
+
topics: ["watch"],
|
|
574
|
+
app: this.inngest
|
|
575
|
+
},
|
|
576
|
+
(message) => {
|
|
577
|
+
const event = {
|
|
578
|
+
id: crypto.randomUUID(),
|
|
579
|
+
type: "watch",
|
|
580
|
+
runId,
|
|
581
|
+
data: message.data,
|
|
582
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
583
|
+
};
|
|
584
|
+
for (const callback of callbacks) {
|
|
585
|
+
callback(event);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
);
|
|
589
|
+
this.subscriptions.set(topic, {
|
|
590
|
+
unsubscribe: () => {
|
|
591
|
+
streamPromise.then((stream) => stream.cancel()).catch((err) => {
|
|
592
|
+
console.error("InngestPubSub unsubscribe error:", err);
|
|
593
|
+
});
|
|
594
|
+
},
|
|
595
|
+
callbacks
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Unsubscribe a callback from a topic.
|
|
600
|
+
* If no callbacks remain, the underlying Inngest subscription is cancelled.
|
|
601
|
+
*/
|
|
602
|
+
async unsubscribe(topic, cb) {
|
|
603
|
+
const sub = this.subscriptions.get(topic);
|
|
604
|
+
if (!sub) {
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
sub.callbacks.delete(cb);
|
|
608
|
+
if (sub.callbacks.size === 0) {
|
|
609
|
+
sub.unsubscribe();
|
|
610
|
+
this.subscriptions.delete(topic);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Flush any pending operations. No-op for Inngest.
|
|
615
|
+
*/
|
|
616
|
+
async flush() {
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Clean up all subscriptions during graceful shutdown.
|
|
620
|
+
*/
|
|
621
|
+
async close() {
|
|
622
|
+
for (const [, sub] of this.subscriptions) {
|
|
623
|
+
sub.unsubscribe();
|
|
624
|
+
}
|
|
625
|
+
this.subscriptions.clear();
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
var InngestRun = class extends workflows.Run {
|
|
629
|
+
inngest;
|
|
630
|
+
serializedStepGraph;
|
|
631
|
+
#mastra;
|
|
632
|
+
constructor(params, inngest) {
|
|
633
|
+
super(params);
|
|
634
|
+
this.inngest = inngest;
|
|
635
|
+
this.serializedStepGraph = params.serializedStepGraph;
|
|
636
|
+
this.#mastra = params.mastra;
|
|
637
|
+
}
|
|
638
|
+
async getRuns(eventId) {
|
|
639
|
+
const maxRetries = 3;
|
|
640
|
+
let lastError = null;
|
|
641
|
+
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
642
|
+
try {
|
|
643
|
+
const response = await fetch(
|
|
644
|
+
`${this.inngest.apiBaseUrl ?? "https://api.inngest.com"}/v1/events/${eventId}/runs`,
|
|
645
|
+
{
|
|
646
|
+
headers: {
|
|
647
|
+
Authorization: `Bearer ${process.env.INNGEST_SIGNING_KEY}`
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
);
|
|
651
|
+
if (response.status === 429) {
|
|
652
|
+
const retryAfter = parseInt(response.headers.get("retry-after") || "2", 10);
|
|
653
|
+
await new Promise((resolve) => setTimeout(resolve, retryAfter * 1e3));
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
if (!response.ok) {
|
|
657
|
+
throw new Error(`Inngest API error: ${response.status} ${response.statusText}`);
|
|
658
|
+
}
|
|
659
|
+
const text = await response.text();
|
|
660
|
+
if (!text) {
|
|
661
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3 * (attempt + 1)));
|
|
662
|
+
continue;
|
|
663
|
+
}
|
|
664
|
+
const json = JSON.parse(text);
|
|
665
|
+
return json.data;
|
|
666
|
+
} catch (error) {
|
|
667
|
+
lastError = error;
|
|
668
|
+
if (attempt < maxRetries - 1) {
|
|
669
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3 * Math.pow(2, attempt)));
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
throw new inngest.NonRetriableError(`Failed to get runs after ${maxRetries} attempts: ${lastError?.message}`);
|
|
674
|
+
}
|
|
675
|
+
async getRunOutput(eventId, maxWaitMs = 3e5) {
|
|
676
|
+
const startTime = Date.now();
|
|
677
|
+
const storage = this.#mastra?.getStorage();
|
|
678
|
+
const workflowsStore = await storage?.getStore("workflows");
|
|
679
|
+
while (Date.now() - startTime < maxWaitMs) {
|
|
680
|
+
let runs;
|
|
681
|
+
try {
|
|
682
|
+
runs = await this.getRuns(eventId);
|
|
683
|
+
} catch (error) {
|
|
684
|
+
if (error instanceof inngest.NonRetriableError) {
|
|
685
|
+
throw error;
|
|
686
|
+
}
|
|
687
|
+
throw new inngest.NonRetriableError(
|
|
688
|
+
`Failed to poll workflow status: ${error instanceof Error ? error.message : String(error)}`
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
if (runs?.[0]?.status === "Completed" && runs?.[0]?.event_id === eventId) {
|
|
692
|
+
return runs[0];
|
|
693
|
+
}
|
|
694
|
+
if (runs?.[0]?.status === "Failed") {
|
|
695
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
696
|
+
workflowName: this.workflowId,
|
|
697
|
+
runId: this.runId
|
|
698
|
+
});
|
|
699
|
+
if (snapshot?.context) {
|
|
700
|
+
snapshot.context = workflows.hydrateSerializedStepErrors(snapshot.context);
|
|
701
|
+
}
|
|
702
|
+
return {
|
|
703
|
+
output: {
|
|
704
|
+
result: {
|
|
705
|
+
steps: snapshot?.context,
|
|
706
|
+
status: "failed",
|
|
707
|
+
// Get the original error from NonRetriableError's cause (which contains the workflow result)
|
|
708
|
+
error: error.getErrorFromUnknown(runs?.[0]?.output?.cause?.error, { serializeStack: false })
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
if (runs?.[0]?.status === "Cancelled") {
|
|
714
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
715
|
+
workflowName: this.workflowId,
|
|
716
|
+
runId: this.runId
|
|
717
|
+
});
|
|
718
|
+
return { output: { result: { steps: snapshot?.context, status: "canceled" } } };
|
|
719
|
+
}
|
|
720
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3 + Math.random() * 1e3));
|
|
721
|
+
}
|
|
722
|
+
throw new inngest.NonRetriableError(`Workflow did not complete within ${maxWaitMs}ms`);
|
|
723
|
+
}
|
|
724
|
+
async cancel() {
|
|
725
|
+
const storage = this.#mastra?.getStorage();
|
|
726
|
+
await this.inngest.send({
|
|
727
|
+
name: `cancel.workflow.${this.workflowId}`,
|
|
728
|
+
data: {
|
|
729
|
+
runId: this.runId
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
const workflowsStore = await storage?.getStore("workflows");
|
|
733
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
734
|
+
workflowName: this.workflowId,
|
|
735
|
+
runId: this.runId
|
|
736
|
+
});
|
|
737
|
+
if (snapshot) {
|
|
738
|
+
await workflowsStore?.persistWorkflowSnapshot({
|
|
739
|
+
workflowName: this.workflowId,
|
|
740
|
+
runId: this.runId,
|
|
741
|
+
resourceId: this.resourceId,
|
|
742
|
+
snapshot: {
|
|
743
|
+
...snapshot,
|
|
744
|
+
status: "canceled",
|
|
745
|
+
value: snapshot.value
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
async start(args) {
|
|
751
|
+
return this._start(args);
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Starts the workflow execution without waiting for completion (fire-and-forget).
|
|
755
|
+
* Returns immediately with the runId after sending the event to Inngest.
|
|
756
|
+
* The workflow executes independently in Inngest.
|
|
757
|
+
* Use this when you don't need to wait for the result or want to avoid polling failures.
|
|
758
|
+
*/
|
|
759
|
+
async startAsync(args) {
|
|
760
|
+
const workflowsStore = await this.#mastra.getStorage()?.getStore("workflows");
|
|
761
|
+
await workflowsStore?.persistWorkflowSnapshot({
|
|
762
|
+
workflowName: this.workflowId,
|
|
763
|
+
runId: this.runId,
|
|
764
|
+
resourceId: this.resourceId,
|
|
765
|
+
snapshot: {
|
|
766
|
+
runId: this.runId,
|
|
767
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
768
|
+
status: "running",
|
|
769
|
+
value: {},
|
|
770
|
+
context: {},
|
|
771
|
+
activePaths: [],
|
|
772
|
+
suspendedPaths: {},
|
|
773
|
+
activeStepsPath: {},
|
|
774
|
+
resumeLabels: {},
|
|
775
|
+
waitingPaths: {},
|
|
776
|
+
timestamp: Date.now()
|
|
777
|
+
}
|
|
778
|
+
});
|
|
779
|
+
const inputDataToUse = await this._validateInput(args.inputData);
|
|
780
|
+
const initialStateToUse = await this._validateInitialState(args.initialState ?? {});
|
|
781
|
+
const eventOutput = await this.inngest.send({
|
|
782
|
+
name: `workflow.${this.workflowId}`,
|
|
783
|
+
data: {
|
|
784
|
+
inputData: inputDataToUse,
|
|
785
|
+
initialState: initialStateToUse,
|
|
786
|
+
runId: this.runId,
|
|
787
|
+
resourceId: this.resourceId,
|
|
788
|
+
outputOptions: args.outputOptions,
|
|
789
|
+
tracingOptions: args.tracingOptions,
|
|
790
|
+
requestContext: args.requestContext ? Object.fromEntries(args.requestContext.entries()) : {},
|
|
791
|
+
perStep: args.perStep
|
|
792
|
+
}
|
|
793
|
+
});
|
|
794
|
+
const eventId = eventOutput.ids[0];
|
|
795
|
+
if (!eventId) {
|
|
796
|
+
throw new Error("Event ID is not set");
|
|
797
|
+
}
|
|
798
|
+
return { runId: this.runId };
|
|
799
|
+
}
|
|
800
|
+
async _start({
|
|
801
|
+
inputData,
|
|
802
|
+
initialState,
|
|
803
|
+
outputOptions,
|
|
804
|
+
tracingOptions,
|
|
805
|
+
format,
|
|
806
|
+
requestContext,
|
|
807
|
+
perStep
|
|
808
|
+
}) {
|
|
809
|
+
const workflowsStore = await this.#mastra.getStorage()?.getStore("workflows");
|
|
810
|
+
await workflowsStore?.persistWorkflowSnapshot({
|
|
811
|
+
workflowName: this.workflowId,
|
|
812
|
+
runId: this.runId,
|
|
813
|
+
resourceId: this.resourceId,
|
|
814
|
+
snapshot: {
|
|
815
|
+
runId: this.runId,
|
|
816
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
817
|
+
status: "running",
|
|
818
|
+
value: {},
|
|
819
|
+
context: {},
|
|
820
|
+
activePaths: [],
|
|
821
|
+
suspendedPaths: {},
|
|
822
|
+
activeStepsPath: {},
|
|
823
|
+
resumeLabels: {},
|
|
824
|
+
waitingPaths: {},
|
|
825
|
+
timestamp: Date.now()
|
|
826
|
+
}
|
|
827
|
+
});
|
|
828
|
+
const inputDataToUse = await this._validateInput(inputData);
|
|
829
|
+
const initialStateToUse = await this._validateInitialState(initialState ?? {});
|
|
830
|
+
const eventOutput = await this.inngest.send({
|
|
831
|
+
name: `workflow.${this.workflowId}`,
|
|
832
|
+
data: {
|
|
833
|
+
inputData: inputDataToUse,
|
|
834
|
+
initialState: initialStateToUse,
|
|
835
|
+
runId: this.runId,
|
|
836
|
+
resourceId: this.resourceId,
|
|
837
|
+
outputOptions,
|
|
838
|
+
tracingOptions,
|
|
839
|
+
format,
|
|
840
|
+
requestContext: requestContext ? Object.fromEntries(requestContext.entries()) : {},
|
|
841
|
+
perStep
|
|
842
|
+
}
|
|
843
|
+
});
|
|
844
|
+
const eventId = eventOutput.ids[0];
|
|
845
|
+
if (!eventId) {
|
|
846
|
+
throw new Error("Event ID is not set");
|
|
847
|
+
}
|
|
848
|
+
const runOutput = await this.getRunOutput(eventId);
|
|
849
|
+
const result = runOutput?.output?.result;
|
|
850
|
+
this.hydrateFailedResult(result);
|
|
851
|
+
if (result.status !== "suspended") {
|
|
852
|
+
this.cleanup?.();
|
|
853
|
+
}
|
|
854
|
+
return result;
|
|
855
|
+
}
|
|
856
|
+
async resume(params) {
|
|
857
|
+
const p = this._resume(params).then((result) => {
|
|
858
|
+
if (result.status !== "suspended") {
|
|
859
|
+
this.closeStreamAction?.().catch(() => {
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
return result;
|
|
863
|
+
});
|
|
864
|
+
this.executionResults = p;
|
|
865
|
+
return p;
|
|
866
|
+
}
|
|
867
|
+
async _resume(params) {
|
|
868
|
+
const storage = this.#mastra?.getStorage();
|
|
869
|
+
let steps = [];
|
|
870
|
+
if (typeof params.step === "string") {
|
|
871
|
+
steps = params.step.split(".");
|
|
872
|
+
} else {
|
|
873
|
+
steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
|
|
874
|
+
(step) => typeof step === "string" ? step : step?.id
|
|
875
|
+
);
|
|
876
|
+
}
|
|
877
|
+
const workflowsStore = await storage?.getStore("workflows");
|
|
878
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
879
|
+
workflowName: this.workflowId,
|
|
880
|
+
runId: this.runId
|
|
881
|
+
});
|
|
882
|
+
const suspendedStep = this.workflowSteps[steps?.[0] ?? ""];
|
|
883
|
+
const resumeDataToUse = await this._validateResumeData(params.resumeData, suspendedStep);
|
|
884
|
+
const persistedRequestContext = snapshot?.requestContext ?? {};
|
|
885
|
+
const newRequestContext = params.requestContext ? Object.fromEntries(params.requestContext.entries()) : {};
|
|
886
|
+
const mergedRequestContext = { ...persistedRequestContext, ...newRequestContext };
|
|
887
|
+
const eventOutput = await this.inngest.send({
|
|
888
|
+
name: `workflow.${this.workflowId}`,
|
|
889
|
+
data: {
|
|
890
|
+
inputData: resumeDataToUse,
|
|
891
|
+
initialState: snapshot?.value ?? {},
|
|
892
|
+
runId: this.runId,
|
|
893
|
+
workflowId: this.workflowId,
|
|
894
|
+
stepResults: snapshot?.context,
|
|
895
|
+
resume: {
|
|
896
|
+
steps,
|
|
897
|
+
stepResults: snapshot?.context,
|
|
898
|
+
resumePayload: resumeDataToUse,
|
|
899
|
+
resumePath: steps?.[0] ? snapshot?.suspendedPaths?.[steps?.[0]] : void 0
|
|
900
|
+
},
|
|
901
|
+
requestContext: mergedRequestContext,
|
|
902
|
+
perStep: params.perStep
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
const eventId = eventOutput.ids[0];
|
|
906
|
+
if (!eventId) {
|
|
907
|
+
throw new Error("Event ID is not set");
|
|
908
|
+
}
|
|
909
|
+
const runOutput = await this.getRunOutput(eventId);
|
|
910
|
+
const result = runOutput?.output?.result;
|
|
911
|
+
this.hydrateFailedResult(result);
|
|
912
|
+
return result;
|
|
913
|
+
}
|
|
914
|
+
async timeTravel(params) {
|
|
915
|
+
const p = this._timeTravel(params).then((result) => {
|
|
916
|
+
if (result.status !== "suspended") {
|
|
917
|
+
this.closeStreamAction?.().catch(() => {
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
return result;
|
|
921
|
+
});
|
|
922
|
+
this.executionResults = p;
|
|
923
|
+
return p;
|
|
924
|
+
}
|
|
925
|
+
async _timeTravel(params) {
|
|
926
|
+
if (!params.step || Array.isArray(params.step) && params.step?.length === 0) {
|
|
927
|
+
throw new Error("Step is required and must be a valid step or array of steps");
|
|
928
|
+
}
|
|
929
|
+
let steps = [];
|
|
930
|
+
if (typeof params.step === "string") {
|
|
931
|
+
steps = params.step.split(".");
|
|
932
|
+
} else {
|
|
933
|
+
steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
|
|
934
|
+
(step) => typeof step === "string" ? step : step?.id
|
|
935
|
+
);
|
|
936
|
+
}
|
|
937
|
+
if (steps.length === 0) {
|
|
938
|
+
throw new Error("No steps provided to timeTravel");
|
|
939
|
+
}
|
|
940
|
+
const storage = this.#mastra?.getStorage();
|
|
941
|
+
const workflowsStore = await storage?.getStore("workflows");
|
|
942
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
943
|
+
workflowName: this.workflowId,
|
|
944
|
+
runId: this.runId
|
|
945
|
+
});
|
|
946
|
+
if (!snapshot) {
|
|
947
|
+
await workflowsStore?.persistWorkflowSnapshot({
|
|
948
|
+
workflowName: this.workflowId,
|
|
949
|
+
runId: this.runId,
|
|
950
|
+
resourceId: this.resourceId,
|
|
951
|
+
snapshot: {
|
|
952
|
+
runId: this.runId,
|
|
953
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
954
|
+
status: "pending",
|
|
955
|
+
value: {},
|
|
956
|
+
context: {},
|
|
957
|
+
activePaths: [],
|
|
958
|
+
suspendedPaths: {},
|
|
959
|
+
activeStepsPath: {},
|
|
960
|
+
resumeLabels: {},
|
|
961
|
+
waitingPaths: {},
|
|
962
|
+
timestamp: Date.now()
|
|
963
|
+
}
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
if (snapshot?.status === "running") {
|
|
967
|
+
throw new Error("This workflow run is still running, cannot time travel");
|
|
968
|
+
}
|
|
969
|
+
let inputDataToUse = params.inputData;
|
|
970
|
+
if (inputDataToUse && steps.length === 1) {
|
|
971
|
+
inputDataToUse = await this._validateTimetravelInputData(params.inputData, this.workflowSteps[steps[0]]);
|
|
972
|
+
}
|
|
973
|
+
const timeTravelData = workflows.createTimeTravelExecutionParams({
|
|
974
|
+
steps,
|
|
975
|
+
inputData: inputDataToUse,
|
|
976
|
+
resumeData: params.resumeData,
|
|
977
|
+
context: params.context,
|
|
978
|
+
nestedStepsContext: params.nestedStepsContext,
|
|
979
|
+
snapshot: snapshot ?? { context: {} },
|
|
980
|
+
graph: this.executionGraph,
|
|
981
|
+
initialState: params.initialState,
|
|
982
|
+
perStep: params.perStep
|
|
983
|
+
});
|
|
984
|
+
const eventOutput = await this.inngest.send({
|
|
985
|
+
name: `workflow.${this.workflowId}`,
|
|
986
|
+
data: {
|
|
987
|
+
initialState: timeTravelData.state,
|
|
988
|
+
runId: this.runId,
|
|
989
|
+
workflowId: this.workflowId,
|
|
990
|
+
stepResults: timeTravelData.stepResults,
|
|
991
|
+
timeTravel: timeTravelData,
|
|
992
|
+
tracingOptions: params.tracingOptions,
|
|
993
|
+
outputOptions: params.outputOptions,
|
|
994
|
+
requestContext: params.requestContext ? Object.fromEntries(params.requestContext.entries()) : {},
|
|
995
|
+
perStep: params.perStep
|
|
996
|
+
}
|
|
997
|
+
});
|
|
998
|
+
const eventId = eventOutput.ids[0];
|
|
999
|
+
if (!eventId) {
|
|
1000
|
+
throw new Error("Event ID is not set");
|
|
1001
|
+
}
|
|
1002
|
+
const runOutput = await this.getRunOutput(eventId);
|
|
1003
|
+
const result = runOutput?.output?.result;
|
|
1004
|
+
this.hydrateFailedResult(result);
|
|
1005
|
+
return result;
|
|
1006
|
+
}
|
|
1007
|
+
watch(cb) {
|
|
1008
|
+
let active = true;
|
|
1009
|
+
const streamPromise = realtime.subscribe(
|
|
1010
|
+
{
|
|
1011
|
+
channel: `workflow:${this.workflowId}:${this.runId}`,
|
|
1012
|
+
topics: ["watch"],
|
|
1013
|
+
app: this.inngest
|
|
1014
|
+
},
|
|
1015
|
+
(message) => {
|
|
1016
|
+
if (active) {
|
|
1017
|
+
cb(message.data);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
);
|
|
1021
|
+
return () => {
|
|
1022
|
+
active = false;
|
|
1023
|
+
streamPromise.then(async (stream) => {
|
|
1024
|
+
return stream.cancel();
|
|
1025
|
+
}).catch((err) => {
|
|
1026
|
+
console.error(err);
|
|
1027
|
+
});
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
1030
|
+
streamLegacy({ inputData, requestContext } = {}) {
|
|
1031
|
+
const { readable, writable } = new TransformStream();
|
|
1032
|
+
const writer = writable.getWriter();
|
|
1033
|
+
void writer.write({
|
|
1034
|
+
// @ts-expect-error - stream event type mismatch
|
|
1035
|
+
type: "start",
|
|
1036
|
+
payload: { runId: this.runId }
|
|
1037
|
+
});
|
|
1038
|
+
const unwatch = this.watch(async (event) => {
|
|
1039
|
+
try {
|
|
1040
|
+
const e = {
|
|
1041
|
+
...event,
|
|
1042
|
+
type: event.type.replace("workflow-", "")
|
|
1043
|
+
};
|
|
1044
|
+
if (e.type === "step-output") {
|
|
1045
|
+
e.type = e.payload.output.type;
|
|
1046
|
+
e.payload = e.payload.output.payload;
|
|
1047
|
+
}
|
|
1048
|
+
await writer.write(e);
|
|
1049
|
+
} catch {
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
this.closeStreamAction = async () => {
|
|
1053
|
+
await writer.write({
|
|
1054
|
+
type: "finish",
|
|
1055
|
+
// @ts-expect-error - stream event type mismatch
|
|
1056
|
+
payload: { runId: this.runId }
|
|
1057
|
+
});
|
|
1058
|
+
unwatch();
|
|
1059
|
+
try {
|
|
1060
|
+
await writer.close();
|
|
1061
|
+
} catch (err) {
|
|
1062
|
+
console.error("Error closing stream:", err);
|
|
1063
|
+
} finally {
|
|
1064
|
+
writer.releaseLock();
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
this.executionResults = this._start({ inputData, requestContext, format: "legacy" }).then((result) => {
|
|
1068
|
+
if (result.status !== "suspended") {
|
|
1069
|
+
this.closeStreamAction?.().catch(() => {
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
return result;
|
|
1073
|
+
});
|
|
1074
|
+
return {
|
|
1075
|
+
stream: readable,
|
|
1076
|
+
getWorkflowState: () => this.executionResults
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
stream({
|
|
1080
|
+
inputData,
|
|
1081
|
+
requestContext,
|
|
1082
|
+
tracingOptions,
|
|
1083
|
+
closeOnSuspend = true,
|
|
1084
|
+
initialState,
|
|
1085
|
+
outputOptions,
|
|
1086
|
+
perStep
|
|
1087
|
+
} = {}) {
|
|
1088
|
+
if (this.closeStreamAction && this.streamOutput) {
|
|
1089
|
+
return this.streamOutput;
|
|
1090
|
+
}
|
|
1091
|
+
this.closeStreamAction = async () => {
|
|
1092
|
+
};
|
|
1093
|
+
const self = this;
|
|
1094
|
+
const stream$1 = new web.ReadableStream({
|
|
1095
|
+
async start(controller) {
|
|
1096
|
+
const unwatch = self.watch(async ({ type, from = stream.ChunkFrom.WORKFLOW, payload }) => {
|
|
1097
|
+
controller.enqueue({
|
|
1098
|
+
type,
|
|
1099
|
+
runId: self.runId,
|
|
1100
|
+
from,
|
|
1101
|
+
payload: {
|
|
1102
|
+
stepName: payload?.id,
|
|
1103
|
+
...payload
|
|
1104
|
+
}
|
|
1105
|
+
});
|
|
1106
|
+
});
|
|
1107
|
+
self.closeStreamAction = async () => {
|
|
1108
|
+
unwatch();
|
|
1109
|
+
try {
|
|
1110
|
+
await controller.close();
|
|
1111
|
+
} catch (err) {
|
|
1112
|
+
console.error("Error closing stream:", err);
|
|
1113
|
+
}
|
|
1114
|
+
};
|
|
1115
|
+
const executionResultsPromise = self._start({
|
|
1116
|
+
inputData,
|
|
1117
|
+
requestContext,
|
|
1118
|
+
// tracingContext, // We are not able to pass a reference to a span here, what to do?
|
|
1119
|
+
initialState,
|
|
1120
|
+
tracingOptions,
|
|
1121
|
+
outputOptions,
|
|
1122
|
+
format: "vnext",
|
|
1123
|
+
perStep
|
|
1124
|
+
});
|
|
1125
|
+
let executionResults;
|
|
1126
|
+
try {
|
|
1127
|
+
executionResults = await executionResultsPromise;
|
|
1128
|
+
if (closeOnSuspend) {
|
|
1129
|
+
self.closeStreamAction?.().catch(() => {
|
|
1130
|
+
});
|
|
1131
|
+
} else if (executionResults.status !== "suspended") {
|
|
1132
|
+
self.closeStreamAction?.().catch(() => {
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
if (self.streamOutput) {
|
|
1136
|
+
self.streamOutput.updateResults(
|
|
1137
|
+
executionResults
|
|
1138
|
+
);
|
|
1139
|
+
}
|
|
1140
|
+
} catch (err) {
|
|
1141
|
+
self.streamOutput?.rejectResults(err);
|
|
1142
|
+
self.closeStreamAction?.().catch(() => {
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
});
|
|
1147
|
+
this.streamOutput = new stream.WorkflowRunOutput({
|
|
1148
|
+
runId: this.runId,
|
|
1149
|
+
workflowId: this.workflowId,
|
|
1150
|
+
stream: stream$1
|
|
1151
|
+
});
|
|
1152
|
+
return this.streamOutput;
|
|
1153
|
+
}
|
|
1154
|
+
timeTravelStream({
|
|
1155
|
+
inputData,
|
|
1156
|
+
resumeData,
|
|
1157
|
+
initialState,
|
|
1158
|
+
step,
|
|
1159
|
+
context,
|
|
1160
|
+
nestedStepsContext,
|
|
1161
|
+
requestContext,
|
|
1162
|
+
// tracingContext,
|
|
1163
|
+
tracingOptions,
|
|
1164
|
+
outputOptions,
|
|
1165
|
+
perStep
|
|
1166
|
+
}) {
|
|
1167
|
+
this.closeStreamAction = async () => {
|
|
1168
|
+
};
|
|
1169
|
+
const self = this;
|
|
1170
|
+
const stream$1 = new web.ReadableStream({
|
|
1171
|
+
async start(controller) {
|
|
1172
|
+
const unwatch = self.watch(async ({ type, from = stream.ChunkFrom.WORKFLOW, payload }) => {
|
|
1173
|
+
controller.enqueue({
|
|
1174
|
+
type,
|
|
1175
|
+
runId: self.runId,
|
|
1176
|
+
from,
|
|
1177
|
+
payload: {
|
|
1178
|
+
stepName: payload?.id,
|
|
1179
|
+
...payload
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
});
|
|
1183
|
+
self.closeStreamAction = async () => {
|
|
1184
|
+
unwatch();
|
|
1185
|
+
try {
|
|
1186
|
+
controller.close();
|
|
1187
|
+
} catch (err) {
|
|
1188
|
+
console.error("Error closing stream:", err);
|
|
1189
|
+
}
|
|
1190
|
+
};
|
|
1191
|
+
const executionResultsPromise = self._timeTravel({
|
|
1192
|
+
inputData,
|
|
1193
|
+
step,
|
|
1194
|
+
context,
|
|
1195
|
+
nestedStepsContext,
|
|
1196
|
+
resumeData,
|
|
1197
|
+
initialState,
|
|
1198
|
+
requestContext,
|
|
1199
|
+
tracingOptions,
|
|
1200
|
+
outputOptions,
|
|
1201
|
+
perStep
|
|
1202
|
+
});
|
|
1203
|
+
self.executionResults = executionResultsPromise;
|
|
1204
|
+
let executionResults;
|
|
1205
|
+
try {
|
|
1206
|
+
executionResults = await executionResultsPromise;
|
|
1207
|
+
self.closeStreamAction?.().catch(() => {
|
|
1208
|
+
});
|
|
1209
|
+
if (self.streamOutput) {
|
|
1210
|
+
self.streamOutput.updateResults(executionResults);
|
|
1211
|
+
}
|
|
1212
|
+
} catch (err) {
|
|
1213
|
+
self.streamOutput?.rejectResults(err);
|
|
1214
|
+
self.closeStreamAction?.().catch(() => {
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
});
|
|
1219
|
+
this.streamOutput = new stream.WorkflowRunOutput({
|
|
1220
|
+
runId: this.runId,
|
|
1221
|
+
workflowId: this.workflowId,
|
|
1222
|
+
stream: stream$1
|
|
1223
|
+
});
|
|
1224
|
+
return this.streamOutput;
|
|
1225
|
+
}
|
|
1226
|
+
/**
|
|
1227
|
+
* Hydrates errors in a failed workflow result back to proper Error instances.
|
|
1228
|
+
* This ensures error.cause chains and custom properties are preserved.
|
|
1229
|
+
*/
|
|
1230
|
+
hydrateFailedResult(result) {
|
|
1231
|
+
if (result.status === "failed") {
|
|
1232
|
+
result.error = error.getErrorFromUnknown(result.error, { serializeStack: false });
|
|
1233
|
+
if (result.steps) {
|
|
1234
|
+
workflows.hydrateSerializedStepErrors(result.steps);
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
};
|
|
1239
|
+
|
|
1240
|
+
// src/workflow.ts
|
|
1241
|
+
var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
1242
|
+
#mastra;
|
|
1243
|
+
inngest;
|
|
1244
|
+
function;
|
|
1245
|
+
cronFunction;
|
|
1246
|
+
flowControlConfig;
|
|
1247
|
+
cronConfig;
|
|
1248
|
+
constructor(params, inngest) {
|
|
1249
|
+
const { concurrency, rateLimit, throttle, debounce, priority, cron, inputData, initialState, ...workflowParams } = params;
|
|
1250
|
+
super(workflowParams);
|
|
1251
|
+
this.engineType = "inngest";
|
|
1252
|
+
const flowControlEntries = Object.entries({ concurrency, rateLimit, throttle, debounce, priority }).filter(
|
|
1253
|
+
([_, value]) => value !== void 0
|
|
1254
|
+
);
|
|
1255
|
+
this.flowControlConfig = flowControlEntries.length > 0 ? Object.fromEntries(flowControlEntries) : void 0;
|
|
1256
|
+
this.#mastra = params.mastra;
|
|
1257
|
+
this.inngest = inngest;
|
|
1258
|
+
if (cron) {
|
|
1259
|
+
this.cronConfig = { cron, inputData, initialState };
|
|
1260
|
+
}
|
|
274
1261
|
}
|
|
275
|
-
async
|
|
1262
|
+
async listWorkflowRuns(args) {
|
|
276
1263
|
const storage = this.#mastra?.getStorage();
|
|
277
1264
|
if (!storage) {
|
|
278
1265
|
this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
|
|
279
|
-
return
|
|
1266
|
+
return { runs: [], total: 0 };
|
|
1267
|
+
}
|
|
1268
|
+
const workflowsStore = await storage.getStore("workflows");
|
|
1269
|
+
if (!workflowsStore) {
|
|
1270
|
+
return { runs: [], total: 0 };
|
|
280
1271
|
}
|
|
281
|
-
|
|
282
|
-
return run ?? (this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null);
|
|
1272
|
+
return workflowsStore.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
|
|
283
1273
|
}
|
|
284
1274
|
__registerMastra(mastra) {
|
|
1275
|
+
super.__registerMastra(mastra);
|
|
285
1276
|
this.#mastra = mastra;
|
|
286
1277
|
this.executionEngine.__registerMastra(mastra);
|
|
287
1278
|
const updateNested = (step) => {
|
|
@@ -299,18 +1290,10 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
299
1290
|
}
|
|
300
1291
|
}
|
|
301
1292
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
createRun(_options) {
|
|
307
|
-
throw new Error(
|
|
308
|
-
"createRun() has been deprecated. Please use createRunAsync() instead.\n\nMigration guide:\n Before: const run = workflow.createRun();\n After: const run = await workflow.createRunAsync();\n\nNote: createRunAsync() is an async method, so make sure your calling function is async."
|
|
309
|
-
);
|
|
310
|
-
}
|
|
311
|
-
async createRunAsync(options) {
|
|
312
|
-
const runIdToUse = options?.runId || crypto.randomUUID();
|
|
313
|
-
const run = this.runs.get(runIdToUse) ?? new InngestRun(
|
|
1293
|
+
async createRun(options) {
|
|
1294
|
+
const runIdToUse = options?.runId || crypto$1.randomUUID();
|
|
1295
|
+
const existingInMemoryRun = this.runs.get(runIdToUse);
|
|
1296
|
+
const newRun = new InngestRun(
|
|
314
1297
|
{
|
|
315
1298
|
workflowId: this.id,
|
|
316
1299
|
runId: runIdToUse,
|
|
@@ -321,14 +1304,25 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
321
1304
|
mastra: this.#mastra,
|
|
322
1305
|
retryConfig: this.retryConfig,
|
|
323
1306
|
cleanup: () => this.runs.delete(runIdToUse),
|
|
324
|
-
workflowSteps: this.steps
|
|
1307
|
+
workflowSteps: this.steps,
|
|
1308
|
+
workflowEngineType: this.engineType,
|
|
1309
|
+
validateInputs: this.options.validateInputs
|
|
325
1310
|
},
|
|
326
1311
|
this.inngest
|
|
327
1312
|
);
|
|
1313
|
+
const run = existingInMemoryRun ?? newRun;
|
|
328
1314
|
this.runs.set(runIdToUse, run);
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
1315
|
+
const shouldPersistSnapshot = this.options.shouldPersistSnapshot({
|
|
1316
|
+
workflowStatus: run.workflowRunStatus,
|
|
1317
|
+
stepResults: {}
|
|
1318
|
+
});
|
|
1319
|
+
const existingStoredRun = await this.getWorkflowRunById(runIdToUse, {
|
|
1320
|
+
withNestedWorkflows: false
|
|
1321
|
+
});
|
|
1322
|
+
const existsInStorage = existingStoredRun && !existingStoredRun.isFromInMemory;
|
|
1323
|
+
if (!existsInStorage && shouldPersistSnapshot) {
|
|
1324
|
+
const workflowsStore = await this.mastra?.getStorage()?.getStore("workflows");
|
|
1325
|
+
await workflowsStore?.persistWorkflowSnapshot({
|
|
332
1326
|
workflowName: this.id,
|
|
333
1327
|
runId: runIdToUse,
|
|
334
1328
|
resourceId: options?.resourceId,
|
|
@@ -338,18 +1332,43 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
338
1332
|
value: {},
|
|
339
1333
|
context: {},
|
|
340
1334
|
activePaths: [],
|
|
1335
|
+
activeStepsPath: {},
|
|
341
1336
|
waitingPaths: {},
|
|
342
1337
|
serializedStepGraph: this.serializedStepGraph,
|
|
343
1338
|
suspendedPaths: {},
|
|
1339
|
+
resumeLabels: {},
|
|
344
1340
|
result: void 0,
|
|
345
1341
|
error: void 0,
|
|
346
|
-
// @ts-ignore
|
|
347
1342
|
timestamp: Date.now()
|
|
348
1343
|
}
|
|
349
1344
|
});
|
|
350
1345
|
}
|
|
351
1346
|
return run;
|
|
352
1347
|
}
|
|
1348
|
+
//createCronFunction is only called if cronConfig.cron is defined.
|
|
1349
|
+
createCronFunction() {
|
|
1350
|
+
if (this.cronFunction) {
|
|
1351
|
+
return this.cronFunction;
|
|
1352
|
+
}
|
|
1353
|
+
this.cronFunction = this.inngest.createFunction(
|
|
1354
|
+
{
|
|
1355
|
+
id: `workflow.${this.id}.cron`,
|
|
1356
|
+
retries: 0,
|
|
1357
|
+
cancelOn: [{ event: `cancel.workflow.${this.id}` }],
|
|
1358
|
+
...this.flowControlConfig
|
|
1359
|
+
},
|
|
1360
|
+
{ cron: this.cronConfig?.cron ?? "" },
|
|
1361
|
+
async () => {
|
|
1362
|
+
const run = await this.createRun();
|
|
1363
|
+
const result = await run.start({
|
|
1364
|
+
inputData: this.cronConfig?.inputData,
|
|
1365
|
+
initialState: this.cronConfig?.initialState
|
|
1366
|
+
});
|
|
1367
|
+
return { result, runId: run.runId };
|
|
1368
|
+
}
|
|
1369
|
+
);
|
|
1370
|
+
return this.cronFunction;
|
|
1371
|
+
}
|
|
353
1372
|
getFunction() {
|
|
354
1373
|
if (this.function) {
|
|
355
1374
|
return this.function;
|
|
@@ -357,58 +1376,134 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
357
1376
|
this.function = this.inngest.createFunction(
|
|
358
1377
|
{
|
|
359
1378
|
id: `workflow.${this.id}`,
|
|
360
|
-
|
|
361
|
-
retries: this.retryConfig?.attempts ?? 0,
|
|
1379
|
+
retries: 0,
|
|
362
1380
|
cancelOn: [{ event: `cancel.workflow.${this.id}` }],
|
|
363
1381
|
// Spread flow control configuration
|
|
364
1382
|
...this.flowControlConfig
|
|
365
1383
|
},
|
|
366
1384
|
{ event: `workflow.${this.id}` },
|
|
367
1385
|
async ({ event, step, attempt, publish }) => {
|
|
368
|
-
let {
|
|
1386
|
+
let {
|
|
1387
|
+
inputData,
|
|
1388
|
+
initialState,
|
|
1389
|
+
runId,
|
|
1390
|
+
resourceId,
|
|
1391
|
+
resume,
|
|
1392
|
+
outputOptions,
|
|
1393
|
+
format,
|
|
1394
|
+
timeTravel,
|
|
1395
|
+
perStep,
|
|
1396
|
+
tracingOptions
|
|
1397
|
+
} = event.data;
|
|
369
1398
|
if (!runId) {
|
|
370
1399
|
runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
|
|
371
|
-
return crypto.randomUUID();
|
|
1400
|
+
return crypto$1.randomUUID();
|
|
372
1401
|
});
|
|
373
1402
|
}
|
|
374
|
-
const
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
1403
|
+
const pubsub = new InngestPubSub(this.inngest, this.id, publish);
|
|
1404
|
+
const requestContext = new di.RequestContext(Object.entries(event.data.requestContext ?? {}));
|
|
1405
|
+
const mastra = this.#mastra;
|
|
1406
|
+
const tracingPolicy = this.options.tracingPolicy;
|
|
1407
|
+
const workflowSpanData = await step.run(`workflow.${this.id}.span.start`, async () => {
|
|
1408
|
+
const observability$1 = mastra?.observability?.getSelectedInstance({ requestContext });
|
|
1409
|
+
if (!observability$1) return void 0;
|
|
1410
|
+
const span = observability$1.startSpan({
|
|
1411
|
+
type: observability.SpanType.WORKFLOW_RUN,
|
|
1412
|
+
name: `workflow run: '${this.id}'`,
|
|
1413
|
+
entityType: observability.EntityType.WORKFLOW_RUN,
|
|
1414
|
+
entityId: this.id,
|
|
1415
|
+
input: inputData,
|
|
1416
|
+
metadata: {
|
|
1417
|
+
resourceId,
|
|
1418
|
+
runId
|
|
1419
|
+
},
|
|
1420
|
+
tracingPolicy,
|
|
1421
|
+
tracingOptions,
|
|
1422
|
+
requestContext
|
|
1423
|
+
});
|
|
1424
|
+
return span?.exportSpan();
|
|
1425
|
+
});
|
|
1426
|
+
const engine = new InngestExecutionEngine(this.#mastra, step, attempt, this.options);
|
|
1427
|
+
let result;
|
|
1428
|
+
try {
|
|
1429
|
+
result = await engine.execute({
|
|
1430
|
+
workflowId: this.id,
|
|
1431
|
+
runId,
|
|
1432
|
+
resourceId,
|
|
1433
|
+
graph: this.executionGraph,
|
|
1434
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
1435
|
+
input: inputData,
|
|
1436
|
+
initialState,
|
|
1437
|
+
pubsub,
|
|
1438
|
+
retryConfig: this.retryConfig,
|
|
1439
|
+
requestContext,
|
|
1440
|
+
resume,
|
|
1441
|
+
timeTravel,
|
|
1442
|
+
perStep,
|
|
1443
|
+
format,
|
|
1444
|
+
abortController: new AbortController(),
|
|
1445
|
+
// For Inngest, we don't pass workflowSpan - step spans use tracingIds instead
|
|
1446
|
+
workflowSpan: void 0,
|
|
1447
|
+
// Pass tracing IDs for durable span operations
|
|
1448
|
+
tracingIds: workflowSpanData ? {
|
|
1449
|
+
traceId: workflowSpanData.traceId,
|
|
1450
|
+
workflowSpanId: workflowSpanData.id
|
|
1451
|
+
} : void 0,
|
|
1452
|
+
outputOptions,
|
|
1453
|
+
outputWriter: async (chunk) => {
|
|
1454
|
+
try {
|
|
1455
|
+
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1456
|
+
type: "watch",
|
|
1457
|
+
runId,
|
|
1458
|
+
data: chunk
|
|
1459
|
+
});
|
|
1460
|
+
} catch (err) {
|
|
1461
|
+
this.logger.debug?.("Failed to publish watch event:", err);
|
|
1462
|
+
}
|
|
378
1463
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
1464
|
+
});
|
|
1465
|
+
} catch (error) {
|
|
1466
|
+
throw error;
|
|
1467
|
+
}
|
|
1468
|
+
await step.run(`workflow.${this.id}.finalize`, async () => {
|
|
1469
|
+
if (result.status !== "paused") {
|
|
1470
|
+
await engine.invokeLifecycleCallbacksInternal({
|
|
1471
|
+
status: result.status,
|
|
1472
|
+
result: "result" in result ? result.result : void 0,
|
|
1473
|
+
error: "error" in result ? result.error : void 0,
|
|
1474
|
+
steps: result.steps,
|
|
1475
|
+
tripwire: "tripwire" in result ? result.tripwire : void 0,
|
|
1476
|
+
runId,
|
|
1477
|
+
workflowId: this.id,
|
|
1478
|
+
resourceId,
|
|
1479
|
+
input: inputData,
|
|
1480
|
+
requestContext,
|
|
1481
|
+
state: result.state ?? initialState ?? {}
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
1484
|
+
if (workflowSpanData) {
|
|
1485
|
+
const observability = mastra?.observability?.getSelectedInstance({ requestContext });
|
|
1486
|
+
if (observability) {
|
|
1487
|
+
const workflowSpan = observability.rebuildSpan(workflowSpanData);
|
|
1488
|
+
if (result.status === "failed") {
|
|
1489
|
+
workflowSpan.error({
|
|
1490
|
+
error: result.error instanceof Error ? result.error : new Error(String(result.error)),
|
|
1491
|
+
attributes: { status: "failed" }
|
|
1492
|
+
});
|
|
1493
|
+
} else {
|
|
1494
|
+
workflowSpan.end({
|
|
1495
|
+
output: result.status === "success" ? result.result : void 0,
|
|
1496
|
+
attributes: { status: result.status }
|
|
1497
|
+
});
|
|
1498
|
+
}
|
|
387
1499
|
}
|
|
388
|
-
},
|
|
389
|
-
on: (_event, _callback) => {
|
|
390
|
-
},
|
|
391
|
-
off: (_event, _callback) => {
|
|
392
|
-
},
|
|
393
|
-
once: (_event, _callback) => {
|
|
394
1500
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
graph: this.executionGraph,
|
|
402
|
-
serializedStepGraph: this.serializedStepGraph,
|
|
403
|
-
input: inputData,
|
|
404
|
-
emitter,
|
|
405
|
-
retryConfig: this.retryConfig,
|
|
406
|
-
runtimeContext: new di.RuntimeContext(),
|
|
407
|
-
// TODO
|
|
408
|
-
resume,
|
|
409
|
-
abortController: new AbortController(),
|
|
410
|
-
currentSpan: void 0
|
|
411
|
-
// TODO: Pass actual parent AI span from workflow execution context
|
|
1501
|
+
if (result.status === "failed") {
|
|
1502
|
+
throw new inngest.NonRetriableError(`Workflow failed`, {
|
|
1503
|
+
cause: result
|
|
1504
|
+
});
|
|
1505
|
+
}
|
|
1506
|
+
return result;
|
|
412
1507
|
});
|
|
413
1508
|
return { result, runId };
|
|
414
1509
|
}
|
|
@@ -429,968 +1524,731 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
429
1524
|
});
|
|
430
1525
|
}
|
|
431
1526
|
getFunctions() {
|
|
432
|
-
return [
|
|
1527
|
+
return [
|
|
1528
|
+
this.getFunction(),
|
|
1529
|
+
...this.cronConfig?.cron ? [this.createCronFunction()] : [],
|
|
1530
|
+
...this.getNestedFunctions(this.executionGraph.steps)
|
|
1531
|
+
];
|
|
433
1532
|
}
|
|
434
1533
|
};
|
|
435
|
-
function
|
|
436
|
-
|
|
1534
|
+
function prepareServeOptions({ mastra, inngest, functions: userFunctions = [], registerOptions }) {
|
|
1535
|
+
const wfs = mastra.listWorkflows();
|
|
1536
|
+
const workflowFunctions = Array.from(
|
|
1537
|
+
new Set(
|
|
1538
|
+
Object.values(wfs).flatMap((wf) => {
|
|
1539
|
+
if (wf instanceof InngestWorkflow) {
|
|
1540
|
+
wf.__registerMastra(mastra);
|
|
1541
|
+
return wf.getFunctions();
|
|
1542
|
+
}
|
|
1543
|
+
return [];
|
|
1544
|
+
})
|
|
1545
|
+
)
|
|
1546
|
+
);
|
|
1547
|
+
return {
|
|
1548
|
+
...registerOptions,
|
|
1549
|
+
client: inngest,
|
|
1550
|
+
functions: [...workflowFunctions, ...userFunctions]
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
function createServe(adapter) {
|
|
1554
|
+
return (options) => {
|
|
1555
|
+
const serveOptions = prepareServeOptions(options);
|
|
1556
|
+
return adapter(serveOptions);
|
|
1557
|
+
};
|
|
1558
|
+
}
|
|
1559
|
+
var serve = createServe(hono.serve);
|
|
1560
|
+
|
|
1561
|
+
// src/types.ts
|
|
1562
|
+
var _compatibilityCheck = true;
|
|
1563
|
+
|
|
1564
|
+
// src/index.ts
|
|
1565
|
+
function isInngestWorkflow(input) {
|
|
1566
|
+
return input instanceof InngestWorkflow;
|
|
437
1567
|
}
|
|
438
|
-
function
|
|
439
|
-
return
|
|
1568
|
+
function isAgent(input) {
|
|
1569
|
+
return input instanceof agent.Agent;
|
|
440
1570
|
}
|
|
441
|
-
function
|
|
1571
|
+
function isToolStep(input) {
|
|
1572
|
+
return input instanceof tools.Tool;
|
|
1573
|
+
}
|
|
1574
|
+
function isStepParams(input) {
|
|
1575
|
+
return input !== null && typeof input === "object" && "id" in input && "execute" in input && !(input instanceof agent.Agent) && !(input instanceof tools.Tool) && !(input instanceof InngestWorkflow);
|
|
1576
|
+
}
|
|
1577
|
+
function isProcessor(obj) {
|
|
1578
|
+
return obj !== null && typeof obj === "object" && "id" in obj && typeof obj.id === "string" && !(obj instanceof agent.Agent) && !(obj instanceof tools.Tool) && !(obj instanceof InngestWorkflow) && (typeof obj.processInput === "function" || typeof obj.processInputStep === "function" || typeof obj.processOutputStream === "function" || typeof obj.processOutputResult === "function" || typeof obj.processOutputStep === "function");
|
|
1579
|
+
}
|
|
1580
|
+
function createStep(params, agentOrToolOptions) {
|
|
1581
|
+
if (isInngestWorkflow(params)) {
|
|
1582
|
+
return params;
|
|
1583
|
+
}
|
|
442
1584
|
if (isAgent(params)) {
|
|
443
|
-
return
|
|
444
|
-
id: params.name,
|
|
445
|
-
description: params.getDescription(),
|
|
446
|
-
// @ts-ignore
|
|
447
|
-
inputSchema: zod.z.object({
|
|
448
|
-
prompt: zod.z.string()
|
|
449
|
-
}),
|
|
450
|
-
// @ts-ignore
|
|
451
|
-
outputSchema: zod.z.object({
|
|
452
|
-
text: zod.z.string()
|
|
453
|
-
}),
|
|
454
|
-
execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort, tracingContext }) => {
|
|
455
|
-
let streamPromise = {};
|
|
456
|
-
streamPromise.promise = new Promise((resolve, reject) => {
|
|
457
|
-
streamPromise.resolve = resolve;
|
|
458
|
-
streamPromise.reject = reject;
|
|
459
|
-
});
|
|
460
|
-
const toolData = {
|
|
461
|
-
name: params.name,
|
|
462
|
-
args: inputData
|
|
463
|
-
};
|
|
464
|
-
const { fullStream } = await params.stream(inputData.prompt, {
|
|
465
|
-
runtimeContext,
|
|
466
|
-
tracingContext,
|
|
467
|
-
onFinish: (result) => {
|
|
468
|
-
streamPromise.resolve(result.text);
|
|
469
|
-
},
|
|
470
|
-
abortSignal
|
|
471
|
-
});
|
|
472
|
-
if (abortSignal.aborted) {
|
|
473
|
-
return abort();
|
|
474
|
-
}
|
|
475
|
-
await emitter.emit("watch-v2", {
|
|
476
|
-
type: "tool-call-streaming-start",
|
|
477
|
-
...toolData ?? {}
|
|
478
|
-
});
|
|
479
|
-
for await (const chunk of fullStream) {
|
|
480
|
-
if (chunk.type === "text-delta") {
|
|
481
|
-
await emitter.emit("watch-v2", {
|
|
482
|
-
type: "tool-call-delta",
|
|
483
|
-
...toolData ?? {},
|
|
484
|
-
argsTextDelta: chunk.textDelta
|
|
485
|
-
});
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
await emitter.emit("watch-v2", {
|
|
489
|
-
type: "tool-call-streaming-finish",
|
|
490
|
-
...toolData ?? {}
|
|
491
|
-
});
|
|
492
|
-
return {
|
|
493
|
-
text: await streamPromise.promise
|
|
494
|
-
};
|
|
495
|
-
},
|
|
496
|
-
component: params.component
|
|
497
|
-
};
|
|
1585
|
+
return createStepFromAgent(params, agentOrToolOptions);
|
|
498
1586
|
}
|
|
499
|
-
if (
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
return
|
|
504
|
-
// TODO: tool probably should have strong id type
|
|
505
|
-
// @ts-ignore
|
|
506
|
-
id: params.id,
|
|
507
|
-
description: params.description,
|
|
508
|
-
inputSchema: params.inputSchema,
|
|
509
|
-
outputSchema: params.outputSchema,
|
|
510
|
-
execute: async ({ inputData, mastra, runtimeContext, tracingContext, suspend, resumeData }) => {
|
|
511
|
-
return params.execute({
|
|
512
|
-
context: inputData,
|
|
513
|
-
mastra: aiTracing.wrapMastra(mastra, tracingContext),
|
|
514
|
-
runtimeContext,
|
|
515
|
-
tracingContext,
|
|
516
|
-
suspend,
|
|
517
|
-
resumeData
|
|
518
|
-
});
|
|
519
|
-
},
|
|
520
|
-
component: "TOOL"
|
|
521
|
-
};
|
|
1587
|
+
if (isToolStep(params)) {
|
|
1588
|
+
return createStepFromTool(params, agentOrToolOptions);
|
|
1589
|
+
}
|
|
1590
|
+
if (isStepParams(params)) {
|
|
1591
|
+
return createStepFromParams(params);
|
|
522
1592
|
}
|
|
1593
|
+
if (isProcessor(params)) {
|
|
1594
|
+
return createStepFromProcessor(params);
|
|
1595
|
+
}
|
|
1596
|
+
throw new Error("Invalid input: expected StepParams, Agent, ToolStep, Processor, or InngestWorkflow");
|
|
1597
|
+
}
|
|
1598
|
+
function createStepFromParams(params) {
|
|
523
1599
|
return {
|
|
524
1600
|
id: params.id,
|
|
525
1601
|
description: params.description,
|
|
526
1602
|
inputSchema: params.inputSchema,
|
|
1603
|
+
stateSchema: params.stateSchema,
|
|
527
1604
|
outputSchema: params.outputSchema,
|
|
528
1605
|
resumeSchema: params.resumeSchema,
|
|
529
1606
|
suspendSchema: params.suspendSchema,
|
|
530
|
-
|
|
1607
|
+
scorers: params.scorers,
|
|
1608
|
+
retries: params.retries,
|
|
1609
|
+
execute: params.execute.bind(params)
|
|
531
1610
|
};
|
|
532
1611
|
}
|
|
533
|
-
function
|
|
1612
|
+
function createStepFromAgent(params, agentOrToolOptions) {
|
|
1613
|
+
const options = agentOrToolOptions ?? {};
|
|
1614
|
+
const outputSchema = options?.structuredOutput?.schema ?? zod.z.object({ text: zod.z.string() });
|
|
1615
|
+
const { retries, scorers, ...agentOptions } = options ?? {};
|
|
534
1616
|
return {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
wf.commit();
|
|
559
|
-
return wf;
|
|
560
|
-
}
|
|
561
|
-
};
|
|
562
|
-
}
|
|
563
|
-
var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
564
|
-
inngestStep;
|
|
565
|
-
inngestAttempts;
|
|
566
|
-
constructor(mastra, inngestStep, inngestAttempts = 0, options) {
|
|
567
|
-
super({ mastra, options });
|
|
568
|
-
this.inngestStep = inngestStep;
|
|
569
|
-
this.inngestAttempts = inngestAttempts;
|
|
570
|
-
}
|
|
571
|
-
async execute(params) {
|
|
572
|
-
await params.emitter.emit("watch-v2", {
|
|
573
|
-
type: "workflow-start",
|
|
574
|
-
payload: { runId: params.runId }
|
|
575
|
-
});
|
|
576
|
-
const result = await super.execute(params);
|
|
577
|
-
await params.emitter.emit("watch-v2", {
|
|
578
|
-
type: "workflow-finish",
|
|
579
|
-
payload: { runId: params.runId }
|
|
580
|
-
});
|
|
581
|
-
return result;
|
|
582
|
-
}
|
|
583
|
-
async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
|
|
584
|
-
const base = {
|
|
585
|
-
status: lastOutput.status,
|
|
586
|
-
steps: stepResults
|
|
587
|
-
};
|
|
588
|
-
if (lastOutput.status === "success") {
|
|
589
|
-
await emitter.emit("watch", {
|
|
590
|
-
type: "watch",
|
|
591
|
-
payload: {
|
|
592
|
-
workflowState: {
|
|
593
|
-
status: lastOutput.status,
|
|
594
|
-
steps: stepResults,
|
|
595
|
-
result: lastOutput.output
|
|
596
|
-
}
|
|
597
|
-
},
|
|
598
|
-
eventTimestamp: Date.now()
|
|
599
|
-
});
|
|
600
|
-
base.result = lastOutput.output;
|
|
601
|
-
} else if (lastOutput.status === "failed") {
|
|
602
|
-
base.error = error instanceof Error ? error?.stack ?? error.message : lastOutput?.error instanceof Error ? lastOutput.error.message : lastOutput.error ?? error ?? "Unknown error";
|
|
603
|
-
await emitter.emit("watch", {
|
|
604
|
-
type: "watch",
|
|
605
|
-
payload: {
|
|
606
|
-
workflowState: {
|
|
607
|
-
status: lastOutput.status,
|
|
608
|
-
steps: stepResults,
|
|
609
|
-
result: null,
|
|
610
|
-
error: base.error
|
|
611
|
-
}
|
|
612
|
-
},
|
|
613
|
-
eventTimestamp: Date.now()
|
|
614
|
-
});
|
|
615
|
-
} else if (lastOutput.status === "suspended") {
|
|
616
|
-
await emitter.emit("watch", {
|
|
617
|
-
type: "watch",
|
|
618
|
-
payload: {
|
|
619
|
-
workflowState: {
|
|
620
|
-
status: lastOutput.status,
|
|
621
|
-
steps: stepResults,
|
|
622
|
-
result: null,
|
|
623
|
-
error: null
|
|
624
|
-
}
|
|
625
|
-
},
|
|
626
|
-
eventTimestamp: Date.now()
|
|
627
|
-
});
|
|
628
|
-
const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
|
|
629
|
-
if (stepResult?.status === "suspended") {
|
|
630
|
-
const nestedPath = stepResult?.payload?.__workflow_meta?.path;
|
|
631
|
-
return nestedPath ? [[stepId, ...nestedPath]] : [[stepId]];
|
|
632
|
-
}
|
|
633
|
-
return [];
|
|
634
|
-
});
|
|
635
|
-
base.suspended = suspendedStepIds;
|
|
636
|
-
}
|
|
637
|
-
executionSpan?.end();
|
|
638
|
-
return base;
|
|
639
|
-
}
|
|
640
|
-
// async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
|
|
641
|
-
// await this.inngestStep.sleep(id, duration);
|
|
642
|
-
// }
|
|
643
|
-
async executeSleep({
|
|
644
|
-
workflowId,
|
|
645
|
-
runId,
|
|
646
|
-
entry,
|
|
647
|
-
prevOutput,
|
|
648
|
-
stepResults,
|
|
649
|
-
emitter,
|
|
650
|
-
abortController,
|
|
651
|
-
runtimeContext,
|
|
652
|
-
executionContext,
|
|
653
|
-
writableStream,
|
|
654
|
-
tracingContext
|
|
655
|
-
}) {
|
|
656
|
-
let { duration, fn } = entry;
|
|
657
|
-
const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
658
|
-
type: aiTracing.AISpanType.WORKFLOW_SLEEP,
|
|
659
|
-
name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
|
|
660
|
-
attributes: {
|
|
661
|
-
durationMs: duration,
|
|
662
|
-
sleepType: fn ? "dynamic" : "fixed"
|
|
663
|
-
},
|
|
664
|
-
tracingPolicy: this.options?.tracingPolicy
|
|
665
|
-
});
|
|
666
|
-
if (fn) {
|
|
667
|
-
const stepCallId = crypto.randomUUID();
|
|
668
|
-
duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
|
|
669
|
-
return await fn({
|
|
670
|
-
runId,
|
|
671
|
-
workflowId,
|
|
672
|
-
mastra: this.mastra,
|
|
673
|
-
runtimeContext,
|
|
674
|
-
inputData: prevOutput,
|
|
675
|
-
runCount: -1,
|
|
676
|
-
tracingContext: {
|
|
677
|
-
currentSpan: sleepSpan
|
|
678
|
-
},
|
|
679
|
-
getInitData: () => stepResults?.input,
|
|
680
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
681
|
-
// TODO: this function shouldn't have suspend probably?
|
|
682
|
-
suspend: async (_suspendPayload) => {
|
|
683
|
-
},
|
|
684
|
-
bail: () => {
|
|
685
|
-
},
|
|
686
|
-
abort: () => {
|
|
687
|
-
abortController?.abort();
|
|
688
|
-
},
|
|
689
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
690
|
-
// TODO: add streamVNext support
|
|
691
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
692
|
-
engine: { step: this.inngestStep },
|
|
693
|
-
abortSignal: abortController?.signal,
|
|
694
|
-
writer: new tools.ToolStream(
|
|
695
|
-
{
|
|
696
|
-
prefix: "workflow-step",
|
|
697
|
-
callId: stepCallId,
|
|
698
|
-
name: "sleep",
|
|
699
|
-
runId
|
|
700
|
-
},
|
|
701
|
-
writableStream
|
|
702
|
-
)
|
|
703
|
-
});
|
|
1617
|
+
id: params.name,
|
|
1618
|
+
description: params.getDescription(),
|
|
1619
|
+
inputSchema: zod.z.object({
|
|
1620
|
+
prompt: zod.z.string()
|
|
1621
|
+
}),
|
|
1622
|
+
outputSchema,
|
|
1623
|
+
retries,
|
|
1624
|
+
scorers,
|
|
1625
|
+
execute: async ({
|
|
1626
|
+
inputData,
|
|
1627
|
+
runId,
|
|
1628
|
+
[_constants.PUBSUB_SYMBOL]: pubsub,
|
|
1629
|
+
[_constants.STREAM_FORMAT_SYMBOL]: streamFormat,
|
|
1630
|
+
requestContext,
|
|
1631
|
+
tracingContext,
|
|
1632
|
+
abortSignal,
|
|
1633
|
+
abort,
|
|
1634
|
+
writer
|
|
1635
|
+
}) => {
|
|
1636
|
+
let streamPromise = {};
|
|
1637
|
+
streamPromise.promise = new Promise((resolve, reject) => {
|
|
1638
|
+
streamPromise.resolve = resolve;
|
|
1639
|
+
streamPromise.reject = reject;
|
|
704
1640
|
});
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
prevOutput,
|
|
724
|
-
stepResults,
|
|
725
|
-
emitter,
|
|
726
|
-
abortController,
|
|
727
|
-
runtimeContext,
|
|
728
|
-
executionContext,
|
|
729
|
-
writableStream,
|
|
730
|
-
tracingContext
|
|
731
|
-
}) {
|
|
732
|
-
let { date, fn } = entry;
|
|
733
|
-
const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
734
|
-
type: aiTracing.AISpanType.WORKFLOW_SLEEP,
|
|
735
|
-
name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
|
|
736
|
-
attributes: {
|
|
737
|
-
untilDate: date,
|
|
738
|
-
durationMs: date ? Math.max(0, date.getTime() - Date.now()) : void 0,
|
|
739
|
-
sleepType: fn ? "dynamic" : "fixed"
|
|
740
|
-
},
|
|
741
|
-
tracingPolicy: this.options?.tracingPolicy
|
|
742
|
-
});
|
|
743
|
-
if (fn) {
|
|
744
|
-
date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
|
|
745
|
-
const stepCallId = crypto.randomUUID();
|
|
746
|
-
return await fn({
|
|
747
|
-
runId,
|
|
748
|
-
workflowId,
|
|
749
|
-
mastra: this.mastra,
|
|
750
|
-
runtimeContext,
|
|
751
|
-
inputData: prevOutput,
|
|
752
|
-
runCount: -1,
|
|
753
|
-
tracingContext: {
|
|
754
|
-
currentSpan: sleepUntilSpan
|
|
755
|
-
},
|
|
756
|
-
getInitData: () => stepResults?.input,
|
|
757
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
758
|
-
// TODO: this function shouldn't have suspend probably?
|
|
759
|
-
suspend: async (_suspendPayload) => {
|
|
760
|
-
},
|
|
761
|
-
bail: () => {
|
|
762
|
-
},
|
|
763
|
-
abort: () => {
|
|
764
|
-
abortController?.abort();
|
|
1641
|
+
let structuredResult = null;
|
|
1642
|
+
const toolData = {
|
|
1643
|
+
name: params.name,
|
|
1644
|
+
args: inputData
|
|
1645
|
+
};
|
|
1646
|
+
let stream;
|
|
1647
|
+
if ((await params.getModel()).specificationVersion === "v1") {
|
|
1648
|
+
const { fullStream } = await params.streamLegacy(inputData.prompt, {
|
|
1649
|
+
...agentOptions ?? {},
|
|
1650
|
+
requestContext,
|
|
1651
|
+
tracingContext,
|
|
1652
|
+
onFinish: (result) => {
|
|
1653
|
+
const resultWithObject = result;
|
|
1654
|
+
if (agentOptions?.structuredOutput?.schema && resultWithObject.object) {
|
|
1655
|
+
structuredResult = resultWithObject.object;
|
|
1656
|
+
}
|
|
1657
|
+
streamPromise.resolve(result.text);
|
|
1658
|
+
void agentOptions?.onFinish?.(result);
|
|
765
1659
|
},
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
writableStream
|
|
779
|
-
)
|
|
780
|
-
});
|
|
781
|
-
});
|
|
782
|
-
if (date && !(date instanceof Date)) {
|
|
783
|
-
date = new Date(date);
|
|
784
|
-
}
|
|
785
|
-
const time = !date ? 0 : date.getTime() - Date.now();
|
|
786
|
-
sleepUntilSpan?.update({
|
|
787
|
-
attributes: {
|
|
788
|
-
durationMs: Math.max(0, time)
|
|
789
|
-
}
|
|
790
|
-
});
|
|
791
|
-
}
|
|
792
|
-
if (!(date instanceof Date)) {
|
|
793
|
-
sleepUntilSpan?.end();
|
|
794
|
-
return;
|
|
795
|
-
}
|
|
796
|
-
try {
|
|
797
|
-
await this.inngestStep.sleepUntil(entry.id, date);
|
|
798
|
-
sleepUntilSpan?.end();
|
|
799
|
-
} catch (e) {
|
|
800
|
-
sleepUntilSpan?.error({ error: e });
|
|
801
|
-
throw e;
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
async executeWaitForEvent({ event, timeout }) {
|
|
805
|
-
const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
|
|
806
|
-
event: `user-event-${event}`,
|
|
807
|
-
timeout: timeout ?? 5e3
|
|
808
|
-
});
|
|
809
|
-
if (eventData === null) {
|
|
810
|
-
throw "Timeout waiting for event";
|
|
811
|
-
}
|
|
812
|
-
return eventData?.data;
|
|
813
|
-
}
|
|
814
|
-
async executeStep({
|
|
815
|
-
step,
|
|
816
|
-
stepResults,
|
|
817
|
-
executionContext,
|
|
818
|
-
resume,
|
|
819
|
-
prevOutput,
|
|
820
|
-
emitter,
|
|
821
|
-
abortController,
|
|
822
|
-
runtimeContext,
|
|
823
|
-
tracingContext,
|
|
824
|
-
writableStream,
|
|
825
|
-
disableScorers
|
|
826
|
-
}) {
|
|
827
|
-
const stepAISpan = tracingContext?.currentSpan?.createChildSpan({
|
|
828
|
-
name: `workflow step: '${step.id}'`,
|
|
829
|
-
type: aiTracing.AISpanType.WORKFLOW_STEP,
|
|
830
|
-
input: prevOutput,
|
|
831
|
-
attributes: {
|
|
832
|
-
stepId: step.id
|
|
833
|
-
},
|
|
834
|
-
tracingPolicy: this.options?.tracingPolicy
|
|
835
|
-
});
|
|
836
|
-
const { inputData, validationError } = await workflows.validateStepInput({
|
|
837
|
-
prevOutput,
|
|
838
|
-
step,
|
|
839
|
-
validateInputs: this.options?.validateInputs ?? false
|
|
840
|
-
});
|
|
841
|
-
const startedAt = await this.inngestStep.run(
|
|
842
|
-
`workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
|
|
843
|
-
async () => {
|
|
844
|
-
const startedAt2 = Date.now();
|
|
845
|
-
await emitter.emit("watch", {
|
|
846
|
-
type: "watch",
|
|
847
|
-
payload: {
|
|
848
|
-
currentStep: {
|
|
849
|
-
id: step.id,
|
|
850
|
-
status: "running"
|
|
851
|
-
},
|
|
852
|
-
workflowState: {
|
|
853
|
-
status: "running",
|
|
854
|
-
steps: {
|
|
855
|
-
...stepResults,
|
|
856
|
-
[step.id]: {
|
|
857
|
-
status: "running"
|
|
858
|
-
}
|
|
859
|
-
},
|
|
860
|
-
result: null,
|
|
861
|
-
error: null
|
|
1660
|
+
abortSignal
|
|
1661
|
+
});
|
|
1662
|
+
stream = fullStream;
|
|
1663
|
+
} else {
|
|
1664
|
+
const modelOutput = await params.stream(inputData.prompt, {
|
|
1665
|
+
...agentOptions ?? {},
|
|
1666
|
+
requestContext,
|
|
1667
|
+
tracingContext,
|
|
1668
|
+
onFinish: (result) => {
|
|
1669
|
+
const resultWithObject = result;
|
|
1670
|
+
if (agentOptions?.structuredOutput?.schema && resultWithObject.object) {
|
|
1671
|
+
structuredResult = resultWithObject.object;
|
|
862
1672
|
}
|
|
1673
|
+
streamPromise.resolve(result.text);
|
|
1674
|
+
void agentOptions?.onFinish?.(result);
|
|
863
1675
|
},
|
|
864
|
-
|
|
865
|
-
});
|
|
866
|
-
await emitter.emit("watch-v2", {
|
|
867
|
-
type: "workflow-step-start",
|
|
868
|
-
payload: {
|
|
869
|
-
id: step.id,
|
|
870
|
-
status: "running",
|
|
871
|
-
payload: inputData,
|
|
872
|
-
startedAt: startedAt2
|
|
873
|
-
}
|
|
1676
|
+
abortSignal
|
|
874
1677
|
});
|
|
875
|
-
|
|
1678
|
+
stream = modelOutput.fullStream;
|
|
876
1679
|
}
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
if (isResume) {
|
|
883
|
-
runId = stepResults[resume?.steps?.[0]]?.payload?.__workflow_meta?.runId ?? crypto.randomUUID();
|
|
884
|
-
const snapshot = await this.mastra?.getStorage()?.loadWorkflowSnapshot({
|
|
885
|
-
workflowName: step.id,
|
|
886
|
-
runId
|
|
1680
|
+
if (streamFormat === "legacy") {
|
|
1681
|
+
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1682
|
+
type: "watch",
|
|
1683
|
+
runId,
|
|
1684
|
+
data: { type: "tool-call-streaming-start", ...toolData ?? {} }
|
|
887
1685
|
});
|
|
888
|
-
const
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
runId,
|
|
893
|
-
resume: {
|
|
1686
|
+
for await (const chunk of stream) {
|
|
1687
|
+
if (chunk.type === "text-delta") {
|
|
1688
|
+
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1689
|
+
type: "watch",
|
|
894
1690
|
runId,
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
resumePayload: resume.resumePayload,
|
|
898
|
-
// @ts-ignore
|
|
899
|
-
resumePath: snapshot?.suspendedPaths?.[resume.steps?.[1]]
|
|
900
|
-
}
|
|
1691
|
+
data: { type: "tool-call-delta", ...toolData ?? {}, argsTextDelta: chunk.textDelta }
|
|
1692
|
+
});
|
|
901
1693
|
}
|
|
1694
|
+
}
|
|
1695
|
+
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1696
|
+
type: "watch",
|
|
1697
|
+
runId,
|
|
1698
|
+
data: { type: "tool-call-streaming-finish", ...toolData ?? {} }
|
|
902
1699
|
});
|
|
903
|
-
result = invokeResp.result;
|
|
904
|
-
runId = invokeResp.runId;
|
|
905
1700
|
} else {
|
|
906
|
-
const
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
inputData
|
|
910
|
-
}
|
|
911
|
-
});
|
|
912
|
-
result = invokeResp.result;
|
|
913
|
-
runId = invokeResp.runId;
|
|
1701
|
+
for await (const chunk of stream) {
|
|
1702
|
+
await writer.write(chunk);
|
|
1703
|
+
}
|
|
914
1704
|
}
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
1705
|
+
if (abortSignal.aborted) {
|
|
1706
|
+
return abort();
|
|
1707
|
+
}
|
|
1708
|
+
if (structuredResult !== null) {
|
|
1709
|
+
return structuredResult;
|
|
1710
|
+
}
|
|
1711
|
+
return {
|
|
1712
|
+
text: await streamPromise.promise
|
|
1713
|
+
};
|
|
1714
|
+
},
|
|
1715
|
+
component: params.component
|
|
1716
|
+
};
|
|
1717
|
+
}
|
|
1718
|
+
function createStepFromTool(params, agentOrToolOptions) {
|
|
1719
|
+
const toolOpts = agentOrToolOptions;
|
|
1720
|
+
if (!params.inputSchema || !params.outputSchema) {
|
|
1721
|
+
throw new Error("Tool must have input and output schemas defined");
|
|
1722
|
+
}
|
|
1723
|
+
return {
|
|
1724
|
+
id: params.id,
|
|
1725
|
+
description: params.description,
|
|
1726
|
+
inputSchema: params.inputSchema,
|
|
1727
|
+
outputSchema: params.outputSchema,
|
|
1728
|
+
resumeSchema: params.resumeSchema,
|
|
1729
|
+
suspendSchema: params.suspendSchema,
|
|
1730
|
+
retries: toolOpts?.retries,
|
|
1731
|
+
scorers: toolOpts?.scorers,
|
|
1732
|
+
execute: async ({
|
|
1733
|
+
inputData,
|
|
1734
|
+
mastra,
|
|
1735
|
+
requestContext,
|
|
1736
|
+
tracingContext,
|
|
1737
|
+
suspend,
|
|
1738
|
+
resumeData,
|
|
1739
|
+
runId,
|
|
1740
|
+
workflowId,
|
|
1741
|
+
state,
|
|
1742
|
+
setState
|
|
1743
|
+
}) => {
|
|
1744
|
+
const toolContext = {
|
|
1745
|
+
mastra,
|
|
1746
|
+
requestContext,
|
|
1747
|
+
tracingContext,
|
|
1748
|
+
workflow: {
|
|
1749
|
+
runId,
|
|
1750
|
+
resumeData,
|
|
1751
|
+
suspend,
|
|
1752
|
+
workflowId,
|
|
1753
|
+
state,
|
|
1754
|
+
setState
|
|
1755
|
+
}
|
|
1756
|
+
};
|
|
1757
|
+
return params.execute(inputData, toolContext);
|
|
1758
|
+
},
|
|
1759
|
+
component: "TOOL"
|
|
1760
|
+
};
|
|
1761
|
+
}
|
|
1762
|
+
function createStepFromProcessor(processor) {
|
|
1763
|
+
const getProcessorEntityType = (phase) => {
|
|
1764
|
+
switch (phase) {
|
|
1765
|
+
case "input":
|
|
1766
|
+
return observability.EntityType.INPUT_PROCESSOR;
|
|
1767
|
+
case "inputStep":
|
|
1768
|
+
return observability.EntityType.INPUT_STEP_PROCESSOR;
|
|
1769
|
+
case "outputStream":
|
|
1770
|
+
case "outputResult":
|
|
1771
|
+
return observability.EntityType.OUTPUT_PROCESSOR;
|
|
1772
|
+
case "outputStep":
|
|
1773
|
+
return observability.EntityType.OUTPUT_STEP_PROCESSOR;
|
|
1774
|
+
default:
|
|
1775
|
+
return observability.EntityType.OUTPUT_PROCESSOR;
|
|
1776
|
+
}
|
|
1777
|
+
};
|
|
1778
|
+
const getSpanNamePrefix = (phase) => {
|
|
1779
|
+
switch (phase) {
|
|
1780
|
+
case "input":
|
|
1781
|
+
return "input processor";
|
|
1782
|
+
case "inputStep":
|
|
1783
|
+
return "input step processor";
|
|
1784
|
+
case "outputStream":
|
|
1785
|
+
return "output stream processor";
|
|
1786
|
+
case "outputResult":
|
|
1787
|
+
return "output processor";
|
|
1788
|
+
case "outputStep":
|
|
1789
|
+
return "output step processor";
|
|
1790
|
+
default:
|
|
1791
|
+
return "processor";
|
|
1792
|
+
}
|
|
1793
|
+
};
|
|
1794
|
+
const hasPhaseMethod = (phase) => {
|
|
1795
|
+
switch (phase) {
|
|
1796
|
+
case "input":
|
|
1797
|
+
return !!processor.processInput;
|
|
1798
|
+
case "inputStep":
|
|
1799
|
+
return !!processor.processInputStep;
|
|
1800
|
+
case "outputStream":
|
|
1801
|
+
return !!processor.processOutputStream;
|
|
1802
|
+
case "outputResult":
|
|
1803
|
+
return !!processor.processOutputResult;
|
|
1804
|
+
case "outputStep":
|
|
1805
|
+
return !!processor.processOutputStep;
|
|
1806
|
+
default:
|
|
1807
|
+
return false;
|
|
1808
|
+
}
|
|
1809
|
+
};
|
|
1810
|
+
return {
|
|
1811
|
+
id: `processor:${processor.id}`,
|
|
1812
|
+
description: processor.name ?? `Processor ${processor.id}`,
|
|
1813
|
+
inputSchema: processors.ProcessorStepSchema,
|
|
1814
|
+
outputSchema: processors.ProcessorStepOutputSchema,
|
|
1815
|
+
execute: async ({ inputData, requestContext, tracingContext }) => {
|
|
1816
|
+
const input = inputData;
|
|
1817
|
+
const {
|
|
1818
|
+
phase,
|
|
1819
|
+
messages,
|
|
1820
|
+
messageList,
|
|
1821
|
+
stepNumber,
|
|
1822
|
+
systemMessages,
|
|
1823
|
+
part,
|
|
1824
|
+
streamParts,
|
|
1825
|
+
state,
|
|
1826
|
+
finishReason,
|
|
1827
|
+
toolCalls,
|
|
1828
|
+
text,
|
|
1829
|
+
retryCount,
|
|
1830
|
+
// inputStep phase fields for model/tools configuration
|
|
1831
|
+
model,
|
|
1832
|
+
tools,
|
|
1833
|
+
toolChoice,
|
|
1834
|
+
activeTools,
|
|
1835
|
+
providerOptions,
|
|
1836
|
+
modelSettings,
|
|
1837
|
+
structuredOutput,
|
|
1838
|
+
steps
|
|
1839
|
+
} = input;
|
|
1840
|
+
const abort = (reason, options) => {
|
|
1841
|
+
throw new agent.TripWire(reason || `Tripwire triggered by ${processor.id}`, options, processor.id);
|
|
1842
|
+
};
|
|
1843
|
+
if (!hasPhaseMethod(phase)) {
|
|
1844
|
+
return input;
|
|
1845
|
+
}
|
|
1846
|
+
const currentSpan = tracingContext?.currentSpan;
|
|
1847
|
+
const parentSpan = phase === "inputStep" || phase === "outputStep" ? currentSpan?.findParent(observability.SpanType.MODEL_STEP) || currentSpan : currentSpan?.findParent(observability.SpanType.AGENT_RUN) || currentSpan;
|
|
1848
|
+
const processorSpan = phase !== "outputStream" ? parentSpan?.createChildSpan({
|
|
1849
|
+
type: observability.SpanType.PROCESSOR_RUN,
|
|
1850
|
+
name: `${getSpanNamePrefix(phase)}: ${processor.id}`,
|
|
1851
|
+
entityType: getProcessorEntityType(phase),
|
|
1852
|
+
entityId: processor.id,
|
|
1853
|
+
entityName: processor.name ?? processor.id,
|
|
1854
|
+
input: { phase, messageCount: messages?.length },
|
|
1855
|
+
attributes: {
|
|
1856
|
+
processorExecutor: "workflow",
|
|
1857
|
+
// Read processorIndex from processor (set in combineProcessorsIntoWorkflow)
|
|
1858
|
+
processorIndex: processor.processorIndex
|
|
1859
|
+
}
|
|
1860
|
+
}) : void 0;
|
|
1861
|
+
const processorTracingContext = processorSpan ? { currentSpan: processorSpan } : tracingContext;
|
|
1862
|
+
const baseContext = {
|
|
1863
|
+
abort,
|
|
1864
|
+
retryCount: retryCount ?? 0,
|
|
1865
|
+
requestContext,
|
|
1866
|
+
tracingContext: processorTracingContext
|
|
1867
|
+
};
|
|
1868
|
+
const passThrough = {
|
|
1869
|
+
phase,
|
|
1870
|
+
// Auto-create MessageList from messages if not provided
|
|
1871
|
+
// This enables running processor workflows from the UI where messageList can't be serialized
|
|
1872
|
+
messageList: messageList ?? (Array.isArray(messages) ? new agent.MessageList().add(messages, "input").addSystem(systemMessages ?? []) : void 0),
|
|
1873
|
+
stepNumber,
|
|
1874
|
+
systemMessages,
|
|
1875
|
+
streamParts,
|
|
1876
|
+
state,
|
|
1877
|
+
finishReason,
|
|
1878
|
+
toolCalls,
|
|
1879
|
+
text,
|
|
1880
|
+
retryCount,
|
|
1881
|
+
// inputStep phase fields for model/tools configuration
|
|
1882
|
+
model,
|
|
1883
|
+
tools,
|
|
1884
|
+
toolChoice,
|
|
1885
|
+
activeTools,
|
|
1886
|
+
providerOptions,
|
|
1887
|
+
modelSettings,
|
|
1888
|
+
structuredOutput,
|
|
1889
|
+
steps
|
|
1890
|
+
};
|
|
1891
|
+
const executePhaseWithSpan = async (fn) => {
|
|
1892
|
+
try {
|
|
1893
|
+
const result = await fn();
|
|
1894
|
+
processorSpan?.end({ output: result });
|
|
1895
|
+
return result;
|
|
1896
|
+
} catch (error) {
|
|
1897
|
+
if (error instanceof agent.TripWire) {
|
|
1898
|
+
processorSpan?.end({ output: { tripwire: error.message } });
|
|
1899
|
+
} else {
|
|
1900
|
+
processorSpan?.error({ error, endSpan: true });
|
|
1901
|
+
}
|
|
1902
|
+
throw error;
|
|
1903
|
+
}
|
|
1904
|
+
};
|
|
1905
|
+
return executePhaseWithSpan(async () => {
|
|
1906
|
+
switch (phase) {
|
|
1907
|
+
case "input": {
|
|
1908
|
+
if (processor.processInput) {
|
|
1909
|
+
if (!passThrough.messageList) {
|
|
1910
|
+
throw new error.MastraError({
|
|
1911
|
+
category: error.ErrorCategory.USER,
|
|
1912
|
+
domain: error.ErrorDomain.MASTRA_WORKFLOW,
|
|
1913
|
+
id: "PROCESSOR_MISSING_MESSAGE_LIST",
|
|
1914
|
+
text: `Processor ${processor.id} requires messageList or messages for processInput phase`
|
|
1915
|
+
});
|
|
943
1916
|
}
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
for (const [stepName, stepResult] of suspendedSteps) {
|
|
952
|
-
const suspendPath = [stepName, ...stepResult?.payload?.__workflow_meta?.path ?? []];
|
|
953
|
-
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
954
|
-
await emitter.emit("watch", {
|
|
955
|
-
type: "watch",
|
|
956
|
-
payload: {
|
|
957
|
-
currentStep: {
|
|
958
|
-
id: step.id,
|
|
959
|
-
status: "suspended",
|
|
960
|
-
payload: { ...stepResult?.payload, __workflow_meta: { runId, path: suspendPath } }
|
|
961
|
-
},
|
|
962
|
-
workflowState: {
|
|
963
|
-
status: "running",
|
|
964
|
-
steps: stepResults,
|
|
965
|
-
result: null,
|
|
966
|
-
error: null
|
|
967
|
-
}
|
|
968
|
-
},
|
|
969
|
-
eventTimestamp: Date.now()
|
|
1917
|
+
const idsBeforeProcessing = messages.map((m) => m.id);
|
|
1918
|
+
const check = passThrough.messageList.makeMessageSourceChecker();
|
|
1919
|
+
const result = await processor.processInput({
|
|
1920
|
+
...baseContext,
|
|
1921
|
+
messages,
|
|
1922
|
+
messageList: passThrough.messageList,
|
|
1923
|
+
systemMessages: systemMessages ?? []
|
|
970
1924
|
});
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
1925
|
+
if (result instanceof agent.MessageList) {
|
|
1926
|
+
if (result !== passThrough.messageList) {
|
|
1927
|
+
throw new error.MastraError({
|
|
1928
|
+
category: error.ErrorCategory.USER,
|
|
1929
|
+
domain: error.ErrorDomain.MASTRA_WORKFLOW,
|
|
1930
|
+
id: "PROCESSOR_RETURNED_EXTERNAL_MESSAGE_LIST",
|
|
1931
|
+
text: `Processor ${processor.id} returned a MessageList instance other than the one passed in. Use the messageList argument instead.`
|
|
1932
|
+
});
|
|
976
1933
|
}
|
|
1934
|
+
return {
|
|
1935
|
+
...passThrough,
|
|
1936
|
+
messages: result.get.all.db(),
|
|
1937
|
+
systemMessages: result.getAllSystemMessages()
|
|
1938
|
+
};
|
|
1939
|
+
} else if (Array.isArray(result)) {
|
|
1940
|
+
processors.ProcessorRunner.applyMessagesToMessageList(
|
|
1941
|
+
result,
|
|
1942
|
+
passThrough.messageList,
|
|
1943
|
+
idsBeforeProcessing,
|
|
1944
|
+
check,
|
|
1945
|
+
"input"
|
|
1946
|
+
);
|
|
1947
|
+
return { ...passThrough, messages: result };
|
|
1948
|
+
} else if (result && "messages" in result && "systemMessages" in result) {
|
|
1949
|
+
const typedResult = result;
|
|
1950
|
+
processors.ProcessorRunner.applyMessagesToMessageList(
|
|
1951
|
+
typedResult.messages,
|
|
1952
|
+
passThrough.messageList,
|
|
1953
|
+
idsBeforeProcessing,
|
|
1954
|
+
check,
|
|
1955
|
+
"input"
|
|
1956
|
+
);
|
|
1957
|
+
passThrough.messageList.replaceAllSystemMessages(typedResult.systemMessages);
|
|
1958
|
+
return {
|
|
1959
|
+
...passThrough,
|
|
1960
|
+
messages: typedResult.messages,
|
|
1961
|
+
systemMessages: typedResult.systemMessages
|
|
1962
|
+
};
|
|
1963
|
+
}
|
|
1964
|
+
return { ...passThrough, messages };
|
|
1965
|
+
}
|
|
1966
|
+
return { ...passThrough, messages };
|
|
1967
|
+
}
|
|
1968
|
+
case "inputStep": {
|
|
1969
|
+
if (processor.processInputStep) {
|
|
1970
|
+
if (!passThrough.messageList) {
|
|
1971
|
+
throw new error.MastraError({
|
|
1972
|
+
category: error.ErrorCategory.USER,
|
|
1973
|
+
domain: error.ErrorDomain.MASTRA_WORKFLOW,
|
|
1974
|
+
id: "PROCESSOR_MISSING_MESSAGE_LIST",
|
|
1975
|
+
text: `Processor ${processor.id} requires messageList or messages for processInputStep phase`
|
|
1976
|
+
});
|
|
1977
|
+
}
|
|
1978
|
+
const idsBeforeProcessing = messages.map((m) => m.id);
|
|
1979
|
+
const check = passThrough.messageList.makeMessageSourceChecker();
|
|
1980
|
+
const result = await processor.processInputStep({
|
|
1981
|
+
...baseContext,
|
|
1982
|
+
messages,
|
|
1983
|
+
messageList: passThrough.messageList,
|
|
1984
|
+
stepNumber: stepNumber ?? 0,
|
|
1985
|
+
systemMessages: systemMessages ?? [],
|
|
1986
|
+
// Pass model/tools configuration fields - types match ProcessInputStepArgs
|
|
1987
|
+
model,
|
|
1988
|
+
tools,
|
|
1989
|
+
toolChoice,
|
|
1990
|
+
activeTools,
|
|
1991
|
+
providerOptions,
|
|
1992
|
+
modelSettings,
|
|
1993
|
+
structuredOutput,
|
|
1994
|
+
steps: steps ?? []
|
|
977
1995
|
});
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1996
|
+
const validatedResult = await processors.ProcessorRunner.validateAndFormatProcessInputStepResult(result, {
|
|
1997
|
+
messageList: passThrough.messageList,
|
|
1998
|
+
processor,
|
|
1999
|
+
stepNumber: stepNumber ?? 0
|
|
2000
|
+
});
|
|
2001
|
+
if (validatedResult.messages) {
|
|
2002
|
+
processors.ProcessorRunner.applyMessagesToMessageList(
|
|
2003
|
+
validatedResult.messages,
|
|
2004
|
+
passThrough.messageList,
|
|
2005
|
+
idsBeforeProcessing,
|
|
2006
|
+
check
|
|
2007
|
+
);
|
|
2008
|
+
}
|
|
2009
|
+
if (validatedResult.systemMessages) {
|
|
2010
|
+
passThrough.messageList.replaceAllSystemMessages(validatedResult.systemMessages);
|
|
2011
|
+
}
|
|
2012
|
+
return { ...passThrough, messages, ...validatedResult };
|
|
985
2013
|
}
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
2014
|
+
return { ...passThrough, messages };
|
|
2015
|
+
}
|
|
2016
|
+
case "outputStream": {
|
|
2017
|
+
if (processor.processOutputStream) {
|
|
2018
|
+
const spanKey = `__outputStreamSpan_${processor.id}`;
|
|
2019
|
+
const mutableState = state ?? {};
|
|
2020
|
+
let processorSpan2 = mutableState[spanKey];
|
|
2021
|
+
if (!processorSpan2 && parentSpan) {
|
|
2022
|
+
processorSpan2 = parentSpan.createChildSpan({
|
|
2023
|
+
type: observability.SpanType.PROCESSOR_RUN,
|
|
2024
|
+
name: `output stream processor: ${processor.id}`,
|
|
2025
|
+
entityType: observability.EntityType.OUTPUT_PROCESSOR,
|
|
2026
|
+
entityId: processor.id,
|
|
2027
|
+
entityName: processor.name ?? processor.id,
|
|
2028
|
+
input: { phase, streamParts: [] },
|
|
2029
|
+
attributes: {
|
|
2030
|
+
processorExecutor: "workflow",
|
|
2031
|
+
processorIndex: processor.processorIndex
|
|
2032
|
+
}
|
|
2033
|
+
});
|
|
2034
|
+
mutableState[spanKey] = processorSpan2;
|
|
2035
|
+
}
|
|
2036
|
+
if (processorSpan2) {
|
|
2037
|
+
processorSpan2.input = {
|
|
2038
|
+
phase,
|
|
2039
|
+
streamParts: streamParts ?? [],
|
|
2040
|
+
totalChunks: (streamParts ?? []).length
|
|
2041
|
+
};
|
|
2042
|
+
}
|
|
2043
|
+
const processorTracingContext2 = processorSpan2 ? { currentSpan: processorSpan2 } : baseContext.tracingContext;
|
|
2044
|
+
let result;
|
|
2045
|
+
try {
|
|
2046
|
+
result = await processor.processOutputStream({
|
|
2047
|
+
...baseContext,
|
|
2048
|
+
tracingContext: processorTracingContext2,
|
|
2049
|
+
part,
|
|
2050
|
+
streamParts: streamParts ?? [],
|
|
2051
|
+
state: mutableState,
|
|
2052
|
+
messageList: passThrough.messageList
|
|
2053
|
+
// Optional for stream processing
|
|
2054
|
+
});
|
|
2055
|
+
if (part && part.type === "finish") {
|
|
2056
|
+
processorSpan2?.end({ output: result });
|
|
2057
|
+
delete mutableState[spanKey];
|
|
999
2058
|
}
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
2059
|
+
} catch (error) {
|
|
2060
|
+
if (error instanceof agent.TripWire) {
|
|
2061
|
+
processorSpan2?.end({ output: { tripwire: error.message } });
|
|
2062
|
+
} else {
|
|
2063
|
+
processorSpan2?.error({ error, endSpan: true });
|
|
2064
|
+
}
|
|
2065
|
+
delete mutableState[spanKey];
|
|
2066
|
+
throw error;
|
|
1008
2067
|
}
|
|
1009
|
-
|
|
2068
|
+
return { ...passThrough, state: mutableState, part: result };
|
|
2069
|
+
}
|
|
2070
|
+
return { ...passThrough, part };
|
|
1010
2071
|
}
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
status: "running",
|
|
1021
|
-
steps: stepResults,
|
|
1022
|
-
result: null,
|
|
1023
|
-
error: null
|
|
2072
|
+
case "outputResult": {
|
|
2073
|
+
if (processor.processOutputResult) {
|
|
2074
|
+
if (!passThrough.messageList) {
|
|
2075
|
+
throw new error.MastraError({
|
|
2076
|
+
category: error.ErrorCategory.USER,
|
|
2077
|
+
domain: error.ErrorDomain.MASTRA_WORKFLOW,
|
|
2078
|
+
id: "PROCESSOR_MISSING_MESSAGE_LIST",
|
|
2079
|
+
text: `Processor ${processor.id} requires messageList or messages for processOutputResult phase`
|
|
2080
|
+
});
|
|
1024
2081
|
}
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
2082
|
+
const idsBeforeProcessing = messages.map((m) => m.id);
|
|
2083
|
+
const check = passThrough.messageList.makeMessageSourceChecker();
|
|
2084
|
+
const result = await processor.processOutputResult({
|
|
2085
|
+
...baseContext,
|
|
2086
|
+
messages,
|
|
2087
|
+
messageList: passThrough.messageList
|
|
2088
|
+
});
|
|
2089
|
+
if (result instanceof agent.MessageList) {
|
|
2090
|
+
if (result !== passThrough.messageList) {
|
|
2091
|
+
throw new error.MastraError({
|
|
2092
|
+
category: error.ErrorCategory.USER,
|
|
2093
|
+
domain: error.ErrorDomain.MASTRA_WORKFLOW,
|
|
2094
|
+
id: "PROCESSOR_RETURNED_EXTERNAL_MESSAGE_LIST",
|
|
2095
|
+
text: `Processor ${processor.id} returned a MessageList instance other than the one passed in. Use the messageList argument instead.`
|
|
2096
|
+
});
|
|
2097
|
+
}
|
|
2098
|
+
return {
|
|
2099
|
+
...passThrough,
|
|
2100
|
+
messages: result.get.all.db(),
|
|
2101
|
+
systemMessages: result.getAllSystemMessages()
|
|
2102
|
+
};
|
|
2103
|
+
} else if (Array.isArray(result)) {
|
|
2104
|
+
processors.ProcessorRunner.applyMessagesToMessageList(
|
|
2105
|
+
result,
|
|
2106
|
+
passThrough.messageList,
|
|
2107
|
+
idsBeforeProcessing,
|
|
2108
|
+
check,
|
|
2109
|
+
"response"
|
|
2110
|
+
);
|
|
2111
|
+
return { ...passThrough, messages: result };
|
|
2112
|
+
} else if (result && "messages" in result && "systemMessages" in result) {
|
|
2113
|
+
const typedResult = result;
|
|
2114
|
+
processors.ProcessorRunner.applyMessagesToMessageList(
|
|
2115
|
+
typedResult.messages,
|
|
2116
|
+
passThrough.messageList,
|
|
2117
|
+
idsBeforeProcessing,
|
|
2118
|
+
check,
|
|
2119
|
+
"response"
|
|
2120
|
+
);
|
|
2121
|
+
passThrough.messageList.replaceAllSystemMessages(typedResult.systemMessages);
|
|
2122
|
+
return {
|
|
2123
|
+
...passThrough,
|
|
2124
|
+
messages: typedResult.messages,
|
|
2125
|
+
systemMessages: typedResult.systemMessages
|
|
2126
|
+
};
|
|
2127
|
+
}
|
|
2128
|
+
return { ...passThrough, messages };
|
|
1041
2129
|
}
|
|
1042
|
-
|
|
1043
|
-
return { executionContext, result: { status: "success", output: result?.result } };
|
|
1044
|
-
}
|
|
1045
|
-
);
|
|
1046
|
-
Object.assign(executionContext, res.executionContext);
|
|
1047
|
-
return res.result;
|
|
1048
|
-
}
|
|
1049
|
-
const stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
|
|
1050
|
-
let execResults;
|
|
1051
|
-
let suspended;
|
|
1052
|
-
let bailed;
|
|
1053
|
-
try {
|
|
1054
|
-
if (validationError) {
|
|
1055
|
-
throw validationError;
|
|
1056
|
-
}
|
|
1057
|
-
const result = await step.execute({
|
|
1058
|
-
runId: executionContext.runId,
|
|
1059
|
-
mastra: this.mastra,
|
|
1060
|
-
runtimeContext,
|
|
1061
|
-
writableStream,
|
|
1062
|
-
inputData,
|
|
1063
|
-
resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
|
|
1064
|
-
tracingContext: {
|
|
1065
|
-
currentSpan: stepAISpan
|
|
1066
|
-
},
|
|
1067
|
-
getInitData: () => stepResults?.input,
|
|
1068
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1069
|
-
suspend: async (suspendPayload) => {
|
|
1070
|
-
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
1071
|
-
suspended = { payload: suspendPayload };
|
|
1072
|
-
},
|
|
1073
|
-
bail: (result2) => {
|
|
1074
|
-
bailed = { payload: result2 };
|
|
1075
|
-
},
|
|
1076
|
-
resume: {
|
|
1077
|
-
steps: resume?.steps?.slice(1) || [],
|
|
1078
|
-
resumePayload: resume?.resumePayload,
|
|
1079
|
-
// @ts-ignore
|
|
1080
|
-
runId: stepResults[step.id]?.payload?.__workflow_meta?.runId
|
|
1081
|
-
},
|
|
1082
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1083
|
-
engine: {
|
|
1084
|
-
step: this.inngestStep
|
|
1085
|
-
},
|
|
1086
|
-
abortSignal: abortController.signal
|
|
1087
|
-
});
|
|
1088
|
-
const endedAt = Date.now();
|
|
1089
|
-
execResults = {
|
|
1090
|
-
status: "success",
|
|
1091
|
-
output: result,
|
|
1092
|
-
startedAt,
|
|
1093
|
-
endedAt,
|
|
1094
|
-
payload: inputData,
|
|
1095
|
-
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
1096
|
-
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1097
|
-
};
|
|
1098
|
-
} catch (e) {
|
|
1099
|
-
execResults = {
|
|
1100
|
-
status: "failed",
|
|
1101
|
-
payload: inputData,
|
|
1102
|
-
error: e instanceof Error ? e.message : String(e),
|
|
1103
|
-
endedAt: Date.now(),
|
|
1104
|
-
startedAt,
|
|
1105
|
-
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
1106
|
-
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1107
|
-
};
|
|
1108
|
-
}
|
|
1109
|
-
if (suspended) {
|
|
1110
|
-
execResults = {
|
|
1111
|
-
status: "suspended",
|
|
1112
|
-
suspendedPayload: suspended.payload,
|
|
1113
|
-
payload: inputData,
|
|
1114
|
-
suspendedAt: Date.now(),
|
|
1115
|
-
startedAt,
|
|
1116
|
-
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
1117
|
-
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1118
|
-
};
|
|
1119
|
-
} else if (bailed) {
|
|
1120
|
-
execResults = { status: "bailed", output: bailed.payload, payload: inputData, endedAt: Date.now(), startedAt };
|
|
1121
|
-
}
|
|
1122
|
-
if (execResults.status === "failed") {
|
|
1123
|
-
if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
|
|
1124
|
-
const error = new Error(execResults.error);
|
|
1125
|
-
stepAISpan?.error({ error });
|
|
1126
|
-
throw error;
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
await emitter.emit("watch", {
|
|
1130
|
-
type: "watch",
|
|
1131
|
-
payload: {
|
|
1132
|
-
currentStep: {
|
|
1133
|
-
id: step.id,
|
|
1134
|
-
...execResults
|
|
1135
|
-
},
|
|
1136
|
-
workflowState: {
|
|
1137
|
-
status: "running",
|
|
1138
|
-
steps: { ...stepResults, [step.id]: execResults },
|
|
1139
|
-
result: null,
|
|
1140
|
-
error: null
|
|
1141
|
-
}
|
|
1142
|
-
},
|
|
1143
|
-
eventTimestamp: Date.now()
|
|
1144
|
-
});
|
|
1145
|
-
if (execResults.status === "suspended") {
|
|
1146
|
-
await emitter.emit("watch-v2", {
|
|
1147
|
-
type: "workflow-step-suspended",
|
|
1148
|
-
payload: {
|
|
1149
|
-
id: step.id,
|
|
1150
|
-
...execResults
|
|
1151
|
-
}
|
|
1152
|
-
});
|
|
1153
|
-
} else {
|
|
1154
|
-
await emitter.emit("watch-v2", {
|
|
1155
|
-
type: "workflow-step-result",
|
|
1156
|
-
payload: {
|
|
1157
|
-
id: step.id,
|
|
1158
|
-
...execResults
|
|
1159
|
-
}
|
|
1160
|
-
});
|
|
1161
|
-
await emitter.emit("watch-v2", {
|
|
1162
|
-
type: "workflow-step-finish",
|
|
1163
|
-
payload: {
|
|
1164
|
-
id: step.id,
|
|
1165
|
-
metadata: {}
|
|
1166
|
-
}
|
|
1167
|
-
});
|
|
1168
|
-
}
|
|
1169
|
-
stepAISpan?.end({ output: execResults });
|
|
1170
|
-
return { result: execResults, executionContext, stepResults };
|
|
1171
|
-
});
|
|
1172
|
-
if (disableScorers !== false) {
|
|
1173
|
-
await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}.score`, async () => {
|
|
1174
|
-
if (step.scorers) {
|
|
1175
|
-
await this.runScorers({
|
|
1176
|
-
scorers: step.scorers,
|
|
1177
|
-
runId: executionContext.runId,
|
|
1178
|
-
input: inputData,
|
|
1179
|
-
output: stepRes.result,
|
|
1180
|
-
workflowId: executionContext.workflowId,
|
|
1181
|
-
stepId: step.id,
|
|
1182
|
-
runtimeContext,
|
|
1183
|
-
disableScorers,
|
|
1184
|
-
tracingContext: { currentSpan: stepAISpan }
|
|
1185
|
-
});
|
|
1186
|
-
}
|
|
1187
|
-
});
|
|
1188
|
-
}
|
|
1189
|
-
Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
|
|
1190
|
-
Object.assign(stepResults, stepRes.stepResults);
|
|
1191
|
-
return stepRes.result;
|
|
1192
|
-
}
|
|
1193
|
-
async persistStepUpdate({
|
|
1194
|
-
workflowId,
|
|
1195
|
-
runId,
|
|
1196
|
-
stepResults,
|
|
1197
|
-
resourceId,
|
|
1198
|
-
executionContext,
|
|
1199
|
-
serializedStepGraph,
|
|
1200
|
-
workflowStatus,
|
|
1201
|
-
result,
|
|
1202
|
-
error
|
|
1203
|
-
}) {
|
|
1204
|
-
await this.inngestStep.run(
|
|
1205
|
-
`workflow.${workflowId}.run.${runId}.path.${JSON.stringify(executionContext.executionPath)}.stepUpdate`,
|
|
1206
|
-
async () => {
|
|
1207
|
-
await this.mastra?.getStorage()?.persistWorkflowSnapshot({
|
|
1208
|
-
workflowName: workflowId,
|
|
1209
|
-
runId,
|
|
1210
|
-
resourceId,
|
|
1211
|
-
snapshot: {
|
|
1212
|
-
runId,
|
|
1213
|
-
value: {},
|
|
1214
|
-
context: stepResults,
|
|
1215
|
-
activePaths: [],
|
|
1216
|
-
suspendedPaths: executionContext.suspendedPaths,
|
|
1217
|
-
waitingPaths: {},
|
|
1218
|
-
serializedStepGraph,
|
|
1219
|
-
status: workflowStatus,
|
|
1220
|
-
result,
|
|
1221
|
-
error,
|
|
1222
|
-
// @ts-ignore
|
|
1223
|
-
timestamp: Date.now()
|
|
2130
|
+
return { ...passThrough, messages };
|
|
1224
2131
|
}
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
prevStep,
|
|
1235
|
-
stepResults,
|
|
1236
|
-
serializedStepGraph,
|
|
1237
|
-
resume,
|
|
1238
|
-
executionContext,
|
|
1239
|
-
emitter,
|
|
1240
|
-
abortController,
|
|
1241
|
-
runtimeContext,
|
|
1242
|
-
writableStream,
|
|
1243
|
-
disableScorers,
|
|
1244
|
-
tracingContext
|
|
1245
|
-
}) {
|
|
1246
|
-
const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
1247
|
-
type: aiTracing.AISpanType.WORKFLOW_CONDITIONAL,
|
|
1248
|
-
name: `conditional: '${entry.conditions.length} conditions'`,
|
|
1249
|
-
input: prevOutput,
|
|
1250
|
-
attributes: {
|
|
1251
|
-
conditionCount: entry.conditions.length
|
|
1252
|
-
},
|
|
1253
|
-
tracingPolicy: this.options?.tracingPolicy
|
|
1254
|
-
});
|
|
1255
|
-
let execResults;
|
|
1256
|
-
const truthyIndexes = (await Promise.all(
|
|
1257
|
-
entry.conditions.map(
|
|
1258
|
-
(cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
|
|
1259
|
-
const evalSpan = conditionalSpan?.createChildSpan({
|
|
1260
|
-
type: aiTracing.AISpanType.WORKFLOW_CONDITIONAL_EVAL,
|
|
1261
|
-
name: `condition: '${index}'`,
|
|
1262
|
-
input: prevOutput,
|
|
1263
|
-
attributes: {
|
|
1264
|
-
conditionIndex: index
|
|
1265
|
-
},
|
|
1266
|
-
tracingPolicy: this.options?.tracingPolicy
|
|
1267
|
-
});
|
|
1268
|
-
try {
|
|
1269
|
-
const result = await cond({
|
|
1270
|
-
runId,
|
|
1271
|
-
workflowId,
|
|
1272
|
-
mastra: this.mastra,
|
|
1273
|
-
runtimeContext,
|
|
1274
|
-
runCount: -1,
|
|
1275
|
-
inputData: prevOutput,
|
|
1276
|
-
tracingContext: {
|
|
1277
|
-
currentSpan: evalSpan
|
|
1278
|
-
},
|
|
1279
|
-
getInitData: () => stepResults?.input,
|
|
1280
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1281
|
-
// TODO: this function shouldn't have suspend probably?
|
|
1282
|
-
suspend: async (_suspendPayload) => {
|
|
1283
|
-
},
|
|
1284
|
-
bail: () => {
|
|
1285
|
-
},
|
|
1286
|
-
abort: () => {
|
|
1287
|
-
abortController.abort();
|
|
1288
|
-
},
|
|
1289
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1290
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1291
|
-
// TODO: add streamVNext support
|
|
1292
|
-
engine: {
|
|
1293
|
-
step: this.inngestStep
|
|
1294
|
-
},
|
|
1295
|
-
abortSignal: abortController.signal,
|
|
1296
|
-
writer: new tools.ToolStream(
|
|
1297
|
-
{
|
|
1298
|
-
prefix: "workflow-step",
|
|
1299
|
-
callId: crypto.randomUUID(),
|
|
1300
|
-
name: "conditional",
|
|
1301
|
-
runId
|
|
1302
|
-
},
|
|
1303
|
-
writableStream
|
|
1304
|
-
)
|
|
1305
|
-
});
|
|
1306
|
-
evalSpan?.end({
|
|
1307
|
-
output: result,
|
|
1308
|
-
attributes: {
|
|
1309
|
-
result: !!result
|
|
2132
|
+
case "outputStep": {
|
|
2133
|
+
if (processor.processOutputStep) {
|
|
2134
|
+
if (!passThrough.messageList) {
|
|
2135
|
+
throw new error.MastraError({
|
|
2136
|
+
category: error.ErrorCategory.USER,
|
|
2137
|
+
domain: error.ErrorDomain.MASTRA_WORKFLOW,
|
|
2138
|
+
id: "PROCESSOR_MISSING_MESSAGE_LIST",
|
|
2139
|
+
text: `Processor ${processor.id} requires messageList or messages for processOutputStep phase`
|
|
2140
|
+
});
|
|
1310
2141
|
}
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
2142
|
+
const idsBeforeProcessing = messages.map((m) => m.id);
|
|
2143
|
+
const check = passThrough.messageList.makeMessageSourceChecker();
|
|
2144
|
+
const result = await processor.processOutputStep({
|
|
2145
|
+
...baseContext,
|
|
2146
|
+
messages,
|
|
2147
|
+
messageList: passThrough.messageList,
|
|
2148
|
+
stepNumber: stepNumber ?? 0,
|
|
2149
|
+
finishReason,
|
|
2150
|
+
toolCalls,
|
|
2151
|
+
text,
|
|
2152
|
+
systemMessages: systemMessages ?? [],
|
|
2153
|
+
steps: steps ?? []
|
|
2154
|
+
});
|
|
2155
|
+
if (result instanceof agent.MessageList) {
|
|
2156
|
+
if (result !== passThrough.messageList) {
|
|
2157
|
+
throw new error.MastraError({
|
|
2158
|
+
category: error.ErrorCategory.USER,
|
|
2159
|
+
domain: error.ErrorDomain.MASTRA_WORKFLOW,
|
|
2160
|
+
id: "PROCESSOR_RETURNED_EXTERNAL_MESSAGE_LIST",
|
|
2161
|
+
text: `Processor ${processor.id} returned a MessageList instance other than the one passed in. Use the messageList argument instead.`
|
|
2162
|
+
});
|
|
2163
|
+
}
|
|
2164
|
+
return {
|
|
2165
|
+
...passThrough,
|
|
2166
|
+
messages: result.get.all.db(),
|
|
2167
|
+
systemMessages: result.getAllSystemMessages()
|
|
2168
|
+
};
|
|
2169
|
+
} else if (Array.isArray(result)) {
|
|
2170
|
+
processors.ProcessorRunner.applyMessagesToMessageList(
|
|
2171
|
+
result,
|
|
2172
|
+
passThrough.messageList,
|
|
2173
|
+
idsBeforeProcessing,
|
|
2174
|
+
check,
|
|
2175
|
+
"response"
|
|
2176
|
+
);
|
|
2177
|
+
return { ...passThrough, messages: result };
|
|
2178
|
+
} else if (result && "messages" in result && "systemMessages" in result) {
|
|
2179
|
+
const typedResult = result;
|
|
2180
|
+
processors.ProcessorRunner.applyMessagesToMessageList(
|
|
2181
|
+
typedResult.messages,
|
|
2182
|
+
passThrough.messageList,
|
|
2183
|
+
idsBeforeProcessing,
|
|
2184
|
+
check,
|
|
2185
|
+
"response"
|
|
2186
|
+
);
|
|
2187
|
+
passThrough.messageList.replaceAllSystemMessages(typedResult.systemMessages);
|
|
2188
|
+
return {
|
|
2189
|
+
...passThrough,
|
|
2190
|
+
messages: typedResult.messages,
|
|
2191
|
+
systemMessages: typedResult.systemMessages
|
|
2192
|
+
};
|
|
1318
2193
|
}
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
})
|
|
1323
|
-
)
|
|
1324
|
-
)).filter((index) => index !== null);
|
|
1325
|
-
const stepsToRun = entry.steps.filter((_, index) => truthyIndexes.includes(index));
|
|
1326
|
-
conditionalSpan?.update({
|
|
1327
|
-
attributes: {
|
|
1328
|
-
truthyIndexes,
|
|
1329
|
-
selectedSteps: stepsToRun.map((s) => s.type === "step" ? s.step.id : `control-${s.type}`)
|
|
1330
|
-
}
|
|
1331
|
-
});
|
|
1332
|
-
const results = await Promise.all(
|
|
1333
|
-
stepsToRun.map(
|
|
1334
|
-
(step, index) => this.executeEntry({
|
|
1335
|
-
workflowId,
|
|
1336
|
-
runId,
|
|
1337
|
-
entry: step,
|
|
1338
|
-
serializedStepGraph,
|
|
1339
|
-
prevStep,
|
|
1340
|
-
stepResults,
|
|
1341
|
-
resume,
|
|
1342
|
-
executionContext: {
|
|
1343
|
-
workflowId,
|
|
1344
|
-
runId,
|
|
1345
|
-
executionPath: [...executionContext.executionPath, index],
|
|
1346
|
-
suspendedPaths: executionContext.suspendedPaths,
|
|
1347
|
-
retryConfig: executionContext.retryConfig,
|
|
1348
|
-
executionSpan: executionContext.executionSpan
|
|
1349
|
-
},
|
|
1350
|
-
emitter,
|
|
1351
|
-
abortController,
|
|
1352
|
-
runtimeContext,
|
|
1353
|
-
writableStream,
|
|
1354
|
-
disableScorers,
|
|
1355
|
-
tracingContext: {
|
|
1356
|
-
currentSpan: conditionalSpan
|
|
1357
|
-
}
|
|
1358
|
-
})
|
|
1359
|
-
)
|
|
1360
|
-
);
|
|
1361
|
-
const hasFailed = results.find((result) => result.result.status === "failed");
|
|
1362
|
-
const hasSuspended = results.find((result) => result.result.status === "suspended");
|
|
1363
|
-
if (hasFailed) {
|
|
1364
|
-
execResults = { status: "failed", error: hasFailed.result.error };
|
|
1365
|
-
} else if (hasSuspended) {
|
|
1366
|
-
execResults = { status: "suspended", payload: hasSuspended.result.suspendPayload };
|
|
1367
|
-
} else {
|
|
1368
|
-
execResults = {
|
|
1369
|
-
status: "success",
|
|
1370
|
-
output: results.reduce((acc, result, index) => {
|
|
1371
|
-
if (result.result.status === "success") {
|
|
1372
|
-
acc[stepsToRun[index].step.id] = result.output;
|
|
2194
|
+
return { ...passThrough, messages };
|
|
2195
|
+
}
|
|
2196
|
+
return { ...passThrough, messages };
|
|
1373
2197
|
}
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
}
|
|
1378
|
-
if (execResults.status === "failed") {
|
|
1379
|
-
conditionalSpan?.error({
|
|
1380
|
-
error: new Error(execResults.error)
|
|
2198
|
+
default:
|
|
2199
|
+
return { ...passThrough, messages };
|
|
2200
|
+
}
|
|
1381
2201
|
});
|
|
1382
|
-
}
|
|
1383
|
-
|
|
1384
|
-
|
|
2202
|
+
},
|
|
2203
|
+
component: "PROCESSOR"
|
|
2204
|
+
};
|
|
2205
|
+
}
|
|
2206
|
+
function init(inngest) {
|
|
2207
|
+
return {
|
|
2208
|
+
createWorkflow(params) {
|
|
2209
|
+
return new InngestWorkflow(
|
|
2210
|
+
params,
|
|
2211
|
+
inngest
|
|
2212
|
+
);
|
|
2213
|
+
},
|
|
2214
|
+
createStep,
|
|
2215
|
+
cloneStep(step, opts) {
|
|
2216
|
+
return {
|
|
2217
|
+
id: opts.id,
|
|
2218
|
+
description: step.description,
|
|
2219
|
+
inputSchema: step.inputSchema,
|
|
2220
|
+
outputSchema: step.outputSchema,
|
|
2221
|
+
resumeSchema: step.resumeSchema,
|
|
2222
|
+
suspendSchema: step.suspendSchema,
|
|
2223
|
+
stateSchema: step.stateSchema,
|
|
2224
|
+
execute: step.execute,
|
|
2225
|
+
retries: step.retries,
|
|
2226
|
+
scorers: step.scorers,
|
|
2227
|
+
component: step.component
|
|
2228
|
+
};
|
|
2229
|
+
},
|
|
2230
|
+
cloneWorkflow(workflow, opts) {
|
|
2231
|
+
const wf = new workflows.Workflow({
|
|
2232
|
+
id: opts.id,
|
|
2233
|
+
inputSchema: workflow.inputSchema,
|
|
2234
|
+
outputSchema: workflow.outputSchema,
|
|
2235
|
+
steps: workflow.stepDefs,
|
|
2236
|
+
mastra: workflow.mastra,
|
|
2237
|
+
options: workflow.options
|
|
1385
2238
|
});
|
|
2239
|
+
wf.setStepFlow(workflow.stepGraph);
|
|
2240
|
+
wf.commit();
|
|
2241
|
+
return wf;
|
|
1386
2242
|
}
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
};
|
|
2243
|
+
};
|
|
2244
|
+
}
|
|
1390
2245
|
|
|
1391
2246
|
exports.InngestExecutionEngine = InngestExecutionEngine;
|
|
2247
|
+
exports.InngestPubSub = InngestPubSub;
|
|
1392
2248
|
exports.InngestRun = InngestRun;
|
|
1393
2249
|
exports.InngestWorkflow = InngestWorkflow;
|
|
2250
|
+
exports._compatibilityCheck = _compatibilityCheck;
|
|
2251
|
+
exports.createServe = createServe;
|
|
1394
2252
|
exports.createStep = createStep;
|
|
1395
2253
|
exports.init = init;
|
|
1396
2254
|
exports.serve = serve;
|