@mastra/inngest 0.0.0-redis-cloud-transporter-20250508203756 → 0.0.0-scorers-api-v2-20250801171841

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/dist/index.cjs CHANGED
@@ -3,34 +3,43 @@
3
3
  var crypto = require('crypto');
4
4
  var realtime = require('@inngest/realtime');
5
5
  var di = require('@mastra/core/di');
6
- var vNext = require('@mastra/core/workflows/vNext');
6
+ var tools = require('@mastra/core/tools');
7
+ var workflows = require('@mastra/core/workflows');
8
+ var _constants = require('@mastra/core/workflows/_constants');
7
9
  var hono = require('inngest/hono');
10
+ var zod = require('zod');
8
11
 
9
12
  // src/index.ts
10
13
  function serve({ mastra, inngest }) {
11
- const wfs = mastra.vnext_getWorkflows();
12
- const functions = Object.values(wfs).flatMap((wf) => {
13
- if (wf instanceof InngestWorkflow) {
14
- wf.__registerMastra(mastra);
15
- return wf.getFunctions();
16
- }
17
- return [];
18
- });
14
+ const wfs = mastra.getWorkflows();
15
+ const functions = Array.from(
16
+ new Set(
17
+ Object.values(wfs).flatMap((wf) => {
18
+ if (wf instanceof InngestWorkflow) {
19
+ wf.__registerMastra(mastra);
20
+ return wf.getFunctions();
21
+ }
22
+ return [];
23
+ })
24
+ )
25
+ );
19
26
  return hono.serve({
20
27
  client: inngest,
21
28
  functions
22
29
  });
23
30
  }
