@mastra/inngest 0.0.0-taofeeqInngest-20250603090617 → 0.0.0-tool-call-parts-20250630193309
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 +214 -2
- package/dist/_tsup-dts-rollup.d.cts +97 -27
- package/dist/_tsup-dts-rollup.d.ts +97 -27
- package/dist/index.cjs +404 -42
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +404 -43
- package/package.json +18 -17
- package/src/index.test.ts +2614 -1169
- package/src/index.ts +621 -61
package/dist/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
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';
|
|
5
6
|
import { EMITTER_SYMBOL } from '@mastra/core/workflows/_constants';
|
|
6
7
|
import { serve as serve$1 } from 'inngest/hono';
|
|
8
|
+
import { z } from 'zod';
|
|
7
9
|
|
|
8
10
|
// src/index.ts
|
|
9
11
|
function serve({ mastra, inngest }) {
|
|
@@ -41,7 +43,7 @@ var InngestRun = class extends Run {
|
|
|
41
43
|
}
|
|
42
44
|
async getRunOutput(eventId) {
|
|
43
45
|
let runs = await this.getRuns(eventId);
|
|
44
|
-
while (runs?.[0]?.status !== "Completed") {
|
|
46
|
+
while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
|
|
45
47
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
46
48
|
runs = await this.getRuns(eventId);
|
|
47
49
|
if (runs?.[0]?.status === "Failed" || runs?.[0]?.status === "Cancelled") {
|
|
@@ -50,6 +52,12 @@ var InngestRun = class extends Run {
|
|
|
50
52
|
}
|
|
51
53
|
return runs?.[0];
|
|
52
54
|
}
|
|
55
|
+
async sendEvent(event, data) {
|
|
56
|
+
await this.inngest.send({
|
|
57
|
+
name: `user-event-${event}`,
|
|
58
|
+
data
|
|
59
|
+
});
|
|
60
|
+
}
|
|
53
61
|
async start({
|
|
54
62
|
inputData
|
|
55
63
|
}) {
|
|
@@ -63,7 +71,8 @@ var InngestRun = class extends Run {
|
|
|
63
71
|
context: {},
|
|
64
72
|
activePaths: [],
|
|
65
73
|
suspendedPaths: {},
|
|
66
|
-
timestamp: Date.now()
|
|
74
|
+
timestamp: Date.now(),
|
|
75
|
+
status: "running"
|
|
67
76
|
}
|
|
68
77
|
});
|
|
69
78
|
const eventOutput = await this.inngest.send({
|
|
@@ -82,10 +91,23 @@ var InngestRun = class extends Run {
|
|
|
82
91
|
if (result.status === "failed") {
|
|
83
92
|
result.error = new Error(result.error);
|
|
84
93
|
}
|
|
85
|
-
|
|
94
|
+
if (result.status !== "suspended") {
|
|
95
|
+
this.cleanup?.();
|
|
96
|
+
}
|
|
86
97
|
return result;
|
|
87
98
|
}
|
|
88
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) {
|
|
89
111
|
const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
|
|
90
112
|
(step) => typeof step === "string" ? step : step?.id
|
|
91
113
|
);
|
|
@@ -119,25 +141,60 @@ var InngestRun = class extends Run {
|
|
|
119
141
|
}
|
|
120
142
|
return result;
|
|
121
143
|
}
|
|
122
|
-
watch(cb) {
|
|
144
|
+
watch(cb, type = "watch") {
|
|
145
|
+
let active = true;
|
|
123
146
|
const streamPromise = subscribe(
|
|
124
147
|
{
|
|
125
148
|
channel: `workflow:${this.workflowId}:${this.runId}`,
|
|
126
|
-
topics: [
|
|
149
|
+
topics: [type],
|
|
127
150
|
app: this.inngest
|
|
128
151
|
},
|
|
129
152
|
(message) => {
|
|
130
|
-
|
|
153
|
+
if (active) {
|
|
154
|
+
cb(message.data);
|
|
155
|
+
}
|
|
131
156
|
}
|
|
132
157
|
);
|
|
133
158
|
return () => {
|
|
134
|
-
|
|
135
|
-
|
|
159
|
+
active = false;
|
|
160
|
+
streamPromise.then(async (stream) => {
|
|
161
|
+
return stream.cancel();
|
|
136
162
|
}).catch((err) => {
|
|
137
163
|
console.error(err);
|
|
138
164
|
});
|
|
139
165
|
};
|
|
140
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
|
+
}
|
|
141
198
|
};
|
|
142
199
|
var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
143
200
|
#mastra;
|
|
@@ -160,11 +217,32 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
160
217
|
const storage = this.#mastra?.getStorage();
|
|
161
218
|
if (!storage) {
|
|
162
219
|
this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
|
|
163
|
-
return null;
|
|
220
|
+
return this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null;
|
|
164
221
|
}
|
|
165
222
|
const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
|
|
166
223
|
return run ?? (this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null);
|
|
167
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
|
+
}
|
|
168
246
|
__registerMastra(mastra) {
|
|
169
247
|
this.#mastra = mastra;
|
|
170
248
|
this.executionEngine.__registerMastra(mastra);
|
|
@@ -201,6 +279,44 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
201
279
|
this.runs.set(runIdToUse, run);
|
|
202
280
|
return run;
|
|
203
281
|
}
|
|
282
|
+
async createRunAsync(options) {
|
|
283
|
+
const runIdToUse = options?.runId || 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,
|
|
291
|
+
mastra: this.#mastra,
|
|
292
|
+
retryConfig: this.retryConfig,
|
|
293
|
+
cleanup: () => this.runs.delete(runIdToUse)
|
|
294
|
+
},
|
|
295
|
+
this.inngest
|
|
296
|
+
);
|
|
297
|
+
this.runs.set(runIdToUse, run);
|
|
298
|
+
const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse);
|
|
299
|
+
if (!workflowSnapshotInStorage) {
|
|
300
|
+
await this.mastra?.getStorage()?.persistWorkflowSnapshot({
|
|
301
|
+
workflowName: this.id,
|
|
302
|
+
runId: runIdToUse,
|
|
303
|
+
snapshot: {
|
|
304
|
+
runId: runIdToUse,
|
|
305
|
+
status: "pending",
|
|
306
|
+
value: {},
|
|
307
|
+
context: {},
|
|
308
|
+
activePaths: [],
|
|
309
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
310
|
+
suspendedPaths: {},
|
|
311
|
+
result: void 0,
|
|
312
|
+
error: void 0,
|
|
313
|
+
// @ts-ignore
|
|
314
|
+
timestamp: Date.now()
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
return run;
|
|
319
|
+
}
|
|
204
320
|
getFunction() {
|
|
205
321
|
if (this.function) {
|
|
206
322
|
return this.function;
|
|
@@ -224,12 +340,18 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
224
340
|
try {
|
|
225
341
|
await publish({
|
|
226
342
|
channel: `workflow:${this.id}:${runId}`,
|
|
227
|
-
topic:
|
|
343
|
+
topic: event2,
|
|
228
344
|
data
|
|
229
345
|
});
|
|
230
346
|
} catch (err) {
|
|
231
347
|
this.logger.error("Error emitting event: " + (err?.stack ?? err?.message ?? err));
|
|
232
348
|
}
|
|
349
|
+
},
|
|
350
|
+
on: (_event, _callback) => {
|
|
351
|
+
},
|
|
352
|
+
off: (_event, _callback) => {
|
|
353
|
+
},
|
|
354
|
+
once: (_event, _callback) => {
|
|
233
355
|
}
|
|
234
356
|
};
|
|
235
357
|
const engine = new InngestExecutionEngine(this.#mastra, step, attempt);
|
|
@@ -267,20 +389,106 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
267
389
|
return [this.getFunction(), ...this.getNestedFunctions(this.executionGraph.steps)];
|
|
268
390
|
}
|
|
269
391
|
};
|
|
270
|
-
function
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
392
|
+
function isAgent(params) {
|
|
393
|
+
return params?.component === "AGENT";
|
|
394
|
+
}
|
|
395
|
+
function isTool(params) {
|
|
396
|
+
return params instanceof Tool;
|
|
397
|
+
}
|
|
398
|
+
function createStep(params) {
|
|
399
|
+
if (isAgent(params)) {
|
|
400
|
+
return {
|
|
401
|
+
id: params.name,
|
|
402
|
+
// @ts-ignore
|
|
403
|
+
inputSchema: z.object({
|
|
404
|
+
prompt: z.string()
|
|
405
|
+
// resourceId: z.string().optional(),
|
|
406
|
+
// threadId: z.string().optional(),
|
|
407
|
+
}),
|
|
408
|
+
// @ts-ignore
|
|
409
|
+
outputSchema: z.object({
|
|
410
|
+
text: z.string()
|
|
411
|
+
}),
|
|
412
|
+
execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext }) => {
|
|
413
|
+
let streamPromise = {};
|
|
414
|
+
streamPromise.promise = new Promise((resolve, reject) => {
|
|
415
|
+
streamPromise.resolve = resolve;
|
|
416
|
+
streamPromise.reject = reject;
|
|
417
|
+
});
|
|
418
|
+
const toolData = {
|
|
419
|
+
name: params.name,
|
|
420
|
+
args: inputData
|
|
421
|
+
};
|
|
422
|
+
await emitter.emit("watch-v2", {
|
|
423
|
+
type: "tool-call-streaming-start",
|
|
424
|
+
...toolData
|
|
425
|
+
});
|
|
426
|
+
const { fullStream } = await params.stream(inputData.prompt, {
|
|
427
|
+
// resourceId: inputData.resourceId,
|
|
428
|
+
// threadId: inputData.threadId,
|
|
429
|
+
runtimeContext,
|
|
430
|
+
onFinish: (result) => {
|
|
431
|
+
streamPromise.resolve(result.text);
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
for await (const chunk of fullStream) {
|
|
435
|
+
switch (chunk.type) {
|
|
436
|
+
case "text-delta":
|
|
437
|
+
await emitter.emit("watch-v2", {
|
|
438
|
+
type: "tool-call-delta",
|
|
439
|
+
...toolData,
|
|
440
|
+
argsTextDelta: chunk.textDelta
|
|
441
|
+
});
|
|
442
|
+
break;
|
|
443
|
+
case "step-start":
|
|
444
|
+
case "step-finish":
|
|
445
|
+
case "finish":
|
|
446
|
+
break;
|
|
447
|
+
case "tool-call":
|
|
448
|
+
case "tool-result":
|
|
449
|
+
case "tool-call-streaming-start":
|
|
450
|
+
case "tool-call-delta":
|
|
451
|
+
case "source":
|
|
452
|
+
case "file":
|
|
453
|
+
default:
|
|
454
|
+
await emitter.emit("watch-v2", chunk);
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return {
|
|
459
|
+
text: await streamPromise.promise
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
if (isTool(params)) {
|
|
465
|
+
if (!params.inputSchema || !params.outputSchema) {
|
|
466
|
+
throw new Error("Tool must have input and output schemas defined");
|
|
467
|
+
}
|
|
468
|
+
return {
|
|
469
|
+
// TODO: tool probably should have strong id type
|
|
470
|
+
// @ts-ignore
|
|
471
|
+
id: params.id,
|
|
472
|
+
inputSchema: params.inputSchema,
|
|
473
|
+
outputSchema: params.outputSchema,
|
|
474
|
+
execute: async ({ inputData, mastra, runtimeContext }) => {
|
|
475
|
+
return params.execute({
|
|
476
|
+
context: inputData,
|
|
477
|
+
mastra,
|
|
478
|
+
runtimeContext
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
return {
|
|
484
|
+
id: params.id,
|
|
485
|
+
description: params.description,
|
|
486
|
+
inputSchema: params.inputSchema,
|
|
487
|
+
outputSchema: params.outputSchema,
|
|
488
|
+
resumeSchema: params.resumeSchema,
|
|
489
|
+
suspendSchema: params.suspendSchema,
|
|
490
|
+
execute: params.execute
|
|
491
|
+
};
|
|
284
492
|
}
|
|
285
493
|
function init(inngest) {
|
|
286
494
|
return {
|
|
@@ -288,8 +496,27 @@ function init(inngest) {
|
|
|
288
496
|
return new InngestWorkflow(params, inngest);
|
|
289
497
|
},
|
|
290
498
|
createStep,
|
|
291
|
-
cloneStep,
|
|
292
|
-
|
|
499
|
+
cloneStep(step, opts) {
|
|
500
|
+
return {
|
|
501
|
+
id: opts.id,
|
|
502
|
+
description: step.description,
|
|
503
|
+
inputSchema: step.inputSchema,
|
|
504
|
+
outputSchema: step.outputSchema,
|
|
505
|
+
execute: step.execute
|
|
506
|
+
};
|
|
507
|
+
},
|
|
508
|
+
cloneWorkflow(workflow, opts) {
|
|
509
|
+
const wf = new Workflow({
|
|
510
|
+
id: opts.id,
|
|
511
|
+
inputSchema: workflow.inputSchema,
|
|
512
|
+
outputSchema: workflow.outputSchema,
|
|
513
|
+
steps: workflow.stepDefs,
|
|
514
|
+
mastra: workflow.mastra
|
|
515
|
+
});
|
|
516
|
+
wf.setStepFlow(workflow.stepGraph);
|
|
517
|
+
wf.commit();
|
|
518
|
+
return wf;
|
|
519
|
+
}
|
|
293
520
|
};
|
|
294
521
|
}
|
|
295
522
|
var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
@@ -300,6 +527,18 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
300
527
|
this.inngestStep = inngestStep;
|
|
301
528
|
this.inngestAttempts = inngestAttempts;
|
|
302
529
|
}
|
|
530
|
+
async execute(params) {
|
|
531
|
+
await params.emitter.emit("watch-v2", {
|
|
532
|
+
type: "start",
|
|
533
|
+
payload: { runId: params.runId }
|
|
534
|
+
});
|
|
535
|
+
const result = await super.execute(params);
|
|
536
|
+
await params.emitter.emit("watch-v2", {
|
|
537
|
+
type: "finish",
|
|
538
|
+
payload: { runId: params.runId }
|
|
539
|
+
});
|
|
540
|
+
return result;
|
|
541
|
+
}
|
|
303
542
|
async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
|
|
304
543
|
const base = {
|
|
305
544
|
status: lastOutput.status,
|
|
@@ -380,6 +619,19 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
380
619
|
runtimeContext
|
|
381
620
|
});
|
|
382
621
|
}
|
|
622
|
+
async executeSleep({ id, duration }) {
|
|
623
|
+
await this.inngestStep.sleep(id, duration);
|
|
624
|
+
}
|
|
625
|
+
async executeWaitForEvent({ event, timeout }) {
|
|
626
|
+
const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
|
|
627
|
+
event: `user-event-${event}`,
|
|
628
|
+
timeout: timeout ?? 5e3
|
|
629
|
+
});
|
|
630
|
+
if (eventData === null) {
|
|
631
|
+
throw "Timeout waiting for event";
|
|
632
|
+
}
|
|
633
|
+
return eventData?.data;
|
|
634
|
+
}
|
|
383
635
|
async executeStep({
|
|
384
636
|
step,
|
|
385
637
|
stepResults,
|
|
@@ -389,9 +641,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
389
641
|
emitter,
|
|
390
642
|
runtimeContext
|
|
391
643
|
}) {
|
|
392
|
-
await this.inngestStep.run(
|
|
644
|
+
const startedAt = await this.inngestStep.run(
|
|
393
645
|
`workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
|
|
394
646
|
async () => {
|
|
647
|
+
const startedAt2 = Date.now();
|
|
395
648
|
await emitter.emit("watch", {
|
|
396
649
|
type: "watch",
|
|
397
650
|
payload: {
|
|
@@ -413,6 +666,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
413
666
|
},
|
|
414
667
|
eventTimestamp: Date.now()
|
|
415
668
|
});
|
|
669
|
+
await emitter.emit("watch-v2", {
|
|
670
|
+
type: "step-start",
|
|
671
|
+
payload: {
|
|
672
|
+
id: step.id,
|
|
673
|
+
status: "running"
|
|
674
|
+
}
|
|
675
|
+
});
|
|
676
|
+
return startedAt2;
|
|
416
677
|
}
|
|
417
678
|
);
|
|
418
679
|
if (step instanceof InngestWorkflow) {
|
|
@@ -473,6 +734,15 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
473
734
|
},
|
|
474
735
|
eventTimestamp: Date.now()
|
|
475
736
|
});
|
|
737
|
+
await emitter.emit("watch-v2", {
|
|
738
|
+
type: "step-result",
|
|
739
|
+
payload: {
|
|
740
|
+
id: step.id,
|
|
741
|
+
status: "failed",
|
|
742
|
+
error: result?.error,
|
|
743
|
+
payload: prevOutput
|
|
744
|
+
}
|
|
745
|
+
});
|
|
476
746
|
return { executionContext, result: { status: "failed", error: result?.error } };
|
|
477
747
|
} else if (result.status === "suspended") {
|
|
478
748
|
const suspendedSteps = Object.entries(result.steps).filter(([_stepName, stepResult]) => {
|
|
@@ -499,6 +769,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
499
769
|
},
|
|
500
770
|
eventTimestamp: Date.now()
|
|
501
771
|
});
|
|
772
|
+
await emitter.emit("watch-v2", {
|
|
773
|
+
type: "step-suspended",
|
|
774
|
+
payload: {
|
|
775
|
+
id: step.id,
|
|
776
|
+
status: "suspended"
|
|
777
|
+
}
|
|
778
|
+
});
|
|
502
779
|
return {
|
|
503
780
|
executionContext,
|
|
504
781
|
result: {
|
|
@@ -549,6 +826,21 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
549
826
|
},
|
|
550
827
|
eventTimestamp: Date.now()
|
|
551
828
|
});
|
|
829
|
+
await emitter.emit("watch-v2", {
|
|
830
|
+
type: "step-result",
|
|
831
|
+
payload: {
|
|
832
|
+
id: step.id,
|
|
833
|
+
status: "success",
|
|
834
|
+
output: result?.result
|
|
835
|
+
}
|
|
836
|
+
});
|
|
837
|
+
await emitter.emit("watch-v2", {
|
|
838
|
+
type: "step-finish",
|
|
839
|
+
payload: {
|
|
840
|
+
id: step.id,
|
|
841
|
+
metadata: {}
|
|
842
|
+
}
|
|
843
|
+
});
|
|
552
844
|
return { executionContext, result: { status: "success", output: result?.result } };
|
|
553
845
|
}
|
|
554
846
|
);
|
|
@@ -558,6 +850,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
558
850
|
const stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
|
|
559
851
|
let execResults;
|
|
560
852
|
let suspended;
|
|
853
|
+
let bailed;
|
|
561
854
|
try {
|
|
562
855
|
const result = await step.execute({
|
|
563
856
|
runId: executionContext.runId,
|
|
@@ -577,20 +870,53 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
577
870
|
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
578
871
|
suspended = { payload: suspendPayload };
|
|
579
872
|
},
|
|
873
|
+
bail: (result2) => {
|
|
874
|
+
bailed = { payload: result2 };
|
|
875
|
+
},
|
|
580
876
|
resume: {
|
|
581
877
|
steps: resume?.steps?.slice(1) || [],
|
|
582
878
|
resumePayload: resume?.resumePayload,
|
|
583
879
|
// @ts-ignore
|
|
584
880
|
runId: stepResults[step.id]?.payload?.__workflow_meta?.runId
|
|
585
881
|
},
|
|
586
|
-
emitter
|
|
882
|
+
[EMITTER_SYMBOL]: emitter,
|
|
883
|
+
engine: {
|
|
884
|
+
step: this.inngestStep
|
|
885
|
+
}
|
|
587
886
|
});
|
|
588
|
-
|
|
887
|
+
const endedAt = Date.now();
|
|
888
|
+
execResults = {
|
|
889
|
+
status: "success",
|
|
890
|
+
output: result,
|
|
891
|
+
startedAt,
|
|
892
|
+
endedAt,
|
|
893
|
+
payload: prevOutput,
|
|
894
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
895
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
896
|
+
};
|
|
589
897
|
} catch (e) {
|
|
590
|
-
execResults = {
|
|
898
|
+
execResults = {
|
|
899
|
+
status: "failed",
|
|
900
|
+
payload: prevOutput,
|
|
901
|
+
error: e instanceof Error ? e.message : String(e),
|
|
902
|
+
endedAt: Date.now(),
|
|
903
|
+
startedAt,
|
|
904
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
905
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
906
|
+
};
|
|
591
907
|
}
|
|
592
908
|
if (suspended) {
|
|
593
|
-
execResults = {
|
|
909
|
+
execResults = {
|
|
910
|
+
status: "suspended",
|
|
911
|
+
suspendedPayload: suspended.payload,
|
|
912
|
+
payload: prevOutput,
|
|
913
|
+
suspendedAt: Date.now(),
|
|
914
|
+
startedAt,
|
|
915
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
916
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
917
|
+
};
|
|
918
|
+
} else if (bailed) {
|
|
919
|
+
execResults = { status: "bailed", output: bailed.payload, payload: prevOutput, endedAt: Date.now(), startedAt };
|
|
594
920
|
}
|
|
595
921
|
if (execResults.status === "failed") {
|
|
596
922
|
if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
|
|
@@ -602,18 +928,41 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
602
928
|
payload: {
|
|
603
929
|
currentStep: {
|
|
604
930
|
id: step.id,
|
|
605
|
-
|
|
606
|
-
output: execResults.output
|
|
931
|
+
...execResults
|
|
607
932
|
},
|
|
608
933
|
workflowState: {
|
|
609
934
|
status: "running",
|
|
610
|
-
steps: stepResults,
|
|
935
|
+
steps: { ...stepResults, [step.id]: execResults },
|
|
611
936
|
result: null,
|
|
612
937
|
error: null
|
|
613
938
|
}
|
|
614
939
|
},
|
|
615
940
|
eventTimestamp: Date.now()
|
|
616
941
|
});
|
|
942
|
+
if (execResults.status === "suspended") {
|
|
943
|
+
await emitter.emit("watch-v2", {
|
|
944
|
+
type: "step-suspended",
|
|
945
|
+
payload: {
|
|
946
|
+
id: step.id,
|
|
947
|
+
...execResults
|
|
948
|
+
}
|
|
949
|
+
});
|
|
950
|
+
} else {
|
|
951
|
+
await emitter.emit("watch-v2", {
|
|
952
|
+
type: "step-result",
|
|
953
|
+
payload: {
|
|
954
|
+
id: step.id,
|
|
955
|
+
...execResults
|
|
956
|
+
}
|
|
957
|
+
});
|
|
958
|
+
await emitter.emit("watch-v2", {
|
|
959
|
+
type: "step-finish",
|
|
960
|
+
payload: {
|
|
961
|
+
id: step.id,
|
|
962
|
+
metadata: {}
|
|
963
|
+
}
|
|
964
|
+
});
|
|
965
|
+
}
|
|
617
966
|
return { result: execResults, executionContext, stepResults };
|
|
618
967
|
});
|
|
619
968
|
Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
|
|
@@ -625,7 +974,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
625
974
|
runId,
|
|
626
975
|
stepResults,
|
|
627
976
|
executionContext,
|
|
628
|
-
serializedStepGraph
|
|
977
|
+
serializedStepGraph,
|
|
978
|
+
workflowStatus,
|
|
979
|
+
result,
|
|
980
|
+
error
|
|
629
981
|
}) {
|
|
630
982
|
await this.inngestStep.run(
|
|
631
983
|
`workflow.${workflowId}.run.${runId}.path.${JSON.stringify(executionContext.executionPath)}.stepUpdate`,
|
|
@@ -640,6 +992,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
640
992
|
activePaths: [],
|
|
641
993
|
suspendedPaths: executionContext.suspendedPaths,
|
|
642
994
|
serializedStepGraph,
|
|
995
|
+
status: workflowStatus,
|
|
996
|
+
result,
|
|
997
|
+
error,
|
|
643
998
|
// @ts-ignore
|
|
644
999
|
timestamp: Date.now()
|
|
645
1000
|
}
|
|
@@ -669,6 +1024,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
669
1024
|
runId,
|
|
670
1025
|
mastra: this.mastra,
|
|
671
1026
|
runtimeContext,
|
|
1027
|
+
runCount: -1,
|
|
672
1028
|
inputData: prevOutput,
|
|
673
1029
|
getInitData: () => stepResults?.input,
|
|
674
1030
|
getStepResult: (step) => {
|
|
@@ -684,7 +1040,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
684
1040
|
// TODO: this function shouldn't have suspend probably?
|
|
685
1041
|
suspend: async (_suspendPayload) => {
|
|
686
1042
|
},
|
|
687
|
-
|
|
1043
|
+
bail: () => {
|
|
1044
|
+
},
|
|
1045
|
+
[EMITTER_SYMBOL]: emitter,
|
|
1046
|
+
engine: {
|
|
1047
|
+
step: this.inngestStep
|
|
1048
|
+
}
|
|
688
1049
|
});
|
|
689
1050
|
return result ? index : null;
|
|
690
1051
|
} catch (e) {
|
|
@@ -717,17 +1078,17 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
717
1078
|
})
|
|
718
1079
|
)
|
|
719
1080
|
);
|
|
720
|
-
const hasFailed = results.find((result) => result.status === "failed");
|
|
721
|
-
const hasSuspended = results.find((result) => result.status === "suspended");
|
|
1081
|
+
const hasFailed = results.find((result) => result.result.status === "failed");
|
|
1082
|
+
const hasSuspended = results.find((result) => result.result.status === "suspended");
|
|
722
1083
|
if (hasFailed) {
|
|
723
|
-
execResults = { status: "failed", error: hasFailed.error };
|
|
1084
|
+
execResults = { status: "failed", error: hasFailed.result.error };
|
|
724
1085
|
} else if (hasSuspended) {
|
|
725
|
-
execResults = { status: "suspended", payload: hasSuspended.
|
|
1086
|
+
execResults = { status: "suspended", payload: hasSuspended.result.suspendPayload };
|
|
726
1087
|
} else {
|
|
727
1088
|
execResults = {
|
|
728
1089
|
status: "success",
|
|
729
1090
|
output: results.reduce((acc, result, index) => {
|
|
730
|
-
if (result.status === "success") {
|
|
1091
|
+
if (result.result.status === "success") {
|
|
731
1092
|
acc[stepsToRun[index].step.id] = result.output;
|
|
732
1093
|
}
|
|
733
1094
|
return acc;
|
|
@@ -738,4 +1099,4 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
738
1099
|
}
|
|
739
1100
|
};
|
|
740
1101
|
|
|
741
|
-
export { InngestExecutionEngine, InngestRun, InngestWorkflow, init, serve };
|
|
1102
|
+
export { InngestExecutionEngine, InngestRun, InngestWorkflow, createStep, init, serve };
|