@mastra/inngest 1.0.0-beta.12 → 1.0.0-beta.13
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 +94 -0
- package/dist/execution-engine.d.ts +87 -2
- package/dist/execution-engine.d.ts.map +1 -1
- package/dist/index.cjs +853 -203
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +63 -28
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +854 -204
- package/dist/index.js.map +1 -1
- package/dist/run.d.ts +48 -37
- package/dist/run.d.ts.map +1 -1
- package/dist/types.d.ts +4 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/workflow.d.ts +1 -2
- package/dist/workflow.d.ts.map +1 -1
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import { Agent } from '@mastra/core/agent';
|
|
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';
|
|
2
5
|
import { Tool } from '@mastra/core/tools';
|
|
3
6
|
import { DefaultExecutionEngine, createTimeTravelExecutionParams, Run, hydrateSerializedStepErrors, Workflow } from '@mastra/core/workflows';
|
|
4
7
|
import { PUBSUB_SYMBOL, STREAM_FORMAT_SYMBOL } from '@mastra/core/workflows/_constants';
|
|
@@ -6,7 +9,6 @@ import { z } from 'zod';
|
|
|
6
9
|
import { randomUUID } from 'crypto';
|
|
7
10
|
import { RequestContext } from '@mastra/core/di';
|
|
8
11
|
import { NonRetriableError } from 'inngest';
|
|
9
|
-
import { getErrorFromUnknown } from '@mastra/core/error';
|
|
10
12
|
import { subscribe } from '@inngest/realtime';
|
|
11
13
|
import { PubSub } from '@mastra/core/events';
|
|
12
14
|
import { ReadableStream } from 'stream/web';
|
|
@@ -114,15 +116,32 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
114
116
|
}
|
|
115
117
|
/**
|
|
116
118
|
* Wrap durable operations in Inngest step.run() for durability.
|
|
117
|
-
*
|
|
118
|
-
*
|
|
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.
|
|
119
128
|
*/
|
|
120
129
|
async wrapDurableOperation(operationId, operationFn) {
|
|
121
130
|
return this.inngestStep.run(operationId, async () => {
|
|
122
131
|
try {
|
|
123
132
|
return await operationFn();
|
|
124
133
|
} catch (e) {
|
|
125
|
-
|
|
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
|
+
});
|
|
126
145
|
}
|
|
127
146
|
});
|
|
128
147
|
}
|
|
@@ -144,6 +163,96 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
144
163
|
async invokeLifecycleCallbacksInternal(result) {
|
|
145
164
|
return super.invokeLifecycleCallbacks(result);
|
|
146
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
|
+
}
|
|
147
256
|
/**
|
|
148
257
|
* Execute nested InngestWorkflow using inngestStep.invoke() for durability.
|
|
149
258
|
* This MUST be called directly (not inside step.run()) due to Inngest constraints.
|
|
@@ -162,8 +271,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
162
271
|
inputData,
|
|
163
272
|
pubsub,
|
|
164
273
|
startedAt,
|
|
165
|
-
perStep
|
|
274
|
+
perStep,
|
|
275
|
+
stepSpan
|
|
166
276
|
} = params;
|
|
277
|
+
const nestedTracingContext = executionContext.tracingIds?.traceId ? {
|
|
278
|
+
traceId: executionContext.tracingIds.traceId,
|
|
279
|
+
parentSpanId: stepSpan?.id
|
|
280
|
+
} : void 0;
|
|
167
281
|
const isResume = !!resume?.steps?.length;
|
|
168
282
|
let result;
|
|
169
283
|
let runId;
|
|
@@ -190,7 +304,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
190
304
|
resumePath: resume.steps?.[1] ? snapshot?.suspendedPaths?.[resume.steps?.[1]] : void 0
|
|
191
305
|
},
|
|
192
306
|
outputOptions: { includeState: true },
|
|
193
|
-
perStep
|
|
307
|
+
perStep,
|
|
308
|
+
tracingOptions: nestedTracingContext
|
|
194
309
|
}
|
|
195
310
|
});
|
|
196
311
|
result = invokeResp.result;
|
|
@@ -218,7 +333,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
218
333
|
initialState: executionContext.state ?? {},
|
|
219
334
|
runId: executionContext.runId,
|
|
220
335
|
outputOptions: { includeState: true },
|
|
221
|
-
perStep
|
|
336
|
+
perStep,
|
|
337
|
+
tracingOptions: nestedTracingContext
|
|
222
338
|
}
|
|
223
339
|
});
|
|
224
340
|
result = invokeResp.result;
|
|
@@ -231,7 +347,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
231
347
|
inputData,
|
|
232
348
|
initialState: executionContext.state ?? {},
|
|
233
349
|
outputOptions: { includeState: true },
|
|
234
|
-
perStep
|
|
350
|
+
perStep,
|
|
351
|
+
tracingOptions: nestedTracingContext
|
|
235
352
|
}
|
|
236
353
|
});
|
|
237
354
|
result = invokeResp.result;
|
|
@@ -628,8 +745,8 @@ var InngestRun = class extends Run {
|
|
|
628
745
|
});
|
|
629
746
|
}
|
|
630
747
|
}
|
|
631
|
-
async start(
|
|
632
|
-
return this._start(
|
|
748
|
+
async start(args) {
|
|
749
|
+
return this._start(args);
|
|
633
750
|
}
|
|
634
751
|
/**
|
|
635
752
|
* Starts the workflow execution without waiting for completion (fire-and-forget).
|
|
@@ -637,7 +754,7 @@ var InngestRun = class extends Run {
|
|
|
637
754
|
* The workflow executes independently in Inngest.
|
|
638
755
|
* Use this when you don't need to wait for the result or want to avoid polling failures.
|
|
639
756
|
*/
|
|
640
|
-
async startAsync(
|
|
757
|
+
async startAsync(args) {
|
|
641
758
|
const workflowsStore = await this.#mastra.getStorage()?.getStore("workflows");
|
|
642
759
|
await workflowsStore?.persistWorkflowSnapshot({
|
|
643
760
|
workflowName: this.workflowId,
|
|
@@ -657,8 +774,8 @@ var InngestRun = class extends Run {
|
|
|
657
774
|
timestamp: Date.now()
|
|
658
775
|
}
|
|
659
776
|
});
|
|
660
|
-
const inputDataToUse = await this._validateInput(
|
|
661
|
-
const initialStateToUse = await this._validateInitialState(
|
|
777
|
+
const inputDataToUse = await this._validateInput(args.inputData);
|
|
778
|
+
const initialStateToUse = await this._validateInitialState(args.initialState ?? {});
|
|
662
779
|
const eventOutput = await this.inngest.send({
|
|
663
780
|
name: `workflow.${this.workflowId}`,
|
|
664
781
|
data: {
|
|
@@ -666,10 +783,10 @@ var InngestRun = class extends Run {
|
|
|
666
783
|
initialState: initialStateToUse,
|
|
667
784
|
runId: this.runId,
|
|
668
785
|
resourceId: this.resourceId,
|
|
669
|
-
outputOptions:
|
|
670
|
-
tracingOptions:
|
|
671
|
-
requestContext:
|
|
672
|
-
perStep:
|
|
786
|
+
outputOptions: args.outputOptions,
|
|
787
|
+
tracingOptions: args.tracingOptions,
|
|
788
|
+
requestContext: args.requestContext ? Object.fromEntries(args.requestContext.entries()) : {},
|
|
789
|
+
perStep: args.perStep
|
|
673
790
|
}
|
|
674
791
|
});
|
|
675
792
|
const eventId = eventOutput.ids[0];
|
|
@@ -1041,6 +1158,7 @@ var InngestRun = class extends Run {
|
|
|
1041
1158
|
context,
|
|
1042
1159
|
nestedStepsContext,
|
|
1043
1160
|
requestContext,
|
|
1161
|
+
// tracingContext,
|
|
1044
1162
|
tracingOptions,
|
|
1045
1163
|
outputOptions,
|
|
1046
1164
|
perStep
|
|
@@ -1173,7 +1291,8 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1173
1291
|
}
|
|
1174
1292
|
async createRun(options) {
|
|
1175
1293
|
const runIdToUse = options?.runId || randomUUID();
|
|
1176
|
-
const
|
|
1294
|
+
const existingInMemoryRun = this.runs.get(runIdToUse);
|
|
1295
|
+
const newRun = new InngestRun(
|
|
1177
1296
|
{
|
|
1178
1297
|
workflowId: this.id,
|
|
1179
1298
|
runId: runIdToUse,
|
|
@@ -1190,15 +1309,16 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1190
1309
|
},
|
|
1191
1310
|
this.inngest
|
|
1192
1311
|
);
|
|
1312
|
+
const run = existingInMemoryRun ?? newRun;
|
|
1193
1313
|
this.runs.set(runIdToUse, run);
|
|
1194
1314
|
const shouldPersistSnapshot = this.options.shouldPersistSnapshot({
|
|
1195
1315
|
workflowStatus: run.workflowRunStatus,
|
|
1196
1316
|
stepResults: {}
|
|
1197
1317
|
});
|
|
1198
|
-
const
|
|
1318
|
+
const existingStoredRun = await this.getWorkflowRunById(runIdToUse, {
|
|
1199
1319
|
withNestedWorkflows: false
|
|
1200
1320
|
});
|
|
1201
|
-
const existsInStorage =
|
|
1321
|
+
const existsInStorage = existingStoredRun && !existingStoredRun.isFromInMemory;
|
|
1202
1322
|
if (!existsInStorage && shouldPersistSnapshot) {
|
|
1203
1323
|
const workflowsStore = await this.mastra?.getStorage()?.getStore("workflows");
|
|
1204
1324
|
await workflowsStore?.persistWorkflowSnapshot({
|
|
@@ -1262,7 +1382,18 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1262
1382
|
},
|
|
1263
1383
|
{ event: `workflow.${this.id}` },
|
|
1264
1384
|
async ({ event, step, attempt, publish }) => {
|
|
1265
|
-
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;
|
|
1266
1397
|
if (!runId) {
|
|
1267
1398
|
runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
|
|
1268
1399
|
return randomUUID();
|
|
@@ -1270,37 +1401,69 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1270
1401
|
}
|
|
1271
1402
|
const pubsub = new InngestPubSub(this.inngest, this.id, publish);
|
|
1272
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
|
+
});
|
|
1273
1425
|
const engine = new InngestExecutionEngine(this.#mastra, step, attempt, this.options);
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
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
|
+
}
|
|
1301
1462
|
}
|
|
1302
|
-
}
|
|
1303
|
-
})
|
|
1463
|
+
});
|
|
1464
|
+
} catch (error) {
|
|
1465
|
+
throw error;
|
|
1466
|
+
}
|
|
1304
1467
|
await step.run(`workflow.${this.id}.finalize`, async () => {
|
|
1305
1468
|
if (result.status !== "paused") {
|
|
1306
1469
|
await engine.invokeLifecycleCallbacksInternal({
|
|
@@ -1317,6 +1480,23 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1317
1480
|
state: result.state ?? initialState ?? {}
|
|
1318
1481
|
});
|
|
1319
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
|
+
}
|
|
1320
1500
|
if (result.status === "failed") {
|
|
1321
1501
|
throw new NonRetriableError(`Workflow failed`, {
|
|
1322
1502
|
cause: result
|
|
@@ -1381,164 +1561,163 @@ var serve = createServe(serve$1);
|
|
|
1381
1561
|
var _compatibilityCheck = true;
|
|
1382
1562
|
|
|
1383
1563
|
// src/index.ts
|
|
1564
|
+
function isInngestWorkflow(input) {
|
|
1565
|
+
return input instanceof InngestWorkflow;
|
|
1566
|
+
}
|
|
1567
|
+
function isAgent(input) {
|
|
1568
|
+
return input instanceof Agent;
|
|
1569
|
+
}
|
|
1570
|
+
function isToolStep(input) {
|
|
1571
|
+
return input instanceof Tool;
|
|
1572
|
+
}
|
|
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
|
+
}
|
|
1384
1579
|
function createStep(params, agentOrToolOptions) {
|
|
1385
|
-
if (params
|
|
1580
|
+
if (isInngestWorkflow(params)) {
|
|
1386
1581
|
return params;
|
|
1387
1582
|
}
|
|
1388
|
-
if (params
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
return
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1583
|
+
if (isAgent(params)) {
|
|
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
|
|
1418
1660
|
});
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
// threadId: inputData.threadId,
|
|
1430
|
-
requestContext,
|
|
1431
|
-
tracingContext,
|
|
1432
|
-
onFinish: (result) => {
|
|
1433
|
-
const resultWithObject = result;
|
|
1434
|
-
if (agentOptions?.structuredOutput?.schema && resultWithObject.object) {
|
|
1435
|
-
structuredResult = resultWithObject.object;
|
|
1436
|
-
}
|
|
1437
|
-
streamPromise.resolve(result.text);
|
|
1438
|
-
void agentOptions?.onFinish?.(result);
|
|
1439
|
-
},
|
|
1440
|
-
abortSignal
|
|
1441
|
-
});
|
|
1442
|
-
stream = fullStream;
|
|
1443
|
-
} else {
|
|
1444
|
-
const modelOutput = await params.stream(inputData.prompt, {
|
|
1445
|
-
...agentOptions ?? {},
|
|
1446
|
-
requestContext,
|
|
1447
|
-
tracingContext,
|
|
1448
|
-
onFinish: (result) => {
|
|
1449
|
-
const resultWithObject = result;
|
|
1450
|
-
if (agentOptions?.structuredOutput?.schema && resultWithObject.object) {
|
|
1451
|
-
structuredResult = resultWithObject.object;
|
|
1452
|
-
}
|
|
1453
|
-
streamPromise.resolve(result.text);
|
|
1454
|
-
void agentOptions?.onFinish?.(result);
|
|
1455
|
-
},
|
|
1456
|
-
abortSignal
|
|
1457
|
-
});
|
|
1458
|
-
stream = modelOutput.fullStream;
|
|
1459
|
-
}
|
|
1460
|
-
if (streamFormat === "legacy") {
|
|
1461
|
-
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1462
|
-
type: "watch",
|
|
1463
|
-
runId,
|
|
1464
|
-
data: { type: "tool-call-streaming-start", ...toolData ?? {} }
|
|
1465
|
-
});
|
|
1466
|
-
for await (const chunk of stream) {
|
|
1467
|
-
if (chunk.type === "text-delta") {
|
|
1468
|
-
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1469
|
-
type: "watch",
|
|
1470
|
-
runId,
|
|
1471
|
-
data: { type: "tool-call-delta", ...toolData ?? {}, argsTextDelta: chunk.textDelta }
|
|
1472
|
-
});
|
|
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;
|
|
1473
1671
|
}
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
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
|
+
});
|
|
1483
1692
|
}
|
|
1484
1693
|
}
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
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);
|
|
1490
1702
|
}
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
suspendSchema: params.suspendSchema,
|
|
1510
|
-
resumeSchema: params.resumeSchema,
|
|
1511
|
-
retries: toolOpts?.retries,
|
|
1512
|
-
scorers: toolOpts?.scorers,
|
|
1513
|
-
execute: async ({
|
|
1514
|
-
inputData,
|
|
1515
|
-
mastra,
|
|
1516
|
-
requestContext,
|
|
1517
|
-
tracingContext,
|
|
1518
|
-
suspend,
|
|
1519
|
-
resumeData,
|
|
1520
|
-
runId,
|
|
1521
|
-
workflowId,
|
|
1522
|
-
state,
|
|
1523
|
-
setState
|
|
1524
|
-
}) => {
|
|
1525
|
-
const toolContext = {
|
|
1526
|
-
mastra,
|
|
1527
|
-
requestContext,
|
|
1528
|
-
tracingContext,
|
|
1529
|
-
workflow: {
|
|
1530
|
-
runId,
|
|
1531
|
-
resumeData,
|
|
1532
|
-
suspend,
|
|
1533
|
-
workflowId,
|
|
1534
|
-
state,
|
|
1535
|
-
setState
|
|
1536
|
-
}
|
|
1537
|
-
};
|
|
1538
|
-
return params.execute(inputData, toolContext);
|
|
1539
|
-
},
|
|
1540
|
-
component: "TOOL"
|
|
1541
|
-
};
|
|
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");
|
|
1542
1721
|
}
|
|
1543
1722
|
return {
|
|
1544
1723
|
id: params.id,
|
|
@@ -1547,9 +1726,480 @@ function createStep(params, agentOrToolOptions) {
|
|
|
1547
1726
|
outputSchema: params.outputSchema,
|
|
1548
1727
|
resumeSchema: params.resumeSchema,
|
|
1549
1728
|
suspendSchema: params.suspendSchema,
|
|
1550
|
-
retries:
|
|
1551
|
-
scorers:
|
|
1552
|
-
execute:
|
|
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"
|
|
1553
2203
|
};
|
|
1554
2204
|
}
|
|
1555
2205
|
function init(inngest) {
|