24
- var InngestRun = class extends vNext.Run {
31
+ var InngestRun = class extends workflows.Run {
25
32
  inngest;
33
+ serializedStepGraph;
26
34
  #mastra;
27
35
  constructor(params, inngest) {
28
36
  super(params);
29
37
  this.inngest = inngest;
38
+ this.serializedStepGraph = params.serializedStepGraph;
30
39
  this.#mastra = params.mastra;
31
40
  }
32
41
  async getRuns(eventId) {
33
- const response = await fetch(`${this.inngest.apiBaseUrl}/v1/events/${eventId}/runs`, {
42
+ const response = await fetch(`${this.inngest.apiBaseUrl ?? "https://api.inngest.com"}/v1/events/${eventId}/runs`, {
34
43
  headers: {
35
44
  Authorization: `Bearer ${process.env.INNGEST_SIGNING_KEY}`
36
45
  }
@@ -40,15 +49,50 @@ var InngestRun = class extends vNext.Run {
40
49
  }
41
50
  async getRunOutput(eventId) {
42
51
  let runs = await this.getRuns(eventId);
43
- while (runs?.[0]?.status !== "Completed") {
52
+ while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
44
53
  await new Promise((resolve) => setTimeout(resolve, 1e3));
45
54
  runs = await this.getRuns(eventId);
46
- if (runs?.[0]?.status === "Failed" || runs?.[0]?.status === "Cancelled") {
55
+ if (runs?.[0]?.status === "Failed") {
56
+ console.log("run", runs?.[0]);
47
57
  throw new Error(`Function run ${runs?.[0]?.status}`);
58
+ } else if (runs?.[0]?.status === "Cancelled") {
59
+ const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
60
+ workflowName: this.workflowId,
61
+ runId: this.runId
62
+ });
63
+ return { output: { result: { steps: snapshot?.context, status: "canceled" } } };
48
64
  }
49
65
  }
50
66
  return runs?.[0];
51
67
  }
68
+ async sendEvent(event, data) {
69
+ await this.inngest.send({
70
+ name: `user-event-${event}`,
71
+ data
72
+ });
73
+ }
74
+ async cancel() {
75
+ await this.inngest.send({
76
+ name: `cancel.workflow.${this.workflowId}`,
77
+ data: {
78
+ runId: this.runId
79
+ }
80
+ });
81
+ const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
82
+ workflowName: this.workflowId,
83
+ runId: this.runId
84
+ });
85
+ if (snapshot) {
86
+ await this.#mastra?.storage?.persistWorkflowSnapshot({
87
+ workflowName: this.workflowId,
88
+ runId: this.runId,
89
+ snapshot: {
90
+ ...snapshot,
91
+ status: "canceled"
92
+ }
93
+ });
94
+ }
95
+ }
52
96
  async start({
53
97
  inputData
54
98
  }) {
@@ -57,11 +101,13 @@ var InngestRun = class extends vNext.Run {
57
101
  runId: this.runId,
58
102
  snapshot: {
59
103
  runId: this.runId,
104
+ serializedStepGraph: this.serializedStepGraph,
60
105
  value: {},
61
106
  context: {},
62
107
  activePaths: [],
63
108
  suspendedPaths: {},
64
- timestamp: Date.now()
109
+ timestamp: Date.now(),
110
+ status: "running"
65
111
  }
66
112
  });
67
113
  const eventOutput = await this.inngest.send({
@@ -80,10 +126,23 @@ var InngestRun = class extends vNext.Run {
80
126
  if (result.status === "failed") {
81
127
  result.error = new Error(result.error);
82
128
  }
83
- this.cleanup?.();
129
+ if (result.status !== "suspended") {
130
+ this.cleanup?.();
131
+ }
84
132
  return result;
85
133
  }
86
134
  async resume(params) {
135
+ const p = this._resume(params).then((result) => {
136
+ if (result.status !== "suspended") {
137
+ this.closeStreamAction?.().catch(() => {
138
+ });
139
+ }
140
+ return result;
141
+ });
142
+ this.executionResults = p;
143
+ return p;
144
+ }
145
+ async _resume(params) {
87
146
  const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
88
147
  (step) => typeof step === "string" ? step : step?.id
89
148
  );
@@ -96,6 +155,7 @@ var InngestRun = class extends vNext.Run {
96
155
  data: {
97
156
  inputData: params.resumeData,
98
157
  runId: this.runId,
158
+ workflowId: this.workflowId,
99
159
  stepResults: snapshot?.context,
100
160
  resume: {
101
161
  steps,
@@ -117,27 +177,62 @@ var InngestRun = class extends vNext.Run {
117
177
  }
118
178
  return result;
119
179
  }
120
- watch(cb) {
180
+ watch(cb, type = "watch") {
181
+ let active = true;
121
182
  const streamPromise = realtime.subscribe(
122
183
  {
123
184
  channel: `workflow:${this.workflowId}:${this.runId}`,
124
- topics: ["watch"],
185
+ topics: [type],
125
186
  app: this.inngest
126
187
  },
127
188
  (message) => {
128
- cb(message.data);
189
+ if (active) {
190
+ cb(message.data);
191
+ }
129
192
  }
130
193
  );
131
194
  return () => {
132
- streamPromise.then((stream) => {
133
- stream.cancel();
195
+ active = false;
196
+ streamPromise.then(async (stream) => {
197
+ return stream.cancel();
134
198
  }).catch((err) => {
135
199
  console.error(err);
136
200
  });
137
201
  };
138
202
  }
203
+ stream({ inputData, runtimeContext } = {}) {
204
+ const { readable, writable } = new TransformStream();
205
+ const writer = writable.getWriter();
206
+ const unwatch = this.watch(async (event) => {
207
+ try {
208
+ await writer.write(event);
209
+ } catch {
210
+ }
211
+ }, "watch-v2");
212
+ this.closeStreamAction = async () => {
213
+ unwatch();
214
+ try {
215
+ await writer.close();
216
+ } catch (err) {
217
+ console.error("Error closing stream:", err);
218
+ } finally {
219
+ writer.releaseLock();
220
+ }
221
+ };
222
+ this.executionResults = this.start({ inputData, runtimeContext }).then((result) => {
223
+ if (result.status !== "suspended") {
224
+ this.closeStreamAction?.().catch(() => {
225
+ });
226
+ }
227
+ return result;
228
+ });
229
+ return {
230
+ stream: readable,
231
+ getWorkflowState: () => this.executionResults
232
+ };
233
+ }
139
234
  };
140
- var InngestWorkflow = class _InngestWorkflow extends vNext.NewWorkflow {
235
+ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
141
236
  #mastra;
142
237
  inngest;
143
238
  function;
@@ -158,11 +253,32 @@ var InngestWorkflow = class _InngestWorkflow extends vNext.NewWorkflow {
158
253
  const storage = this.#mastra?.getStorage();
159
254
  if (!storage) {
160
255
  this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
161
- return null;
256
+ return this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null;
162
257
  }
163
258
  const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
164
259
  return run ?? (this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null);
165
260
  }
261
+ async getWorkflowRunExecutionResult(runId) {
262
+ const storage = this.#mastra?.getStorage();
263
+ if (!storage) {
264
+ this.logger.debug("Cannot get workflow run execution result. Mastra storage is not initialized");
265
+ return null;
266
+ }
267
+ const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
268
+ if (!run?.snapshot) {
269
+ return null;
270
+ }
271
+ if (typeof run.snapshot === "string") {
272
+ return null;
273
+ }
274
+ return {
275
+ status: run.snapshot.status,
276
+ result: run.snapshot.result,
277
+ error: run.snapshot.error,
278
+ payload: run.snapshot.context?.input,
279
+ steps: run.snapshot.context
280
+ };
281
+ }
166
282
  __registerMastra(mastra) {
167
283
  this.#mastra = mastra;
168
284
  this.executionEngine.__registerMastra(mastra);
@@ -189,6 +305,7 @@ var InngestWorkflow = class _InngestWorkflow extends vNext.NewWorkflow {
189
305
  runId: runIdToUse,
190
306
  executionEngine: this.executionEngine,
191
307
  executionGraph: this.executionGraph,
308
+ serializedStepGraph: this.serializedStepGraph,
192
309
  mastra: this.#mastra,
193
310
  retryConfig: this.retryConfig,
194
311
  cleanup: () => this.runs.delete(runIdToUse)
@@ -198,13 +315,55 @@ var InngestWorkflow = class _InngestWorkflow extends vNext.NewWorkflow {
198
315
  this.runs.set(runIdToUse, run);
199
316
  return run;
200
317
  }
318
+ async createRunAsync(options) {
319
+ const runIdToUse = options?.runId || crypto.randomUUID();
320
+ const run = this.runs.get(runIdToUse) ?? new InngestRun(
321
+ {
322
+ workflowId: this.id,
323
+ runId: runIdToUse,
324
+ executionEngine: this.executionEngine,
325
+ executionGraph: this.executionGraph,
326
+ serializedStepGraph: this.serializedStepGraph,
327
+ mastra: this.#mastra,
328
+ retryConfig: this.retryConfig,
329
+ cleanup: () => this.runs.delete(runIdToUse)
330
+ },
331
+ this.inngest
332
+ );
333
+ this.runs.set(runIdToUse, run);
334
+ const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse);
335
+ if (!workflowSnapshotInStorage) {
336
+ await this.mastra?.getStorage()?.persistWorkflowSnapshot({
337
+ workflowName: this.id,
338
+ runId: runIdToUse,
339
+ snapshot: {
340
+ runId: runIdToUse,
341
+ status: "pending",
342
+ value: {},
343
+ context: {},
344
+ activePaths: [],
345
+ serializedStepGraph: this.serializedStepGraph,
346
+ suspendedPaths: {},
347
+ result: void 0,
348
+ error: void 0,
349
+ // @ts-ignore
350
+ timestamp: Date.now()
351
+ }
352
+ });
353
+ }
354
+ return run;
355
+ }
201
356
  getFunction() {
202
357
  if (this.function) {
203
358
  return this.function;
204
359
  }
205
360
  this.function = this.inngest.createFunction(
206
- // @ts-ignore
207
- { id: `workflow.${this.id}`, retries: this.retryConfig?.attempts ?? 0 },
361
+ {
362
+ id: `workflow.${this.id}`,
363
+ // @ts-ignore
364
+ retries: this.retryConfig?.attempts ?? 0,
365
+ cancelOn: [{ event: `cancel.workflow.${this.id}` }]
366
+ },
208
367
  { event: `workflow.${this.id}` },
209
368
  async ({ event, step, attempt, publish }) => {
210
369
  let { inputData, runId, resume } = event.data;
@@ -221,12 +380,18 @@ var InngestWorkflow = class _InngestWorkflow extends vNext.NewWorkflow {
221
380
  try {
222
381
  await publish({
223
382
  channel: `workflow:${this.id}:${runId}`,
224
- topic: "watch",
383
+ topic: event2,
225
384
  data
226
385
  });
227
386
  } catch (err) {
228
387
  this.logger.error("Error emitting event: " + (err?.stack ?? err?.message ?? err));
229
388
  }
389
+ },
390
+ on: (_event, _callback) => {
391
+ },
392
+ off: (_event, _callback) => {
393
+ },
394
+ once: (_event, _callback) => {
230
395
  }
231
396
  };
232
397
  const engine = new InngestExecutionEngine(this.#mastra, step, attempt);
@@ -234,12 +399,14 @@ var InngestWorkflow = class _InngestWorkflow extends vNext.NewWorkflow {
234
399
  workflowId: this.id,
235
400
  runId,
236
401
  graph: this.executionGraph,
402
+ serializedStepGraph: this.serializedStepGraph,
237
403
  input: inputData,
238
404
  emitter,
239
405
  retryConfig: this.retryConfig,
240
406
  runtimeContext: new di.RuntimeContext(),
241
407
  // TODO
242
- resume
408
+ resume,
409
+ abortController: new AbortController()
243
410
  });
244
411
  return { result, runId };
245
412
  }
@@ -263,32 +430,141 @@ var InngestWorkflow = class _InngestWorkflow extends vNext.NewWorkflow {
263
430
  return [this.getFunction(), ...this.getNestedFunctions(this.executionGraph.steps)];
264
431
  }
265
432
  };
266
- function cloneWorkflow(workflow, opts) {
267
- const wf = new InngestWorkflow(
268
- {
269
- id: opts.id,
270
- inputSchema: workflow.inputSchema,
271
- outputSchema: workflow.outputSchema,
272
- steps: workflow.stepDefs,
273
- mastra: workflow.mastra
274
- },
275
- workflow.inngest
276
- );
277
- wf.setStepFlow(workflow.stepGraph);
278
- wf.commit();
279
- return wf;
433
+ function isAgent(params) {
434
+ return params?.component === "AGENT";
435
+ }
436
+ function isTool(params) {
437
+ return params instanceof tools.Tool;
438
+ }
439
+ function createStep(params) {
440
+ if (isAgent(params)) {
441
+ return {
442
+ id: params.name,
443
+ // @ts-ignore
444
+ inputSchema: zod.z.object({
445
+ prompt: zod.z.string()
446
+ // resourceId: z.string().optional(),
447
+ // threadId: z.string().optional(),
448
+ }),
449
+ // @ts-ignore
450
+ outputSchema: zod.z.object({
451
+ text: zod.z.string()
452
+ }),
453
+ execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
454
+ let streamPromise = {};
455
+ streamPromise.promise = new Promise((resolve, reject) => {
456
+ streamPromise.resolve = resolve;
457
+ streamPromise.reject = reject;
458
+ });
459
+ const toolData = {
460
+ name: params.name,
461
+ args: inputData
462
+ };
463
+ await emitter.emit("watch-v2", {
464
+ type: "tool-call-streaming-start",
465
+ ...toolData
466
+ });
467
+ const { fullStream } = await params.stream(inputData.prompt, {
468
+ // resourceId: inputData.resourceId,
469
+ // threadId: inputData.threadId,
470
+ runtimeContext,
471
+ onFinish: (result) => {
472
+ streamPromise.resolve(result.text);
473
+ },
474
+ abortSignal
475
+ });
476
+ if (abortSignal.aborted) {
477
+ return abort();
478
+ }
479
+ for await (const chunk of fullStream) {
480
+ switch (chunk.type) {
481
+ case "text-delta":
482
+ await emitter.emit("watch-v2", {
483
+ type: "tool-call-delta",
484
+ ...toolData,
485
+ argsTextDelta: chunk.textDelta
486
+ });
487
+ break;
488
+ case "step-start":
489
+ case "step-finish":
490
+ case "finish":
491
+ break;
492
+ case "tool-call":
493
+ case "tool-result":
494
+ case "tool-call-streaming-start":
495
+ case "tool-call-delta":
496
+ case "source":
497
+ case "file":
498
+ default:
499
+ await emitter.emit("watch-v2", chunk);
500
+ break;
501
+ }
502
+ }
503
+ return {
504
+ text: await streamPromise.promise
505
+ };
506
+ }
507
+ };
508
+ }
509
+ if (isTool(params)) {
510
+ if (!params.inputSchema || !params.outputSchema) {
511
+ throw new Error("Tool must have input and output schemas defined");
512
+ }
513
+ return {
514
+ // TODO: tool probably should have strong id type
515
+ // @ts-ignore
516
+ id: params.id,
517
+ inputSchema: params.inputSchema,
518
+ outputSchema: params.outputSchema,
519
+ execute: async ({ inputData, mastra, runtimeContext }) => {
520
+ return params.execute({
521
+ context: inputData,
522
+ mastra,
523
+ runtimeContext
524
+ });
525
+ }
526
+ };
527
+ }
528
+ return {
529
+ id: params.id,
530
+ description: params.description,
531
+ inputSchema: params.inputSchema,
532
+ outputSchema: params.outputSchema,
533
+ resumeSchema: params.resumeSchema,
534
+ suspendSchema: params.suspendSchema,
535
+ execute: params.execute
536
+ };
280
537
  }
281
538
  function init(inngest) {
282
539
  return {
283
540
  createWorkflow(params) {
284
541
  return new InngestWorkflow(params, inngest);
285
542
  },
286
- createStep: vNext.createStep,
287
- cloneStep: vNext.cloneStep,
288
- cloneWorkflow
543
+ createStep,
544
+ cloneStep(step, opts) {
545
+ return {
546
+ id: opts.id,
547
+ description: step.description,
548
+ inputSchema: step.inputSchema,
549
+ outputSchema: step.outputSchema,
550
+ execute: step.execute
551
+ };
552
+ },
553
+ cloneWorkflow(workflow, opts) {
554
+ const wf = new workflows.Workflow({
555
+ id: opts.id,
556
+ inputSchema: workflow.inputSchema,
557
+ outputSchema: workflow.outputSchema,
558
+ steps: workflow.stepDefs,
559
+ mastra: workflow.mastra
560
+ });
561
+ wf.setStepFlow(workflow.stepGraph);
562
+ wf.commit();
563
+ return wf;
564
+ }
289
565
  };
290
566
  }
291
- var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
567
+ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
292
568
  inngestStep;
293
569
  inngestAttempts;
294
570
  constructor(mastra, inngestStep, inngestAttempts = 0) {
@@ -296,6 +572,18 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
296
572
  this.inngestStep = inngestStep;
297
573
  this.inngestAttempts = inngestAttempts;
298
574
  }
575
+ async execute(params) {
576
+ await params.emitter.emit("watch-v2", {
577
+ type: "start",
578
+ payload: { runId: params.runId }
579
+ });
580
+ const result = await super.execute(params);
581
+ await params.emitter.emit("watch-v2", {
582
+ type: "finish",
583
+ payload: { runId: params.runId }
584
+ });
585
+ return result;
586
+ }
299
587
  async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
300
588
  const base = {
301
589
  status: lastOutput.status,
@@ -362,7 +650,9 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
362
650
  resume,
363
651
  prevOutput,
364
652
  emitter,
365
- runtimeContext
653
+ abortController,
654
+ runtimeContext,
655
+ writableStream
366
656
  }) {
367
657
  return super.executeStep({
368
658
  workflowId,
@@ -373,9 +663,143 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
373
663
  resume,
374
664
  prevOutput,
375
665
  emitter,
376
- runtimeContext
666
+ abortController,
667
+ runtimeContext,
668
+ writableStream
377
669
  });
378
670
  }
671
+ // async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
672
+ // await this.inngestStep.sleep(id, duration);
673
+ // }
674
+ async executeSleep({
675
+ workflowId,
676
+ runId,
677
+ entry,
678
+ prevOutput,
679
+ stepResults,
680
+ emitter,
681
+ abortController,
682
+ runtimeContext,
683
+ writableStream
684
+ }) {
685
+ let { duration, fn } = entry;
686
+ if (fn) {
687
+ const stepCallId = crypto.randomUUID();
688
+ duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
689
+ return await fn({
690
+ runId,
691
+ workflowId,
692
+ mastra: this.mastra,
693
+ runtimeContext,
694
+ inputData: prevOutput,
695
+ runCount: -1,
696
+ getInitData: () => stepResults?.input,
697
+ getStepResult: (step) => {
698
+ if (!step?.id) {
699
+ return null;
700
+ }
701
+ const result = stepResults[step.id];
702
+ if (result?.status === "success") {
703
+ return result.output;
704
+ }
705
+ return null;
706
+ },
707
+ // TODO: this function shouldn't have suspend probably?
708
+ suspend: async (_suspendPayload) => {
709
+ },
710
+ bail: () => {
711
+ },
712
+ abort: () => {
713
+ abortController?.abort();
714
+ },
715
+ [_constants.EMITTER_SYMBOL]: emitter,
716
+ engine: { step: this.inngestStep },
717
+ abortSignal: abortController?.signal,
718
+ writer: new tools.ToolStream(
719
+ {
720
+ prefix: "step",
721
+ callId: stepCallId,
722
+ name: "sleep",
723
+ runId
724
+ },
725
+ writableStream
726
+ )
727
+ });
728
+ });
729
+ }
730
+ await this.inngestStep.sleep(entry.id, !duration || duration < 0 ? 0 : duration);
731
+ }
732
+ async executeSleepUntil({
733
+ workflowId,
734
+ runId,
735
+ entry,
736
+ prevOutput,
737
+ stepResults,
738
+ emitter,
739
+ abortController,
740
+ runtimeContext,
741
+ writableStream
742
+ }) {
743
+ let { date, fn } = entry;
744
+ if (fn) {
745
+ date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
746
+ const stepCallId = crypto.randomUUID();
747
+ return await fn({
748
+ runId,
749
+ workflowId,
750
+ mastra: this.mastra,
751
+ runtimeContext,
752
+ inputData: prevOutput,
753
+ runCount: -1,
754
+ getInitData: () => stepResults?.input,
755
+ getStepResult: (step) => {
756
+ if (!step?.id) {
757
+ return null;
758
+ }
759
+ const result = stepResults[step.id];
760
+ if (result?.status === "success") {
761
+ return result.output;
762
+ }
763
+ return null;
764
+ },
765
+ // TODO: this function shouldn't have suspend probably?
766
+ suspend: async (_suspendPayload) => {
767
+ },
768
+ bail: () => {
769
+ },
770
+ abort: () => {
771
+ abortController?.abort();
772
+ },
773
+ [_constants.EMITTER_SYMBOL]: emitter,
774
+ engine: { step: this.inngestStep },
775
+ abortSignal: abortController?.signal,
776
+ writer: new tools.ToolStream(
777
+ {
778
+ prefix: "step",
779
+ callId: stepCallId,
780
+ name: "sleep",
781
+ runId
782
+ },
783
+ writableStream
784
+ )
785
+ });
786
+ });
787
+ }
788
+ if (!(date instanceof Date)) {
789
+ return;
790
+ }
791
+ await this.inngestStep.sleepUntil(entry.id, date);
792
+ }
793
+ async executeWaitForEvent({ event, timeout }) {
794
+ const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
795
+ event: `user-event-${event}`,
796
+ timeout: timeout ?? 5e3
797
+ });
798
+ if (eventData === null) {
799
+ throw "Timeout waiting for event";
800
+ }
801
+ return eventData?.data;
802
+ }
379
803
  async executeStep({
380
804
  step,
381
805
  stepResults,
@@ -383,11 +807,14 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
383
807
  resume,
384
808
  prevOutput,
385
809
  emitter,
386
- runtimeContext
810
+ abortController,
811
+ runtimeContext,
812
+ writableStream
387
813
  }) {
388
- await this.inngestStep.run(
814
+ const startedAt = await this.inngestStep.run(
389
815
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
390
816
  async () => {
817
+ const startedAt2 = Date.now();
391
818
  await emitter.emit("watch", {
392
819
  type: "watch",
393
820
  payload: {
@@ -409,6 +836,16 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
409
836
  },
410
837
  eventTimestamp: Date.now()
411
838
  });
839
+ await emitter.emit("watch-v2", {
840
+ type: "step-start",
841
+ payload: {
842
+ id: step.id,
843
+ status: "running",
844
+ payload: prevOutput,
845
+ startedAt: startedAt2
846
+ }
847
+ });
848
+ return startedAt2;
412
849
  }
413
850
  );
414
851
  if (step instanceof InngestWorkflow) {
@@ -469,6 +906,15 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
469
906
  },
470
907
  eventTimestamp: Date.now()
471
908
  });
909
+ await emitter.emit("watch-v2", {
910
+ type: "step-result",
911
+ payload: {
912
+ id: step.id,
913
+ status: "failed",
914
+ error: result?.error,
915
+ payload: prevOutput
916
+ }
917
+ });
472
918
  return { executionContext, result: { status: "failed", error: result?.error } };
473
919
  } else if (result.status === "suspended") {
474
920
  const suspendedSteps = Object.entries(result.steps).filter(([_stepName, stepResult]) => {
@@ -495,6 +941,13 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
495
941
  },
496
942
  eventTimestamp: Date.now()
497
943
  });
944
+ await emitter.emit("watch-v2", {
945
+ type: "step-suspended",
946
+ payload: {
947
+ id: step.id,
948
+ status: "suspended"
949
+ }
950
+ });
498
951
  return {
499
952
  executionContext,
500
953
  result: {
@@ -545,6 +998,21 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
545
998
  },
546
999
  eventTimestamp: Date.now()
547
1000
  });
1001
+ await emitter.emit("watch-v2", {
1002
+ type: "step-result",
1003
+ payload: {
1004
+ id: step.id,
1005
+ status: "success",
1006
+ output: result?.result
1007
+ }
1008
+ });
1009
+ await emitter.emit("watch-v2", {
1010
+ type: "step-finish",
1011
+ payload: {
1012
+ id: step.id,
1013
+ metadata: {}
1014
+ }
1015
+ });
548
1016
  return { executionContext, result: { status: "success", output: result?.result } };
549
1017
  }
