@mastra/inngest 0.0.0-ai-sdk-network-text-delta-20251017172601 → 0.0.0-allow-to-pass-a-mastra-url-instance-20251105224938
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 +187 -3
- package/dist/index.cjs +371 -392
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +72 -69
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +372 -393
- package/dist/index.js.map +1 -1
- package/package.json +12 -9
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var crypto = require('crypto');
|
|
4
|
+
var web = require('stream/web');
|
|
4
5
|
var realtime = require('@inngest/realtime');
|
|
5
|
-
var aiTracing = require('@mastra/core/ai-tracing');
|
|
6
6
|
var di = require('@mastra/core/di');
|
|
7
|
+
var observability = require('@mastra/core/observability');
|
|
8
|
+
var stream = require('@mastra/core/stream');
|
|
7
9
|
var tools = require('@mastra/core/tools');
|
|
8
10
|
var workflows = require('@mastra/core/workflows');
|
|
9
11
|
var _constants = require('@mastra/core/workflows/_constants');
|
|
@@ -18,7 +20,7 @@ function serve({
|
|
|
18
20
|
functions: userFunctions = [],
|
|
19
21
|
registerOptions
|
|
20
22
|
}) {
|
|
21
|
-
const wfs = mastra.
|
|
23
|
+
const wfs = mastra.listWorkflows();
|
|
22
24
|
const workflowFunctions = Array.from(
|
|
23
25
|
new Set(
|
|
24
26
|
Object.values(wfs).flatMap((wf) => {
|
|
@@ -57,11 +59,12 @@ var InngestRun = class extends workflows.Run {
|
|
|
57
59
|
}
|
|
58
60
|
async getRunOutput(eventId) {
|
|
59
61
|
let runs = await this.getRuns(eventId);
|
|
62
|
+
const storage = this.#mastra?.getStorage();
|
|
60
63
|
while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
|
|
61
64
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
62
65
|
runs = await this.getRuns(eventId);
|
|
63
66
|
if (runs?.[0]?.status === "Failed") {
|
|
64
|
-
const snapshot = await
|
|
67
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
65
68
|
workflowName: this.workflowId,
|
|
66
69
|
runId: this.runId
|
|
67
70
|
});
|
|
@@ -70,7 +73,7 @@ var InngestRun = class extends workflows.Run {
|
|
|
70
73
|
};
|
|
71
74
|
}
|
|
72
75
|
if (runs?.[0]?.status === "Cancelled") {
|
|
73
|
-
const snapshot = await
|
|
76
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
74
77
|
workflowName: this.workflowId,
|
|
75
78
|
runId: this.runId
|
|
76
79
|
});
|
|
@@ -79,25 +82,20 @@ var InngestRun = class extends workflows.Run {
|
|
|
79
82
|
}
|
|
80
83
|
return runs?.[0];
|
|
81
84
|
}
|
|
82
|
-
async sendEvent(event, data) {
|
|
83
|
-
await this.inngest.send({
|
|
84
|
-
name: `user-event-${event}`,
|
|
85
|
-
data
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
85
|
async cancel() {
|
|
86
|
+
const storage = this.#mastra?.getStorage();
|
|
89
87
|
await this.inngest.send({
|
|
90
88
|
name: `cancel.workflow.${this.workflowId}`,
|
|
91
89
|
data: {
|
|
92
90
|
runId: this.runId
|
|
93
91
|
}
|
|
94
92
|
});
|
|
95
|
-
const snapshot = await
|
|
93
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
96
94
|
workflowName: this.workflowId,
|
|
97
95
|
runId: this.runId
|
|
98
96
|
});
|
|
99
97
|
if (snapshot) {
|
|
100
|
-
await
|
|
98
|
+
await storage?.persistWorkflowSnapshot({
|
|
101
99
|
workflowName: this.workflowId,
|
|
102
100
|
runId: this.runId,
|
|
103
101
|
resourceId: this.resourceId,
|
|
@@ -108,9 +106,15 @@ var InngestRun = class extends workflows.Run {
|
|
|
108
106
|
});
|
|
109
107
|
}
|
|
110
108
|
}
|
|
111
|
-
async start({
|
|
109
|
+
async start(params) {
|
|
110
|
+
return this._start(params);
|
|
111
|
+
}
|
|
112
|
+
async _start({
|
|
112
113
|
inputData,
|
|
113
|
-
initialState
|
|
114
|
+
initialState,
|
|
115
|
+
outputOptions,
|
|
116
|
+
tracingOptions,
|
|
117
|
+
format
|
|
114
118
|
}) {
|
|
115
119
|
await this.#mastra.getStorage()?.persistWorkflowSnapshot({
|
|
116
120
|
workflowName: this.workflowId,
|
|
@@ -137,7 +141,10 @@ var InngestRun = class extends workflows.Run {
|
|
|
137
141
|
inputData: inputDataToUse,
|
|
138
142
|
initialState: initialStateToUse,
|
|
139
143
|
runId: this.runId,
|
|
140
|
-
resourceId: this.resourceId
|
|
144
|
+
resourceId: this.resourceId,
|
|
145
|
+
outputOptions,
|
|
146
|
+
tracingOptions,
|
|
147
|
+
format
|
|
141
148
|
}
|
|
142
149
|
});
|
|
143
150
|
const eventId = eventOutput.ids[0];
|
|
@@ -166,10 +173,11 @@ var InngestRun = class extends workflows.Run {
|
|
|
166
173
|
return p;
|
|
167
174
|
}
|
|
168
175
|
async _resume(params) {
|
|
176
|
+
const storage = this.#mastra?.getStorage();
|
|
169
177
|
const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
|
|
170
178
|
(step) => typeof step === "string" ? step : step?.id
|
|
171
179
|
);
|
|
172
|
-
const snapshot = await
|
|
180
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
173
181
|
workflowName: this.workflowId,
|
|
174
182
|
runId: this.runId
|
|
175
183
|
});
|
|
@@ -203,12 +211,12 @@ var InngestRun = class extends workflows.Run {
|
|
|
203
211
|
}
|
|
204
212
|
return result;
|
|
205
213
|
}
|
|
206
|
-
watch(cb
|
|
214
|
+
watch(cb) {
|
|
207
215
|
let active = true;
|
|
208
216
|
const streamPromise = realtime.subscribe(
|
|
209
217
|
{
|
|
210
218
|
channel: `workflow:${this.workflowId}:${this.runId}`,
|
|
211
|
-
topics: [
|
|
219
|
+
topics: ["watch"],
|
|
212
220
|
app: this.inngest
|
|
213
221
|
},
|
|
214
222
|
(message) => {
|
|
@@ -226,20 +234,35 @@ var InngestRun = class extends workflows.Run {
|
|
|
226
234
|
});
|
|
227
235
|
};
|
|
228
236
|
}
|
|
229
|
-
|
|
237
|
+
streamLegacy({ inputData, requestContext } = {}) {
|
|
230
238
|
const { readable, writable } = new TransformStream();
|
|
231
239
|
const writer = writable.getWriter();
|
|
232
240
|
const unwatch = this.watch(async (event) => {
|
|
233
241
|
try {
|
|
242
|
+
await writer.write({
|
|
243
|
+
// @ts-ignore
|
|
244
|
+
type: "start",
|
|
245
|
+
// @ts-ignore
|
|
246
|
+
payload: { runId: this.runId }
|
|
247
|
+
});
|
|
234
248
|
const e = {
|
|
235
249
|
...event,
|
|
236
250
|
type: event.type.replace("workflow-", "")
|
|
237
251
|
};
|
|
252
|
+
if (e.type === "step-output") {
|
|
253
|
+
e.type = e.payload.output.type;
|
|
254
|
+
e.payload = e.payload.output.payload;
|
|
255
|
+
}
|
|
238
256
|
await writer.write(e);
|
|
239
257
|
} catch {
|
|
240
258
|
}
|
|
241
|
-
}
|
|
259
|
+
});
|
|
242
260
|
this.closeStreamAction = async () => {
|
|
261
|
+
await writer.write({
|
|
262
|
+
type: "finish",
|
|
263
|
+
// @ts-ignore
|
|
264
|
+
payload: { runId: this.runId }
|
|
265
|
+
});
|
|
243
266
|
unwatch();
|
|
244
267
|
try {
|
|
245
268
|
await writer.close();
|
|
@@ -249,7 +272,7 @@ var InngestRun = class extends workflows.Run {
|
|
|
249
272
|
writer.releaseLock();
|
|
250
273
|
}
|
|
251
274
|
};
|
|
252
|
-
this.executionResults = this.
|
|
275
|
+
this.executionResults = this._start({ inputData, requestContext, format: "legacy" }).then((result) => {
|
|
253
276
|
if (result.status !== "suspended") {
|
|
254
277
|
this.closeStreamAction?.().catch(() => {
|
|
255
278
|
});
|
|
@@ -261,6 +284,82 @@ var InngestRun = class extends workflows.Run {
|
|
|
261
284
|
getWorkflowState: () => this.executionResults
|
|
262
285
|
};
|
|
263
286
|
}
|
|
287
|
+
stream({
|
|
288
|
+
inputData,
|
|
289
|
+
requestContext,
|
|
290
|
+
tracingOptions,
|
|
291
|
+
closeOnSuspend = true,
|
|
292
|
+
initialState,
|
|
293
|
+
outputOptions
|
|
294
|
+
} = {}) {
|
|
295
|
+
if (this.closeStreamAction && this.streamOutput) {
|
|
296
|
+
return this.streamOutput;
|
|
297
|
+
}
|
|
298
|
+
this.closeStreamAction = async () => {
|
|
299
|
+
};
|
|
300
|
+
const self = this;
|
|
301
|
+
const stream$1 = new web.ReadableStream({
|
|
302
|
+
async start(controller) {
|
|
303
|
+
const unwatch = self.watch(async ({ type, from = stream.ChunkFrom.WORKFLOW, payload }) => {
|
|
304
|
+
controller.enqueue({
|
|
305
|
+
type,
|
|
306
|
+
runId: self.runId,
|
|
307
|
+
from,
|
|
308
|
+
payload: {
|
|
309
|
+
stepName: payload?.id,
|
|
310
|
+
...payload
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
self.closeStreamAction = async () => {
|
|
315
|
+
unwatch();
|
|
316
|
+
try {
|
|
317
|
+
await controller.close();
|
|
318
|
+
} catch (err) {
|
|
319
|
+
console.error("Error closing stream:", err);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
const executionResultsPromise = self._start({
|
|
323
|
+
inputData,
|
|
324
|
+
requestContext,
|
|
325
|
+
// tracingContext, // We are not able to pass a reference to a span here, what to do?
|
|
326
|
+
initialState,
|
|
327
|
+
tracingOptions,
|
|
328
|
+
outputOptions,
|
|
329
|
+
format: "vnext"
|
|
330
|
+
});
|
|
331
|
+
let executionResults;
|
|
332
|
+
try {
|
|
333
|
+
executionResults = await executionResultsPromise;
|
|
334
|
+
if (closeOnSuspend) {
|
|
335
|
+
self.closeStreamAction?.().catch(() => {
|
|
336
|
+
});
|
|
337
|
+
} else if (executionResults.status !== "suspended") {
|
|
338
|
+
self.closeStreamAction?.().catch(() => {
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
if (self.streamOutput) {
|
|
342
|
+
self.streamOutput.updateResults(
|
|
343
|
+
executionResults
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
} catch (err) {
|
|
347
|
+
self.streamOutput?.rejectResults(err);
|
|
348
|
+
self.closeStreamAction?.().catch(() => {
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
this.streamOutput = new stream.WorkflowRunOutput({
|
|
354
|
+
runId: this.runId,
|
|
355
|
+
workflowId: this.workflowId,
|
|
356
|
+
stream: stream$1
|
|
357
|
+
});
|
|
358
|
+
return this.streamOutput;
|
|
359
|
+
}
|
|
360
|
+
streamVNext(args = {}) {
|
|
361
|
+
return this.stream(args);
|
|
362
|
+
}
|
|
264
363
|
};
|
|
265
364
|
var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
266
365
|
#mastra;
|
|
@@ -277,13 +376,13 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
277
376
|
this.#mastra = params.mastra;
|
|
278
377
|
this.inngest = inngest;
|
|
279
378
|
}
|
|
280
|
-
async
|
|
379
|
+
async listWorkflowRuns(args) {
|
|
281
380
|
const storage = this.#mastra?.getStorage();
|
|
282
381
|
if (!storage) {
|
|
283
382
|
this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
|
|
284
383
|
return { runs: [], total: 0 };
|
|
285
384
|
}
|
|
286
|
-
return storage.
|
|
385
|
+
return storage.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
|
|
287
386
|
}
|
|
288
387
|
async getWorkflowRunById(runId) {
|
|
289
388
|
const storage = this.#mastra?.getStorage();
|
|
@@ -312,16 +411,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
312
411
|
}
|
|
313
412
|
}
|
|
314
413
|
}
|
|
315
|
-
|
|
316
|
-
* @deprecated Use createRunAsync() instead.
|
|
317
|
-
* @throws {Error} Always throws an error directing users to use createRunAsync()
|
|
318
|
-
*/
|
|
319
|
-
createRun(_options) {
|
|
320
|
-
throw new Error(
|
|
321
|
-
"createRun() has been deprecated. Please use createRunAsync() instead.\n\nMigration guide:\n Before: const run = workflow.createRun();\n After: const run = await workflow.createRunAsync();\n\nNote: createRunAsync() is an async method, so make sure your calling function is async."
|
|
322
|
-
);
|
|
323
|
-
}
|
|
324
|
-
async createRunAsync(options) {
|
|
414
|
+
async createRun(options) {
|
|
325
415
|
const runIdToUse = options?.runId || crypto.randomUUID();
|
|
326
416
|
const run = this.runs.get(runIdToUse) ?? new InngestRun(
|
|
327
417
|
{
|
|
@@ -383,7 +473,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
383
473
|
},
|
|
384
474
|
{ event: `workflow.${this.id}` },
|
|
385
475
|
async ({ event, step, attempt, publish }) => {
|
|
386
|
-
let { inputData, initialState, runId, resourceId, resume, outputOptions } = event.data;
|
|
476
|
+
let { inputData, initialState, runId, resourceId, resume, outputOptions, format } = event.data;
|
|
387
477
|
if (!runId) {
|
|
388
478
|
runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
|
|
389
479
|
return crypto.randomUUID();
|
|
@@ -422,13 +512,19 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
422
512
|
initialState,
|
|
423
513
|
emitter,
|
|
424
514
|
retryConfig: this.retryConfig,
|
|
425
|
-
|
|
515
|
+
requestContext: new di.RequestContext(),
|
|
426
516
|
// TODO
|
|
427
517
|
resume,
|
|
518
|
+
format,
|
|
428
519
|
abortController: new AbortController(),
|
|
429
|
-
currentSpan:
|
|
430
|
-
|
|
431
|
-
|
|
520
|
+
// currentSpan: undefined, // TODO: Pass actual parent Span from workflow execution context
|
|
521
|
+
outputOptions,
|
|
522
|
+
writableStream: new WritableStream({
|
|
523
|
+
write(chunk) {
|
|
524
|
+
void emitter.emit("watch", chunk).catch(() => {
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
})
|
|
432
528
|
});
|
|
433
529
|
await step.run(`workflow.${this.id}.finalize`, async () => {
|
|
434
530
|
if (result.status === "failed") {
|
|
@@ -466,7 +562,7 @@ function isAgent(params) {
|
|
|
466
562
|
function isTool(params) {
|
|
467
563
|
return params instanceof tools.Tool;
|
|
468
564
|
}
|
|
469
|
-
function createStep(params) {
|
|
565
|
+
function createStep(params, agentOptions) {
|
|
470
566
|
if (isAgent(params)) {
|
|
471
567
|
return {
|
|
472
568
|
id: params.name,
|
|
@@ -474,12 +570,23 @@ function createStep(params) {
|
|
|
474
570
|
// @ts-ignore
|
|
475
571
|
inputSchema: zod.z.object({
|
|
476
572
|
prompt: zod.z.string()
|
|
573
|
+
// resourceId: z.string().optional(),
|
|
574
|
+
// threadId: z.string().optional(),
|
|
477
575
|
}),
|
|
478
576
|
// @ts-ignore
|
|
479
577
|
outputSchema: zod.z.object({
|
|
480
578
|
text: zod.z.string()
|
|
481
579
|
}),
|
|
482
|
-
execute: async ({
|
|
580
|
+
execute: async ({
|
|
581
|
+
inputData,
|
|
582
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
583
|
+
[_constants.STREAM_FORMAT_SYMBOL]: streamFormat,
|
|
584
|
+
requestContext,
|
|
585
|
+
tracingContext,
|
|
586
|
+
abortSignal,
|
|
587
|
+
abort,
|
|
588
|
+
writer
|
|
589
|
+
}) => {
|
|
483
590
|
let streamPromise = {};
|
|
484
591
|
streamPromise.promise = new Promise((resolve, reject) => {
|
|
485
592
|
streamPromise.resolve = resolve;
|
|
@@ -489,61 +596,60 @@ function createStep(params) {
|
|
|
489
596
|
name: params.name,
|
|
490
597
|
args: inputData
|
|
491
598
|
};
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
599
|
+
let stream;
|
|
600
|
+
if ((await params.getModel()).specificationVersion === "v1") {
|
|
601
|
+
const { fullStream } = await params.streamLegacy(inputData.prompt, {
|
|
602
|
+
...agentOptions ?? {},
|
|
603
|
+
// resourceId: inputData.resourceId,
|
|
604
|
+
// threadId: inputData.threadId,
|
|
605
|
+
requestContext,
|
|
495
606
|
tracingContext,
|
|
496
607
|
onFinish: (result) => {
|
|
497
608
|
streamPromise.resolve(result.text);
|
|
609
|
+
void agentOptions?.onFinish?.(result);
|
|
498
610
|
},
|
|
499
611
|
abortSignal
|
|
500
612
|
});
|
|
501
|
-
|
|
502
|
-
return abort();
|
|
503
|
-
}
|
|
504
|
-
await emitter.emit("watch-v2", {
|
|
505
|
-
type: "tool-call-streaming-start",
|
|
506
|
-
...toolData ?? {}
|
|
507
|
-
});
|
|
508
|
-
for await (const chunk of fullStream) {
|
|
509
|
-
if (chunk.type === "text-delta") {
|
|
510
|
-
await emitter.emit("watch-v2", {
|
|
511
|
-
type: "tool-call-delta",
|
|
512
|
-
...toolData ?? {},
|
|
513
|
-
argsTextDelta: chunk.payload.text
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
}
|
|
613
|
+
stream = fullStream;
|
|
517
614
|
} else {
|
|
518
|
-
const
|
|
519
|
-
|
|
615
|
+
const modelOutput = await params.stream(inputData.prompt, {
|
|
616
|
+
...agentOptions ?? {},
|
|
617
|
+
requestContext,
|
|
520
618
|
tracingContext,
|
|
521
619
|
onFinish: (result) => {
|
|
522
620
|
streamPromise.resolve(result.text);
|
|
621
|
+
void agentOptions?.onFinish?.(result);
|
|
523
622
|
},
|
|
524
623
|
abortSignal
|
|
525
624
|
});
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
await emitter.emit("watch
|
|
625
|
+
stream = modelOutput.fullStream;
|
|
626
|
+
}
|
|
627
|
+
if (streamFormat === "legacy") {
|
|
628
|
+
await emitter.emit("watch", {
|
|
530
629
|
type: "tool-call-streaming-start",
|
|
531
630
|
...toolData ?? {}
|
|
532
631
|
});
|
|
533
|
-
for await (const chunk of
|
|
632
|
+
for await (const chunk of stream) {
|
|
534
633
|
if (chunk.type === "text-delta") {
|
|
535
|
-
await emitter.emit("watch
|
|
634
|
+
await emitter.emit("watch", {
|
|
536
635
|
type: "tool-call-delta",
|
|
537
636
|
...toolData ?? {},
|
|
538
637
|
argsTextDelta: chunk.textDelta
|
|
539
638
|
});
|
|
540
639
|
}
|
|
541
640
|
}
|
|
641
|
+
await emitter.emit("watch", {
|
|
642
|
+
type: "tool-call-streaming-finish",
|
|
643
|
+
...toolData ?? {}
|
|
644
|
+
});
|
|
645
|
+
} else {
|
|
646
|
+
for await (const chunk of stream) {
|
|
647
|
+
await writer.write(chunk);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
if (abortSignal.aborted) {
|
|
651
|
+
return abort();
|
|
542
652
|
}
|
|
543
|
-
await emitter.emit("watch-v2", {
|
|
544
|
-
type: "tool-call-streaming-finish",
|
|
545
|
-
...toolData ?? {}
|
|
546
|
-
});
|
|
547
653
|
return {
|
|
548
654
|
text: await streamPromise.promise
|
|
549
655
|
};
|
|
@@ -562,11 +668,11 @@ function createStep(params) {
|
|
|
562
668
|
description: params.description,
|
|
563
669
|
inputSchema: params.inputSchema,
|
|
564
670
|
outputSchema: params.outputSchema,
|
|
565
|
-
execute: async ({ inputData, mastra,
|
|
671
|
+
execute: async ({ inputData, mastra, requestContext, tracingContext, suspend, resumeData }) => {
|
|
566
672
|
return params.execute({
|
|
567
673
|
context: inputData,
|
|
568
|
-
mastra:
|
|
569
|
-
|
|
674
|
+
mastra: observability.wrapMastra(mastra, tracingContext),
|
|
675
|
+
requestContext,
|
|
570
676
|
tracingContext,
|
|
571
677
|
suspend,
|
|
572
678
|
resumeData
|
|
@@ -629,63 +735,16 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
629
735
|
this.inngestStep = inngestStep;
|
|
630
736
|
this.inngestAttempts = inngestAttempts;
|
|
631
737
|
}
|
|
632
|
-
async
|
|
633
|
-
await params.emitter.emit("watch-v2", {
|
|
634
|
-
type: "workflow-start",
|
|
635
|
-
payload: { runId: params.runId }
|
|
636
|
-
});
|
|
637
|
-
const result = await super.execute(params);
|
|
638
|
-
await params.emitter.emit("watch-v2", {
|
|
639
|
-
type: "workflow-finish",
|
|
640
|
-
payload: { runId: params.runId }
|
|
641
|
-
});
|
|
642
|
-
return result;
|
|
643
|
-
}
|
|
644
|
-
async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
|
|
738
|
+
async fmtReturnValue(emitter, stepResults, lastOutput, error) {
|
|
645
739
|
const base = {
|
|
646
740
|
status: lastOutput.status,
|
|
647
741
|
steps: stepResults
|
|
648
742
|
};
|
|
649
743
|
if (lastOutput.status === "success") {
|
|
650
|
-
await emitter.emit("watch", {
|
|
651
|
-
type: "watch",
|
|
652
|
-
payload: {
|
|
653
|
-
workflowState: {
|
|
654
|
-
status: lastOutput.status,
|
|
655
|
-
steps: stepResults,
|
|
656
|
-
result: lastOutput.output
|
|
657
|
-
}
|
|
658
|
-
},
|
|
659
|
-
eventTimestamp: Date.now()
|
|
660
|
-
});
|
|
661
744
|
base.result = lastOutput.output;
|
|
662
745
|
} else if (lastOutput.status === "failed") {
|
|
663
746
|
base.error = error instanceof Error ? error?.stack ?? error.message : lastOutput?.error instanceof Error ? lastOutput.error.message : lastOutput.error ?? error ?? "Unknown error";
|
|
664
|
-
await emitter.emit("watch", {
|
|
665
|
-
type: "watch",
|
|
666
|
-
payload: {
|
|
667
|
-
workflowState: {
|
|
668
|
-
status: lastOutput.status,
|
|
669
|
-
steps: stepResults,
|
|
670
|
-
result: null,
|
|
671
|
-
error: base.error
|
|
672
|
-
}
|
|
673
|
-
},
|
|
674
|
-
eventTimestamp: Date.now()
|
|
675
|
-
});
|
|
676
747
|
} else if (lastOutput.status === "suspended") {
|
|
677
|
-
await emitter.emit("watch", {
|
|
678
|
-
type: "watch",
|
|
679
|
-
payload: {
|
|
680
|
-
workflowState: {
|
|
681
|
-
status: lastOutput.status,
|
|
682
|
-
steps: stepResults,
|
|
683
|
-
result: null,
|
|
684
|
-
error: null
|
|
685
|
-
}
|
|
686
|
-
},
|
|
687
|
-
eventTimestamp: Date.now()
|
|
688
|
-
});
|
|
689
748
|
const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
|
|
690
749
|
if (stepResult?.status === "suspended") {
|
|
691
750
|
const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
|
|
@@ -695,7 +754,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
695
754
|
});
|
|
696
755
|
base.suspended = suspendedStepIds;
|
|
697
756
|
}
|
|
698
|
-
executionSpan?.end();
|
|
699
757
|
return base;
|
|
700
758
|
}
|
|
701
759
|
// async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
|
|
@@ -709,14 +767,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
709
767
|
stepResults,
|
|
710
768
|
emitter,
|
|
711
769
|
abortController,
|
|
712
|
-
|
|
770
|
+
requestContext,
|
|
713
771
|
executionContext,
|
|
714
772
|
writableStream,
|
|
715
773
|
tracingContext
|
|
716
774
|
}) {
|
|
717
775
|
let { duration, fn } = entry;
|
|
718
776
|
const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
719
|
-
type:
|
|
777
|
+
type: observability.SpanType.WORKFLOW_SLEEP,
|
|
720
778
|
name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
|
|
721
779
|
attributes: {
|
|
722
780
|
durationMs: duration,
|
|
@@ -727,45 +785,53 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
727
785
|
if (fn) {
|
|
728
786
|
const stepCallId = crypto.randomUUID();
|
|
729
787
|
duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
|
|
730
|
-
return await fn(
|
|
731
|
-
|
|
732
|
-
workflowId,
|
|
733
|
-
mastra: this.mastra,
|
|
734
|
-
runtimeContext,
|
|
735
|
-
inputData: prevOutput,
|
|
736
|
-
state: executionContext.state,
|
|
737
|
-
setState: (state) => {
|
|
738
|
-
executionContext.state = state;
|
|
739
|
-
},
|
|
740
|
-
runCount: -1,
|
|
741
|
-
tracingContext: {
|
|
742
|
-
currentSpan: sleepSpan
|
|
743
|
-
},
|
|
744
|
-
getInitData: () => stepResults?.input,
|
|
745
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
746
|
-
// TODO: this function shouldn't have suspend probably?
|
|
747
|
-
suspend: async (_suspendPayload) => {
|
|
748
|
-
},
|
|
749
|
-
bail: () => {
|
|
750
|
-
},
|
|
751
|
-
abort: () => {
|
|
752
|
-
abortController?.abort();
|
|
753
|
-
},
|
|
754
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
755
|
-
// TODO: add streamVNext support
|
|
756
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
757
|
-
engine: { step: this.inngestStep },
|
|
758
|
-
abortSignal: abortController?.signal,
|
|
759
|
-
writer: new tools.ToolStream(
|
|
788
|
+
return await fn(
|
|
789
|
+
workflows.createDeprecationProxy(
|
|
760
790
|
{
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
791
|
+
runId,
|
|
792
|
+
workflowId,
|
|
793
|
+
mastra: this.mastra,
|
|
794
|
+
requestContext,
|
|
795
|
+
inputData: prevOutput,
|
|
796
|
+
state: executionContext.state,
|
|
797
|
+
setState: (state) => {
|
|
798
|
+
executionContext.state = state;
|
|
799
|
+
},
|
|
800
|
+
retryCount: -1,
|
|
801
|
+
tracingContext: {
|
|
802
|
+
currentSpan: sleepSpan
|
|
803
|
+
},
|
|
804
|
+
getInitData: () => stepResults?.input,
|
|
805
|
+
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
806
|
+
// TODO: this function shouldn't have suspend probably?
|
|
807
|
+
suspend: async (_suspendPayload) => {
|
|
808
|
+
},
|
|
809
|
+
bail: () => {
|
|
810
|
+
},
|
|
811
|
+
abort: () => {
|
|
812
|
+
abortController?.abort();
|
|
813
|
+
},
|
|
814
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
815
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
816
|
+
engine: { step: this.inngestStep },
|
|
817
|
+
abortSignal: abortController?.signal,
|
|
818
|
+
writer: new tools.ToolStream(
|
|
819
|
+
{
|
|
820
|
+
prefix: "workflow-step",
|
|
821
|
+
callId: stepCallId,
|
|
822
|
+
name: "sleep",
|
|
823
|
+
runId
|
|
824
|
+
},
|
|
825
|
+
writableStream
|
|
826
|
+
)
|
|
765
827
|
},
|
|
766
|
-
|
|
828
|
+
{
|
|
829
|
+
paramName: "runCount",
|
|
830
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
831
|
+
logger: this.logger
|
|
832
|
+
}
|
|
767
833
|
)
|
|
768
|
-
|
|
834
|
+
);
|
|
769
835
|
});
|
|
770
836
|
sleepSpan?.update({
|
|
771
837
|
attributes: {
|
|
@@ -789,14 +855,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
789
855
|
stepResults,
|
|
790
856
|
emitter,
|
|
791
857
|
abortController,
|
|
792
|
-
|
|
858
|
+
requestContext,
|
|
793
859
|
executionContext,
|
|
794
860
|
writableStream,
|
|
795
861
|
tracingContext
|
|
796
862
|
}) {
|
|
797
863
|
let { date, fn } = entry;
|
|
798
864
|
const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
799
|
-
type:
|
|
865
|
+
type: observability.SpanType.WORKFLOW_SLEEP,
|
|
800
866
|
name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
|
|
801
867
|
attributes: {
|
|
802
868
|
untilDate: date,
|
|
@@ -808,45 +874,53 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
808
874
|
if (fn) {
|
|
809
875
|
date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
|
|
810
876
|
const stepCallId = crypto.randomUUID();
|
|
811
|
-
return await fn(
|
|
812
|
-
|
|
813
|
-
workflowId,
|
|
814
|
-
mastra: this.mastra,
|
|
815
|
-
runtimeContext,
|
|
816
|
-
inputData: prevOutput,
|
|
817
|
-
state: executionContext.state,
|
|
818
|
-
setState: (state) => {
|
|
819
|
-
executionContext.state = state;
|
|
820
|
-
},
|
|
821
|
-
runCount: -1,
|
|
822
|
-
tracingContext: {
|
|
823
|
-
currentSpan: sleepUntilSpan
|
|
824
|
-
},
|
|
825
|
-
getInitData: () => stepResults?.input,
|
|
826
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
827
|
-
// TODO: this function shouldn't have suspend probably?
|
|
828
|
-
suspend: async (_suspendPayload) => {
|
|
829
|
-
},
|
|
830
|
-
bail: () => {
|
|
831
|
-
},
|
|
832
|
-
abort: () => {
|
|
833
|
-
abortController?.abort();
|
|
834
|
-
},
|
|
835
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
836
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
837
|
-
// TODO: add streamVNext support
|
|
838
|
-
engine: { step: this.inngestStep },
|
|
839
|
-
abortSignal: abortController?.signal,
|
|
840
|
-
writer: new tools.ToolStream(
|
|
877
|
+
return await fn(
|
|
878
|
+
workflows.createDeprecationProxy(
|
|
841
879
|
{
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
880
|
+
runId,
|
|
881
|
+
workflowId,
|
|
882
|
+
mastra: this.mastra,
|
|
883
|
+
requestContext,
|
|
884
|
+
inputData: prevOutput,
|
|
885
|
+
state: executionContext.state,
|
|
886
|
+
setState: (state) => {
|
|
887
|
+
executionContext.state = state;
|
|
888
|
+
},
|
|
889
|
+
retryCount: -1,
|
|
890
|
+
tracingContext: {
|
|
891
|
+
currentSpan: sleepUntilSpan
|
|
892
|
+
},
|
|
893
|
+
getInitData: () => stepResults?.input,
|
|
894
|
+
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
895
|
+
// TODO: this function shouldn't have suspend probably?
|
|
896
|
+
suspend: async (_suspendPayload) => {
|
|
897
|
+
},
|
|
898
|
+
bail: () => {
|
|
899
|
+
},
|
|
900
|
+
abort: () => {
|
|
901
|
+
abortController?.abort();
|
|
902
|
+
},
|
|
903
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
904
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
905
|
+
engine: { step: this.inngestStep },
|
|
906
|
+
abortSignal: abortController?.signal,
|
|
907
|
+
writer: new tools.ToolStream(
|
|
908
|
+
{
|
|
909
|
+
prefix: "workflow-step",
|
|
910
|
+
callId: stepCallId,
|
|
911
|
+
name: "sleep",
|
|
912
|
+
runId
|
|
913
|
+
},
|
|
914
|
+
writableStream
|
|
915
|
+
)
|
|
846
916
|
},
|
|
847
|
-
|
|
917
|
+
{
|
|
918
|
+
paramName: "runCount",
|
|
919
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
920
|
+
logger: this.logger
|
|
921
|
+
}
|
|
848
922
|
)
|
|
849
|
-
|
|
923
|
+
);
|
|
850
924
|
});
|
|
851
925
|
if (date && !(date instanceof Date)) {
|
|
852
926
|
date = new Date(date);
|
|
@@ -870,16 +944,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
870
944
|
throw e;
|
|
871
945
|
}
|
|
872
946
|
}
|
|
873
|
-
async executeWaitForEvent({ event, timeout }) {
|
|
874
|
-
const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
|
|
875
|
-
event: `user-event-${event}`,
|
|
876
|
-
timeout: timeout ?? 5e3
|
|
877
|
-
});
|
|
878
|
-
if (eventData === null) {
|
|
879
|
-
throw "Timeout waiting for event";
|
|
880
|
-
}
|
|
881
|
-
return eventData?.data;
|
|
882
|
-
}
|
|
883
947
|
async executeStep({
|
|
884
948
|
step,
|
|
885
949
|
stepResults,
|
|
@@ -888,14 +952,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
888
952
|
prevOutput,
|
|
889
953
|
emitter,
|
|
890
954
|
abortController,
|
|
891
|
-
|
|
955
|
+
requestContext,
|
|
892
956
|
tracingContext,
|
|
893
957
|
writableStream,
|
|
894
958
|
disableScorers
|
|
895
959
|
}) {
|
|
896
|
-
const
|
|
960
|
+
const stepSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
897
961
|
name: `workflow step: '${step.id}'`,
|
|
898
|
-
type:
|
|
962
|
+
type: observability.SpanType.WORKFLOW_STEP,
|
|
899
963
|
input: prevOutput,
|
|
900
964
|
attributes: {
|
|
901
965
|
stepId: step.id
|
|
@@ -912,27 +976,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
912
976
|
async () => {
|
|
913
977
|
const startedAt2 = Date.now();
|
|
914
978
|
await emitter.emit("watch", {
|
|
915
|
-
type: "watch",
|
|
916
|
-
payload: {
|
|
917
|
-
currentStep: {
|
|
918
|
-
id: step.id,
|
|
919
|
-
status: "running"
|
|
920
|
-
},
|
|
921
|
-
workflowState: {
|
|
922
|
-
status: "running",
|
|
923
|
-
steps: {
|
|
924
|
-
...stepResults,
|
|
925
|
-
[step.id]: {
|
|
926
|
-
status: "running"
|
|
927
|
-
}
|
|
928
|
-
},
|
|
929
|
-
result: null,
|
|
930
|
-
error: null
|
|
931
|
-
}
|
|
932
|
-
},
|
|
933
|
-
eventTimestamp: Date.now()
|
|
934
|
-
});
|
|
935
|
-
await emitter.emit("watch-v2", {
|
|
936
979
|
type: "workflow-step-start",
|
|
937
980
|
payload: {
|
|
938
981
|
id: step.id,
|
|
@@ -1008,23 +1051,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1008
1051
|
async () => {
|
|
1009
1052
|
if (result.status === "failed") {
|
|
1010
1053
|
await emitter.emit("watch", {
|
|
1011
|
-
type: "watch",
|
|
1012
|
-
payload: {
|
|
1013
|
-
currentStep: {
|
|
1014
|
-
id: step.id,
|
|
1015
|
-
status: "failed",
|
|
1016
|
-
error: result?.error
|
|
1017
|
-
},
|
|
1018
|
-
workflowState: {
|
|
1019
|
-
status: "running",
|
|
1020
|
-
steps: stepResults,
|
|
1021
|
-
result: null,
|
|
1022
|
-
error: null
|
|
1023
|
-
}
|
|
1024
|
-
},
|
|
1025
|
-
eventTimestamp: Date.now()
|
|
1026
|
-
});
|
|
1027
|
-
await emitter.emit("watch-v2", {
|
|
1028
1054
|
type: "workflow-step-result",
|
|
1029
1055
|
payload: {
|
|
1030
1056
|
id: step.id,
|
|
@@ -1043,27 +1069,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1043
1069
|
const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
|
|
1044
1070
|
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
1045
1071
|
await emitter.emit("watch", {
|
|
1046
|
-
type: "watch",
|
|
1047
|
-
payload: {
|
|
1048
|
-
currentStep: {
|
|
1049
|
-
id: step.id,
|
|
1050
|
-
status: "suspended",
|
|
1051
|
-
payload: stepResult.payload,
|
|
1052
|
-
suspendPayload: {
|
|
1053
|
-
...stepResult?.suspendPayload,
|
|
1054
|
-
__workflow_meta: { runId, path: suspendPath }
|
|
1055
|
-
}
|
|
1056
|
-
},
|
|
1057
|
-
workflowState: {
|
|
1058
|
-
status: "running",
|
|
1059
|
-
steps: stepResults,
|
|
1060
|
-
result: null,
|
|
1061
|
-
error: null
|
|
1062
|
-
}
|
|
1063
|
-
},
|
|
1064
|
-
eventTimestamp: Date.now()
|
|
1065
|
-
});
|
|
1066
|
-
await emitter.emit("watch-v2", {
|
|
1067
1072
|
type: "workflow-step-suspended",
|
|
1068
1073
|
payload: {
|
|
1069
1074
|
id: step.id,
|
|
@@ -1082,23 +1087,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1082
1087
|
}
|
|
1083
1088
|
};
|
|
1084
1089
|
}
|
|
1085
|
-
await emitter.emit("watch", {
|
|
1086
|
-
type: "watch",
|
|
1087
|
-
payload: {
|
|
1088
|
-
currentStep: {
|
|
1089
|
-
id: step.id,
|
|
1090
|
-
status: "suspended",
|
|
1091
|
-
payload: {}
|
|
1092
|
-
},
|
|
1093
|
-
workflowState: {
|
|
1094
|
-
status: "running",
|
|
1095
|
-
steps: stepResults,
|
|
1096
|
-
result: null,
|
|
1097
|
-
error: null
|
|
1098
|
-
}
|
|
1099
|
-
},
|
|
1100
|
-
eventTimestamp: Date.now()
|
|
1101
|
-
});
|
|
1102
1090
|
return {
|
|
1103
1091
|
executionContext,
|
|
1104
1092
|
result: {
|
|
@@ -1108,23 +1096,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1108
1096
|
};
|
|
1109
1097
|
}
|
|
1110
1098
|
await emitter.emit("watch", {
|
|
1111
|
-
type: "watch",
|
|
1112
|
-
payload: {
|
|
1113
|
-
currentStep: {
|
|
1114
|
-
id: step.id,
|
|
1115
|
-
status: "success",
|
|
1116
|
-
output: result?.result
|
|
1117
|
-
},
|
|
1118
|
-
workflowState: {
|
|
1119
|
-
status: "running",
|
|
1120
|
-
steps: stepResults,
|
|
1121
|
-
result: null,
|
|
1122
|
-
error: null
|
|
1123
|
-
}
|
|
1124
|
-
},
|
|
1125
|
-
eventTimestamp: Date.now()
|
|
1126
|
-
});
|
|
1127
|
-
await emitter.emit("watch-v2", {
|
|
1128
1099
|
type: "workflow-step-result",
|
|
1129
1100
|
payload: {
|
|
1130
1101
|
id: step.id,
|
|
@@ -1132,7 +1103,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1132
1103
|
output: result?.result
|
|
1133
1104
|
}
|
|
1134
1105
|
});
|
|
1135
|
-
await emitter.emit("watch
|
|
1106
|
+
await emitter.emit("watch", {
|
|
1136
1107
|
type: "workflow-step-finish",
|
|
1137
1108
|
payload: {
|
|
1138
1109
|
id: step.id,
|
|
@@ -1152,6 +1123,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1152
1123
|
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1153
1124
|
};
|
|
1154
1125
|
}
|
|
1126
|
+
const stepCallId = crypto.randomUUID();
|
|
1155
1127
|
let stepRes;
|
|
1156
1128
|
try {
|
|
1157
1129
|
stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
|
|
@@ -1165,8 +1137,16 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1165
1137
|
const result = await step.execute({
|
|
1166
1138
|
runId: executionContext.runId,
|
|
1167
1139
|
mastra: this.mastra,
|
|
1168
|
-
|
|
1169
|
-
|
|
1140
|
+
requestContext,
|
|
1141
|
+
writer: new tools.ToolStream(
|
|
1142
|
+
{
|
|
1143
|
+
prefix: "workflow-step",
|
|
1144
|
+
callId: stepCallId,
|
|
1145
|
+
name: step.id,
|
|
1146
|
+
runId: executionContext.runId
|
|
1147
|
+
},
|
|
1148
|
+
writableStream
|
|
1149
|
+
),
|
|
1170
1150
|
state: executionContext?.state ?? {},
|
|
1171
1151
|
setState: (state) => {
|
|
1172
1152
|
executionContext.state = state;
|
|
@@ -1174,14 +1154,20 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1174
1154
|
inputData,
|
|
1175
1155
|
resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
|
|
1176
1156
|
tracingContext: {
|
|
1177
|
-
currentSpan:
|
|
1157
|
+
currentSpan: stepSpan
|
|
1178
1158
|
},
|
|
1179
1159
|
getInitData: () => stepResults?.input,
|
|
1180
1160
|
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1181
1161
|
suspend: async (suspendPayload, suspendOptions) => {
|
|
1182
1162
|
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
1183
1163
|
if (suspendOptions?.resumeLabel) {
|
|
1184
|
-
|
|
1164
|
+
const resumeLabel = Array.isArray(suspendOptions.resumeLabel) ? suspendOptions.resumeLabel : [suspendOptions.resumeLabel];
|
|
1165
|
+
for (const label of resumeLabel) {
|
|
1166
|
+
executionContext.resumeLabels[label] = {
|
|
1167
|
+
stepId: step.id,
|
|
1168
|
+
foreachIndex: executionContext.foreachIndex
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1185
1171
|
}
|
|
1186
1172
|
suspended = { payload: suspendPayload };
|
|
1187
1173
|
},
|
|
@@ -1195,6 +1181,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1195
1181
|
runId: stepResults[step.id]?.suspendPayload?.__workflow_meta?.runId
|
|
1196
1182
|
},
|
|
1197
1183
|
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1184
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1198
1185
|
engine: {
|
|
1199
1186
|
step: this.inngestStep
|
|
1200
1187
|
},
|
|
@@ -1222,7 +1209,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1222
1209
|
};
|
|
1223
1210
|
execResults = stepFailure;
|
|
1224
1211
|
const fallbackErrorMessage = `Step ${step.id} failed`;
|
|
1225
|
-
|
|
1212
|
+
stepSpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
|
|
1226
1213
|
throw new inngest.RetryAfterError(execResults.error ?? fallbackErrorMessage, executionContext.retryConfig.delay, {
|
|
1227
1214
|
cause: execResults
|
|
1228
1215
|
});
|
|
@@ -1246,24 +1233,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1246
1233
|
startedAt
|
|
1247
1234
|
};
|
|
1248
1235
|
}
|
|
1249
|
-
await emitter.emit("watch", {
|
|
1250
|
-
type: "watch",
|
|
1251
|
-
payload: {
|
|
1252
|
-
currentStep: {
|
|
1253
|
-
id: step.id,
|
|
1254
|
-
...execResults
|
|
1255
|
-
},
|
|
1256
|
-
workflowState: {
|
|
1257
|
-
status: "running",
|
|
1258
|
-
steps: { ...stepResults, [step.id]: execResults },
|
|
1259
|
-
result: null,
|
|
1260
|
-
error: null
|
|
1261
|
-
}
|
|
1262
|
-
},
|
|
1263
|
-
eventTimestamp: Date.now()
|
|
1264
|
-
});
|
|
1265
1236
|
if (execResults.status === "suspended") {
|
|
1266
|
-
await emitter.emit("watch
|
|
1237
|
+
await emitter.emit("watch", {
|
|
1267
1238
|
type: "workflow-step-suspended",
|
|
1268
1239
|
payload: {
|
|
1269
1240
|
id: step.id,
|
|
@@ -1271,14 +1242,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1271
1242
|
}
|
|
1272
1243
|
});
|
|
1273
1244
|
} else {
|
|
1274
|
-
await emitter.emit("watch
|
|
1245
|
+
await emitter.emit("watch", {
|
|
1275
1246
|
type: "workflow-step-result",
|
|
1276
1247
|
payload: {
|
|
1277
1248
|
id: step.id,
|
|
1278
1249
|
...execResults
|
|
1279
1250
|
}
|
|
1280
1251
|
});
|
|
1281
|
-
await emitter.emit("watch
|
|
1252
|
+
await emitter.emit("watch", {
|
|
1282
1253
|
type: "workflow-step-finish",
|
|
1283
1254
|
payload: {
|
|
1284
1255
|
id: step.id,
|
|
@@ -1286,7 +1257,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1286
1257
|
}
|
|
1287
1258
|
});
|
|
1288
1259
|
}
|
|
1289
|
-
|
|
1260
|
+
stepSpan?.end({ output: execResults });
|
|
1290
1261
|
return { result: execResults, executionContext, stepResults };
|
|
1291
1262
|
});
|
|
1292
1263
|
} catch (e) {
|
|
@@ -1316,9 +1287,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1316
1287
|
output: stepRes.result,
|
|
1317
1288
|
workflowId: executionContext.workflowId,
|
|
1318
1289
|
stepId: step.id,
|
|
1319
|
-
|
|
1290
|
+
requestContext,
|
|
1320
1291
|
disableScorers,
|
|
1321
|
-
tracingContext: { currentSpan:
|
|
1292
|
+
tracingContext: { currentSpan: stepSpan }
|
|
1322
1293
|
});
|
|
1323
1294
|
}
|
|
1324
1295
|
});
|
|
@@ -1374,20 +1345,18 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1374
1345
|
runId,
|
|
1375
1346
|
entry,
|
|
1376
1347
|
prevOutput,
|
|
1377
|
-
prevStep,
|
|
1378
1348
|
stepResults,
|
|
1379
|
-
serializedStepGraph,
|
|
1380
1349
|
resume,
|
|
1381
1350
|
executionContext,
|
|
1382
1351
|
emitter,
|
|
1383
1352
|
abortController,
|
|
1384
|
-
|
|
1353
|
+
requestContext,
|
|
1385
1354
|
writableStream,
|
|
1386
1355
|
disableScorers,
|
|
1387
1356
|
tracingContext
|
|
1388
1357
|
}) {
|
|
1389
1358
|
const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
1390
|
-
type:
|
|
1359
|
+
type: observability.SpanType.WORKFLOW_CONDITIONAL,
|
|
1391
1360
|
name: `conditional: '${entry.conditions.length} conditions'`,
|
|
1392
1361
|
input: prevOutput,
|
|
1393
1362
|
attributes: {
|
|
@@ -1400,7 +1369,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1400
1369
|
entry.conditions.map(
|
|
1401
1370
|
(cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
|
|
1402
1371
|
const evalSpan = conditionalSpan?.createChildSpan({
|
|
1403
|
-
type:
|
|
1372
|
+
type: observability.SpanType.WORKFLOW_CONDITIONAL_EVAL,
|
|
1404
1373
|
name: `condition: '${index}'`,
|
|
1405
1374
|
input: prevOutput,
|
|
1406
1375
|
attributes: {
|
|
@@ -1409,47 +1378,55 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1409
1378
|
tracingPolicy: this.options?.tracingPolicy
|
|
1410
1379
|
});
|
|
1411
1380
|
try {
|
|
1412
|
-
const result = await cond(
|
|
1413
|
-
|
|
1414
|
-
workflowId,
|
|
1415
|
-
mastra: this.mastra,
|
|
1416
|
-
runtimeContext,
|
|
1417
|
-
runCount: -1,
|
|
1418
|
-
inputData: prevOutput,
|
|
1419
|
-
state: executionContext.state,
|
|
1420
|
-
setState: (state) => {
|
|
1421
|
-
executionContext.state = state;
|
|
1422
|
-
},
|
|
1423
|
-
tracingContext: {
|
|
1424
|
-
currentSpan: evalSpan
|
|
1425
|
-
},
|
|
1426
|
-
getInitData: () => stepResults?.input,
|
|
1427
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1428
|
-
// TODO: this function shouldn't have suspend probably?
|
|
1429
|
-
suspend: async (_suspendPayload) => {
|
|
1430
|
-
},
|
|
1431
|
-
bail: () => {
|
|
1432
|
-
},
|
|
1433
|
-
abort: () => {
|
|
1434
|
-
abortController.abort();
|
|
1435
|
-
},
|
|
1436
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1437
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1438
|
-
// TODO: add streamVNext support
|
|
1439
|
-
engine: {
|
|
1440
|
-
step: this.inngestStep
|
|
1441
|
-
},
|
|
1442
|
-
abortSignal: abortController.signal,
|
|
1443
|
-
writer: new tools.ToolStream(
|
|
1381
|
+
const result = await cond(
|
|
1382
|
+
workflows.createDeprecationProxy(
|
|
1444
1383
|
{
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1384
|
+
runId,
|
|
1385
|
+
workflowId,
|
|
1386
|
+
mastra: this.mastra,
|
|
1387
|
+
requestContext,
|
|
1388
|
+
retryCount: -1,
|
|
1389
|
+
inputData: prevOutput,
|
|
1390
|
+
state: executionContext.state,
|
|
1391
|
+
setState: (state) => {
|
|
1392
|
+
executionContext.state = state;
|
|
1393
|
+
},
|
|
1394
|
+
tracingContext: {
|
|
1395
|
+
currentSpan: evalSpan
|
|
1396
|
+
},
|
|
1397
|
+
getInitData: () => stepResults?.input,
|
|
1398
|
+
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1399
|
+
// TODO: this function shouldn't have suspend probably?
|
|
1400
|
+
suspend: async (_suspendPayload) => {
|
|
1401
|
+
},
|
|
1402
|
+
bail: () => {
|
|
1403
|
+
},
|
|
1404
|
+
abort: () => {
|
|
1405
|
+
abortController.abort();
|
|
1406
|
+
},
|
|
1407
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1408
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1409
|
+
engine: {
|
|
1410
|
+
step: this.inngestStep
|
|
1411
|
+
},
|
|
1412
|
+
abortSignal: abortController.signal,
|
|
1413
|
+
writer: new tools.ToolStream(
|
|
1414
|
+
{
|
|
1415
|
+
prefix: "workflow-step",
|
|
1416
|
+
callId: crypto.randomUUID(),
|
|
1417
|
+
name: "conditional",
|
|
1418
|
+
runId
|
|
1419
|
+
},
|
|
1420
|
+
writableStream
|
|
1421
|
+
)
|
|
1449
1422
|
},
|
|
1450
|
-
|
|
1423
|
+
{
|
|
1424
|
+
paramName: "runCount",
|
|
1425
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
1426
|
+
logger: this.logger
|
|
1427
|
+
}
|
|
1451
1428
|
)
|
|
1452
|
-
|
|
1429
|
+
);
|
|
1453
1430
|
evalSpan?.end({
|
|
1454
1431
|
output: result,
|
|
1455
1432
|
attributes: {
|
|
@@ -1477,13 +1454,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1477
1454
|
}
|
|
1478
1455
|
});
|
|
1479
1456
|
const results = await Promise.all(
|
|
1480
|
-
stepsToRun.map(
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1457
|
+
stepsToRun.map(async (step, index) => {
|
|
1458
|
+
const currStepResult = stepResults[step.step.id];
|
|
1459
|
+
if (currStepResult && currStepResult.status === "success") {
|
|
1460
|
+
return currStepResult;
|
|
1461
|
+
}
|
|
1462
|
+
const result = await this.executeStep({
|
|
1463
|
+
step: step.step,
|
|
1464
|
+
prevOutput,
|
|
1487
1465
|
stepResults,
|
|
1488
1466
|
resume,
|
|
1489
1467
|
executionContext: {
|
|
@@ -1493,31 +1471,32 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1493
1471
|
suspendedPaths: executionContext.suspendedPaths,
|
|
1494
1472
|
resumeLabels: executionContext.resumeLabels,
|
|
1495
1473
|
retryConfig: executionContext.retryConfig,
|
|
1496
|
-
executionSpan: executionContext.executionSpan,
|
|
1497
1474
|
state: executionContext.state
|
|
1498
1475
|
},
|
|
1499
1476
|
emitter,
|
|
1500
1477
|
abortController,
|
|
1501
|
-
|
|
1478
|
+
requestContext,
|
|
1502
1479
|
writableStream,
|
|
1503
1480
|
disableScorers,
|
|
1504
1481
|
tracingContext: {
|
|
1505
1482
|
currentSpan: conditionalSpan
|
|
1506
1483
|
}
|
|
1507
|
-
})
|
|
1508
|
-
|
|
1484
|
+
});
|
|
1485
|
+
stepResults[step.step.id] = result;
|
|
1486
|
+
return result;
|
|
1487
|
+
})
|
|
1509
1488
|
);
|
|
1510
|
-
const hasFailed = results.find((result) => result.
|
|
1511
|
-
const hasSuspended = results.find((result) => result.
|
|
1489
|
+
const hasFailed = results.find((result) => result.status === "failed");
|
|
1490
|
+
const hasSuspended = results.find((result) => result.status === "suspended");
|
|
1512
1491
|
if (hasFailed) {
|
|
1513
|
-
execResults = { status: "failed", error: hasFailed.
|
|
1492
|
+
execResults = { status: "failed", error: hasFailed.error };
|
|
1514
1493
|
} else if (hasSuspended) {
|
|
1515
|
-
execResults = { status: "suspended", suspendPayload: hasSuspended.
|
|
1494
|
+
execResults = { status: "suspended", suspendPayload: hasSuspended.suspendPayload };
|
|
1516
1495
|
} else {
|
|
1517
1496
|
execResults = {
|
|
1518
1497
|
status: "success",
|
|
1519
1498
|
output: results.reduce((acc, result, index) => {
|
|
1520
|
-
if (result.
|
|
1499
|
+
if (result.status === "success") {
|
|
1521
1500
|
acc[stepsToRun[index].step.id] = result.output;
|
|
1522
1501
|
}
|
|
1523
1502
|
return acc;
|