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