550
1018
  );
@@ -554,10 +1022,13 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
554
1022
  const stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
555
1023
  let execResults;
556
1024
  let suspended;
1025
+ let bailed;
557
1026
  try {
558
1027
  const result = await step.execute({
1028
+ runId: executionContext.runId,
559
1029
  mastra: this.mastra,
560
1030
  runtimeContext,
1031
+ writableStream,
561
1032
  inputData: prevOutput,
562
1033
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
563
1034
  getInitData: () => stepResults?.input,
@@ -572,20 +1043,54 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
572
1043
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
573
1044
  suspended = { payload: suspendPayload };
574
1045
  },
1046
+ bail: (result2) => {
1047
+ bailed = { payload: result2 };
1048
+ },
575
1049
  resume: {
576
1050
  steps: resume?.steps?.slice(1) || [],
577
1051
  resumePayload: resume?.resumePayload,
578
1052
  // @ts-ignore
579
1053
  runId: stepResults[step.id]?.payload?.__workflow_meta?.runId
580
1054
  },
581
- emitter
1055
+ [_constants.EMITTER_SYMBOL]: emitter,
1056
+ engine: {
1057
+ step: this.inngestStep
1058
+ },
1059
+ abortSignal: abortController.signal
582
1060
  });
583
- execResults = { status: "success", output: result };
1061
+ const endedAt = Date.now();
1062
+ execResults = {
1063
+ status: "success",
1064
+ output: result,
1065
+ startedAt,
1066
+ endedAt,
1067
+ payload: prevOutput,
1068
+ resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1069
+ resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1070
+ };
584
1071
  } catch (e) {
585
- execResults = { status: "failed", error: e instanceof Error ? e.message : String(e) };
1072
+ execResults = {
1073
+ status: "failed",
1074
+ payload: prevOutput,
1075
+ error: e instanceof Error ? e.message : String(e),
1076
+ endedAt: Date.now(),
1077
+ startedAt,
1078
+ resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1079
+ resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1080
+ };
586
1081
  }
