@mastra/inngest 0.18.4 → 1.0.0-beta.0

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,8 +3,8 @@
3
3
  var crypto = require('crypto');
4
4
  var web = require('stream/web');
5
5
  var realtime = require('@inngest/realtime');
6
- var aiTracing = require('@mastra/core/ai-tracing');
7
6
  var di = require('@mastra/core/di');
7
+ var observability = require('@mastra/core/observability');
8
8
  var stream = require('@mastra/core/stream');
9
9
  var tools = require('@mastra/core/tools');
10
10
  var workflows = require('@mastra/core/workflows');
@@ -20,7 +20,7 @@ function serve({
20
20
  functions: userFunctions = [],
21
21
  registerOptions
22
22
  }) {
23
- const wfs = mastra.getWorkflows();
23
+ const wfs = mastra.listWorkflows();
24
24
  const workflowFunctions = Array.from(
25
25
  new Set(
26
26
  Object.values(wfs).flatMap((wf) => {
@@ -59,11 +59,12 @@ var InngestRun = class extends workflows.Run {
59
59
  }
60
60
  async getRunOutput(eventId) {
61
61
  let runs = await this.getRuns(eventId);
62
+ const storage = this.#mastra?.getStorage();
62
63
  while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
63
64
  await new Promise((resolve) => setTimeout(resolve, 1e3));
64
65
  runs = await this.getRuns(eventId);
65
66
  if (runs?.[0]?.status === "Failed") {
66
- const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
67
+ const snapshot = await storage?.loadWorkflowSnapshot({
67
68
  workflowName: this.workflowId,
68
69
  runId: this.runId
69
70
  });
@@ -72,7 +73,7 @@ var InngestRun = class extends workflows.Run {
72
73
  };
73
74
  }
74
75
  if (runs?.[0]?.status === "Cancelled") {
75
- const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
76
+ const snapshot = await storage?.loadWorkflowSnapshot({
76
77
  workflowName: this.workflowId,
77
78
  runId: this.runId
78
79
  });
@@ -81,25 +82,20 @@ var InngestRun = class extends workflows.Run {
81
82
  }
82
83
  return runs?.[0];
83
84
  }
84
- async sendEvent(event, data) {
85
- await this.inngest.send({
86
- name: `user-event-${event}`,
87
- data
88
- });
89
- }
90
85
  async cancel() {
86
+ const storage = this.#mastra?.getStorage();
91
87
  await this.inngest.send({
92
88
  name: `cancel.workflow.${this.workflowId}`,
93
89
  data: {
94
90
  runId: this.runId
95
91
  }
96
92
  });
97
- const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
93
+ const snapshot = await storage?.loadWorkflowSnapshot({
98
94
  workflowName: this.workflowId,
99
95
  runId: this.runId
100
96
  });
101
97
  if (snapshot) {
102
- await this.#mastra?.storage?.persistWorkflowSnapshot({
98
+ await storage?.persistWorkflowSnapshot({
103
99
  workflowName: this.workflowId,
104
100
  runId: this.runId,
105
101
  resourceId: this.resourceId,
@@ -110,9 +106,15 @@ var InngestRun = class extends workflows.Run {
110
106
  });
111
107
  }
112
108
  }
113
- async start({
109
+ async start(params) {
110
+ return this._start(params);
111
+ }
112
+ async _start({
114
113
  inputData,
115
- initialState
114
+ initialState,
115
+ outputOptions,
116
+ tracingOptions,
117
+ format
116
118
  }) {
117
119
  await this.#mastra.getStorage()?.persistWorkflowSnapshot({
118
120
  workflowName: this.workflowId,
@@ -139,7 +141,10 @@ var InngestRun = class extends workflows.Run {
139
141
  inputData: inputDataToUse,
140
142
  initialState: initialStateToUse,
141
143
  runId: this.runId,
142
- resourceId: this.resourceId
144
+ resourceId: this.resourceId,
145
+ outputOptions,
146
+ tracingOptions,
147
+ format
143
148
  }
144
149
  });
145
150
  const eventId = eventOutput.ids[0];
@@ -168,10 +173,11 @@ var InngestRun = class extends workflows.Run {
168
173
  return p;
169
174
  }
170
175
  async _resume(params) {
176
+ const storage = this.#mastra?.getStorage();
171
177
  const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
172
178
  (step) => typeof step === "string" ? step : step?.id
173
179
  );
174
- const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
180
+ const snapshot = await storage?.loadWorkflowSnapshot({
175
181
  workflowName: this.workflowId,
176
182
  runId: this.runId
177
183
  });
@@ -205,12 +211,12 @@ var InngestRun = class extends workflows.Run {
205
211
  }
206
212
  return result;
207
213
  }
208
- watch(cb, type = "watch") {
214
+ watch(cb) {
209
215
  let active = true;
210
216
  const streamPromise = realtime.subscribe(
211
217
  {
212
218
  channel: `workflow:${this.workflowId}:${this.runId}`,
213
- topics: [type],
219
+ topics: ["watch"],
214
220
  app: this.inngest
215
221
  },
216
222
  (message) => {
@@ -228,20 +234,35 @@ var InngestRun = class extends workflows.Run {
228
234
  });
229
235
  };
230
236
  }
231
- streamLegacy({ inputData, runtimeContext } = {}) {
237
+ streamLegacy({ inputData, requestContext } = {}) {
232
238
  const { readable, writable } = new TransformStream();
233
239
  const writer = writable.getWriter();
234
240
  const unwatch = this.watch(async (event) => {
235
241
  try {
242
+ await writer.write({
243
+ // @ts-ignore
244
+ type: "start",
245
+ // @ts-ignore
246
+ payload: { runId: this.runId }
247
+ });
236
248
  const e = {
237
249
  ...event,
238
250
  type: event.type.replace("workflow-", "")
239
251
  };
252
+ if (e.type === "step-output") {
253
+ e.type = e.payload.output.type;
254
+ e.payload = e.payload.output.payload;
255
+ }
240
256
  await writer.write(e);
241
257
  } catch {
242
258
  }
243
- }, "watch-v2");
259
+ });
244
260
  this.closeStreamAction = async () => {
261
+ await writer.write({
262
+ type: "finish",
263
+ // @ts-ignore
264
+ payload: { runId: this.runId }
265
+ });
245
266
  unwatch();
246
267
  try {
247
268
  await writer.close();
@@ -251,7 +272,7 @@ var InngestRun = class extends workflows.Run {
251
272
  writer.releaseLock();
252
273
  }
253
274
  };
254
- this.executionResults = this.start({ inputData, runtimeContext }).then((result) => {
275
+ this.executionResults = this._start({ inputData, requestContext, format: "legacy" }).then((result) => {
255
276
  if (result.status !== "suspended") {
256
277
  this.closeStreamAction?.().catch(() => {
257
278
  });
@@ -265,11 +286,18 @@ var InngestRun = class extends workflows.Run {
265
286
  }
266
287
  stream({
267
288
  inputData,
268
- runtimeContext,
269
- closeOnSuspend = true
289
+ requestContext,
290
+ tracingOptions,
291
+ closeOnSuspend = true,
292
+ initialState,
293
+ outputOptions
270
294
  } = {}) {
295
+ if (this.closeStreamAction && this.streamOutput) {
296
+ return this.streamOutput;
297
+ }
298
+ this.closeStreamAction = async () => {
299
+ };
271
300
  const self = this;
272
- let streamOutput;
273
301
  const stream$1 = new web.ReadableStream({
274
302
  async start(controller) {
275
303
  const unwatch = self.watch(async ({ type, from = stream.ChunkFrom.WORKFLOW, payload }) => {
@@ -278,11 +306,11 @@ var InngestRun = class extends workflows.Run {
278
306
  runId: self.runId,
279
307
  from,
280
308
  payload: {
281
- stepName: payload.id,
309
+ stepName: payload?.id,
282
310
  ...payload
283
311
  }
284
312
  });
285
- }, "watch-v2");
313
+ });
286
314
  self.closeStreamAction = async () => {
287
315
  unwatch();
288
316
  try {
@@ -291,29 +319,46 @@ var InngestRun = class extends workflows.Run {
291
319
  console.error("Error closing stream:", err);
292
320
  }
293
321
  };
294
- const executionResultsPromise = self.start({
322
+ const executionResultsPromise = self._start({
295
323
  inputData,
296
- runtimeContext
324
+ requestContext,
325
+ // tracingContext, // We are not able to pass a reference to a span here, what to do?
326
+ initialState,
327
+ tracingOptions,
328
+ outputOptions,
329
+ format: "vnext"
297
330
  });
298
- const executionResults = await executionResultsPromise;
299
- if (closeOnSuspend) {
300
- self.closeStreamAction?.().catch(() => {
301
- });
302
- } else if (executionResults.status !== "suspended") {
331
+ let executionResults;
332
+ try {
333
+ executionResults = await executionResultsPromise;
334
+ if (closeOnSuspend) {
335
+ self.closeStreamAction?.().catch(() => {
336
+ });
337
+ } else if (executionResults.status !== "suspended") {
338
+ self.closeStreamAction?.().catch(() => {
339
+ });
340
+ }
341
+ if (self.streamOutput) {
342
+ self.streamOutput.updateResults(
343
+ executionResults
344
+ );
345
+ }
346
+ } catch (err) {
347
+ self.streamOutput?.rejectResults(err);
303
348
  self.closeStreamAction?.().catch(() => {
304
349
  });
305
350
  }
306
- if (streamOutput) {
307
- streamOutput.updateResults(executionResults);
308
- }
309
351
  }
310
352
  });
311
- streamOutput = new stream.WorkflowRunOutput({
353
+ this.streamOutput = new stream.WorkflowRunOutput({
312
354
  runId: this.runId,
313
355
  workflowId: this.workflowId,
314
356
  stream: stream$1
315
357
  });
316
- return streamOutput;
358
+ return this.streamOutput;
359
+ }
360
+ streamVNext(args = {}) {
361
+ return this.stream(args);
317
362
  }
318
363
  };
319
364
  var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
@@ -331,13 +376,13 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
331
376
  this.#mastra = params.mastra;
332
377
  this.inngest = inngest;
333
378
  }
334
- async getWorkflowRuns(args) {
379
+ async listWorkflowRuns(args) {
335
380
  const storage = this.#mastra?.getStorage();
336
381
  if (!storage) {
337
382
  this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
338
383
  return { runs: [], total: 0 };
339
384
  }
340
- return storage.getWorkflowRuns({ workflowName: this.id, ...args ?? {} });
385
+ return storage.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
341
386
  }
342
387
  async getWorkflowRunById(runId) {
343
388
  const storage = this.#mastra?.getStorage();
@@ -366,16 +411,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
366
411
  }
367
412
  }
368
413
  }
369
- /**
370
- * @deprecated Use createRunAsync() instead.
371
- * @throws {Error} Always throws an error directing users to use createRunAsync()
372
- */
373
- createRun(_options) {
374
- throw new Error(
375
- "createRun() has been deprecated. Please use createRunAsync() instead.\n\nMigration guide:\n Before: const run = workflow.createRun();\n After: const run = await workflow.createRunAsync();\n\nNote: createRunAsync() is an async method, so make sure your calling function is async."
376
- );
377
- }
378
- async createRunAsync(options) {
414
+ async createRun(options) {
379
415
  const runIdToUse = options?.runId || crypto.randomUUID();
380
416
  const run = this.runs.get(runIdToUse) ?? new InngestRun(
381
417
  {
@@ -437,7 +473,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
437
473
  },
438
474
  { event: `workflow.${this.id}` },
439
475
  async ({ event, step, attempt, publish }) => {
440
- let { inputData, initialState, runId, resourceId, resume, outputOptions } = event.data;
476
+ let { inputData, initialState, runId, resourceId, resume, outputOptions, format } = event.data;
441
477
  if (!runId) {
442
478
  runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
443
479
  return crypto.randomUUID();
@@ -476,13 +512,19 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
476
512
  initialState,
477
513
  emitter,
478
514
  retryConfig: this.retryConfig,
479
- runtimeContext: new di.RuntimeContext(),
515
+ requestContext: new di.RequestContext(),
480
516
  // TODO
481
517
  resume,
518
+ format,
482
519
  abortController: new AbortController(),
483
- currentSpan: void 0,
484
- // TODO: Pass actual parent AI span from workflow execution context
485
- outputOptions
520
+ // currentSpan: undefined, // TODO: Pass actual parent Span from workflow execution context
521
+ outputOptions,
522
+ writableStream: new WritableStream({
523
+ write(chunk) {
524
+ void emitter.emit("watch", chunk).catch(() => {
525
+ });
526
+ }
527
+ })
486
528
  });
487
529
  await step.run(`workflow.${this.id}.finalize`, async () => {
488
530
  if (result.status === "failed") {
@@ -520,7 +562,7 @@ function isAgent(params) {
520
562
  function isTool(params) {
521
563
  return params instanceof tools.Tool;
522
564
  }
523
- function createStep(params) {
565
+ function createStep(params, agentOptions) {
524
566
  if (isAgent(params)) {
525
567
  return {
526
568
  id: params.name,
@@ -528,12 +570,23 @@ function createStep(params) {
528
570
  // @ts-ignore
529
571
  inputSchema: zod.z.object({
530
572
  prompt: zod.z.string()
573
+ // resourceId: z.string().optional(),
574
+ // threadId: z.string().optional(),
531
575
  }),
532
576
  // @ts-ignore
533
577
  outputSchema: zod.z.object({
534
578
  text: zod.z.string()
535
579
  }),
536
- execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort, tracingContext }) => {
580
+ execute: async ({
581
+ inputData,
582
+ [_constants.EMITTER_SYMBOL]: emitter,
583
+ [_constants.STREAM_FORMAT_SYMBOL]: streamFormat,
584
+ requestContext,
585
+ tracingContext,
586
+ abortSignal,
587
+ abort,
588
+ writer
589
+ }) => {
537
590
  let streamPromise = {};
538
591
  streamPromise.promise = new Promise((resolve, reject) => {
539
592
  streamPromise.resolve = resolve;
@@ -543,61 +596,60 @@ function createStep(params) {
543
596
  name: params.name,
544
597
  args: inputData
545
598
  };
546
- if ((await params.getLLM()).getModel().specificationVersion === `v2`) {
547
- const { fullStream } = await params.stream(inputData.prompt, {
548
- runtimeContext,
599
+ let stream;
600
+ if ((await params.getModel()).specificationVersion === "v1") {
601
+ const { fullStream } = await params.streamLegacy(inputData.prompt, {
602
+ ...agentOptions ?? {},
603
+ // resourceId: inputData.resourceId,
604
+ // threadId: inputData.threadId,
605
+ requestContext,
549
606
  tracingContext,
550
607
  onFinish: (result) => {
551
608
  streamPromise.resolve(result.text);
609
+ void agentOptions?.onFinish?.(result);
552
610
  },
553
611
  abortSignal
554
612
  });
555
- if (abortSignal.aborted) {
556
- return abort();
557
- }
558
- await emitter.emit("watch-v2", {
559
- type: "tool-call-streaming-start",
560
- ...toolData ?? {}
561
- });
562
- for await (const chunk of fullStream) {
563
- if (chunk.type === "text-delta") {
564
- await emitter.emit("watch-v2", {
565
- type: "tool-call-delta",
566
- ...toolData ?? {},
567
- argsTextDelta: chunk.payload.text
568
- });
569
- }
570
- }
613
+ stream = fullStream;
571
614
  } else {
572
- const { fullStream } = await params.streamLegacy(inputData.prompt, {
573
- runtimeContext,
615
+ const modelOutput = await params.stream(inputData.prompt, {
616
+ ...agentOptions ?? {},
617
+ requestContext,
574
618
  tracingContext,
575
619
  onFinish: (result) => {
576
620
  streamPromise.resolve(result.text);
621
+ void agentOptions?.onFinish?.(result);
577
622
  },
578
623
  abortSignal
579
624
  });
580
- if (abortSignal.aborted) {
581
- return abort();
582
- }
583
- await emitter.emit("watch-v2", {
625
+ stream = modelOutput.fullStream;
626
+ }
627
+ if (streamFormat === "legacy") {
628
+ await emitter.emit("watch", {
584
629
  type: "tool-call-streaming-start",
585
630
  ...toolData ?? {}
586
631
  });
587
- for await (const chunk of fullStream) {
632
+ for await (const chunk of stream) {
588
633
  if (chunk.type === "text-delta") {
589
- await emitter.emit("watch-v2", {
634
+ await emitter.emit("watch", {
590
635
  type: "tool-call-delta",
591
636
  ...toolData ?? {},
592
637
  argsTextDelta: chunk.textDelta
593
638
  });
594
639
  }
595
640
  }
641
+ await emitter.emit("watch", {
642
+ type: "tool-call-streaming-finish",
643
+ ...toolData ?? {}
644
+ });
645
+ } else {
646
+ for await (const chunk of stream) {
647
+ await writer.write(chunk);
648
+ }
649
+ }
650
+ if (abortSignal.aborted) {
651
+ return abort();
596
652
  }
597
- await emitter.emit("watch-v2", {
598
- type: "tool-call-streaming-finish",
599
- ...toolData ?? {}
600
- });
601
653
  return {
602
654
  text: await streamPromise.promise
603
655
  };
@@ -616,11 +668,11 @@ function createStep(params) {
616
668
  description: params.description,
617
669
  inputSchema: params.inputSchema,
618
670
  outputSchema: params.outputSchema,
619
- execute: async ({ inputData, mastra, runtimeContext, tracingContext, suspend, resumeData }) => {
671
+ execute: async ({ inputData, mastra, requestContext, tracingContext, suspend, resumeData }) => {
620
672
  return params.execute({
621
673
  context: inputData,
622
- mastra: aiTracing.wrapMastra(mastra, tracingContext),
623
- runtimeContext,
674
+ mastra: observability.wrapMastra(mastra, tracingContext),
675
+ requestContext,
624
676
  tracingContext,
625
677
  suspend,
626
678
  resumeData
@@ -683,63 +735,16 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
683
735
  this.inngestStep = inngestStep;
684
736
  this.inngestAttempts = inngestAttempts;
685
737
  }
686
- async execute(params) {
687
- await params.emitter.emit("watch-v2", {
688
- type: "workflow-start",
689
- payload: { runId: params.runId }
690
- });
691
- const result = await super.execute(params);
692
- await params.emitter.emit("watch-v2", {
693
- type: "workflow-finish",
694
- payload: { runId: params.runId }
695
- });
696
- return result;
697
- }
698
- async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
738
+ async fmtReturnValue(emitter, stepResults, lastOutput, error) {
699
739
  const base = {
700
740
  status: lastOutput.status,
701
741
  steps: stepResults
702
742
  };
703
743
  if (lastOutput.status === "success") {
704
- await emitter.emit("watch", {
705
- type: "watch",
706
- payload: {
707
- workflowState: {
708
- status: lastOutput.status,
709
- steps: stepResults,
710
- result: lastOutput.output
711
- }
712
- },
713
- eventTimestamp: Date.now()
714
- });
715
744
  base.result = lastOutput.output;
716
745
  } else if (lastOutput.status === "failed") {
717
746
  base.error = error instanceof Error ? error?.stack ?? error.message : lastOutput?.error instanceof Error ? lastOutput.error.message : lastOutput.error ?? error ?? "Unknown error";
718
- await emitter.emit("watch", {
719
- type: "watch",
720
- payload: {
721
- workflowState: {
722
- status: lastOutput.status,
723
- steps: stepResults,
724
- result: null,
725
- error: base.error
726
- }
727
- },
728
- eventTimestamp: Date.now()
729
- });
730
747
  } else if (lastOutput.status === "suspended") {
731
- await emitter.emit("watch", {
732
- type: "watch",
733
- payload: {
734
- workflowState: {
735
- status: lastOutput.status,
736
- steps: stepResults,
737
- result: null,
738
- error: null
739
- }
740
- },
741
- eventTimestamp: Date.now()
742
- });
743
748
  const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
744
749
  if (stepResult?.status === "suspended") {
745
750
  const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
@@ -749,7 +754,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
749
754
  });
750
755
  base.suspended = suspendedStepIds;
751
756
  }
752
- executionSpan?.end();
753
757
  return base;
754
758
  }
755
759
  // async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
@@ -763,14 +767,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
763
767
  stepResults,
764
768
  emitter,
765
769
  abortController,
766
- runtimeContext,
770
+ requestContext,
767
771
  executionContext,
768
772
  writableStream,
769
773
  tracingContext
770
774
  }) {
771
775
  let { duration, fn } = entry;
772
776
  const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
773
- type: aiTracing.AISpanType.WORKFLOW_SLEEP,
777
+ type: observability.SpanType.WORKFLOW_SLEEP,
774
778
  name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
775
779
  attributes: {
776
780
  durationMs: duration,
@@ -781,45 +785,53 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
781
785
  if (fn) {
782
786
  const stepCallId = crypto.randomUUID();
783
787
  duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
784
- return await fn({
785
- runId,
786
- workflowId,
787
- mastra: this.mastra,
788
- runtimeContext,
789
- inputData: prevOutput,
790
- state: executionContext.state,
791
- setState: (state) => {
792
- executionContext.state = state;
793
- },
794
- runCount: -1,
795
- tracingContext: {
796
- currentSpan: sleepSpan
797
- },
798
- getInitData: () => stepResults?.input,
799
- getStepResult: workflows.getStepResult.bind(this, stepResults),
800
- // TODO: this function shouldn't have suspend probably?
801
- suspend: async (_suspendPayload) => {
802
- },
803
- bail: () => {
804
- },
805
- abort: () => {
806
- abortController?.abort();
807
- },
808
- [_constants.EMITTER_SYMBOL]: emitter,
809
- // TODO: add streamVNext support
810
- [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
811
- engine: { step: this.inngestStep },
812
- abortSignal: abortController?.signal,
813
- writer: new tools.ToolStream(
788
+ return await fn(
789
+ workflows.createDeprecationProxy(
814
790
  {
815
- prefix: "workflow-step",
816
- callId: stepCallId,
817
- name: "sleep",
818
- runId
791
+ runId,
792
+ workflowId,
793
+ mastra: this.mastra,
794
+ requestContext,
795
+ inputData: prevOutput,
796
+ state: executionContext.state,
797
+ setState: (state) => {
798
+ executionContext.state = state;
799
+ },
800
+ retryCount: -1,
801
+ tracingContext: {
802
+ currentSpan: sleepSpan
803
+ },
804
+ getInitData: () => stepResults?.input,
805
+ getStepResult: workflows.getStepResult.bind(this, stepResults),
806
+ // TODO: this function shouldn't have suspend probably?
807
+ suspend: async (_suspendPayload) => {
808
+ },
809
+ bail: () => {
810
+ },
811
+ abort: () => {
812
+ abortController?.abort();
813
+ },
814
+ [_constants.EMITTER_SYMBOL]: emitter,
815
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
816
+ engine: { step: this.inngestStep },
817
+ abortSignal: abortController?.signal,
818
+ writer: new tools.ToolStream(
819
+ {
820
+ prefix: "workflow-step",
821
+ callId: stepCallId,
822
+ name: "sleep",
823
+ runId
824
+ },
825
+ writableStream
826
+ )
819
827
  },
820
- writableStream
828
+ {
829
+ paramName: "runCount",
830
+ deprecationMessage: workflows.runCountDeprecationMessage,
831
+ logger: this.logger
832
+ }
821
833
  )
822
- });
834
+ );
823
835
  });
824
836
  sleepSpan?.update({
825
837
  attributes: {
@@ -843,14 +855,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
843
855
  stepResults,
844
856
  emitter,
845
857
  abortController,
846
- runtimeContext,
858
+ requestContext,
847
859
  executionContext,
848
860
  writableStream,
849
861
  tracingContext
850
862
  }) {
851
863
  let { date, fn } = entry;
852
864
  const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
853
- type: aiTracing.AISpanType.WORKFLOW_SLEEP,
865
+ type: observability.SpanType.WORKFLOW_SLEEP,
854
866
  name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
855
867
  attributes: {
856
868
  untilDate: date,
@@ -862,45 +874,53 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
862
874
  if (fn) {
863
875
  date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
864
876
  const stepCallId = crypto.randomUUID();
865
- return await fn({
866
- runId,
867
- workflowId,
868
- mastra: this.mastra,
869
- runtimeContext,
870
- inputData: prevOutput,
871
- state: executionContext.state,
872
- setState: (state) => {
873
- executionContext.state = state;
874
- },
875
- runCount: -1,
876
- tracingContext: {
877
- currentSpan: sleepUntilSpan
878
- },
879
- getInitData: () => stepResults?.input,
880
- getStepResult: workflows.getStepResult.bind(this, stepResults),
881
- // TODO: this function shouldn't have suspend probably?
882
- suspend: async (_suspendPayload) => {
883
- },
884
- bail: () => {
885
- },
886
- abort: () => {
887
- abortController?.abort();
888
- },
889
- [_constants.EMITTER_SYMBOL]: emitter,
890
- [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
891
- // TODO: add streamVNext support
892
- engine: { step: this.inngestStep },
893
- abortSignal: abortController?.signal,
894
- writer: new tools.ToolStream(
877
+ return await fn(
878
+ workflows.createDeprecationProxy(
895
879
  {
896
- prefix: "workflow-step",
897
- callId: stepCallId,
898
- name: "sleep",
899
- runId
880
+ runId,
881
+ workflowId,
882
+ mastra: this.mastra,
883
+ requestContext,
884
+ inputData: prevOutput,
885
+ state: executionContext.state,
886
+ setState: (state) => {
887
+ executionContext.state = state;
888
+ },
889
+ retryCount: -1,
890
+ tracingContext: {
891
+ currentSpan: sleepUntilSpan
892
+ },
893
+ getInitData: () => stepResults?.input,
894
+ getStepResult: workflows.getStepResult.bind(this, stepResults),
895
+ // TODO: this function shouldn't have suspend probably?
896
+ suspend: async (_suspendPayload) => {
897
+ },
898
+ bail: () => {
899
+ },
900
+ abort: () => {
901
+ abortController?.abort();
902
+ },
903
+ [_constants.EMITTER_SYMBOL]: emitter,
904
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
905
+ engine: { step: this.inngestStep },
906
+ abortSignal: abortController?.signal,
907
+ writer: new tools.ToolStream(
908
+ {
909
+ prefix: "workflow-step",
910
+ callId: stepCallId,
911
+ name: "sleep",
912
+ runId
913
+ },
914
+ writableStream
915
+ )
900
916
  },
901
- writableStream
917
+ {
918
+ paramName: "runCount",
919
+ deprecationMessage: workflows.runCountDeprecationMessage,
920
+ logger: this.logger
921
+ }
902
922
  )
903
- });
923
+ );
904
924
  });
905
925
  if (date && !(date instanceof Date)) {
906
926
  date = new Date(date);
@@ -924,16 +944,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
924
944
  throw e;
925
945
  }
926
946
  }
927
- async executeWaitForEvent({ event, timeout }) {
928
- const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
929
- event: `user-event-${event}`,
930
- timeout: timeout ?? 5e3
931
- });
932
- if (eventData === null) {
933
- throw "Timeout waiting for event";
934
- }
935
- return eventData?.data;
936
- }
937
947
  async executeStep({
938
948
  step,
939
949
  stepResults,
@@ -942,14 +952,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
942
952
  prevOutput,
943
953
  emitter,
944
954
  abortController,
945
- runtimeContext,
955
+ requestContext,
946
956
  tracingContext,
947
957
  writableStream,
948
958
  disableScorers
949
959
  }) {
950
- const stepAISpan = tracingContext?.currentSpan?.createChildSpan({
960
+ const stepSpan = tracingContext?.currentSpan?.createChildSpan({
951
961
  name: `workflow step: '${step.id}'`,
952
- type: aiTracing.AISpanType.WORKFLOW_STEP,
962
+ type: observability.SpanType.WORKFLOW_STEP,
953
963
  input: prevOutput,
954
964
  attributes: {
955
965
  stepId: step.id
@@ -966,27 +976,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
966
976
  async () => {
967
977
  const startedAt2 = Date.now();
968
978
  await emitter.emit("watch", {
969
- type: "watch",
970
- payload: {
971
- currentStep: {
972
- id: step.id,
973
- status: "running"
974
- },
975
- workflowState: {
976
- status: "running",
977
- steps: {
978
- ...stepResults,
979
- [step.id]: {
980
- status: "running"
981
- }
982
- },
983
- result: null,
984
- error: null
985
- }
986
- },
987
- eventTimestamp: Date.now()
988
- });
989
- await emitter.emit("watch-v2", {
990
979
  type: "workflow-step-start",
991
980
  payload: {
992
981
  id: step.id,
@@ -1062,23 +1051,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1062
1051
  async () => {
1063
1052
  if (result.status === "failed") {
1064
1053
  await emitter.emit("watch", {
1065
- type: "watch",
1066
- payload: {
1067
- currentStep: {
1068
- id: step.id,
1069
- status: "failed",
1070
- error: result?.error
1071
- },
1072
- workflowState: {
1073
- status: "running",
1074
- steps: stepResults,
1075
- result: null,
1076
- error: null
1077
- }
1078
- },
1079
- eventTimestamp: Date.now()
1080
- });
1081
- await emitter.emit("watch-v2", {
1082
1054
  type: "workflow-step-result",
1083
1055
  payload: {
1084
1056
  id: step.id,
@@ -1097,27 +1069,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1097
1069
  const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
1098
1070
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
1099
1071
  await emitter.emit("watch", {
1100
- type: "watch",
1101
- payload: {
1102
- currentStep: {
1103
- id: step.id,
1104
- status: "suspended",
1105
- payload: stepResult.payload,
1106
- suspendPayload: {
1107
- ...stepResult?.suspendPayload,
1108
- __workflow_meta: { runId, path: suspendPath }
1109
- }
1110
- },
1111
- workflowState: {
1112
- status: "running",
1113
- steps: stepResults,
1114
- result: null,
1115
- error: null
1116
- }
1117
- },
1118
- eventTimestamp: Date.now()
1119
- });
1120
- await emitter.emit("watch-v2", {
1121
1072
  type: "workflow-step-suspended",
1122
1073
  payload: {
1123
1074
  id: step.id,
@@ -1136,23 +1087,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1136
1087
  }
1137
1088
  };
1138
1089
  }
1139
- await emitter.emit("watch", {
1140
- type: "watch",
1141
- payload: {
1142
- currentStep: {
1143
- id: step.id,
1144
- status: "suspended",
1145
- payload: {}
1146
- },
1147
- workflowState: {
1148
- status: "running",
1149
- steps: stepResults,
1150
- result: null,
1151
- error: null
1152
- }
1153
- },
1154
- eventTimestamp: Date.now()
1155
- });
1156
1090
  return {
1157
1091
  executionContext,
1158
1092
  result: {
@@ -1162,23 +1096,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1162
1096
  };
1163
1097
  }
1164
1098
  await emitter.emit("watch", {
1165
- type: "watch",
1166
- payload: {
1167
- currentStep: {
1168
- id: step.id,
1169
- status: "success",
1170
- output: result?.result
1171
- },
1172
- workflowState: {
1173
- status: "running",
1174
- steps: stepResults,
1175
- result: null,
1176
- error: null
1177
- }
1178
- },
1179
- eventTimestamp: Date.now()
1180
- });
1181
- await emitter.emit("watch-v2", {
1182
1099
  type: "workflow-step-result",
1183
1100
  payload: {
1184
1101
  id: step.id,
@@ -1186,7 +1103,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1186
1103
  output: result?.result
1187
1104
  }
1188
1105
  });
1189
- await emitter.emit("watch-v2", {
1106
+ await emitter.emit("watch", {
1190
1107
  type: "workflow-step-finish",
1191
1108
  payload: {
1192
1109
  id: step.id,
@@ -1206,6 +1123,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1206
1123
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1207
1124
  };
1208
1125
  }
1126
+ const stepCallId = crypto.randomUUID();
1209
1127
  let stepRes;
1210
1128
  try {
1211
1129
  stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
@@ -1219,8 +1137,16 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1219
1137
  const result = await step.execute({
1220
1138
  runId: executionContext.runId,
1221
1139
  mastra: this.mastra,
1222
- runtimeContext,
1223
- writableStream,
1140
+ requestContext,
1141
+ writer: new tools.ToolStream(
1142
+ {
1143
+ prefix: "workflow-step",
1144
+ callId: stepCallId,
1145
+ name: step.id,
1146
+ runId: executionContext.runId
1147
+ },
1148
+ writableStream
1149
+ ),
1224
1150
  state: executionContext?.state ?? {},
1225
1151
  setState: (state) => {
1226
1152
  executionContext.state = state;
@@ -1228,7 +1154,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1228
1154
  inputData,
1229
1155
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
1230
1156
  tracingContext: {
1231
- currentSpan: stepAISpan
1157
+ currentSpan: stepSpan
1232
1158
  },
1233
1159
  getInitData: () => stepResults?.input,
1234
1160
  getStepResult: workflows.getStepResult.bind(this, stepResults),
@@ -1255,6 +1181,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1255
1181
  runId: stepResults[step.id]?.suspendPayload?.__workflow_meta?.runId
1256
1182
  },
1257
1183
  [_constants.EMITTER_SYMBOL]: emitter,
1184
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
1258
1185
  engine: {
1259
1186
  step: this.inngestStep
1260
1187
  },
@@ -1282,7 +1209,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1282
1209
  };
1283
1210
  execResults = stepFailure;
1284
1211
  const fallbackErrorMessage = `Step ${step.id} failed`;
1285
- stepAISpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
1212
+ stepSpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
1286
1213
  throw new inngest.RetryAfterError(execResults.error ?? fallbackErrorMessage, executionContext.retryConfig.delay, {
1287
1214
  cause: execResults
1288
1215
  });
@@ -1306,24 +1233,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1306
1233
  startedAt
1307
1234
  };
1308
1235
  }
1309
- await emitter.emit("watch", {
1310
- type: "watch",
1311
- payload: {
1312
- currentStep: {
1313
- id: step.id,
1314
- ...execResults
1315
- },
1316
- workflowState: {
1317
- status: "running",
1318
- steps: { ...stepResults, [step.id]: execResults },
1319
- result: null,
1320
- error: null
1321
- }
1322
- },
1323
- eventTimestamp: Date.now()
1324
- });
1325
1236
  if (execResults.status === "suspended") {
1326
- await emitter.emit("watch-v2", {
1237
+ await emitter.emit("watch", {
1327
1238
  type: "workflow-step-suspended",
1328
1239
  payload: {
1329
1240
  id: step.id,
@@ -1331,14 +1242,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1331
1242
  }
1332
1243
  });
1333
1244
  } else {
1334
- await emitter.emit("watch-v2", {
1245
+ await emitter.emit("watch", {
1335
1246
  type: "workflow-step-result",
1336
1247
  payload: {
1337
1248
  id: step.id,
1338
1249
  ...execResults
1339
1250
  }
1340
1251
  });
1341
- await emitter.emit("watch-v2", {
1252
+ await emitter.emit("watch", {
1342
1253
  type: "workflow-step-finish",
1343
1254
  payload: {
1344
1255
  id: step.id,
@@ -1346,7 +1257,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1346
1257
  }
1347
1258
  });
1348
1259
  }
1349
- stepAISpan?.end({ output: execResults });
1260
+ stepSpan?.end({ output: execResults });
1350
1261
  return { result: execResults, executionContext, stepResults };
1351
1262
  });
1352
1263
  } catch (e) {
@@ -1376,9 +1287,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1376
1287
  output: stepRes.result,
1377
1288
  workflowId: executionContext.workflowId,
1378
1289
  stepId: step.id,
1379
- runtimeContext,
1290
+ requestContext,
1380
1291
  disableScorers,
1381
- tracingContext: { currentSpan: stepAISpan }
1292
+ tracingContext: { currentSpan: stepSpan }
1382
1293
  });
1383
1294
  }
1384
1295
  });
@@ -1439,13 +1350,13 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1439
1350
  executionContext,
1440
1351
  emitter,
1441
1352
  abortController,
1442
- runtimeContext,
1353
+ requestContext,
1443
1354
  writableStream,
1444
1355
  disableScorers,
1445
1356
  tracingContext
1446
1357
  }) {
1447
1358
  const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
1448
- type: aiTracing.AISpanType.WORKFLOW_CONDITIONAL,
1359
+ type: observability.SpanType.WORKFLOW_CONDITIONAL,
1449
1360
  name: `conditional: '${entry.conditions.length} conditions'`,
1450
1361
  input: prevOutput,
1451
1362
  attributes: {
@@ -1458,7 +1369,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1458
1369
  entry.conditions.map(
1459
1370
  (cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
1460
1371
  const evalSpan = conditionalSpan?.createChildSpan({
1461
- type: aiTracing.AISpanType.WORKFLOW_CONDITIONAL_EVAL,
1372
+ type: observability.SpanType.WORKFLOW_CONDITIONAL_EVAL,
1462
1373
  name: `condition: '${index}'`,
1463
1374
  input: prevOutput,
1464
1375
  attributes: {
@@ -1467,47 +1378,55 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1467
1378
  tracingPolicy: this.options?.tracingPolicy
1468
1379
  });
1469
1380
  try {
1470
- const result = await cond({
1471
- runId,
1472
- workflowId,
1473
- mastra: this.mastra,
1474
- runtimeContext,
1475
- runCount: -1,
1476
- inputData: prevOutput,
1477
- state: executionContext.state,
1478
- setState: (state) => {
1479
- executionContext.state = state;
1480
- },
1481
- tracingContext: {
1482
- currentSpan: evalSpan
1483
- },
1484
- getInitData: () => stepResults?.input,
1485
- getStepResult: workflows.getStepResult.bind(this, stepResults),
1486
- // TODO: this function shouldn't have suspend probably?
1487
- suspend: async (_suspendPayload) => {
1488
- },
1489
- bail: () => {
1490
- },
1491
- abort: () => {
1492
- abortController.abort();
1493
- },
1494
- [_constants.EMITTER_SYMBOL]: emitter,
1495
- [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
1496
- // TODO: add streamVNext support
1497
- engine: {
1498
- step: this.inngestStep
1499
- },
1500
- abortSignal: abortController.signal,
1501
- writer: new tools.ToolStream(
1381
+ const result = await cond(
1382
+ workflows.createDeprecationProxy(
1502
1383
  {
1503
- prefix: "workflow-step",
1504
- callId: crypto.randomUUID(),
1505
- name: "conditional",
1506
- runId
1384
+ runId,
1385
+ workflowId,
1386
+ mastra: this.mastra,
1387
+ requestContext,
1388
+ retryCount: -1,
1389
+ inputData: prevOutput,
1390
+ state: executionContext.state,
1391
+ setState: (state) => {
1392
+ executionContext.state = state;
1393
+ },
1394
+ tracingContext: {
1395
+ currentSpan: evalSpan
1396
+ },
1397
+ getInitData: () => stepResults?.input,
1398
+ getStepResult: workflows.getStepResult.bind(this, stepResults),
1399
+ // TODO: this function shouldn't have suspend probably?
1400
+ suspend: async (_suspendPayload) => {
1401
+ },
1402
+ bail: () => {
1403
+ },
1404
+ abort: () => {
1405
+ abortController.abort();
1406
+ },
1407
+ [_constants.EMITTER_SYMBOL]: emitter,
1408
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
1409
+ engine: {
1410
+ step: this.inngestStep
1411
+ },
1412
+ abortSignal: abortController.signal,
1413
+ writer: new tools.ToolStream(
1414
+ {
1415
+ prefix: "workflow-step",
1416
+ callId: crypto.randomUUID(),
1417
+ name: "conditional",
1418
+ runId
1419
+ },
1420
+ writableStream
1421
+ )
1507
1422
  },
1508
- writableStream
1423
+ {
1424
+ paramName: "runCount",
1425
+ deprecationMessage: workflows.runCountDeprecationMessage,
1426
+ logger: this.logger
1427
+ }
1509
1428
  )
1510
- });
1429
+ );
1511
1430
  evalSpan?.end({
1512
1431
  output: result,
1513
1432
  attributes: {
@@ -1552,12 +1471,11 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1552
1471
  suspendedPaths: executionContext.suspendedPaths,
1553
1472
  resumeLabels: executionContext.resumeLabels,
1554
1473
  retryConfig: executionContext.retryConfig,
1555
- executionSpan: executionContext.executionSpan,
1556
1474
  state: executionContext.state
1557
1475
  },
1558
1476
  emitter,
1559
1477
  abortController,
1560
- runtimeContext,
1478
+ requestContext,
1561
1479
  writableStream,
1562
1480
  disableScorers,
1563
1481
  tracingContext: {