@mastra/inngest 0.0.0-stream-vnext-usage-20250908171242 → 0.0.0-suspendRuntimeContextTypeFix-20250930142630

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.js CHANGED
@@ -3,15 +3,20 @@ import { subscribe } from '@inngest/realtime';
3
3
  import { wrapMastra, AISpanType } from '@mastra/core/ai-tracing';
4
4
  import { RuntimeContext } from '@mastra/core/di';
5
5
  import { ToolStream, Tool } from '@mastra/core/tools';
6
- import { Run, Workflow, DefaultExecutionEngine } from '@mastra/core/workflows';
6
+ import { Run, Workflow, DefaultExecutionEngine, getStepResult, validateStepInput } from '@mastra/core/workflows';
7
7
  import { EMITTER_SYMBOL, STREAM_FORMAT_SYMBOL } from '@mastra/core/workflows/_constants';
8
8
  import { serve as serve$1 } from 'inngest/hono';
9
9
  import { z } from 'zod';
10
10
 
11
11
  // src/index.ts
12
- function serve({ mastra, inngest }) {
12
+ function serve({
13
+ mastra,
14
+ inngest,
15
+ functions: userFunctions = [],
16
+ registerOptions
17
+ }) {
13
18
  const wfs = mastra.getWorkflows();
14
- const functions = Array.from(
19
+ const workflowFunctions = Array.from(
15
20
  new Set(
16
21
  Object.values(wfs).flatMap((wf) => {
17
22
  if (wf instanceof InngestWorkflow) {
@@ -23,8 +28,9 @@ function serve({ mastra, inngest }) {
23
28
  )
24
29
  );
25
30
  return serve$1({
31
+ ...registerOptions,
26
32
  client: inngest,
27
- functions
33
+ functions: [...workflowFunctions, ...userFunctions]
28
34
  });
29
35
  }
30
36
  var InngestRun = class extends Run {
@@ -52,7 +58,6 @@ var InngestRun = class extends Run {
52
58
  await new Promise((resolve) => setTimeout(resolve, 1e3));
53
59
  runs = await this.getRuns(eventId);
54
60
  if (runs?.[0]?.status === "Failed") {
55
- console.log("run", runs?.[0]);
56
61
  throw new Error(`Function run ${runs?.[0]?.status}`);
57
62
  } else if (runs?.[0]?.status === "Cancelled") {
58
63
  const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
@@ -85,6 +90,7 @@ var InngestRun = class extends Run {
85
90
  await this.#mastra?.storage?.persistWorkflowSnapshot({
86
91
  workflowName: this.workflowId,
87
92
  runId: this.runId,
93
+ resourceId: this.resourceId,
88
94
  snapshot: {
89
95
  ...snapshot,
90
96
  status: "canceled"
@@ -98,6 +104,7 @@ var InngestRun = class extends Run {
98
104
  await this.#mastra.getStorage()?.persistWorkflowSnapshot({
99
105
  workflowName: this.workflowId,
100
106
  runId: this.runId,
107
+ resourceId: this.resourceId,
101
108
  snapshot: {
102
109
  runId: this.runId,
103
110
  serializedStepGraph: this.serializedStepGraph,
@@ -110,11 +117,13 @@ var InngestRun = class extends Run {
110
117
  status: "running"
111
118
  }
112
119
  });
120
+ const inputDataToUse = await this._validateInput(inputData);
113
121
  const eventOutput = await this.inngest.send({
114
122
  name: `workflow.${this.workflowId}`,
115
123
  data: {
116
- inputData,
117
- runId: this.runId
124
+ inputData: inputDataToUse,
125
+ runId: this.runId,
126
+ resourceId: this.resourceId
118
127
  }
119
128
  });
120
129
  const eventId = eventOutput.ids[0];
@@ -150,17 +159,19 @@ var InngestRun = class extends Run {
150
159
  workflowName: this.workflowId,
151
160
  runId: this.runId
152
161
  });
162
+ const suspendedStep = this.workflowSteps[steps?.[0] ?? ""];
163
+ const resumeDataToUse = await this._validateResumeData(params.resumeData, suspendedStep);
153
164
  const eventOutput = await this.inngest.send({
154
165
  name: `workflow.${this.workflowId}`,
155
166
  data: {
156
- inputData: params.resumeData,
167
+ inputData: resumeDataToUse,
157
168
  runId: this.runId,
158
169
  workflowId: this.workflowId,
159
170
  stepResults: snapshot?.context,
160
171
  resume: {
161
172
  steps,
162
173
  stepResults: snapshot?.context,
163
- resumePayload: params.resumeData,
174
+ resumePayload: resumeDataToUse,
164
175
  // @ts-ignore
165
176
  resumePath: snapshot?.suspendedPaths?.[steps?.[0]]
166
177
  }
@@ -202,33 +213,9 @@ var InngestRun = class extends Run {
202
213
  }
203
214
  stream({ inputData, runtimeContext } = {}) {
204
215
  const { readable, writable } = new TransformStream();
205
- let currentToolData = void 0;
206
216
  const writer = writable.getWriter();
207
217
  const unwatch = this.watch(async (event) => {
208
- if (event.type === "workflow-agent-call-start") {
209
- currentToolData = {
210
- name: event.payload.name,
211
- args: event.payload.args
212
- };
213
- await writer.write({
214
- ...event.payload,
215
- type: "tool-call-streaming-start"
216
- });
217
- return;
218
- }
219
218
  try {
220
- if (event.type === "workflow-agent-call-finish") {
221
- return;
222
- } else if (!event.type.startsWith("workflow-")) {
223
- if (event.type === "text-delta") {
224
- await writer.write({
225
- type: "tool-call-delta",
226
- ...currentToolData ?? {},
227
- argsTextDelta: event.textDelta
228
- });
229
- }
230
- return;
231
- }
232
219
  const e = {
233
220
  ...event,
234
221
  type: event.type.replace("workflow-", "")
@@ -310,23 +297,14 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
310
297
  }
311
298
  }
312
299
  }
313
- createRun(options) {
314
- const runIdToUse = options?.runId || randomUUID();
315
- const run = this.runs.get(runIdToUse) ?? new InngestRun(
316
- {
317
- workflowId: this.id,
318
- runId: runIdToUse,
319
- executionEngine: this.executionEngine,
320
- executionGraph: this.executionGraph,
321
- serializedStepGraph: this.serializedStepGraph,
322
- mastra: this.#mastra,
323
- retryConfig: this.retryConfig,
324
- cleanup: () => this.runs.delete(runIdToUse)
325
- },
326
- this.inngest
300
+ /**
301
+ * @deprecated Use createRunAsync() instead.
302
+ * @throws {Error} Always throws an error directing users to use createRunAsync()
303
+ */
304
+ createRun(_options) {
305
+ throw new Error(
306
+ "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."
327
307
  );
328
- this.runs.set(runIdToUse, run);
329
- return run;
330
308
  }
331
309
  async createRunAsync(options) {
332
310
  const runIdToUse = options?.runId || randomUUID();
@@ -334,12 +312,14 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
334
312
  {
335
313
  workflowId: this.id,
336
314
  runId: runIdToUse,
315
+ resourceId: options?.resourceId,
337
316
  executionEngine: this.executionEngine,
338
317
  executionGraph: this.executionGraph,
339
318
  serializedStepGraph: this.serializedStepGraph,
340
319
  mastra: this.#mastra,
341
320
  retryConfig: this.retryConfig,
342
- cleanup: () => this.runs.delete(runIdToUse)
321
+ cleanup: () => this.runs.delete(runIdToUse),
322
+ workflowSteps: this.steps
343
323
  },
344
324
  this.inngest
345
325
  );
@@ -349,6 +329,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
349
329
  await this.mastra?.getStorage()?.persistWorkflowSnapshot({
350
330
  workflowName: this.id,
351
331
  runId: runIdToUse,
332
+ resourceId: options?.resourceId,
352
333
  snapshot: {
353
334
  runId: runIdToUse,
354
335
  status: "pending",
@@ -382,7 +363,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
382
363
  },
383
364
  { event: `workflow.${this.id}` },
384
365
  async ({ event, step, attempt, publish }) => {
385
- let { inputData, runId, resume } = event.data;
366
+ let { inputData, runId, resourceId, resume } = event.data;
386
367
  if (!runId) {
387
368
  runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
388
369
  return randomUUID();
@@ -414,6 +395,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
414
395
  const result = await engine.execute({
415
396
  workflowId: this.id,
416
397
  runId,
398
+ resourceId,
417
399
  graph: this.executionGraph,
418
400
  serializedStepGraph: this.serializedStepGraph,
419
401
  input: inputData,
@@ -458,11 +440,10 @@ function createStep(params) {
458
440
  if (isAgent(params)) {
459
441
  return {
460
442
  id: params.name,
443
+ description: params.getDescription(),
461
444
  // @ts-ignore
462
445
  inputSchema: z.object({
463
446
  prompt: z.string()
464
- // resourceId: z.string().optional(),
465
- // threadId: z.string().optional(),
466
447
  }),
467
448
  // @ts-ignore
468
449
  outputSchema: z.object({
@@ -478,13 +459,7 @@ function createStep(params) {
478
459
  name: params.name,
479
460
  args: inputData
480
461
  };
481
- await emitter.emit("watch-v2", {
482
- type: "workflow-agent-call-start",
483
- payload: toolData
484
- });
485
462
  const { fullStream } = await params.stream(inputData.prompt, {
486
- // resourceId: inputData.resourceId,
487
- // threadId: inputData.threadId,
488
463
  runtimeContext,
489
464
  tracingContext,
490
465
  onFinish: (result) => {
@@ -495,17 +470,28 @@ function createStep(params) {
495
470
  if (abortSignal.aborted) {
496
471
  return abort();
497
472
  }
473
+ await emitter.emit("watch-v2", {
474
+ type: "tool-call-streaming-start",
475
+ ...toolData ?? {}
476
+ });
498
477
  for await (const chunk of fullStream) {
499
- await emitter.emit("watch-v2", chunk);
478
+ if (chunk.type === "text-delta") {
479
+ await emitter.emit("watch-v2", {
480
+ type: "tool-call-delta",
481
+ ...toolData ?? {},
482
+ argsTextDelta: chunk.textDelta
483
+ });
484
+ }
500
485
  }
501
486
  await emitter.emit("watch-v2", {
502
- type: "workflow-agent-call-finish",
503
- payload: toolData
487
+ type: "tool-call-streaming-finish",
488
+ ...toolData ?? {}
504
489
  });
505
490
  return {
506
491
  text: await streamPromise.promise
507
492
  };
508
- }
493
+ },
494
+ component: params.component
509
495
  };
510
496
  }
511
497
  if (isTool(params)) {
@@ -516,16 +502,20 @@ function createStep(params) {
516
502
  // TODO: tool probably should have strong id type
517
503
  // @ts-ignore
518
504
  id: params.id,
505
+ description: params.description,
519
506
  inputSchema: params.inputSchema,
520
507
  outputSchema: params.outputSchema,
521
- execute: async ({ inputData, mastra, runtimeContext, tracingContext }) => {
508
+ execute: async ({ inputData, mastra, runtimeContext, tracingContext, suspend, resumeData }) => {
522
509
  return params.execute({
523
510
  context: inputData,
524
511
  mastra: wrapMastra(mastra, tracingContext),
525
512
  runtimeContext,
526
- tracingContext
513
+ tracingContext,
514
+ suspend,
515
+ resumeData
527
516
  });
528
- }
517
+ },
518
+ component: "TOOL"
529
519
  };
530
520
  }
531
521
  return {
@@ -550,7 +540,8 @@ function init(inngest) {
550
540
  description: step.description,
551
541
  inputSchema: step.inputSchema,
552
542
  outputSchema: step.outputSchema,
553
- execute: step.execute
543
+ execute: step.execute,
544
+ component: step.component
554
545
  };
555
546
  },
556
547
  cloneWorkflow(workflow, opts) {
@@ -570,8 +561,8 @@ function init(inngest) {
570
561
  var InngestExecutionEngine = class extends DefaultExecutionEngine {
571
562
  inngestStep;
572
563
  inngestAttempts;
573
- constructor(mastra, inngestStep, inngestAttempts = 0) {
574
- super({ mastra });
564
+ constructor(mastra, inngestStep, inngestAttempts = 0, options) {
565
+ super({ mastra, options });
575
566
  this.inngestStep = inngestStep;
576
567
  this.inngestAttempts = inngestAttempts;
577
568
  }
@@ -667,7 +658,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
667
658
  attributes: {
668
659
  durationMs: duration,
669
660
  sleepType: fn ? "dynamic" : "fixed"
670
- }
661
+ },
662
+ tracingPolicy: this.options?.tracingPolicy
671
663
  });
672
664
  if (fn) {
673
665
  const stepCallId = randomUUID();
@@ -683,16 +675,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
683
675
  currentSpan: sleepSpan
684
676
  },
685
677
  getInitData: () => stepResults?.input,
686
- getStepResult: (step) => {
687
- if (!step?.id) {
688
- return null;
689
- }
690
- const result = stepResults[step.id];
691
- if (result?.status === "success") {
692
- return result.output;
693
- }
694
- return null;
695
- },
678
+ getStepResult: getStepResult.bind(this, stepResults),
696
679
  // TODO: this function shouldn't have suspend probably?
697
680
  suspend: async (_suspendPayload) => {
698
681
  },
@@ -752,7 +735,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
752
735
  untilDate: date,
753
736
  durationMs: date ? Math.max(0, date.getTime() - Date.now()) : void 0,
754
737
  sleepType: fn ? "dynamic" : "fixed"
755
- }
738
+ },
739
+ tracingPolicy: this.options?.tracingPolicy
756
740
  });
757
741
  if (fn) {
758
742
  date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
@@ -768,16 +752,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
768
752
  currentSpan: sleepUntilSpan
769
753
  },
770
754
  getInitData: () => stepResults?.input,
771
- getStepResult: (step) => {
772
- if (!step?.id) {
773
- return null;
774
- }
775
- const result = stepResults[step.id];
776
- if (result?.status === "success") {
777
- return result.output;
778
- }
779
- return null;
780
- },
755
+ getStepResult: getStepResult.bind(this, stepResults),
781
756
  // TODO: this function shouldn't have suspend probably?
782
757
  suspend: async (_suspendPayload) => {
783
758
  },
@@ -802,6 +777,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
802
777
  )
803
778
  });
804
779
  });
780
+ if (date && !(date instanceof Date)) {
781
+ date = new Date(date);
782
+ }
805
783
  const time = !date ? 0 : date.getTime() - Date.now();
806
784
  sleepUntilSpan?.update({
807
785
  attributes: {
@@ -850,7 +828,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
850
828
  input: prevOutput,
851
829
  attributes: {
852
830
  stepId: step.id
853
- }
831
+ },
832
+ tracingPolicy: this.options?.tracingPolicy
833
+ });
834
+ const { inputData, validationError } = await validateStepInput({
835
+ prevOutput,
836
+ step,
837
+ validateInputs: this.options?.validateInputs ?? false
854
838
  });
855
839
  const startedAt = await this.inngestStep.run(
856
840
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
@@ -882,7 +866,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
882
866
  payload: {
883
867
  id: step.id,
884
868
  status: "running",
885
- payload: prevOutput,
869
+ payload: inputData,
886
870
  startedAt: startedAt2
887
871
  }
888
872
  });
@@ -902,7 +886,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
902
886
  const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
903
887
  function: step.getFunction(),
904
888
  data: {
905
- inputData: prevOutput,
889
+ inputData,
906
890
  runId,
907
891
  resume: {
908
892
  runId,
@@ -920,7 +904,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
920
904
  const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
921
905
  function: step.getFunction(),
922
906
  data: {
923
- inputData: prevOutput
907
+ inputData
924
908
  }
925
909
  });
926
910
  result = invokeResp.result;
@@ -1065,24 +1049,21 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1065
1049
  let suspended;
1066
1050
  let bailed;
1067
1051
  try {
1052
+ if (validationError) {
1053
+ throw validationError;
1054
+ }
1068
1055
  const result = await step.execute({
1069
1056
  runId: executionContext.runId,
1070
1057
  mastra: this.mastra,
1071
1058
  runtimeContext,
1072
1059
  writableStream,
1073
- inputData: prevOutput,
1060
+ inputData,
1074
1061
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
1075
1062
  tracingContext: {
1076
1063
  currentSpan: stepAISpan
1077
1064
  },
1078
1065
  getInitData: () => stepResults?.input,
1079
- getStepResult: (step2) => {
1080
- const result2 = stepResults[step2.id];
1081
- if (result2?.status === "success") {
1082
- return result2.output;
1083
- }
1084
- return null;
1085
- },
1066
+ getStepResult: getStepResult.bind(this, stepResults),
1086
1067
  suspend: async (suspendPayload) => {
1087
1068
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
1088
1069
  suspended = { payload: suspendPayload };
@@ -1108,14 +1089,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1108
1089
  output: result,
1109
1090
  startedAt,
1110
1091
  endedAt,
1111
- payload: prevOutput,
1092
+ payload: inputData,
1112
1093
  resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1113
1094
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1114
1095
  };
1115
1096
  } catch (e) {
1116
1097
  execResults = {
1117
1098
  status: "failed",
1118
- payload: prevOutput,
1099
+ payload: inputData,
1119
1100
  error: e instanceof Error ? e.message : String(e),
1120
1101
  endedAt: Date.now(),
1121
1102
  startedAt,
@@ -1127,14 +1108,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1127
1108
  execResults = {
1128
1109
  status: "suspended",
1129
1110
  suspendedPayload: suspended.payload,
1130
- payload: prevOutput,
1111
+ payload: inputData,
1131
1112
  suspendedAt: Date.now(),
1132
1113
  startedAt,
1133
1114
  resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1134
1115
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1135
1116
  };
1136
1117
  } else if (bailed) {
1137
- execResults = { status: "bailed", output: bailed.payload, payload: prevOutput, endedAt: Date.now(), startedAt };
1118
+ execResults = { status: "bailed", output: bailed.payload, payload: inputData, endedAt: Date.now(), startedAt };
1138
1119
  }
1139
1120
  if (execResults.status === "failed") {
1140
1121
  if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
@@ -1192,7 +1173,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1192
1173
  await this.runScorers({
1193
1174
  scorers: step.scorers,
1194
1175
  runId: executionContext.runId,
1195
- input: prevOutput,
1176
+ input: inputData,
1196
1177
  output: stepRes.result,
1197
1178
  workflowId: executionContext.workflowId,
1198
1179
  stepId: step.id,
@@ -1211,6 +1192,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1211
1192
  workflowId,
1212
1193
  runId,
1213
1194
  stepResults,
1195
+ resourceId,
1214
1196
  executionContext,
1215
1197
  serializedStepGraph,
1216
1198
  workflowStatus,
@@ -1223,6 +1205,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1223
1205
  await this.mastra?.getStorage()?.persistWorkflowSnapshot({
1224
1206
  workflowName: workflowId,
1225
1207
  runId,
1208
+ resourceId,
1226
1209
  snapshot: {
1227
1210
  runId,
1228
1211
  value: {},
@@ -1260,11 +1243,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1260
1243
  }) {
1261
1244
  const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
1262
1245
  type: AISpanType.WORKFLOW_CONDITIONAL,
1263
- name: `conditional: ${entry.conditions.length} conditions`,
1246
+ name: `conditional: '${entry.conditions.length} conditions'`,
1264
1247
  input: prevOutput,
1265
1248
  attributes: {
1266
1249
  conditionCount: entry.conditions.length
1267
- }
1250
+ },
1251
+ tracingPolicy: this.options?.tracingPolicy
1268
1252
  });
1269
1253
  let execResults;
1270
1254
  const truthyIndexes = (await Promise.all(
@@ -1272,11 +1256,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1272
1256
  (cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
1273
1257
  const evalSpan = conditionalSpan?.createChildSpan({
1274
1258
  type: AISpanType.WORKFLOW_CONDITIONAL_EVAL,
1275
- name: `condition ${index}`,
1259
+ name: `condition: '${index}'`,
1276
1260
  input: prevOutput,
1277
1261
  attributes: {
1278
1262
  conditionIndex: index
1279
- }
1263
+ },
1264
+ tracingPolicy: this.options?.tracingPolicy
1280
1265
  });
1281
1266
  try {
1282
1267
  const result = await cond({
@@ -1290,16 +1275,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1290
1275
  currentSpan: evalSpan
1291
1276
  },
1292
1277
  getInitData: () => stepResults?.input,
1293
- getStepResult: (step) => {
1294
- if (!step?.id) {
1295
- return null;
1296
- }
1297
- const result2 = stepResults[step.id];
1298
- if (result2?.status === "success") {
1299
- return result2.output;
1300
- }
1301
- return null;
1302
- },
1278
+ getStepResult: getStepResult.bind(this, stepResults),
1303
1279
  // TODO: this function shouldn't have suspend probably?
1304
1280
  suspend: async (_suspendPayload) => {
1305
1281
  },