@mastra/inngest 1.0.0-beta.8 → 1.0.0
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 +1275 -0
- 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 +102 -5
- package/dist/execution-engine.d.ts.map +1 -1
- package/dist/index.cjs +1041 -289
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +70 -25
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1042 -291
- package/dist/index.js.map +1 -1
- package/dist/run.d.ts +59 -51
- package/dist/run.d.ts.map +1 -1
- package/dist/serve.d.ts +66 -3
- package/dist/serve.d.ts.map +1 -1
- package/dist/types.d.ts +7 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/workflow.d.ts +5 -4
- package/dist/workflow.d.ts.map +1 -1
- package/package.json +22 -10
package/dist/index.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
import { MessageList, Agent, TripWire } from '@mastra/core/agent';
|
|
2
|
+
import { getErrorFromUnknown, MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';
|
|
3
|
+
import { EntityType, SpanType } from '@mastra/core/observability';
|
|
4
|
+
import { ProcessorStepOutputSchema, ProcessorStepSchema, ProcessorRunner } from '@mastra/core/processors';
|
|
1
5
|
import { Tool } from '@mastra/core/tools';
|
|
2
6
|
import { DefaultExecutionEngine, createTimeTravelExecutionParams, Run, hydrateSerializedStepErrors, Workflow } from '@mastra/core/workflows';
|
|
3
7
|
import { PUBSUB_SYMBOL, STREAM_FORMAT_SYMBOL } from '@mastra/core/workflows/_constants';
|
|
4
8
|
import { z } from 'zod';
|
|
5
9
|
import { randomUUID } from 'crypto';
|
|
6
10
|
import { RequestContext } from '@mastra/core/di';
|
|
7
|
-
import {
|
|
8
|
-
import { getErrorFromUnknown } from '@mastra/core/error';
|
|
11
|
+
import { NonRetriableError } from 'inngest';
|
|
9
12
|
import { subscribe } from '@inngest/realtime';
|
|
10
13
|
import { PubSub } from '@mastra/core/events';
|
|
11
14
|
import { ReadableStream } from 'stream/web';
|
|
@@ -58,38 +61,46 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
58
61
|
* After retries exhausted, error propagates here and we return a failed result.
|
|
59
62
|
*/
|
|
60
63
|
async executeStepWithRetry(stepId, runStep, params) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
} catch (e) {
|
|
65
|
-
const cause = e?.cause;
|
|
66
|
-
if (cause?.status === "failed") {
|
|
67
|
-
params.stepSpan?.error({
|
|
68
|
-
error: e,
|
|
69
|
-
attributes: { status: "failed" }
|
|
70
|
-
});
|
|
71
|
-
if (cause.error && !(cause.error instanceof Error)) {
|
|
72
|
-
cause.error = getErrorFromUnknown(cause.error, { serializeStack: false });
|
|
73
|
-
}
|
|
74
|
-
return { ok: false, error: cause };
|
|
64
|
+
for (let i = 0; i < params.retries + 1; i++) {
|
|
65
|
+
if (i > 0 && params.delay) {
|
|
66
|
+
await new Promise((resolve) => setTimeout(resolve, params.delay));
|
|
75
67
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
68
|
+
try {
|
|
69
|
+
const result = await this.wrapDurableOperation(stepId, runStep);
|
|
70
|
+
return { ok: true, result };
|
|
71
|
+
} catch (e) {
|
|
72
|
+
if (i === params.retries) {
|
|
73
|
+
const cause = e?.cause;
|
|
74
|
+
if (cause?.status === "failed") {
|
|
75
|
+
params.stepSpan?.error({
|
|
76
|
+
error: e,
|
|
77
|
+
attributes: { status: "failed" }
|
|
78
|
+
});
|
|
79
|
+
if (cause.error && !(cause.error instanceof Error)) {
|
|
80
|
+
cause.error = getErrorFromUnknown(cause.error, { serializeStack: false });
|
|
81
|
+
}
|
|
82
|
+
return { ok: false, error: cause };
|
|
83
|
+
}
|
|
84
|
+
const errorInstance = getErrorFromUnknown(e, {
|
|
85
|
+
serializeStack: false,
|
|
86
|
+
fallbackMessage: "Unknown step execution error"
|
|
87
|
+
});
|
|
88
|
+
params.stepSpan?.error({
|
|
89
|
+
error: errorInstance,
|
|
90
|
+
attributes: { status: "failed" }
|
|
91
|
+
});
|
|
92
|
+
return {
|
|
93
|
+
ok: false,
|
|
94
|
+
error: {
|
|
95
|
+
status: "failed",
|
|
96
|
+
error: errorInstance,
|
|
97
|
+
endedAt: Date.now()
|
|
98
|
+
}
|
|
99
|
+
};
|
|
90
100
|
}
|
|
91
|
-
}
|
|
101
|
+
}
|
|
92
102
|
}
|
|
103
|
+
return { ok: false, error: { status: "failed", error: new Error("Unknown error"), endedAt: Date.now() } };
|
|
93
104
|
}
|
|
94
105
|
/**
|
|
95
106
|
* Use Inngest's sleep primitive for durability
|
|
@@ -105,28 +116,32 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
105
116
|
}
|
|
106
117
|
/**
|
|
107
118
|
* Wrap durable operations in Inngest step.run() for durability.
|
|
108
|
-
*
|
|
109
|
-
*
|
|
119
|
+
*
|
|
120
|
+
* IMPORTANT: Errors are wrapped with a cause structure before throwing.
|
|
121
|
+
* This is necessary because Inngest's error serialization (serialize-error-cjs)
|
|
122
|
+
* only captures standard Error properties (message, name, stack, code, cause).
|
|
123
|
+
* Custom properties like statusCode, responseHeaders from AI SDK errors would
|
|
124
|
+
* be lost. By putting our serialized error (via getErrorFromUnknown with toJSON())
|
|
125
|
+
* in the cause property, we ensure custom properties survive serialization.
|
|
126
|
+
* The cause property is in serialize-error-cjs's allowlist, and when the cause
|
|
127
|
+
* object is finally JSON.stringify'd, our error's toJSON() is called.
|
|
110
128
|
*/
|
|
111
|
-
async wrapDurableOperation(operationId, operationFn
|
|
129
|
+
async wrapDurableOperation(operationId, operationFn) {
|
|
112
130
|
return this.inngestStep.run(operationId, async () => {
|
|
113
131
|
try {
|
|
114
132
|
return await operationFn();
|
|
115
133
|
} catch (e) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
throw e;
|
|
134
|
+
const errorInstance = getErrorFromUnknown(e, {
|
|
135
|
+
serializeStack: false,
|
|
136
|
+
fallbackMessage: "Unknown step execution error"
|
|
137
|
+
});
|
|
138
|
+
throw new Error(errorInstance.message, {
|
|
139
|
+
cause: {
|
|
140
|
+
status: "failed",
|
|
141
|
+
error: errorInstance,
|
|
142
|
+
endedAt: Date.now()
|
|
143
|
+
}
|
|
144
|
+
});
|
|
130
145
|
}
|
|
131
146
|
});
|
|
132
147
|
}
|
|
@@ -148,6 +163,96 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
148
163
|
async invokeLifecycleCallbacksInternal(result) {
|
|
149
164
|
return super.invokeLifecycleCallbacks(result);
|
|
150
165
|
}
|
|
166
|
+
// =============================================================================
|
|
167
|
+
// Durable Span Lifecycle Hooks
|
|
168
|
+
// =============================================================================
|
|
169
|
+
/**
|
|
170
|
+
* Create a step span durably - on first execution, creates and exports span.
|
|
171
|
+
* On replay, returns cached span data without re-creating.
|
|
172
|
+
*/
|
|
173
|
+
async createStepSpan(params) {
|
|
174
|
+
const { executionContext, operationId, options, parentSpan } = params;
|
|
175
|
+
const parentSpanId = parentSpan?.id ?? executionContext.tracingIds?.workflowSpanId;
|
|
176
|
+
const exportedSpan = await this.wrapDurableOperation(operationId, async () => {
|
|
177
|
+
const observability = this.mastra?.observability?.getSelectedInstance({});
|
|
178
|
+
if (!observability) return void 0;
|
|
179
|
+
const span = observability.startSpan({
|
|
180
|
+
...options,
|
|
181
|
+
entityType: options.entityType,
|
|
182
|
+
traceId: executionContext.tracingIds?.traceId,
|
|
183
|
+
parentSpanId
|
|
184
|
+
});
|
|
185
|
+
return span?.exportSpan();
|
|
186
|
+
});
|
|
187
|
+
if (exportedSpan) {
|
|
188
|
+
const observability = this.mastra?.observability?.getSelectedInstance({});
|
|
189
|
+
return observability?.rebuildSpan(exportedSpan);
|
|
190
|
+
}
|
|
191
|
+
return void 0;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* End a step span durably.
|
|
195
|
+
*/
|
|
196
|
+
async endStepSpan(params) {
|
|
197
|
+
const { span, operationId, endOptions } = params;
|
|
198
|
+
if (!span) return;
|
|
199
|
+
await this.wrapDurableOperation(operationId, async () => {
|
|
200
|
+
span.end(endOptions);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Record error on step span durably.
|
|
205
|
+
*/
|
|
206
|
+
async errorStepSpan(params) {
|
|
207
|
+
const { span, operationId, errorOptions } = params;
|
|
208
|
+
if (!span) return;
|
|
209
|
+
await this.wrapDurableOperation(operationId, async () => {
|
|
210
|
+
span.error(errorOptions);
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Create a generic child span durably (for control-flow operations).
|
|
215
|
+
* On first execution, creates and exports span. On replay, returns cached span data.
|
|
216
|
+
*/
|
|
217
|
+
async createChildSpan(params) {
|
|
218
|
+
const { executionContext, operationId, options, parentSpan } = params;
|
|
219
|
+
const parentSpanId = parentSpan?.id ?? executionContext.tracingIds?.workflowSpanId;
|
|
220
|
+
const exportedSpan = await this.wrapDurableOperation(operationId, async () => {
|
|
221
|
+
const observability = this.mastra?.observability?.getSelectedInstance({});
|
|
222
|
+
if (!observability) return void 0;
|
|
223
|
+
const span = observability.startSpan({
|
|
224
|
+
...options,
|
|
225
|
+
traceId: executionContext.tracingIds?.traceId,
|
|
226
|
+
parentSpanId
|
|
227
|
+
});
|
|
228
|
+
return span?.exportSpan();
|
|
229
|
+
});
|
|
230
|
+
if (exportedSpan) {
|
|
231
|
+
const observability = this.mastra?.observability?.getSelectedInstance({});
|
|
232
|
+
return observability?.rebuildSpan(exportedSpan);
|
|
233
|
+
}
|
|
234
|
+
return void 0;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* End a generic child span durably (for control-flow operations).
|
|
238
|
+
*/
|
|
239
|
+
async endChildSpan(params) {
|
|
240
|
+
const { span, operationId, endOptions } = params;
|
|
241
|
+
if (!span) return;
|
|
242
|
+
await this.wrapDurableOperation(operationId, async () => {
|
|
243
|
+
span.end(endOptions);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Record error on a generic child span durably (for control-flow operations).
|
|
248
|
+
*/
|
|
249
|
+
async errorChildSpan(params) {
|
|
250
|
+
const { span, operationId, errorOptions } = params;
|
|
251
|
+
if (!span) return;
|
|
252
|
+
await this.wrapDurableOperation(operationId, async () => {
|
|
253
|
+
span.error(errorOptions);
|
|
254
|
+
});
|
|
255
|
+
}
|
|
151
256
|
/**
|
|
152
257
|
* Execute nested InngestWorkflow using inngestStep.invoke() for durability.
|
|
153
258
|
* This MUST be called directly (not inside step.run()) due to Inngest constraints.
|
|
@@ -156,7 +261,23 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
156
261
|
if (!(params.step instanceof InngestWorkflow)) {
|
|
157
262
|
return null;
|
|
158
263
|
}
|
|
159
|
-
const {
|
|
264
|
+
const {
|
|
265
|
+
step,
|
|
266
|
+
stepResults,
|
|
267
|
+
executionContext,
|
|
268
|
+
resume,
|
|
269
|
+
timeTravel,
|
|
270
|
+
prevOutput,
|
|
271
|
+
inputData,
|
|
272
|
+
pubsub,
|
|
273
|
+
startedAt,
|
|
274
|
+
perStep,
|
|
275
|
+
stepSpan
|
|
276
|
+
} = params;
|
|
277
|
+
const nestedTracingContext = executionContext.tracingIds?.traceId ? {
|
|
278
|
+
traceId: executionContext.tracingIds.traceId,
|
|
279
|
+
parentSpanId: stepSpan?.id
|
|
280
|
+
} : void 0;
|
|
160
281
|
const isResume = !!resume?.steps?.length;
|
|
161
282
|
let result;
|
|
162
283
|
let runId;
|
|
@@ -164,7 +285,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
164
285
|
try {
|
|
165
286
|
if (isResume) {
|
|
166
287
|
runId = stepResults[resume?.steps?.[0] ?? ""]?.suspendPayload?.__workflow_meta?.runId ?? randomUUID();
|
|
167
|
-
const
|
|
288
|
+
const workflowsStore = await this.mastra?.getStorage()?.getStore("workflows");
|
|
289
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
168
290
|
workflowName: step.id,
|
|
169
291
|
runId
|
|
170
292
|
});
|
|
@@ -181,14 +303,17 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
181
303
|
resumePayload: resume.resumePayload,
|
|
182
304
|
resumePath: resume.steps?.[1] ? snapshot?.suspendedPaths?.[resume.steps?.[1]] : void 0
|
|
183
305
|
},
|
|
184
|
-
outputOptions: { includeState: true }
|
|
306
|
+
outputOptions: { includeState: true },
|
|
307
|
+
perStep,
|
|
308
|
+
tracingOptions: nestedTracingContext
|
|
185
309
|
}
|
|
186
310
|
});
|
|
187
311
|
result = invokeResp.result;
|
|
188
312
|
runId = invokeResp.runId;
|
|
189
313
|
executionContext.state = invokeResp.result.state;
|
|
190
314
|
} else if (isTimeTravel) {
|
|
191
|
-
const
|
|
315
|
+
const workflowsStoreForTimeTravel = await this.mastra?.getStorage()?.getStore("workflows");
|
|
316
|
+
const snapshot = await workflowsStoreForTimeTravel?.loadWorkflowSnapshot({
|
|
192
317
|
workflowName: step.id,
|
|
193
318
|
runId: executionContext.runId
|
|
194
319
|
}) ?? { context: {} };
|
|
@@ -207,7 +332,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
207
332
|
timeTravel: timeTravelParams,
|
|
208
333
|
initialState: executionContext.state ?? {},
|
|
209
334
|
runId: executionContext.runId,
|
|
210
|
-
outputOptions: { includeState: true }
|
|
335
|
+
outputOptions: { includeState: true },
|
|
336
|
+
perStep,
|
|
337
|
+
tracingOptions: nestedTracingContext
|
|
211
338
|
}
|
|
212
339
|
});
|
|
213
340
|
result = invokeResp.result;
|
|
@@ -219,7 +346,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
219
346
|
data: {
|
|
220
347
|
inputData,
|
|
221
348
|
initialState: executionContext.state ?? {},
|
|
222
|
-
outputOptions: { includeState: true }
|
|
349
|
+
outputOptions: { includeState: true },
|
|
350
|
+
perStep,
|
|
351
|
+
tracingOptions: nestedTracingContext
|
|
223
352
|
}
|
|
224
353
|
});
|
|
225
354
|
result = invokeResp.result;
|
|
@@ -258,7 +387,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
258
387
|
}
|
|
259
388
|
}
|
|
260
389
|
});
|
|
261
|
-
return { executionContext, result: { status: "failed", error: result?.error } };
|
|
390
|
+
return { executionContext, result: { status: "failed", error: result?.error, endedAt: Date.now() } };
|
|
262
391
|
} else if (result.status === "suspended") {
|
|
263
392
|
const suspendedSteps = Object.entries(result.steps).filter(([_stepName, stepResult]) => {
|
|
264
393
|
const stepRes = stepResult;
|
|
@@ -282,6 +411,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
282
411
|
executionContext,
|
|
283
412
|
result: {
|
|
284
413
|
status: "suspended",
|
|
414
|
+
suspendedAt: Date.now(),
|
|
285
415
|
payload: stepResult.payload,
|
|
286
416
|
suspendPayload: {
|
|
287
417
|
...stepResult?.suspendPayload,
|
|
@@ -294,6 +424,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
294
424
|
executionContext,
|
|
295
425
|
result: {
|
|
296
426
|
status: "suspended",
|
|
427
|
+
suspendedAt: Date.now(),
|
|
297
428
|
payload: {}
|
|
298
429
|
}
|
|
299
430
|
};
|
|
@@ -315,9 +446,34 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
315
446
|
executionContext,
|
|
316
447
|
result: {
|
|
317
448
|
status: "tripwire",
|
|
318
|
-
tripwire: result?.tripwire
|
|
449
|
+
tripwire: result?.tripwire,
|
|
450
|
+
endedAt: Date.now()
|
|
319
451
|
}
|
|
320
452
|
};
|
|
453
|
+
} else if (perStep || result.status === "paused") {
|
|
454
|
+
await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
|
|
455
|
+
type: "watch",
|
|
456
|
+
runId: executionContext.runId,
|
|
457
|
+
data: {
|
|
458
|
+
type: "workflow-step-result",
|
|
459
|
+
payload: {
|
|
460
|
+
id: step.id,
|
|
461
|
+
status: "paused"
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
|
|
466
|
+
type: "watch",
|
|
467
|
+
runId: executionContext.runId,
|
|
468
|
+
data: {
|
|
469
|
+
type: "workflow-step-finish",
|
|
470
|
+
payload: {
|
|
471
|
+
id: step.id,
|
|
472
|
+
metadata: {}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
return { executionContext, result: { status: "paused" } };
|
|
321
477
|
}
|
|
322
478
|
await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
|
|
323
479
|
type: "watch",
|
|
@@ -342,14 +498,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
342
498
|
}
|
|
343
499
|
}
|
|
344
500
|
});
|
|
345
|
-
return { executionContext, result: { status: "success", output: result?.result } };
|
|
501
|
+
return { executionContext, result: { status: "success", output: result?.result, endedAt: Date.now() } };
|
|
346
502
|
}
|
|
347
503
|
);
|
|
348
504
|
Object.assign(executionContext, res.executionContext);
|
|
349
505
|
return {
|
|
350
506
|
...res.result,
|
|
351
507
|
startedAt,
|
|
352
|
-
endedAt: Date.now(),
|
|
353
508
|
payload: inputData,
|
|
354
509
|
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
355
510
|
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
@@ -518,6 +673,7 @@ var InngestRun = class extends Run {
|
|
|
518
673
|
async getRunOutput(eventId, maxWaitMs = 3e5) {
|
|
519
674
|
const startTime = Date.now();
|
|
520
675
|
const storage = this.#mastra?.getStorage();
|
|
676
|
+
const workflowsStore = await storage?.getStore("workflows");
|
|
521
677
|
while (Date.now() - startTime < maxWaitMs) {
|
|
522
678
|
let runs;
|
|
523
679
|
try {
|
|
@@ -534,7 +690,7 @@ var InngestRun = class extends Run {
|
|
|
534
690
|
return runs[0];
|
|
535
691
|
}
|
|
536
692
|
if (runs?.[0]?.status === "Failed") {
|
|
537
|
-
const snapshot = await
|
|
693
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
538
694
|
workflowName: this.workflowId,
|
|
539
695
|
runId: this.runId
|
|
540
696
|
});
|
|
@@ -553,7 +709,7 @@ var InngestRun = class extends Run {
|
|
|
553
709
|
};
|
|
554
710
|
}
|
|
555
711
|
if (runs?.[0]?.status === "Cancelled") {
|
|
556
|
-
const snapshot = await
|
|
712
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
557
713
|
workflowName: this.workflowId,
|
|
558
714
|
runId: this.runId
|
|
559
715
|
});
|
|
@@ -571,12 +727,13 @@ var InngestRun = class extends Run {
|
|
|
571
727
|
runId: this.runId
|
|
572
728
|
}
|
|
573
729
|
});
|
|
574
|
-
const
|
|
730
|
+
const workflowsStore = await storage?.getStore("workflows");
|
|
731
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
575
732
|
workflowName: this.workflowId,
|
|
576
733
|
runId: this.runId
|
|
577
734
|
});
|
|
578
735
|
if (snapshot) {
|
|
579
|
-
await
|
|
736
|
+
await workflowsStore?.persistWorkflowSnapshot({
|
|
580
737
|
workflowName: this.workflowId,
|
|
581
738
|
runId: this.runId,
|
|
582
739
|
resourceId: this.resourceId,
|
|
@@ -588,8 +745,8 @@ var InngestRun = class extends Run {
|
|
|
588
745
|
});
|
|
589
746
|
}
|
|
590
747
|
}
|
|
591
|
-
async start(
|
|
592
|
-
return this._start(
|
|
748
|
+
async start(args) {
|
|
749
|
+
return this._start(args);
|
|
593
750
|
}
|
|
594
751
|
/**
|
|
595
752
|
* Starts the workflow execution without waiting for completion (fire-and-forget).
|
|
@@ -597,8 +754,9 @@ var InngestRun = class extends Run {
|
|
|
597
754
|
* The workflow executes independently in Inngest.
|
|
598
755
|
* Use this when you don't need to wait for the result or want to avoid polling failures.
|
|
599
756
|
*/
|
|
600
|
-
async startAsync(
|
|
601
|
-
await this.#mastra.getStorage()?.
|
|
757
|
+
async startAsync(args) {
|
|
758
|
+
const workflowsStore = await this.#mastra.getStorage()?.getStore("workflows");
|
|
759
|
+
await workflowsStore?.persistWorkflowSnapshot({
|
|
602
760
|
workflowName: this.workflowId,
|
|
603
761
|
runId: this.runId,
|
|
604
762
|
resourceId: this.resourceId,
|
|
@@ -616,8 +774,8 @@ var InngestRun = class extends Run {
|
|
|
616
774
|
timestamp: Date.now()
|
|
617
775
|
}
|
|
618
776
|
});
|
|
619
|
-
const inputDataToUse = await this._validateInput(
|
|
620
|
-
const initialStateToUse = await this._validateInitialState(
|
|
777
|
+
const inputDataToUse = await this._validateInput(args.inputData);
|
|
778
|
+
const initialStateToUse = await this._validateInitialState(args.initialState ?? {});
|
|
621
779
|
const eventOutput = await this.inngest.send({
|
|
622
780
|
name: `workflow.${this.workflowId}`,
|
|
623
781
|
data: {
|
|
@@ -625,9 +783,10 @@ var InngestRun = class extends Run {
|
|
|
625
783
|
initialState: initialStateToUse,
|
|
626
784
|
runId: this.runId,
|
|
627
785
|
resourceId: this.resourceId,
|
|
628
|
-
outputOptions:
|
|
629
|
-
tracingOptions:
|
|
630
|
-
requestContext:
|
|
786
|
+
outputOptions: args.outputOptions,
|
|
787
|
+
tracingOptions: args.tracingOptions,
|
|
788
|
+
requestContext: args.requestContext ? Object.fromEntries(args.requestContext.entries()) : {},
|
|
789
|
+
perStep: args.perStep
|
|
631
790
|
}
|
|
632
791
|
});
|
|
633
792
|
const eventId = eventOutput.ids[0];
|
|
@@ -642,9 +801,11 @@ var InngestRun = class extends Run {
|
|
|
642
801
|
outputOptions,
|
|
643
802
|
tracingOptions,
|
|
644
803
|
format,
|
|
645
|
-
requestContext
|
|
804
|
+
requestContext,
|
|
805
|
+
perStep
|
|
646
806
|
}) {
|
|
647
|
-
await this.#mastra.getStorage()?.
|
|
807
|
+
const workflowsStore = await this.#mastra.getStorage()?.getStore("workflows");
|
|
808
|
+
await workflowsStore?.persistWorkflowSnapshot({
|
|
648
809
|
workflowName: this.workflowId,
|
|
649
810
|
runId: this.runId,
|
|
650
811
|
resourceId: this.resourceId,
|
|
@@ -674,7 +835,8 @@ var InngestRun = class extends Run {
|
|
|
674
835
|
outputOptions,
|
|
675
836
|
tracingOptions,
|
|
676
837
|
format,
|
|
677
|
-
requestContext: requestContext ? Object.fromEntries(requestContext.entries()) : {}
|
|
838
|
+
requestContext: requestContext ? Object.fromEntries(requestContext.entries()) : {},
|
|
839
|
+
perStep
|
|
678
840
|
}
|
|
679
841
|
});
|
|
680
842
|
const eventId = eventOutput.ids[0];
|
|
@@ -710,7 +872,8 @@ var InngestRun = class extends Run {
|
|
|
710
872
|
(step) => typeof step === "string" ? step : step?.id
|
|
711
873
|
);
|
|
712
874
|
}
|
|
713
|
-
const
|
|
875
|
+
const workflowsStore = await storage?.getStore("workflows");
|
|
876
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
714
877
|
workflowName: this.workflowId,
|
|
715
878
|
runId: this.runId
|
|
716
879
|
});
|
|
@@ -733,7 +896,8 @@ var InngestRun = class extends Run {
|
|
|
733
896
|
resumePayload: resumeDataToUse,
|
|
734
897
|
resumePath: steps?.[0] ? snapshot?.suspendedPaths?.[steps?.[0]] : void 0
|
|
735
898
|
},
|
|
736
|
-
requestContext: mergedRequestContext
|
|
899
|
+
requestContext: mergedRequestContext,
|
|
900
|
+
perStep: params.perStep
|
|
737
901
|
}
|
|
738
902
|
});
|
|
739
903
|
const eventId = eventOutput.ids[0];
|
|
@@ -772,12 +936,13 @@ var InngestRun = class extends Run {
|
|
|
772
936
|
throw new Error("No steps provided to timeTravel");
|
|
773
937
|
}
|
|
774
938
|
const storage = this.#mastra?.getStorage();
|
|
775
|
-
const
|
|
939
|
+
const workflowsStore = await storage?.getStore("workflows");
|
|
940
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
776
941
|
workflowName: this.workflowId,
|
|
777
942
|
runId: this.runId
|
|
778
943
|
});
|
|
779
944
|
if (!snapshot) {
|
|
780
|
-
await
|
|
945
|
+
await workflowsStore?.persistWorkflowSnapshot({
|
|
781
946
|
workflowName: this.workflowId,
|
|
782
947
|
runId: this.runId,
|
|
783
948
|
resourceId: this.resourceId,
|
|
@@ -811,7 +976,8 @@ var InngestRun = class extends Run {
|
|
|
811
976
|
nestedStepsContext: params.nestedStepsContext,
|
|
812
977
|
snapshot: snapshot ?? { context: {} },
|
|
813
978
|
graph: this.executionGraph,
|
|
814
|
-
initialState: params.initialState
|
|
979
|
+
initialState: params.initialState,
|
|
980
|
+
perStep: params.perStep
|
|
815
981
|
});
|
|
816
982
|
const eventOutput = await this.inngest.send({
|
|
817
983
|
name: `workflow.${this.workflowId}`,
|
|
@@ -823,7 +989,8 @@ var InngestRun = class extends Run {
|
|
|
823
989
|
timeTravel: timeTravelData,
|
|
824
990
|
tracingOptions: params.tracingOptions,
|
|
825
991
|
outputOptions: params.outputOptions,
|
|
826
|
-
requestContext: params.requestContext ? Object.fromEntries(params.requestContext.entries()) : {}
|
|
992
|
+
requestContext: params.requestContext ? Object.fromEntries(params.requestContext.entries()) : {},
|
|
993
|
+
perStep: params.perStep
|
|
827
994
|
}
|
|
828
995
|
});
|
|
829
996
|
const eventId = eventOutput.ids[0];
|
|
@@ -914,7 +1081,8 @@ var InngestRun = class extends Run {
|
|
|
914
1081
|
tracingOptions,
|
|
915
1082
|
closeOnSuspend = true,
|
|
916
1083
|
initialState,
|
|
917
|
-
outputOptions
|
|
1084
|
+
outputOptions,
|
|
1085
|
+
perStep
|
|
918
1086
|
} = {}) {
|
|
919
1087
|
if (this.closeStreamAction && this.streamOutput) {
|
|
920
1088
|
return this.streamOutput;
|
|
@@ -950,7 +1118,8 @@ var InngestRun = class extends Run {
|
|
|
950
1118
|
initialState,
|
|
951
1119
|
tracingOptions,
|
|
952
1120
|
outputOptions,
|
|
953
|
-
format: "vnext"
|
|
1121
|
+
format: "vnext",
|
|
1122
|
+
perStep
|
|
954
1123
|
});
|
|
955
1124
|
let executionResults;
|
|
956
1125
|
try {
|
|
@@ -981,9 +1150,6 @@ var InngestRun = class extends Run {
|
|
|
981
1150
|
});
|
|
982
1151
|
return this.streamOutput;
|
|
983
1152
|
}
|
|
984
|
-
streamVNext(args = {}) {
|
|
985
|
-
return this.stream(args);
|
|
986
|
-
}
|
|
987
1153
|
timeTravelStream({
|
|
988
1154
|
inputData,
|
|
989
1155
|
resumeData,
|
|
@@ -992,8 +1158,10 @@ var InngestRun = class extends Run {
|
|
|
992
1158
|
context,
|
|
993
1159
|
nestedStepsContext,
|
|
994
1160
|
requestContext,
|
|
1161
|
+
// tracingContext,
|
|
995
1162
|
tracingOptions,
|
|
996
|
-
outputOptions
|
|
1163
|
+
outputOptions,
|
|
1164
|
+
perStep
|
|
997
1165
|
}) {
|
|
998
1166
|
this.closeStreamAction = async () => {
|
|
999
1167
|
};
|
|
@@ -1028,7 +1196,8 @@ var InngestRun = class extends Run {
|
|
|
1028
1196
|
initialState,
|
|
1029
1197
|
requestContext,
|
|
1030
1198
|
tracingOptions,
|
|
1031
|
-
outputOptions
|
|
1199
|
+
outputOptions,
|
|
1200
|
+
perStep
|
|
1032
1201
|
});
|
|
1033
1202
|
self.executionResults = executionResultsPromise;
|
|
1034
1203
|
let executionResults;
|
|
@@ -1072,9 +1241,11 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1072
1241
|
#mastra;
|
|
1073
1242
|
inngest;
|
|
1074
1243
|
function;
|
|
1244
|
+
cronFunction;
|
|
1075
1245
|
flowControlConfig;
|
|
1246
|
+
cronConfig;
|
|
1076
1247
|
constructor(params, inngest) {
|
|
1077
|
-
const { concurrency, rateLimit, throttle, debounce, priority, ...workflowParams } = params;
|
|
1248
|
+
const { concurrency, rateLimit, throttle, debounce, priority, cron, inputData, initialState, ...workflowParams } = params;
|
|
1078
1249
|
super(workflowParams);
|
|
1079
1250
|
this.engineType = "inngest";
|
|
1080
1251
|
const flowControlEntries = Object.entries({ concurrency, rateLimit, throttle, debounce, priority }).filter(
|
|
@@ -1083,6 +1254,9 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1083
1254
|
this.flowControlConfig = flowControlEntries.length > 0 ? Object.fromEntries(flowControlEntries) : void 0;
|
|
1084
1255
|
this.#mastra = params.mastra;
|
|
1085
1256
|
this.inngest = inngest;
|
|
1257
|
+
if (cron) {
|
|
1258
|
+
this.cronConfig = { cron, inputData, initialState };
|
|
1259
|
+
}
|
|
1086
1260
|
}
|
|
1087
1261
|
async listWorkflowRuns(args) {
|
|
1088
1262
|
const storage = this.#mastra?.getStorage();
|
|
@@ -1090,16 +1264,11 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1090
1264
|
this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
|
|
1091
1265
|
return { runs: [], total: 0 };
|
|
1092
1266
|
}
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
const storage = this.#mastra?.getStorage();
|
|
1097
|
-
if (!storage) {
|
|
1098
|
-
this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
|
|
1099
|
-
return this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null;
|
|
1267
|
+
const workflowsStore = await storage.getStore("workflows");
|
|
1268
|
+
if (!workflowsStore) {
|
|
1269
|
+
return { runs: [], total: 0 };
|
|
1100
1270
|
}
|
|
1101
|
-
|
|
1102
|
-
return run ?? (this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null);
|
|
1271
|
+
return workflowsStore.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
|
|
1103
1272
|
}
|
|
1104
1273
|
__registerMastra(mastra) {
|
|
1105
1274
|
super.__registerMastra(mastra);
|
|
@@ -1122,7 +1291,8 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1122
1291
|
}
|
|
1123
1292
|
async createRun(options) {
|
|
1124
1293
|
const runIdToUse = options?.runId || randomUUID();
|
|
1125
|
-
const
|
|
1294
|
+
const existingInMemoryRun = this.runs.get(runIdToUse);
|
|
1295
|
+
const newRun = new InngestRun(
|
|
1126
1296
|
{
|
|
1127
1297
|
workflowId: this.id,
|
|
1128
1298
|
runId: runIdToUse,
|
|
@@ -1139,14 +1309,19 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1139
1309
|
},
|
|
1140
1310
|
this.inngest
|
|
1141
1311
|
);
|
|
1312
|
+
const run = existingInMemoryRun ?? newRun;
|
|
1142
1313
|
this.runs.set(runIdToUse, run);
|
|
1143
1314
|
const shouldPersistSnapshot = this.options.shouldPersistSnapshot({
|
|
1144
1315
|
workflowStatus: run.workflowRunStatus,
|
|
1145
1316
|
stepResults: {}
|
|
1146
1317
|
});
|
|
1147
|
-
const
|
|
1148
|
-
|
|
1149
|
-
|
|
1318
|
+
const existingStoredRun = await this.getWorkflowRunById(runIdToUse, {
|
|
1319
|
+
withNestedWorkflows: false
|
|
1320
|
+
});
|
|
1321
|
+
const existsInStorage = existingStoredRun && !existingStoredRun.isFromInMemory;
|
|
1322
|
+
if (!existsInStorage && shouldPersistSnapshot) {
|
|
1323
|
+
const workflowsStore = await this.mastra?.getStorage()?.getStore("workflows");
|
|
1324
|
+
await workflowsStore?.persistWorkflowSnapshot({
|
|
1150
1325
|
workflowName: this.id,
|
|
1151
1326
|
runId: runIdToUse,
|
|
1152
1327
|
resourceId: options?.resourceId,
|
|
@@ -1169,6 +1344,30 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1169
1344
|
}
|
|
1170
1345
|
return run;
|
|
1171
1346
|
}
|
|
1347
|
+
//createCronFunction is only called if cronConfig.cron is defined.
|
|
1348
|
+
createCronFunction() {
|
|
1349
|
+
if (this.cronFunction) {
|
|
1350
|
+
return this.cronFunction;
|
|
1351
|
+
}
|
|
1352
|
+
this.cronFunction = this.inngest.createFunction(
|
|
1353
|
+
{
|
|
1354
|
+
id: `workflow.${this.id}.cron`,
|
|
1355
|
+
retries: 0,
|
|
1356
|
+
cancelOn: [{ event: `cancel.workflow.${this.id}` }],
|
|
1357
|
+
...this.flowControlConfig
|
|
1358
|
+
},
|
|
1359
|
+
{ cron: this.cronConfig?.cron ?? "" },
|
|
1360
|
+
async () => {
|
|
1361
|
+
const run = await this.createRun();
|
|
1362
|
+
const result = await run.start({
|
|
1363
|
+
inputData: this.cronConfig?.inputData,
|
|
1364
|
+
initialState: this.cronConfig?.initialState
|
|
1365
|
+
});
|
|
1366
|
+
return { result, runId: run.runId };
|
|
1367
|
+
}
|
|
1368
|
+
);
|
|
1369
|
+
return this.cronFunction;
|
|
1370
|
+
}
|
|
1172
1371
|
getFunction() {
|
|
1173
1372
|
if (this.function) {
|
|
1174
1373
|
return this.function;
|
|
@@ -1176,52 +1375,128 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1176
1375
|
this.function = this.inngest.createFunction(
|
|
1177
1376
|
{
|
|
1178
1377
|
id: `workflow.${this.id}`,
|
|
1179
|
-
retries:
|
|
1378
|
+
retries: 0,
|
|
1180
1379
|
cancelOn: [{ event: `cancel.workflow.${this.id}` }],
|
|
1181
1380
|
// Spread flow control configuration
|
|
1182
1381
|
...this.flowControlConfig
|
|
1183
1382
|
},
|
|
1184
1383
|
{ event: `workflow.${this.id}` },
|
|
1185
1384
|
async ({ event, step, attempt, publish }) => {
|
|
1186
|
-
let {
|
|
1385
|
+
let {
|
|
1386
|
+
inputData,
|
|
1387
|
+
initialState,
|
|
1388
|
+
runId,
|
|
1389
|
+
resourceId,
|
|
1390
|
+
resume,
|
|
1391
|
+
outputOptions,
|
|
1392
|
+
format,
|
|
1393
|
+
timeTravel,
|
|
1394
|
+
perStep,
|
|
1395
|
+
tracingOptions
|
|
1396
|
+
} = event.data;
|
|
1187
1397
|
if (!runId) {
|
|
1188
1398
|
runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
|
|
1189
1399
|
return randomUUID();
|
|
1190
1400
|
});
|
|
1191
1401
|
}
|
|
1192
1402
|
const pubsub = new InngestPubSub(this.inngest, this.id, publish);
|
|
1403
|
+
const requestContext = new RequestContext(Object.entries(event.data.requestContext ?? {}));
|
|
1404
|
+
const mastra = this.#mastra;
|
|
1405
|
+
const tracingPolicy = this.options.tracingPolicy;
|
|
1406
|
+
const workflowSpanData = await step.run(`workflow.${this.id}.span.start`, async () => {
|
|
1407
|
+
const observability = mastra?.observability?.getSelectedInstance({ requestContext });
|
|
1408
|
+
if (!observability) return void 0;
|
|
1409
|
+
const span = observability.startSpan({
|
|
1410
|
+
type: SpanType.WORKFLOW_RUN,
|
|
1411
|
+
name: `workflow run: '${this.id}'`,
|
|
1412
|
+
entityType: EntityType.WORKFLOW_RUN,
|
|
1413
|
+
entityId: this.id,
|
|
1414
|
+
input: inputData,
|
|
1415
|
+
metadata: {
|
|
1416
|
+
resourceId,
|
|
1417
|
+
runId
|
|
1418
|
+
},
|
|
1419
|
+
tracingPolicy,
|
|
1420
|
+
tracingOptions,
|
|
1421
|
+
requestContext
|
|
1422
|
+
});
|
|
1423
|
+
return span?.exportSpan();
|
|
1424
|
+
});
|
|
1193
1425
|
const engine = new InngestExecutionEngine(this.#mastra, step, attempt, this.options);
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
}
|
|
1219
|
-
|
|
1426
|
+
let result;
|
|
1427
|
+
try {
|
|
1428
|
+
result = await engine.execute({
|
|
1429
|
+
workflowId: this.id,
|
|
1430
|
+
runId,
|
|
1431
|
+
resourceId,
|
|
1432
|
+
graph: this.executionGraph,
|
|
1433
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
1434
|
+
input: inputData,
|
|
1435
|
+
initialState,
|
|
1436
|
+
pubsub,
|
|
1437
|
+
retryConfig: this.retryConfig,
|
|
1438
|
+
requestContext,
|
|
1439
|
+
resume,
|
|
1440
|
+
timeTravel,
|
|
1441
|
+
perStep,
|
|
1442
|
+
format,
|
|
1443
|
+
abortController: new AbortController(),
|
|
1444
|
+
// For Inngest, we don't pass workflowSpan - step spans use tracingIds instead
|
|
1445
|
+
workflowSpan: void 0,
|
|
1446
|
+
// Pass tracing IDs for durable span operations
|
|
1447
|
+
tracingIds: workflowSpanData ? {
|
|
1448
|
+
traceId: workflowSpanData.traceId,
|
|
1449
|
+
workflowSpanId: workflowSpanData.id
|
|
1450
|
+
} : void 0,
|
|
1451
|
+
outputOptions,
|
|
1452
|
+
outputWriter: async (chunk) => {
|
|
1453
|
+
try {
|
|
1454
|
+
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1455
|
+
type: "watch",
|
|
1456
|
+
runId,
|
|
1457
|
+
data: chunk
|
|
1458
|
+
});
|
|
1459
|
+
} catch (err) {
|
|
1460
|
+
this.logger.debug?.("Failed to publish watch event:", err);
|
|
1461
|
+
}
|
|
1220
1462
|
}
|
|
1221
|
-
}
|
|
1222
|
-
})
|
|
1463
|
+
});
|
|
1464
|
+
} catch (error) {
|
|
1465
|
+
throw error;
|
|
1466
|
+
}
|
|
1223
1467
|
await step.run(`workflow.${this.id}.finalize`, async () => {
|
|
1224
|
-
|
|
1468
|
+
if (result.status !== "paused") {
|
|
1469
|
+
await engine.invokeLifecycleCallbacksInternal({
|
|
1470
|
+
status: result.status,
|
|
1471
|
+
result: "result" in result ? result.result : void 0,
|
|
1472
|
+
error: "error" in result ? result.error : void 0,
|
|
1473
|
+
steps: result.steps,
|
|
1474
|
+
tripwire: "tripwire" in result ? result.tripwire : void 0,
|
|
1475
|
+
runId,
|
|
1476
|
+
workflowId: this.id,
|
|
1477
|
+
resourceId,
|
|
1478
|
+
input: inputData,
|
|
1479
|
+
requestContext,
|
|
1480
|
+
state: result.state ?? initialState ?? {}
|
|
1481
|
+
});
|
|
1482
|
+
}
|
|
1483
|
+
if (workflowSpanData) {
|
|
1484
|
+
const observability = mastra?.observability?.getSelectedInstance({ requestContext });
|
|
1485
|
+
if (observability) {
|
|
1486
|
+
const workflowSpan = observability.rebuildSpan(workflowSpanData);
|
|
1487
|
+
if (result.status === "failed") {
|
|
1488
|
+
workflowSpan.error({
|
|
1489
|
+
error: result.error instanceof Error ? result.error : new Error(String(result.error)),
|
|
1490
|
+
attributes: { status: "failed" }
|
|
1491
|
+
});
|
|
1492
|
+
} else {
|
|
1493
|
+
workflowSpan.end({
|
|
1494
|
+
output: result.status === "success" ? result.result : void 0,
|
|
1495
|
+
attributes: { status: result.status }
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1225
1500
|
if (result.status === "failed") {
|
|
1226
1501
|
throw new NonRetriableError(`Workflow failed`, {
|
|
1227
1502
|
cause: result
|
|
@@ -1248,15 +1523,14 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1248
1523
|
});
|
|
1249
1524
|
}
|
|
1250
1525
|
getFunctions() {
|
|
1251
|
-
return [
|
|
1526
|
+
return [
|
|
1527
|
+
this.getFunction(),
|
|
1528
|
+
...this.cronConfig?.cron ? [this.createCronFunction()] : [],
|
|
1529
|
+
...this.getNestedFunctions(this.executionGraph.steps)
|
|
1530
|
+
];
|
|
1252
1531
|
}
|
|
1253
1532
|
};
|
|
1254
|
-
function
|
|
1255
|
-
mastra,
|
|
1256
|
-
inngest,
|
|
1257
|
-
functions: userFunctions = [],
|
|
1258
|
-
registerOptions
|
|
1259
|
-
}) {
|
|
1533
|
+
function prepareServeOptions({ mastra, inngest, functions: userFunctions = [], registerOptions }) {
|
|
1260
1534
|
const wfs = mastra.listWorkflows();
|
|
1261
1535
|
const workflowFunctions = Array.from(
|
|
1262
1536
|
new Set(
|
|
@@ -1269,177 +1543,181 @@ function serve({
|
|
|
1269
1543
|
})
|
|
1270
1544
|
)
|
|
1271
1545
|
);
|
|
1272
|
-
return
|
|
1546
|
+
return {
|
|
1273
1547
|
...registerOptions,
|
|
1274
1548
|
client: inngest,
|
|
1275
1549
|
functions: [...workflowFunctions, ...userFunctions]
|
|
1276
|
-
}
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1552
|
+
function createServe(adapter) {
|
|
1553
|
+
return (options) => {
|
|
1554
|
+
const serveOptions = prepareServeOptions(options);
|
|
1555
|
+
return adapter(serveOptions);
|
|
1556
|
+
};
|
|
1277
1557
|
}
|
|
1558
|
+
var serve = createServe(serve$1);
|
|
1278
1559
|
|
|
1279
1560
|
// src/types.ts
|
|
1280
1561
|
var _compatibilityCheck = true;
|
|
1281
1562
|
|
|
1282
1563
|
// src/index.ts
|
|
1283
|
-
function
|
|
1284
|
-
return
|
|
1564
|
+
function isInngestWorkflow(input) {
|
|
1565
|
+
return input instanceof InngestWorkflow;
|
|
1285
1566
|
}
|
|
1286
|
-
function
|
|
1287
|
-
return
|
|
1567
|
+
function isAgent(input) {
|
|
1568
|
+
return input instanceof Agent;
|
|
1288
1569
|
}
|
|
1289
|
-
function
|
|
1290
|
-
return
|
|
1570
|
+
function isToolStep(input) {
|
|
1571
|
+
return input instanceof Tool;
|
|
1291
1572
|
}
|
|
1292
|
-
function
|
|
1573
|
+
function isStepParams(input) {
|
|
1574
|
+
return input !== null && typeof input === "object" && "id" in input && "execute" in input && !(input instanceof Agent) && !(input instanceof Tool) && !(input instanceof InngestWorkflow);
|
|
1575
|
+
}
|
|
1576
|
+
function isProcessor(obj) {
|
|
1577
|
+
return obj !== null && typeof obj === "object" && "id" in obj && typeof obj.id === "string" && !(obj instanceof Agent) && !(obj instanceof Tool) && !(obj instanceof InngestWorkflow) && (typeof obj.processInput === "function" || typeof obj.processInputStep === "function" || typeof obj.processOutputStream === "function" || typeof obj.processOutputResult === "function" || typeof obj.processOutputStep === "function");
|
|
1578
|
+
}
|
|
1579
|
+
function createStep(params, agentOrToolOptions) {
|
|
1293
1580
|
if (isInngestWorkflow(params)) {
|
|
1294
1581
|
return params;
|
|
1295
1582
|
}
|
|
1296
1583
|
if (isAgent(params)) {
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1584
|
+
return createStepFromAgent(params, agentOrToolOptions);
|
|
1585
|
+
}
|
|
1586
|
+
if (isToolStep(params)) {
|
|
1587
|
+
return createStepFromTool(params, agentOrToolOptions);
|
|
1588
|
+
}
|
|
1589
|
+
if (isStepParams(params)) {
|
|
1590
|
+
return createStepFromParams(params);
|
|
1591
|
+
}
|
|
1592
|
+
if (isProcessor(params)) {
|
|
1593
|
+
return createStepFromProcessor(params);
|
|
1594
|
+
}
|
|
1595
|
+
throw new Error("Invalid input: expected StepParams, Agent, ToolStep, Processor, or InngestWorkflow");
|
|
1596
|
+
}
|
|
1597
|
+
function createStepFromParams(params) {
|
|
1598
|
+
return {
|
|
1599
|
+
id: params.id,
|
|
1600
|
+
description: params.description,
|
|
1601
|
+
inputSchema: params.inputSchema,
|
|
1602
|
+
stateSchema: params.stateSchema,
|
|
1603
|
+
outputSchema: params.outputSchema,
|
|
1604
|
+
resumeSchema: params.resumeSchema,
|
|
1605
|
+
suspendSchema: params.suspendSchema,
|
|
1606
|
+
scorers: params.scorers,
|
|
1607
|
+
retries: params.retries,
|
|
1608
|
+
execute: params.execute.bind(params)
|
|
1609
|
+
};
|
|
1610
|
+
}
|
|
1611
|
+
function createStepFromAgent(params, agentOrToolOptions) {
|
|
1612
|
+
const options = agentOrToolOptions ?? {};
|
|
1613
|
+
const outputSchema = options?.structuredOutput?.schema ?? z.object({ text: z.string() });
|
|
1614
|
+
const { retries, scorers, ...agentOptions } = options ?? {};
|
|
1615
|
+
return {
|
|
1616
|
+
id: params.name,
|
|
1617
|
+
description: params.getDescription(),
|
|
1618
|
+
inputSchema: z.object({
|
|
1619
|
+
prompt: z.string()
|
|
1620
|
+
}),
|
|
1621
|
+
outputSchema,
|
|
1622
|
+
retries,
|
|
1623
|
+
scorers,
|
|
1624
|
+
execute: async ({
|
|
1625
|
+
inputData,
|
|
1626
|
+
runId,
|
|
1627
|
+
[PUBSUB_SYMBOL]: pubsub,
|
|
1628
|
+
[STREAM_FORMAT_SYMBOL]: streamFormat,
|
|
1629
|
+
requestContext,
|
|
1630
|
+
tracingContext,
|
|
1631
|
+
abortSignal,
|
|
1632
|
+
abort,
|
|
1633
|
+
writer
|
|
1634
|
+
}) => {
|
|
1635
|
+
let streamPromise = {};
|
|
1636
|
+
streamPromise.promise = new Promise((resolve, reject) => {
|
|
1637
|
+
streamPromise.resolve = resolve;
|
|
1638
|
+
streamPromise.reject = reject;
|
|
1639
|
+
});
|
|
1640
|
+
let structuredResult = null;
|
|
1641
|
+
const toolData = {
|
|
1642
|
+
name: params.name,
|
|
1643
|
+
args: inputData
|
|
1644
|
+
};
|
|
1645
|
+
let stream;
|
|
1646
|
+
if ((await params.getModel()).specificationVersion === "v1") {
|
|
1647
|
+
const { fullStream } = await params.streamLegacy(inputData.prompt, {
|
|
1648
|
+
...agentOptions ?? {},
|
|
1649
|
+
requestContext,
|
|
1650
|
+
tracingContext,
|
|
1651
|
+
onFinish: (result) => {
|
|
1652
|
+
const resultWithObject = result;
|
|
1653
|
+
if (agentOptions?.structuredOutput?.schema && resultWithObject.object) {
|
|
1654
|
+
structuredResult = resultWithObject.object;
|
|
1655
|
+
}
|
|
1656
|
+
streamPromise.resolve(result.text);
|
|
1657
|
+
void agentOptions?.onFinish?.(result);
|
|
1658
|
+
},
|
|
1659
|
+
abortSignal
|
|
1322
1660
|
});
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
// threadId: inputData.threadId,
|
|
1334
|
-
requestContext,
|
|
1335
|
-
tracingContext,
|
|
1336
|
-
onFinish: (result) => {
|
|
1337
|
-
const resultWithObject = result;
|
|
1338
|
-
if (agentOptions?.structuredOutput?.schema && resultWithObject.object) {
|
|
1339
|
-
structuredResult = resultWithObject.object;
|
|
1340
|
-
}
|
|
1341
|
-
streamPromise.resolve(result.text);
|
|
1342
|
-
void agentOptions?.onFinish?.(result);
|
|
1343
|
-
},
|
|
1344
|
-
abortSignal
|
|
1345
|
-
});
|
|
1346
|
-
stream = fullStream;
|
|
1347
|
-
} else {
|
|
1348
|
-
const modelOutput = await params.stream(inputData.prompt, {
|
|
1349
|
-
...agentOptions ?? {},
|
|
1350
|
-
requestContext,
|
|
1351
|
-
tracingContext,
|
|
1352
|
-
onFinish: (result) => {
|
|
1353
|
-
const resultWithObject = result;
|
|
1354
|
-
if (agentOptions?.structuredOutput?.schema && resultWithObject.object) {
|
|
1355
|
-
structuredResult = resultWithObject.object;
|
|
1356
|
-
}
|
|
1357
|
-
streamPromise.resolve(result.text);
|
|
1358
|
-
void agentOptions?.onFinish?.(result);
|
|
1359
|
-
},
|
|
1360
|
-
abortSignal
|
|
1361
|
-
});
|
|
1362
|
-
stream = modelOutput.fullStream;
|
|
1363
|
-
}
|
|
1364
|
-
if (streamFormat === "legacy") {
|
|
1365
|
-
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1366
|
-
type: "watch",
|
|
1367
|
-
runId,
|
|
1368
|
-
data: { type: "tool-call-streaming-start", ...toolData ?? {} }
|
|
1369
|
-
});
|
|
1370
|
-
for await (const chunk of stream) {
|
|
1371
|
-
if (chunk.type === "text-delta") {
|
|
1372
|
-
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1373
|
-
type: "watch",
|
|
1374
|
-
runId,
|
|
1375
|
-
data: { type: "tool-call-delta", ...toolData ?? {}, argsTextDelta: chunk.textDelta }
|
|
1376
|
-
});
|
|
1661
|
+
stream = fullStream;
|
|
1662
|
+
} else {
|
|
1663
|
+
const modelOutput = await params.stream(inputData.prompt, {
|
|
1664
|
+
...agentOptions ?? {},
|
|
1665
|
+
requestContext,
|
|
1666
|
+
tracingContext,
|
|
1667
|
+
onFinish: (result) => {
|
|
1668
|
+
const resultWithObject = result;
|
|
1669
|
+
if (agentOptions?.structuredOutput?.schema && resultWithObject.object) {
|
|
1670
|
+
structuredResult = resultWithObject.object;
|
|
1377
1671
|
}
|
|
1672
|
+
streamPromise.resolve(result.text);
|
|
1673
|
+
void agentOptions?.onFinish?.(result);
|
|
1674
|
+
},
|
|
1675
|
+
abortSignal
|
|
1676
|
+
});
|
|
1677
|
+
stream = modelOutput.fullStream;
|
|
1678
|
+
}
|
|
1679
|
+
if (streamFormat === "legacy") {
|
|
1680
|
+
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1681
|
+
type: "watch",
|
|
1682
|
+
runId,
|
|
1683
|
+
data: { type: "tool-call-streaming-start", ...toolData ?? {} }
|
|
1684
|
+
});
|
|
1685
|
+
for await (const chunk of stream) {
|
|
1686
|
+
if (chunk.type === "text-delta") {
|
|
1687
|
+
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1688
|
+
type: "watch",
|
|
1689
|
+
runId,
|
|
1690
|
+
data: { type: "tool-call-delta", ...toolData ?? {}, argsTextDelta: chunk.textDelta }
|
|
1691
|
+
});
|
|
1378
1692
|
}
|
|
1379
|
-
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1380
|
-
type: "watch",
|
|
1381
|
-
runId,
|
|
1382
|
-
data: { type: "tool-call-streaming-finish", ...toolData ?? {} }
|
|
1383
|
-
});
|
|
1384
|
-
} else {
|
|
1385
|
-
for await (const chunk of stream) {
|
|
1386
|
-
await writer.write(chunk);
|
|
1387
|
-
}
|
|
1388
|
-
}
|
|
1389
|
-
if (abortSignal.aborted) {
|
|
1390
|
-
return abort();
|
|
1391
1693
|
}
|
|
1392
|
-
|
|
1393
|
-
|
|
1694
|
+
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1695
|
+
type: "watch",
|
|
1696
|
+
runId,
|
|
1697
|
+
data: { type: "tool-call-streaming-finish", ...toolData ?? {} }
|
|
1698
|
+
});
|
|
1699
|
+
} else {
|
|
1700
|
+
for await (const chunk of stream) {
|
|
1701
|
+
await writer.write(chunk);
|
|
1394
1702
|
}
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
}
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
}
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
resumeSchema: params.resumeSchema,
|
|
1414
|
-
execute: async ({
|
|
1415
|
-
inputData,
|
|
1416
|
-
mastra,
|
|
1417
|
-
requestContext,
|
|
1418
|
-
tracingContext,
|
|
1419
|
-
suspend,
|
|
1420
|
-
resumeData,
|
|
1421
|
-
runId,
|
|
1422
|
-
workflowId,
|
|
1423
|
-
state,
|
|
1424
|
-
setState
|
|
1425
|
-
}) => {
|
|
1426
|
-
const toolContext = {
|
|
1427
|
-
mastra,
|
|
1428
|
-
requestContext,
|
|
1429
|
-
tracingContext,
|
|
1430
|
-
workflow: {
|
|
1431
|
-
runId,
|
|
1432
|
-
resumeData,
|
|
1433
|
-
suspend,
|
|
1434
|
-
workflowId,
|
|
1435
|
-
state,
|
|
1436
|
-
setState
|
|
1437
|
-
}
|
|
1438
|
-
};
|
|
1439
|
-
return params.execute(inputData, toolContext);
|
|
1440
|
-
},
|
|
1441
|
-
component: "TOOL"
|
|
1442
|
-
};
|
|
1703
|
+
}
|
|
1704
|
+
if (abortSignal.aborted) {
|
|
1705
|
+
return abort();
|
|
1706
|
+
}
|
|
1707
|
+
if (structuredResult !== null) {
|
|
1708
|
+
return structuredResult;
|
|
1709
|
+
}
|
|
1710
|
+
return {
|
|
1711
|
+
text: await streamPromise.promise
|
|
1712
|
+
};
|
|
1713
|
+
},
|
|
1714
|
+
component: params.component
|
|
1715
|
+
};
|
|
1716
|
+
}
|
|
1717
|
+
function createStepFromTool(params, agentOrToolOptions) {
|
|
1718
|
+
const toolOpts = agentOrToolOptions;
|
|
1719
|
+
if (!params.inputSchema || !params.outputSchema) {
|
|
1720
|
+
throw new Error("Tool must have input and output schemas defined");
|
|
1443
1721
|
}
|
|
1444
1722
|
return {
|
|
1445
1723
|
id: params.id,
|
|
@@ -1448,7 +1726,480 @@ function createStep(params, agentOptions) {
|
|
|
1448
1726
|
outputSchema: params.outputSchema,
|
|
1449
1727
|
resumeSchema: params.resumeSchema,
|
|
1450
1728
|
suspendSchema: params.suspendSchema,
|
|
1451
|
-
|
|
1729
|
+
retries: toolOpts?.retries,
|
|
1730
|
+
scorers: toolOpts?.scorers,
|
|
1731
|
+
execute: async ({
|
|
1732
|
+
inputData,
|
|
1733
|
+
mastra,
|
|
1734
|
+
requestContext,
|
|
1735
|
+
tracingContext,
|
|
1736
|
+
suspend,
|
|
1737
|
+
resumeData,
|
|
1738
|
+
runId,
|
|
1739
|
+
workflowId,
|
|
1740
|
+
state,
|
|
1741
|
+
setState
|
|
1742
|
+
}) => {
|
|
1743
|
+
const toolContext = {
|
|
1744
|
+
mastra,
|
|
1745
|
+
requestContext,
|
|
1746
|
+
tracingContext,
|
|
1747
|
+
workflow: {
|
|
1748
|
+
runId,
|
|
1749
|
+
resumeData,
|
|
1750
|
+
suspend,
|
|
1751
|
+
workflowId,
|
|
1752
|
+
state,
|
|
1753
|
+
setState
|
|
1754
|
+
}
|
|
1755
|
+
};
|
|
1756
|
+
return params.execute(inputData, toolContext);
|
|
1757
|
+
},
|
|
1758
|
+
component: "TOOL"
|
|
1759
|
+
};
|
|
1760
|
+
}
|
|
1761
|
+
function createStepFromProcessor(processor) {
|
|
1762
|
+
const getProcessorEntityType = (phase) => {
|
|
1763
|
+
switch (phase) {
|
|
1764
|
+
case "input":
|
|
1765
|
+
return EntityType.INPUT_PROCESSOR;
|
|
1766
|
+
case "inputStep":
|
|
1767
|
+
return EntityType.INPUT_STEP_PROCESSOR;
|
|
1768
|
+
case "outputStream":
|
|
1769
|
+
case "outputResult":
|
|
1770
|
+
return EntityType.OUTPUT_PROCESSOR;
|
|
1771
|
+
case "outputStep":
|
|
1772
|
+
return EntityType.OUTPUT_STEP_PROCESSOR;
|
|
1773
|
+
default:
|
|
1774
|
+
return EntityType.OUTPUT_PROCESSOR;
|
|
1775
|
+
}
|
|
1776
|
+
};
|
|
1777
|
+
const getSpanNamePrefix = (phase) => {
|
|
1778
|
+
switch (phase) {
|
|
1779
|
+
case "input":
|
|
1780
|
+
return "input processor";
|
|
1781
|
+
case "inputStep":
|
|
1782
|
+
return "input step processor";
|
|
1783
|
+
case "outputStream":
|
|
1784
|
+
return "output stream processor";
|
|
1785
|
+
case "outputResult":
|
|
1786
|
+
return "output processor";
|
|
1787
|
+
case "outputStep":
|
|
1788
|
+
return "output step processor";
|
|
1789
|
+
default:
|
|
1790
|
+
return "processor";
|
|
1791
|
+
}
|
|
1792
|
+
};
|
|
1793
|
+
const hasPhaseMethod = (phase) => {
|
|
1794
|
+
switch (phase) {
|
|
1795
|
+
case "input":
|
|
1796
|
+
return !!processor.processInput;
|
|
1797
|
+
case "inputStep":
|
|
1798
|
+
return !!processor.processInputStep;
|
|
1799
|
+
case "outputStream":
|
|
1800
|
+
return !!processor.processOutputStream;
|
|
1801
|
+
case "outputResult":
|
|
1802
|
+
return !!processor.processOutputResult;
|
|
1803
|
+
case "outputStep":
|
|
1804
|
+
return !!processor.processOutputStep;
|
|
1805
|
+
default:
|
|
1806
|
+
return false;
|
|
1807
|
+
}
|
|
1808
|
+
};
|
|
1809
|
+
return {
|
|
1810
|
+
id: `processor:${processor.id}`,
|
|
1811
|
+
description: processor.name ?? `Processor ${processor.id}`,
|
|
1812
|
+
inputSchema: ProcessorStepSchema,
|
|
1813
|
+
outputSchema: ProcessorStepOutputSchema,
|
|
1814
|
+
execute: async ({ inputData, requestContext, tracingContext }) => {
|
|
1815
|
+
const input = inputData;
|
|
1816
|
+
const {
|
|
1817
|
+
phase,
|
|
1818
|
+
messages,
|
|
1819
|
+
messageList,
|
|
1820
|
+
stepNumber,
|
|
1821
|
+
systemMessages,
|
|
1822
|
+
part,
|
|
1823
|
+
streamParts,
|
|
1824
|
+
state,
|
|
1825
|
+
finishReason,
|
|
1826
|
+
toolCalls,
|
|
1827
|
+
text,
|
|
1828
|
+
retryCount,
|
|
1829
|
+
// inputStep phase fields for model/tools configuration
|
|
1830
|
+
model,
|
|
1831
|
+
tools,
|
|
1832
|
+
toolChoice,
|
|
1833
|
+
activeTools,
|
|
1834
|
+
providerOptions,
|
|
1835
|
+
modelSettings,
|
|
1836
|
+
structuredOutput,
|
|
1837
|
+
steps
|
|
1838
|
+
} = input;
|
|
1839
|
+
const abort = (reason, options) => {
|
|
1840
|
+
throw new TripWire(reason || `Tripwire triggered by ${processor.id}`, options, processor.id);
|
|
1841
|
+
};
|
|
1842
|
+
if (!hasPhaseMethod(phase)) {
|
|
1843
|
+
return input;
|
|
1844
|
+
}
|
|
1845
|
+
const currentSpan = tracingContext?.currentSpan;
|
|
1846
|
+
const parentSpan = phase === "inputStep" || phase === "outputStep" ? currentSpan?.findParent(SpanType.MODEL_STEP) || currentSpan : currentSpan?.findParent(SpanType.AGENT_RUN) || currentSpan;
|
|
1847
|
+
const processorSpan = phase !== "outputStream" ? parentSpan?.createChildSpan({
|
|
1848
|
+
type: SpanType.PROCESSOR_RUN,
|
|
1849
|
+
name: `${getSpanNamePrefix(phase)}: ${processor.id}`,
|
|
1850
|
+
entityType: getProcessorEntityType(phase),
|
|
1851
|
+
entityId: processor.id,
|
|
1852
|
+
entityName: processor.name ?? processor.id,
|
|
1853
|
+
input: { phase, messageCount: messages?.length },
|
|
1854
|
+
attributes: {
|
|
1855
|
+
processorExecutor: "workflow",
|
|
1856
|
+
// Read processorIndex from processor (set in combineProcessorsIntoWorkflow)
|
|
1857
|
+
processorIndex: processor.processorIndex
|
|
1858
|
+
}
|
|
1859
|
+
}) : void 0;
|
|
1860
|
+
const processorTracingContext = processorSpan ? { currentSpan: processorSpan } : tracingContext;
|
|
1861
|
+
const baseContext = {
|
|
1862
|
+
abort,
|
|
1863
|
+
retryCount: retryCount ?? 0,
|
|
1864
|
+
requestContext,
|
|
1865
|
+
tracingContext: processorTracingContext
|
|
1866
|
+
};
|
|
1867
|
+
const passThrough = {
|
|
1868
|
+
phase,
|
|
1869
|
+
// Auto-create MessageList from messages if not provided
|
|
1870
|
+
// This enables running processor workflows from the UI where messageList can't be serialized
|
|
1871
|
+
messageList: messageList ?? (Array.isArray(messages) ? new MessageList().add(messages, "input").addSystem(systemMessages ?? []) : void 0),
|
|
1872
|
+
stepNumber,
|
|
1873
|
+
systemMessages,
|
|
1874
|
+
streamParts,
|
|
1875
|
+
state,
|
|
1876
|
+
finishReason,
|
|
1877
|
+
toolCalls,
|
|
1878
|
+
text,
|
|
1879
|
+
retryCount,
|
|
1880
|
+
// inputStep phase fields for model/tools configuration
|
|
1881
|
+
model,
|
|
1882
|
+
tools,
|
|
1883
|
+
toolChoice,
|
|
1884
|
+
activeTools,
|
|
1885
|
+
providerOptions,
|
|
1886
|
+
modelSettings,
|
|
1887
|
+
structuredOutput,
|
|
1888
|
+
steps
|
|
1889
|
+
};
|
|
1890
|
+
const executePhaseWithSpan = async (fn) => {
|
|
1891
|
+
try {
|
|
1892
|
+
const result = await fn();
|
|
1893
|
+
processorSpan?.end({ output: result });
|
|
1894
|
+
return result;
|
|
1895
|
+
} catch (error) {
|
|
1896
|
+
if (error instanceof TripWire) {
|
|
1897
|
+
processorSpan?.end({ output: { tripwire: error.message } });
|
|
1898
|
+
} else {
|
|
1899
|
+
processorSpan?.error({ error, endSpan: true });
|
|
1900
|
+
}
|
|
1901
|
+
throw error;
|
|
1902
|
+
}
|
|
1903
|
+
};
|
|
1904
|
+
return executePhaseWithSpan(async () => {
|
|
1905
|
+
switch (phase) {
|
|
1906
|
+
case "input": {
|
|
1907
|
+
if (processor.processInput) {
|
|
1908
|
+
if (!passThrough.messageList) {
|
|
1909
|
+
throw new MastraError({
|
|
1910
|
+
category: ErrorCategory.USER,
|
|
1911
|
+
domain: ErrorDomain.MASTRA_WORKFLOW,
|
|
1912
|
+
id: "PROCESSOR_MISSING_MESSAGE_LIST",
|
|
1913
|
+
text: `Processor ${processor.id} requires messageList or messages for processInput phase`
|
|
1914
|
+
});
|
|
1915
|
+
}
|
|
1916
|
+
const idsBeforeProcessing = messages.map((m) => m.id);
|
|
1917
|
+
const check = passThrough.messageList.makeMessageSourceChecker();
|
|
1918
|
+
const result = await processor.processInput({
|
|
1919
|
+
...baseContext,
|
|
1920
|
+
messages,
|
|
1921
|
+
messageList: passThrough.messageList,
|
|
1922
|
+
systemMessages: systemMessages ?? []
|
|
1923
|
+
});
|
|
1924
|
+
if (result instanceof MessageList) {
|
|
1925
|
+
if (result !== passThrough.messageList) {
|
|
1926
|
+
throw new MastraError({
|
|
1927
|
+
category: ErrorCategory.USER,
|
|
1928
|
+
domain: ErrorDomain.MASTRA_WORKFLOW,
|
|
1929
|
+
id: "PROCESSOR_RETURNED_EXTERNAL_MESSAGE_LIST",
|
|
1930
|
+
text: `Processor ${processor.id} returned a MessageList instance other than the one passed in. Use the messageList argument instead.`
|
|
1931
|
+
});
|
|
1932
|
+
}
|
|
1933
|
+
return {
|
|
1934
|
+
...passThrough,
|
|
1935
|
+
messages: result.get.all.db(),
|
|
1936
|
+
systemMessages: result.getAllSystemMessages()
|
|
1937
|
+
};
|
|
1938
|
+
} else if (Array.isArray(result)) {
|
|
1939
|
+
ProcessorRunner.applyMessagesToMessageList(
|
|
1940
|
+
result,
|
|
1941
|
+
passThrough.messageList,
|
|
1942
|
+
idsBeforeProcessing,
|
|
1943
|
+
check,
|
|
1944
|
+
"input"
|
|
1945
|
+
);
|
|
1946
|
+
return { ...passThrough, messages: result };
|
|
1947
|
+
} else if (result && "messages" in result && "systemMessages" in result) {
|
|
1948
|
+
const typedResult = result;
|
|
1949
|
+
ProcessorRunner.applyMessagesToMessageList(
|
|
1950
|
+
typedResult.messages,
|
|
1951
|
+
passThrough.messageList,
|
|
1952
|
+
idsBeforeProcessing,
|
|
1953
|
+
check,
|
|
1954
|
+
"input"
|
|
1955
|
+
);
|
|
1956
|
+
passThrough.messageList.replaceAllSystemMessages(typedResult.systemMessages);
|
|
1957
|
+
return {
|
|
1958
|
+
...passThrough,
|
|
1959
|
+
messages: typedResult.messages,
|
|
1960
|
+
systemMessages: typedResult.systemMessages
|
|
1961
|
+
};
|
|
1962
|
+
}
|
|
1963
|
+
return { ...passThrough, messages };
|
|
1964
|
+
}
|
|
1965
|
+
return { ...passThrough, messages };
|
|
1966
|
+
}
|
|
1967
|
+
case "inputStep": {
|
|
1968
|
+
if (processor.processInputStep) {
|
|
1969
|
+
if (!passThrough.messageList) {
|
|
1970
|
+
throw new MastraError({
|
|
1971
|
+
category: ErrorCategory.USER,
|
|
1972
|
+
domain: ErrorDomain.MASTRA_WORKFLOW,
|
|
1973
|
+
id: "PROCESSOR_MISSING_MESSAGE_LIST",
|
|
1974
|
+
text: `Processor ${processor.id} requires messageList or messages for processInputStep phase`
|
|
1975
|
+
});
|
|
1976
|
+
}
|
|
1977
|
+
const idsBeforeProcessing = messages.map((m) => m.id);
|
|
1978
|
+
const check = passThrough.messageList.makeMessageSourceChecker();
|
|
1979
|
+
const result = await processor.processInputStep({
|
|
1980
|
+
...baseContext,
|
|
1981
|
+
messages,
|
|
1982
|
+
messageList: passThrough.messageList,
|
|
1983
|
+
stepNumber: stepNumber ?? 0,
|
|
1984
|
+
systemMessages: systemMessages ?? [],
|
|
1985
|
+
// Pass model/tools configuration fields - types match ProcessInputStepArgs
|
|
1986
|
+
model,
|
|
1987
|
+
tools,
|
|
1988
|
+
toolChoice,
|
|
1989
|
+
activeTools,
|
|
1990
|
+
providerOptions,
|
|
1991
|
+
modelSettings,
|
|
1992
|
+
structuredOutput,
|
|
1993
|
+
steps: steps ?? []
|
|
1994
|
+
});
|
|
1995
|
+
const validatedResult = await ProcessorRunner.validateAndFormatProcessInputStepResult(result, {
|
|
1996
|
+
messageList: passThrough.messageList,
|
|
1997
|
+
processor,
|
|
1998
|
+
stepNumber: stepNumber ?? 0
|
|
1999
|
+
});
|
|
2000
|
+
if (validatedResult.messages) {
|
|
2001
|
+
ProcessorRunner.applyMessagesToMessageList(
|
|
2002
|
+
validatedResult.messages,
|
|
2003
|
+
passThrough.messageList,
|
|
2004
|
+
idsBeforeProcessing,
|
|
2005
|
+
check
|
|
2006
|
+
);
|
|
2007
|
+
}
|
|
2008
|
+
if (validatedResult.systemMessages) {
|
|
2009
|
+
passThrough.messageList.replaceAllSystemMessages(validatedResult.systemMessages);
|
|
2010
|
+
}
|
|
2011
|
+
return { ...passThrough, messages, ...validatedResult };
|
|
2012
|
+
}
|
|
2013
|
+
return { ...passThrough, messages };
|
|
2014
|
+
}
|
|
2015
|
+
case "outputStream": {
|
|
2016
|
+
if (processor.processOutputStream) {
|
|
2017
|
+
const spanKey = `__outputStreamSpan_${processor.id}`;
|
|
2018
|
+
const mutableState = state ?? {};
|
|
2019
|
+
let processorSpan2 = mutableState[spanKey];
|
|
2020
|
+
if (!processorSpan2 && parentSpan) {
|
|
2021
|
+
processorSpan2 = parentSpan.createChildSpan({
|
|
2022
|
+
type: SpanType.PROCESSOR_RUN,
|
|
2023
|
+
name: `output stream processor: ${processor.id}`,
|
|
2024
|
+
entityType: EntityType.OUTPUT_PROCESSOR,
|
|
2025
|
+
entityId: processor.id,
|
|
2026
|
+
entityName: processor.name ?? processor.id,
|
|
2027
|
+
input: { phase, streamParts: [] },
|
|
2028
|
+
attributes: {
|
|
2029
|
+
processorExecutor: "workflow",
|
|
2030
|
+
processorIndex: processor.processorIndex
|
|
2031
|
+
}
|
|
2032
|
+
});
|
|
2033
|
+
mutableState[spanKey] = processorSpan2;
|
|
2034
|
+
}
|
|
2035
|
+
if (processorSpan2) {
|
|
2036
|
+
processorSpan2.input = {
|
|
2037
|
+
phase,
|
|
2038
|
+
streamParts: streamParts ?? [],
|
|
2039
|
+
totalChunks: (streamParts ?? []).length
|
|
2040
|
+
};
|
|
2041
|
+
}
|
|
2042
|
+
const processorTracingContext2 = processorSpan2 ? { currentSpan: processorSpan2 } : baseContext.tracingContext;
|
|
2043
|
+
let result;
|
|
2044
|
+
try {
|
|
2045
|
+
result = await processor.processOutputStream({
|
|
2046
|
+
...baseContext,
|
|
2047
|
+
tracingContext: processorTracingContext2,
|
|
2048
|
+
part,
|
|
2049
|
+
streamParts: streamParts ?? [],
|
|
2050
|
+
state: mutableState,
|
|
2051
|
+
messageList: passThrough.messageList
|
|
2052
|
+
// Optional for stream processing
|
|
2053
|
+
});
|
|
2054
|
+
if (part && part.type === "finish") {
|
|
2055
|
+
processorSpan2?.end({ output: result });
|
|
2056
|
+
delete mutableState[spanKey];
|
|
2057
|
+
}
|
|
2058
|
+
} catch (error) {
|
|
2059
|
+
if (error instanceof TripWire) {
|
|
2060
|
+
processorSpan2?.end({ output: { tripwire: error.message } });
|
|
2061
|
+
} else {
|
|
2062
|
+
processorSpan2?.error({ error, endSpan: true });
|
|
2063
|
+
}
|
|
2064
|
+
delete mutableState[spanKey];
|
|
2065
|
+
throw error;
|
|
2066
|
+
}
|
|
2067
|
+
return { ...passThrough, state: mutableState, part: result };
|
|
2068
|
+
}
|
|
2069
|
+
return { ...passThrough, part };
|
|
2070
|
+
}
|
|
2071
|
+
case "outputResult": {
|
|
2072
|
+
if (processor.processOutputResult) {
|
|
2073
|
+
if (!passThrough.messageList) {
|
|
2074
|
+
throw new MastraError({
|
|
2075
|
+
category: ErrorCategory.USER,
|
|
2076
|
+
domain: ErrorDomain.MASTRA_WORKFLOW,
|
|
2077
|
+
id: "PROCESSOR_MISSING_MESSAGE_LIST",
|
|
2078
|
+
text: `Processor ${processor.id} requires messageList or messages for processOutputResult phase`
|
|
2079
|
+
});
|
|
2080
|
+
}
|
|
2081
|
+
const idsBeforeProcessing = messages.map((m) => m.id);
|
|
2082
|
+
const check = passThrough.messageList.makeMessageSourceChecker();
|
|
2083
|
+
const result = await processor.processOutputResult({
|
|
2084
|
+
...baseContext,
|
|
2085
|
+
messages,
|
|
2086
|
+
messageList: passThrough.messageList
|
|
2087
|
+
});
|
|
2088
|
+
if (result instanceof MessageList) {
|
|
2089
|
+
if (result !== passThrough.messageList) {
|
|
2090
|
+
throw new MastraError({
|
|
2091
|
+
category: ErrorCategory.USER,
|
|
2092
|
+
domain: ErrorDomain.MASTRA_WORKFLOW,
|
|
2093
|
+
id: "PROCESSOR_RETURNED_EXTERNAL_MESSAGE_LIST",
|
|
2094
|
+
text: `Processor ${processor.id} returned a MessageList instance other than the one passed in. Use the messageList argument instead.`
|
|
2095
|
+
});
|
|
2096
|
+
}
|
|
2097
|
+
return {
|
|
2098
|
+
...passThrough,
|
|
2099
|
+
messages: result.get.all.db(),
|
|
2100
|
+
systemMessages: result.getAllSystemMessages()
|
|
2101
|
+
};
|
|
2102
|
+
} else if (Array.isArray(result)) {
|
|
2103
|
+
ProcessorRunner.applyMessagesToMessageList(
|
|
2104
|
+
result,
|
|
2105
|
+
passThrough.messageList,
|
|
2106
|
+
idsBeforeProcessing,
|
|
2107
|
+
check,
|
|
2108
|
+
"response"
|
|
2109
|
+
);
|
|
2110
|
+
return { ...passThrough, messages: result };
|
|
2111
|
+
} else if (result && "messages" in result && "systemMessages" in result) {
|
|
2112
|
+
const typedResult = result;
|
|
2113
|
+
ProcessorRunner.applyMessagesToMessageList(
|
|
2114
|
+
typedResult.messages,
|
|
2115
|
+
passThrough.messageList,
|
|
2116
|
+
idsBeforeProcessing,
|
|
2117
|
+
check,
|
|
2118
|
+
"response"
|
|
2119
|
+
);
|
|
2120
|
+
passThrough.messageList.replaceAllSystemMessages(typedResult.systemMessages);
|
|
2121
|
+
return {
|
|
2122
|
+
...passThrough,
|
|
2123
|
+
messages: typedResult.messages,
|
|
2124
|
+
systemMessages: typedResult.systemMessages
|
|
2125
|
+
};
|
|
2126
|
+
}
|
|
2127
|
+
return { ...passThrough, messages };
|
|
2128
|
+
}
|
|
2129
|
+
return { ...passThrough, messages };
|
|
2130
|
+
}
|
|
2131
|
+
case "outputStep": {
|
|
2132
|
+
if (processor.processOutputStep) {
|
|
2133
|
+
if (!passThrough.messageList) {
|
|
2134
|
+
throw new MastraError({
|
|
2135
|
+
category: ErrorCategory.USER,
|
|
2136
|
+
domain: ErrorDomain.MASTRA_WORKFLOW,
|
|
2137
|
+
id: "PROCESSOR_MISSING_MESSAGE_LIST",
|
|
2138
|
+
text: `Processor ${processor.id} requires messageList or messages for processOutputStep phase`
|
|
2139
|
+
});
|
|
2140
|
+
}
|
|
2141
|
+
const idsBeforeProcessing = messages.map((m) => m.id);
|
|
2142
|
+
const check = passThrough.messageList.makeMessageSourceChecker();
|
|
2143
|
+
const result = await processor.processOutputStep({
|
|
2144
|
+
...baseContext,
|
|
2145
|
+
messages,
|
|
2146
|
+
messageList: passThrough.messageList,
|
|
2147
|
+
stepNumber: stepNumber ?? 0,
|
|
2148
|
+
finishReason,
|
|
2149
|
+
toolCalls,
|
|
2150
|
+
text,
|
|
2151
|
+
systemMessages: systemMessages ?? [],
|
|
2152
|
+
steps: steps ?? []
|
|
2153
|
+
});
|
|
2154
|
+
if (result instanceof MessageList) {
|
|
2155
|
+
if (result !== passThrough.messageList) {
|
|
2156
|
+
throw new MastraError({
|
|
2157
|
+
category: ErrorCategory.USER,
|
|
2158
|
+
domain: ErrorDomain.MASTRA_WORKFLOW,
|
|
2159
|
+
id: "PROCESSOR_RETURNED_EXTERNAL_MESSAGE_LIST",
|
|
2160
|
+
text: `Processor ${processor.id} returned a MessageList instance other than the one passed in. Use the messageList argument instead.`
|
|
2161
|
+
});
|
|
2162
|
+
}
|
|
2163
|
+
return {
|
|
2164
|
+
...passThrough,
|
|
2165
|
+
messages: result.get.all.db(),
|
|
2166
|
+
systemMessages: result.getAllSystemMessages()
|
|
2167
|
+
};
|
|
2168
|
+
} else if (Array.isArray(result)) {
|
|
2169
|
+
ProcessorRunner.applyMessagesToMessageList(
|
|
2170
|
+
result,
|
|
2171
|
+
passThrough.messageList,
|
|
2172
|
+
idsBeforeProcessing,
|
|
2173
|
+
check,
|
|
2174
|
+
"response"
|
|
2175
|
+
);
|
|
2176
|
+
return { ...passThrough, messages: result };
|
|
2177
|
+
} else if (result && "messages" in result && "systemMessages" in result) {
|
|
2178
|
+
const typedResult = result;
|
|
2179
|
+
ProcessorRunner.applyMessagesToMessageList(
|
|
2180
|
+
typedResult.messages,
|
|
2181
|
+
passThrough.messageList,
|
|
2182
|
+
idsBeforeProcessing,
|
|
2183
|
+
check,
|
|
2184
|
+
"response"
|
|
2185
|
+
);
|
|
2186
|
+
passThrough.messageList.replaceAllSystemMessages(typedResult.systemMessages);
|
|
2187
|
+
return {
|
|
2188
|
+
...passThrough,
|
|
2189
|
+
messages: typedResult.messages,
|
|
2190
|
+
systemMessages: typedResult.systemMessages
|
|
2191
|
+
};
|
|
2192
|
+
}
|
|
2193
|
+
return { ...passThrough, messages };
|
|
2194
|
+
}
|
|
2195
|
+
return { ...passThrough, messages };
|
|
2196
|
+
}
|
|
2197
|
+
default:
|
|
2198
|
+
return { ...passThrough, messages };
|
|
2199
|
+
}
|
|
2200
|
+
});
|
|
2201
|
+
},
|
|
2202
|
+
component: "PROCESSOR"
|
|
1452
2203
|
};
|
|
1453
2204
|
}
|
|
1454
2205
|
function init(inngest) {
|
|
@@ -1491,6 +2242,6 @@ function init(inngest) {
|
|
|
1491
2242
|
};
|
|
1492
2243
|
}
|
|
1493
2244
|
|
|
1494
|
-
export { InngestExecutionEngine, InngestPubSub, InngestRun, InngestWorkflow, _compatibilityCheck, createStep, init, serve };
|
|
2245
|
+
export { InngestExecutionEngine, InngestPubSub, InngestRun, InngestWorkflow, _compatibilityCheck, createServe, createStep, init, serve };
|
|
1495
2246
|
//# sourceMappingURL=index.js.map
|
|
1496
2247
|
//# sourceMappingURL=index.js.map
|