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