587
1082
  if (suspended) {
588
- execResults = { status: "suspended", payload: suspended.payload };
1083
+ execResults = {
1084
+ status: "suspended",
1085
+ suspendedPayload: suspended.payload,
1086
+ payload: prevOutput,
1087
+ suspendedAt: Date.now(),
1088
+ startedAt,
1089
+ resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1090
+ resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1091
+ };
1092
+ } else if (bailed) {
1093
+ execResults = { status: "bailed", output: bailed.payload, payload: prevOutput, endedAt: Date.now(), startedAt };
589
1094
  }
590
1095
  if (execResults.status === "failed") {
591
1096
  if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
@@ -597,18 +1102,41 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
597
1102
  payload: {
598
1103
  currentStep: {
599
1104
  id: step.id,
600
- status: execResults.status,
601
- output: execResults.output
1105
+ ...execResults
602
1106
  },
603
1107
  workflowState: {
604
1108
  status: "running",
605
- steps: stepResults,
1109
+ steps: { ...stepResults, [step.id]: execResults },
606
1110
  result: null,
607
1111
  error: null
608
1112
  }
609
1113
  },
610
1114
  eventTimestamp: Date.now()
611
1115
  });
1116
+ if (execResults.status === "suspended") {
1117
+ await emitter.emit("watch-v2", {
1118
+ type: "step-suspended",
1119
+ payload: {
1120
+ id: step.id,
1121
+ ...execResults
1122
+ }
1123
+ });
1124
+ } else {
1125
+ await emitter.emit("watch-v2", {
1126
+ type: "step-result",
1127
+ payload: {
1128
+ id: step.id,
1129
+ ...execResults
1130
+ }
1131
+ });
1132
+ await emitter.emit("watch-v2", {
1133
+ type: "step-finish",
1134
+ payload: {
1135
+ id: step.id,
1136
+ metadata: {}
1137
+ }
1138
+ });
1139
+ }
612
1140
  return { result: execResults, executionContext, stepResults };
