@mastra/inngest 0.0.0-cloud-transporter-20250513033346 → 0.0.0-custom-instrumentation-20250626084921
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 +366 -2
- package/dist/_tsup-dts-rollup.d.cts +128 -53
- package/dist/_tsup-dts-rollup.d.ts +128 -53
- package/dist/index.cjs +405 -47
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +402 -45
- package/package.json +19 -15
- package/src/index.test.ts +2684 -1254
- package/src/index.ts +642 -85
package/dist/index.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
2
|
import { subscribe } from '@inngest/realtime';
|
|
3
3
|
import { RuntimeContext } from '@mastra/core/di';
|
|
4
|
-
import {
|
|
4
|
+
import { Tool } from '@mastra/core/tools';
|
|
5
|
+
import { Run, Workflow, DefaultExecutionEngine } from '@mastra/core/workflows';
|
|
6
|
+
import { EMITTER_SYMBOL } from '@mastra/core/workflows/_constants';
|
|
5
7
|
import { serve as serve$1 } from 'inngest/hono';
|
|
8
|
+
import { z } from 'zod';
|
|
6
9
|
|
|
7
10
|
// src/index.ts
|
|
8
11
|
function serve({ mastra, inngest }) {
|
|
9
|
-
const wfs = mastra.
|
|
12
|
+
const wfs = mastra.getWorkflows();
|
|
10
13
|
const functions = Object.values(wfs).flatMap((wf) => {
|
|
11
14
|
if (wf instanceof InngestWorkflow) {
|
|
12
15
|
wf.__registerMastra(mastra);
|
|
@@ -21,14 +24,16 @@ function serve({ mastra, inngest }) {
|
|
|
21
24
|
}
|
|
22
25
|
var InngestRun = class extends Run {
|
|
23
26
|
inngest;
|
|
27
|
+
serializedStepGraph;
|
|
24
28
|
#mastra;
|
|
25
29
|
constructor(params, inngest) {
|
|
26
30
|
super(params);
|
|
27
31
|
this.inngest = inngest;
|
|
32
|
+
this.serializedStepGraph = params.serializedStepGraph;
|
|
28
33
|
this.#mastra = params.mastra;
|
|
29
34
|
}
|
|
30
35
|
async getRuns(eventId) {
|
|
31
|
-
const response = await fetch(`${this.inngest.apiBaseUrl}/v1/events/${eventId}/runs`, {
|
|
36
|
+
const response = await fetch(`${this.inngest.apiBaseUrl ?? "https://api.inngest.com"}/v1/events/${eventId}/runs`, {
|
|
32
37
|
headers: {
|
|
33
38
|
Authorization: `Bearer ${process.env.INNGEST_SIGNING_KEY}`
|
|
34
39
|
}
|
|
@@ -38,7 +43,7 @@ var InngestRun = class extends Run {
|
|
|
38
43
|
}
|
|
39
44
|
async getRunOutput(eventId) {
|
|
40
45
|
let runs = await this.getRuns(eventId);
|
|
41
|
-
while (runs?.[0]?.status !== "Completed") {
|
|
46
|
+
while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
|
|
42
47
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
43
48
|
runs = await this.getRuns(eventId);
|
|
44
49
|
if (runs?.[0]?.status === "Failed" || runs?.[0]?.status === "Cancelled") {
|
|
@@ -47,6 +52,12 @@ var InngestRun = class extends Run {
|
|
|
47
52
|
}
|
|
48
53
|
return runs?.[0];
|
|
49
54
|
}
|
|
55
|
+
async sendEvent(event, data) {
|
|
56
|
+
await this.inngest.send({
|
|
57
|
+
name: `user-event-${event}`,
|
|
58
|
+
data
|
|
59
|
+
});
|
|
60
|
+
}
|
|
50
61
|
async start({
|
|
51
62
|
inputData
|
|
52
63
|
}) {
|
|
@@ -55,11 +66,13 @@ var InngestRun = class extends Run {
|
|
|
55
66
|
runId: this.runId,
|
|
56
67
|
snapshot: {
|
|
57
68
|
runId: this.runId,
|
|
69
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
58
70
|
value: {},
|
|
59
71
|
context: {},
|
|
60
72
|
activePaths: [],
|
|
61
73
|
suspendedPaths: {},
|
|
62
|
-
timestamp: Date.now()
|
|
74
|
+
timestamp: Date.now(),
|
|
75
|
+
status: "running"
|
|
63
76
|
}
|
|
64
77
|
});
|
|
65
78
|
const eventOutput = await this.inngest.send({
|
|
@@ -82,6 +95,17 @@ var InngestRun = class extends Run {
|
|
|
82
95
|
return result;
|
|
83
96
|
}
|
|
84
97
|
async resume(params) {
|
|
98
|
+
const p = this._resume(params).then((result) => {
|
|
99
|
+
if (result.status !== "suspended") {
|
|
100
|
+
this.closeStreamAction?.().catch(() => {
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
});
|
|
105
|
+
this.executionResults = p;
|
|
106
|
+
return p;
|
|
107
|
+
}
|
|
108
|
+
async _resume(params) {
|
|
85
109
|
const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
|
|
86
110
|
(step) => typeof step === "string" ? step : step?.id
|
|
87
111
|
);
|
|
@@ -115,27 +139,62 @@ var InngestRun = class extends Run {
|
|
|
115
139
|
}
|
|
116
140
|
return result;
|
|
117
141
|
}
|
|
118
|
-
watch(cb) {
|
|
142
|
+
watch(cb, type = "watch") {
|
|
143
|
+
let active = true;
|
|
119
144
|
const streamPromise = subscribe(
|
|
120
145
|
{
|
|
121
146
|
channel: `workflow:${this.workflowId}:${this.runId}`,
|
|
122
|
-
topics: [
|
|
147
|
+
topics: [type],
|
|
123
148
|
app: this.inngest
|
|
124
149
|
},
|
|
125
150
|
(message) => {
|
|
126
|
-
|
|
151
|
+
if (active) {
|
|
152
|
+
cb(message.data);
|
|
153
|
+
}
|
|
127
154
|
}
|
|
128
155
|
);
|
|
129
156
|
return () => {
|
|
130
|
-
|
|
131
|
-
|
|
157
|
+
active = false;
|
|
158
|
+
streamPromise.then(async (stream) => {
|
|
159
|
+
return stream.cancel();
|
|
132
160
|
}).catch((err) => {
|
|
133
161
|
console.error(err);
|
|
134
162
|
});
|
|
135
163
|
};
|
|
136
164
|
}
|
|
165
|
+
stream({ inputData, runtimeContext } = {}) {
|
|
166
|
+
const { readable, writable } = new TransformStream();
|
|
167
|
+
const writer = writable.getWriter();
|
|
168
|
+
const unwatch = this.watch(async (event) => {
|
|
169
|
+
try {
|
|
170
|
+
await writer.write(event);
|
|
171
|
+
} catch {
|
|
172
|
+
}
|
|
173
|
+
}, "watch-v2");
|
|
174
|
+
this.closeStreamAction = async () => {
|
|
175
|
+
unwatch();
|
|
176
|
+
try {
|
|
177
|
+
await writer.close();
|
|
178
|
+
} catch (err) {
|
|
179
|
+
console.error("Error closing stream:", err);
|
|
180
|
+
} finally {
|
|
181
|
+
writer.releaseLock();
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
this.executionResults = this.start({ inputData, runtimeContext }).then((result) => {
|
|
185
|
+
if (result.status !== "suspended") {
|
|
186
|
+
this.closeStreamAction?.().catch(() => {
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
});
|
|
191
|
+
return {
|
|
192
|
+
stream: readable,
|
|
193
|
+
getWorkflowState: () => this.executionResults
|
|
194
|
+
};
|
|
195
|
+
}
|
|
137
196
|
};
|
|
138
|
-
var InngestWorkflow = class _InngestWorkflow extends
|
|
197
|
+
var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
139
198
|
#mastra;
|
|
140
199
|
inngest;
|
|
141
200
|
function;
|
|
@@ -156,11 +215,32 @@ var InngestWorkflow = class _InngestWorkflow extends NewWorkflow {
|
|
|
156
215
|
const storage = this.#mastra?.getStorage();
|
|
157
216
|
if (!storage) {
|
|
158
217
|
this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
|
|
159
|
-
return null;
|
|
218
|
+
return this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null;
|
|
160
219
|
}
|
|
161
220
|
const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
|
|
162
221
|
return run ?? (this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null);
|
|
163
222
|
}
|
|
223
|
+
async getWorkflowRunExecutionResult(runId) {
|
|
224
|
+
const storage = this.#mastra?.getStorage();
|
|
225
|
+
if (!storage) {
|
|
226
|
+
this.logger.debug("Cannot get workflow run execution result. Mastra storage is not initialized");
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
|
|
230
|
+
if (!run?.snapshot) {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
if (typeof run.snapshot === "string") {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
status: run.snapshot.status,
|
|
238
|
+
result: run.snapshot.result,
|
|
239
|
+
error: run.snapshot.error,
|
|
240
|
+
payload: run.snapshot.context?.input,
|
|
241
|
+
steps: run.snapshot.context
|
|
242
|
+
};
|
|
243
|
+
}
|
|
164
244
|
__registerMastra(mastra) {
|
|
165
245
|
this.#mastra = mastra;
|
|
166
246
|
this.executionEngine.__registerMastra(mastra);
|
|
@@ -187,6 +267,25 @@ var InngestWorkflow = class _InngestWorkflow extends NewWorkflow {
|
|
|
187
267
|
runId: runIdToUse,
|
|
188
268
|
executionEngine: this.executionEngine,
|
|
189
269
|
executionGraph: this.executionGraph,
|
|
270
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
271
|
+
mastra: this.#mastra,
|
|
272
|
+
retryConfig: this.retryConfig,
|
|
273
|
+
cleanup: () => this.runs.delete(runIdToUse)
|
|
274
|
+
},
|
|
275
|
+
this.inngest
|
|
276
|
+
);
|
|
277
|
+
this.runs.set(runIdToUse, run);
|
|
278
|
+
return run;
|
|
279
|
+
}
|
|
280
|
+
async createRunAsync(options) {
|
|
281
|
+
const runIdToUse = options?.runId || randomUUID();
|
|
282
|
+
const run = this.runs.get(runIdToUse) ?? new InngestRun(
|
|
283
|
+
{
|
|
284
|
+
workflowId: this.id,
|
|
285
|
+
runId: runIdToUse,
|
|
286
|
+
executionEngine: this.executionEngine,
|
|
287
|
+
executionGraph: this.executionGraph,
|
|
288
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
190
289
|
mastra: this.#mastra,
|
|
191
290
|
retryConfig: this.retryConfig,
|
|
192
291
|
cleanup: () => this.runs.delete(runIdToUse)
|
|
@@ -194,6 +293,23 @@ var InngestWorkflow = class _InngestWorkflow extends NewWorkflow {
|
|
|
194
293
|
this.inngest
|
|
195
294
|
);
|
|
196
295
|
this.runs.set(runIdToUse, run);
|
|
296
|
+
await this.mastra?.getStorage()?.persistWorkflowSnapshot({
|
|
297
|
+
workflowName: this.id,
|
|
298
|
+
runId: runIdToUse,
|
|
299
|
+
snapshot: {
|
|
300
|
+
runId: runIdToUse,
|
|
301
|
+
status: "pending",
|
|
302
|
+
value: {},
|
|
303
|
+
context: {},
|
|
304
|
+
activePaths: [],
|
|
305
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
306
|
+
suspendedPaths: {},
|
|
307
|
+
result: void 0,
|
|
308
|
+
error: void 0,
|
|
309
|
+
// @ts-ignore
|
|
310
|
+
timestamp: Date.now()
|
|
311
|
+
}
|
|
312
|
+
});
|
|
197
313
|
return run;
|
|
198
314
|
}
|
|
199
315
|
getFunction() {
|
|
@@ -219,12 +335,18 @@ var InngestWorkflow = class _InngestWorkflow extends NewWorkflow {
|
|
|
219
335
|
try {
|
|
220
336
|
await publish({
|
|
221
337
|
channel: `workflow:${this.id}:${runId}`,
|
|
222
|
-
topic:
|
|
338
|
+
topic: event2,
|
|
223
339
|
data
|
|
224
340
|
});
|
|
225
341
|
} catch (err) {
|
|
226
342
|
this.logger.error("Error emitting event: " + (err?.stack ?? err?.message ?? err));
|
|
227
343
|
}
|
|
344
|
+
},
|
|
345
|
+
on: (_event, _callback) => {
|
|
346
|
+
},
|
|
347
|
+
off: (_event, _callback) => {
|
|
348
|
+
},
|
|
349
|
+
once: (_event, _callback) => {
|
|
228
350
|
}
|
|
229
351
|
};
|
|
230
352
|
const engine = new InngestExecutionEngine(this.#mastra, step, attempt);
|
|
@@ -232,6 +354,7 @@ var InngestWorkflow = class _InngestWorkflow extends NewWorkflow {
|
|
|
232
354
|
workflowId: this.id,
|
|
233
355
|
runId,
|
|
234
356
|
graph: this.executionGraph,
|
|
357
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
235
358
|
input: inputData,
|
|
236
359
|
emitter,
|
|
237
360
|
retryConfig: this.retryConfig,
|
|
@@ -261,20 +384,106 @@ var InngestWorkflow = class _InngestWorkflow extends NewWorkflow {
|
|
|
261
384
|
return [this.getFunction(), ...this.getNestedFunctions(this.executionGraph.steps)];
|
|
262
385
|
}
|
|
263
386
|
};
|
|
264
|
-
function
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
387
|
+
function isAgent(params) {
|
|
388
|
+
return params?.component === "AGENT";
|
|
389
|
+
}
|
|
390
|
+
function isTool(params) {
|
|
391
|
+
return params instanceof Tool;
|
|
392
|
+
}
|
|
393
|
+
function createStep(params) {
|
|
394
|
+
if (isAgent(params)) {
|
|
395
|
+
return {
|
|
396
|
+
id: params.name,
|
|
397
|
+
// @ts-ignore
|
|
398
|
+
inputSchema: z.object({
|
|
399
|
+
prompt: z.string()
|
|
400
|
+
// resourceId: z.string().optional(),
|
|
401
|
+
// threadId: z.string().optional(),
|
|
402
|
+
}),
|
|
403
|
+
// @ts-ignore
|
|
404
|
+
outputSchema: z.object({
|
|
405
|
+
text: z.string()
|
|
406
|
+
}),
|
|
407
|
+
execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext }) => {
|
|
408
|
+
let streamPromise = {};
|
|
409
|
+
streamPromise.promise = new Promise((resolve, reject) => {
|
|
410
|
+
streamPromise.resolve = resolve;
|
|
411
|
+
streamPromise.reject = reject;
|
|
412
|
+
});
|
|
413
|
+
const toolData = {
|
|
414
|
+
name: params.name,
|
|
415
|
+
args: inputData
|
|
416
|
+
};
|
|
417
|
+
await emitter.emit("watch-v2", {
|
|
418
|
+
type: "tool-call-streaming-start",
|
|
419
|
+
...toolData
|
|
420
|
+
});
|
|
421
|
+
const { fullStream } = await params.stream(inputData.prompt, {
|
|
422
|
+
// resourceId: inputData.resourceId,
|
|
423
|
+
// threadId: inputData.threadId,
|
|
424
|
+
runtimeContext,
|
|
425
|
+
onFinish: (result) => {
|
|
426
|
+
streamPromise.resolve(result.text);
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
for await (const chunk of fullStream) {
|
|
430
|
+
switch (chunk.type) {
|
|
431
|
+
case "text-delta":
|
|
432
|
+
await emitter.emit("watch-v2", {
|
|
433
|
+
type: "tool-call-delta",
|
|
434
|
+
...toolData,
|
|
435
|
+
argsTextDelta: chunk.textDelta
|
|
436
|
+
});
|
|
437
|
+
break;
|
|
438
|
+
case "step-start":
|
|
439
|
+
case "step-finish":
|
|
440
|
+
case "finish":
|
|
441
|
+
break;
|
|
442
|
+
case "tool-call":
|
|
443
|
+
case "tool-result":
|
|
444
|
+
case "tool-call-streaming-start":
|
|
445
|
+
case "tool-call-delta":
|
|
446
|
+
case "source":
|
|
447
|
+
case "file":
|
|
448
|
+
default:
|
|
449
|
+
await emitter.emit("watch-v2", chunk);
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return {
|
|
454
|
+
text: await streamPromise.promise
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
if (isTool(params)) {
|
|
460
|
+
if (!params.inputSchema || !params.outputSchema) {
|
|
461
|
+
throw new Error("Tool must have input and output schemas defined");
|
|
462
|
+
}
|
|
463
|
+
return {
|
|
464
|
+
// TODO: tool probably should have strong id type
|
|
465
|
+
// @ts-ignore
|
|
466
|
+
id: params.id,
|
|
467
|
+
inputSchema: params.inputSchema,
|
|
468
|
+
outputSchema: params.outputSchema,
|
|
469
|
+
execute: async ({ inputData, mastra, runtimeContext }) => {
|
|
470
|
+
return params.execute({
|
|
471
|
+
context: inputData,
|
|
472
|
+
mastra,
|
|
473
|
+
runtimeContext
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
return {
|
|
479
|
+
id: params.id,
|
|
480
|
+
description: params.description,
|
|
481
|
+
inputSchema: params.inputSchema,
|
|
482
|
+
outputSchema: params.outputSchema,
|
|
483
|
+
resumeSchema: params.resumeSchema,
|
|
484
|
+
suspendSchema: params.suspendSchema,
|
|
485
|
+
execute: params.execute
|
|
486
|
+
};
|
|
278
487
|
}
|
|
279
488
|
function init(inngest) {
|
|
280
489
|
return {
|
|
@@ -282,8 +491,27 @@ function init(inngest) {
|
|
|
282
491
|
return new InngestWorkflow(params, inngest);
|
|
283
492
|
},
|
|
284
493
|
createStep,
|
|
285
|
-
cloneStep,
|
|
286
|
-
|
|
494
|
+
cloneStep(step, opts) {
|
|
495
|
+
return {
|
|
496
|
+
id: opts.id,
|
|
497
|
+
description: step.description,
|
|
498
|
+
inputSchema: step.inputSchema,
|
|
499
|
+
outputSchema: step.outputSchema,
|
|
500
|
+
execute: step.execute
|
|
501
|
+
};
|
|
502
|
+
},
|
|
503
|
+
cloneWorkflow(workflow, opts) {
|
|
504
|
+
const wf = new Workflow({
|
|
505
|
+
id: opts.id,
|
|
506
|
+
inputSchema: workflow.inputSchema,
|
|
507
|
+
outputSchema: workflow.outputSchema,
|
|
508
|
+
steps: workflow.stepDefs,
|
|
509
|
+
mastra: workflow.mastra
|
|
510
|
+
});
|
|
511
|
+
wf.setStepFlow(workflow.stepGraph);
|
|
512
|
+
wf.commit();
|
|
513
|
+
return wf;
|
|
514
|
+
}
|
|
287
515
|
};
|
|
288
516
|
}
|
|
289
517
|
var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
@@ -294,6 +522,18 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
294
522
|
this.inngestStep = inngestStep;
|
|
295
523
|
this.inngestAttempts = inngestAttempts;
|
|
296
524
|
}
|
|
525
|
+
async execute(params) {
|
|
526
|
+
await params.emitter.emit("watch-v2", {
|
|
527
|
+
type: "start",
|
|
528
|
+
payload: { runId: params.runId }
|
|
529
|
+
});
|
|
530
|
+
const result = await super.execute(params);
|
|
531
|
+
await params.emitter.emit("watch-v2", {
|
|
532
|
+
type: "finish",
|
|
533
|
+
payload: { runId: params.runId }
|
|
534
|
+
});
|
|
535
|
+
return result;
|
|
536
|
+
}
|
|
297
537
|
async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
|
|
298
538
|
const base = {
|
|
299
539
|
status: lastOutput.status,
|
|
@@ -374,6 +614,19 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
374
614
|
runtimeContext
|
|
375
615
|
});
|
|
376
616
|
}
|
|
617
|
+
async executeSleep({ id, duration }) {
|
|
618
|
+
await this.inngestStep.sleep(id, duration);
|
|
619
|
+
}
|
|
620
|
+
async executeWaitForEvent({ event, timeout }) {
|
|
621
|
+
const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
|
|
622
|
+
event: `user-event-${event}`,
|
|
623
|
+
timeout: timeout ?? 5e3
|
|
624
|
+
});
|
|
625
|
+
if (eventData === null) {
|
|
626
|
+
throw "Timeout waiting for event";
|
|
627
|
+
}
|
|
628
|
+
return eventData?.data;
|
|
629
|
+
}
|
|
377
630
|
async executeStep({
|
|
378
631
|
step,
|
|
379
632
|
stepResults,
|
|
@@ -383,9 +636,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
383
636
|
emitter,
|
|
384
637
|
runtimeContext
|
|
385
638
|
}) {
|
|
386
|
-
await this.inngestStep.run(
|
|
639
|
+
const startedAt = await this.inngestStep.run(
|
|
387
640
|
`workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
|
|
388
641
|
async () => {
|
|
642
|
+
const startedAt2 = Date.now();
|
|
389
643
|
await emitter.emit("watch", {
|
|
390
644
|
type: "watch",
|
|
391
645
|
payload: {
|
|
@@ -407,6 +661,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
407
661
|
},
|
|
408
662
|
eventTimestamp: Date.now()
|
|
409
663
|
});
|
|
664
|
+
await emitter.emit("watch-v2", {
|
|
665
|
+
type: "step-start",
|
|
666
|
+
payload: {
|
|
667
|
+
id: step.id
|
|
668
|
+
}
|
|
669
|
+
});
|
|
670
|
+
return startedAt2;
|
|
410
671
|
}
|
|
411
672
|
);
|
|
412
673
|
if (step instanceof InngestWorkflow) {
|
|
@@ -467,6 +728,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
467
728
|
},
|
|
468
729
|
eventTimestamp: Date.now()
|
|
469
730
|
});
|
|
731
|
+
await emitter.emit("watch-v2", {
|
|
732
|
+
type: "step-result",
|
|
733
|
+
payload: {
|
|
734
|
+
id: step.id,
|
|
735
|
+
status: "failed"
|
|
736
|
+
}
|
|
737
|
+
});
|
|
470
738
|
return { executionContext, result: { status: "failed", error: result?.error } };
|
|
471
739
|
} else if (result.status === "suspended") {
|
|
472
740
|
const suspendedSteps = Object.entries(result.steps).filter(([_stepName, stepResult]) => {
|
|
@@ -493,6 +761,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
493
761
|
},
|
|
494
762
|
eventTimestamp: Date.now()
|
|
495
763
|
});
|
|
764
|
+
await emitter.emit("watch-v2", {
|
|
765
|
+
type: "step-suspended",
|
|
766
|
+
payload: {
|
|
767
|
+
id: step.id
|
|
768
|
+
}
|
|
769
|
+
});
|
|
496
770
|
return {
|
|
497
771
|
executionContext,
|
|
498
772
|
result: {
|
|
@@ -543,6 +817,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
543
817
|
},
|
|
544
818
|
eventTimestamp: Date.now()
|
|
545
819
|
});
|
|
820
|
+
await emitter.emit("watch-v2", {
|
|
821
|
+
type: "step-finish",
|
|
822
|
+
payload: {
|
|
823
|
+
id: step.id,
|
|
824
|
+
metadata: {}
|
|
825
|
+
}
|
|
826
|
+
});
|
|
546
827
|
return { executionContext, result: { status: "success", output: result?.result } };
|
|
547
828
|
}
|
|
548
829
|
);
|
|
@@ -552,8 +833,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
552
833
|
const stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
|
|
553
834
|
let execResults;
|
|
554
835
|
let suspended;
|
|
836
|
+
let bailed;
|
|
555
837
|
try {
|
|
556
838
|
const result = await step.execute({
|
|
839
|
+
runId: executionContext.runId,
|
|
557
840
|
mastra: this.mastra,
|
|
558
841
|
runtimeContext,
|
|
559
842
|
inputData: prevOutput,
|
|
@@ -570,20 +853,53 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
570
853
|
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
571
854
|
suspended = { payload: suspendPayload };
|
|
572
855
|
},
|
|
856
|
+
bail: (result2) => {
|
|
857
|
+
bailed = { payload: result2 };
|
|
858
|
+
},
|
|
573
859
|
resume: {
|
|
574
860
|
steps: resume?.steps?.slice(1) || [],
|
|
575
861
|
resumePayload: resume?.resumePayload,
|
|
576
862
|
// @ts-ignore
|
|
577
863
|
runId: stepResults[step.id]?.payload?.__workflow_meta?.runId
|
|
578
864
|
},
|
|
579
|
-
emitter
|
|
865
|
+
[EMITTER_SYMBOL]: emitter,
|
|
866
|
+
engine: {
|
|
867
|
+
step: this.inngestStep
|
|
868
|
+
}
|
|
580
869
|
});
|
|
581
|
-
|
|
870
|
+
const endedAt = Date.now();
|
|
871
|
+
execResults = {
|
|
872
|
+
status: "success",
|
|
873
|
+
output: result,
|
|
874
|
+
startedAt,
|
|
875
|
+
endedAt,
|
|
876
|
+
payload: prevOutput,
|
|
877
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
878
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
879
|
+
};
|
|
582
880
|
} catch (e) {
|
|
583
|
-
execResults = {
|
|
881
|
+
execResults = {
|
|
882
|
+
status: "failed",
|
|
883
|
+
payload: prevOutput,
|
|
884
|
+
error: e instanceof Error ? e.message : String(e),
|
|
885
|
+
endedAt: Date.now(),
|
|
886
|
+
startedAt,
|
|
887
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
888
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
889
|
+
};
|
|
584
890
|
}
|
|
585
891
|
if (suspended) {
|
|
586
|
-
execResults = {
|
|
892
|
+
execResults = {
|
|
893
|
+
status: "suspended",
|
|
894
|
+
suspendedPayload: suspended.payload,
|
|
895
|
+
payload: prevOutput,
|
|
896
|
+
suspendedAt: Date.now(),
|
|
897
|
+
startedAt,
|
|
898
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
899
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
900
|
+
};
|
|
901
|
+
} else if (bailed) {
|
|
902
|
+
execResults = { status: "bailed", output: bailed.payload, payload: prevOutput, endedAt: Date.now(), startedAt };
|
|
587
903
|
}
|
|
588
904
|
if (execResults.status === "failed") {
|
|
589
905
|
if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
|
|
@@ -595,18 +911,43 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
595
911
|
payload: {
|
|
596
912
|
currentStep: {
|
|
597
913
|
id: step.id,
|
|
598
|
-
|
|
599
|
-
output: execResults.output
|
|
914
|
+
...execResults
|
|
600
915
|
},
|
|
601
916
|
workflowState: {
|
|
602
917
|
status: "running",
|
|
603
|
-
steps: stepResults,
|
|
918
|
+
steps: { ...stepResults, [step.id]: execResults },
|
|
604
919
|
result: null,
|
|
605
920
|
error: null
|
|
606
921
|
}
|
|
607
922
|
},
|
|
608
923
|
eventTimestamp: Date.now()
|
|
609
924
|
});
|
|
925
|
+
if (execResults.status === "suspended") {
|
|
926
|
+
await emitter.emit("watch-v2", {
|
|
927
|
+
type: "step-suspended",
|
|
928
|
+
payload: {
|
|
929
|
+
id: step.id,
|
|
930
|
+
status: execResults.status,
|
|
931
|
+
output: execResults.status === "success" ? execResults?.output : void 0
|
|
932
|
+
}
|
|
933
|
+
});
|
|
934
|
+
} else {
|
|
935
|
+
await emitter.emit("watch-v2", {
|
|
936
|
+
type: "step-result",
|
|
937
|
+
payload: {
|
|
938
|
+
id: step.id,
|
|
939
|
+
status: execResults.status,
|
|
940
|
+
output: execResults.status === "success" ? execResults?.output : void 0
|
|
941
|
+
}
|
|
942
|
+
});
|
|
943
|
+
await emitter.emit("watch-v2", {
|
|
944
|
+
type: "step-finish",
|
|
945
|
+
payload: {
|
|
946
|
+
id: step.id,
|
|
947
|
+
metadata: {}
|
|
948
|
+
}
|
|
949
|
+
});
|
|
950
|
+
}
|
|
610
951
|
return { result: execResults, executionContext, stepResults };
|
|
611
952
|
});
|
|
612
953
|
Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
|
|
@@ -617,7 +958,11 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
617
958
|
workflowId,
|
|
618
959
|
runId,
|
|
619
960
|
stepResults,
|
|
620
|
-
executionContext
|
|
961
|
+
executionContext,
|
|
962
|
+
serializedStepGraph,
|
|
963
|
+
workflowStatus,
|
|
964
|
+
result,
|
|
965
|
+
error
|
|
621
966
|
}) {
|
|
622
967
|
await this.inngestStep.run(
|
|
623
968
|
`workflow.${workflowId}.run.${runId}.path.${JSON.stringify(executionContext.executionPath)}.stepUpdate`,
|
|
@@ -631,6 +976,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
631
976
|
context: stepResults,
|
|
632
977
|
activePaths: [],
|
|
633
978
|
suspendedPaths: executionContext.suspendedPaths,
|
|
979
|
+
serializedStepGraph,
|
|
980
|
+
status: workflowStatus,
|
|
981
|
+
result,
|
|
982
|
+
error,
|
|
634
983
|
// @ts-ignore
|
|
635
984
|
timestamp: Date.now()
|
|
636
985
|
}
|
|
@@ -645,6 +994,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
645
994
|
prevOutput,
|
|
646
995
|
prevStep,
|
|
647
996
|
stepResults,
|
|
997
|
+
serializedStepGraph,
|
|
648
998
|
resume,
|
|
649
999
|
executionContext,
|
|
650
1000
|
emitter,
|
|
@@ -656,6 +1006,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
656
1006
|
(cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
|
|
657
1007
|
try {
|
|
658
1008
|
const result = await cond({
|
|
1009
|
+
runId,
|
|
659
1010
|
mastra: this.mastra,
|
|
660
1011
|
runtimeContext,
|
|
661
1012
|
inputData: prevOutput,
|
|
@@ -673,7 +1024,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
673
1024
|
// TODO: this function shouldn't have suspend probably?
|
|
674
1025
|
suspend: async (_suspendPayload) => {
|
|
675
1026
|
},
|
|
676
|
-
|
|
1027
|
+
bail: () => {
|
|
1028
|
+
},
|
|
1029
|
+
[EMITTER_SYMBOL]: emitter,
|
|
1030
|
+
engine: {
|
|
1031
|
+
step: this.inngestStep
|
|
1032
|
+
}
|
|
677
1033
|
});
|
|
678
1034
|
return result ? index : null;
|
|
679
1035
|
} catch (e) {
|
|
@@ -692,6 +1048,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
692
1048
|
prevStep,
|
|
693
1049
|
stepResults,
|
|
694
1050
|
resume,
|
|
1051
|
+
serializedStepGraph,
|
|
695
1052
|
executionContext: {
|
|
696
1053
|
workflowId,
|
|
697
1054
|
runId,
|
|
@@ -705,17 +1062,17 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
705
1062
|
})
|
|
706
1063
|
)
|
|
707
1064
|
);
|
|
708
|
-
const hasFailed = results.find((result) => result.status === "failed");
|
|
709
|
-
const hasSuspended = results.find((result) => result.status === "suspended");
|
|
1065
|
+
const hasFailed = results.find((result) => result.result.status === "failed");
|
|
1066
|
+
const hasSuspended = results.find((result) => result.result.status === "suspended");
|
|
710
1067
|
if (hasFailed) {
|
|
711
|
-
execResults = { status: "failed", error: hasFailed.error };
|
|
1068
|
+
execResults = { status: "failed", error: hasFailed.result.error };
|
|
712
1069
|
} else if (hasSuspended) {
|
|
713
|
-
execResults = { status: "suspended", payload: hasSuspended.
|
|
1070
|
+
execResults = { status: "suspended", payload: hasSuspended.result.suspendPayload };
|
|
714
1071
|
} else {
|
|
715
1072
|
execResults = {
|
|
716
1073
|
status: "success",
|
|
717
1074
|
output: results.reduce((acc, result, index) => {
|
|
718
|
-
if (result.status === "success") {
|
|
1075
|
+
if (result.result.status === "success") {
|
|
719
1076
|
acc[stepsToRun[index].step.id] = result.output;
|
|
720
1077
|
}
|
|
721
1078
|
return acc;
|
|
@@ -726,4 +1083,4 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
726
1083
|
}
|
|
727
1084
|
};
|
|
728
1085
|
|
|
729
|
-
export { InngestExecutionEngine, InngestRun, InngestWorkflow, init, serve };
|
|
1086
|
+
export { InngestExecutionEngine, InngestRun, InngestWorkflow, createStep, init, serve };
|