613
1141
  });
614
1142
  Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
@@ -619,7 +1147,11 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
619
1147
  workflowId,
620
1148
  runId,
621
1149
  stepResults,
622
- executionContext
1150
+ executionContext,
1151
+ serializedStepGraph,
1152
+ workflowStatus,
1153
+ result,
1154
+ error
623
1155
  }) {
624
1156
  await this.inngestStep.run(
625
1157
  `workflow.${workflowId}.run.${runId}.path.${JSON.stringify(executionContext.executionPath)}.stepUpdate`,
@@ -633,6 +1165,10 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
633
1165
  context: stepResults,
634
1166
  activePaths: [],
635
1167
  suspendedPaths: executionContext.suspendedPaths,
1168
+ serializedStepGraph,
1169
+ status: workflowStatus,
1170
+ result,
1171
+ error,
636
1172
  // @ts-ignore
637
1173
  timestamp: Date.now()
638
1174
  }
@@ -647,10 +1183,13 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
647
1183
  prevOutput,
648
1184
  prevStep,
649
1185
  stepResults,
1186
+ serializedStepGraph,
650
1187
  resume,
651
1188
  executionContext,
652
1189
  emitter,
653
- runtimeContext
1190
+ abortController,
1191
+ runtimeContext,
1192
+ writableStream
654
1193
  }) {
655
1194
  let execResults;
656
1195
  const truthyIndexes = (await Promise.all(
@@ -658,8 +1197,11 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
658
1197
  (cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
659
1198
  try {
660
1199
  const result = await cond({
1200
+ runId,
1201
+ workflowId,
661
1202
  mastra: this.mastra,
662
1203
  runtimeContext,
1204
+ runCount: -1,
663
1205
  inputData: prevOutput,
664
1206
  getInitData: () => stepResults?.input,
665
1207
  getStepResult: (step) => {
@@ -675,7 +1217,25 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
675
1217
  // TODO: this function shouldn't have suspend probably?
676
1218
  suspend: async (_suspendPayload) => {
677
1219
  },
678
- emitter
1220
+ bail: () => {
1221
+ },
1222
+ abort: () => {
1223
+ abortController.abort();
1224
+ },
1225
+ [_constants.EMITTER_SYMBOL]: emitter,
1226
+ engine: {
1227
+ step: this.inngestStep
1228
+ },
1229
+ abortSignal: abortController.signal,
1230
+ writer: new tools.ToolStream(
1231
+ {
1232
+ prefix: "step",
1233
+ callId: crypto.randomUUID(),
1234
+ name: "conditional",
1235
+ runId
1236
+ },
1237
+ writableStream
1238
+ )
679
1239
  });
680
1240
  return result ? index : null;
681
1241
  } catch (e) {
@@ -694,6 +1254,7 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
694
1254
  prevStep,
695
1255
  stepResults,
696
1256
  resume,
1257
+ serializedStepGraph,
697
1258
  executionContext: {
698
1259
  workflowId,
699
1260
  runId,
@@ -703,21 +1264,23 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
703
1264
  executionSpan: executionContext.executionSpan
704
1265
  },
705
1266
  emitter,
706
- runtimeContext
1267
+ abortController,
1268
+ runtimeContext,
1269
+ writableStream
707
1270
  })
708
1271
  )
709
1272
  );
710
- const hasFailed = results.find((result) => result.status === "failed");
711
- const hasSuspended = results.find((result) => result.status === "suspended");
1273
+ const hasFailed = results.find((result) => result.result.status === "failed");
1274
+ const hasSuspended = results.find((result) => result.result.status === "suspended");
712
1275
  if (hasFailed) {
713
- execResults = { status: "failed", error: hasFailed.error };
1276
+ execResults = { status: "failed", error: hasFailed.result.error };
714
1277
  } else if (hasSuspended) {
715
- execResults = { status: "suspended", payload: hasSuspended.payload };
1278
+ execResults = { status: "suspended", payload: hasSuspended.result.suspendPayload };
716
1279
  } else {
717
1280
  execResults = {
718
1281
  status: "success",
719
1282
  output: results.reduce((acc, result, index) => {
720
- if (result.status === "success") {
1283
+ if (result.result.status === "success") {
721
1284
  acc[stepsToRun[index].step.id] = result.output;
722
1285
  }
723
1286
  return acc;
@@ -731,5 +1294,8 @@ var InngestExecutionEngine = class extends vNext.DefaultExecutionEngine {
731
1294
  exports.InngestExecutionEngine = InngestExecutionEngine;
732
1295
  exports.InngestRun = InngestRun;
733
1296
  exports.InngestWorkflow = InngestWorkflow;
1297
+ exports.createStep = createStep;
734
1298
  exports.init = init;
735
1299
  exports.serve = serve;
1300
+ //# sourceMappingURL=index.cjs.map
1301
+ //# sourceMappingURL=index.cjs.map