@mastra/inngest 0.0.0-custom-instrumentation-20250626084921 → 0.0.0-fix-generate-title-20250616171351

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/src/index.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import { randomUUID } from 'crypto';
2
- import type { ReadableStream } from 'node:stream/web';
3
2
  import { subscribe } from '@inngest/realtime';
4
- import type { Agent, Mastra, ToolExecutionContext, WorkflowRun, WorkflowRuns } from '@mastra/core';
3
+ import { Agent, Tool } from '@mastra/core';
4
+ import type { Mastra, ToolExecutionContext, WorkflowRun, WorkflowRuns } from '@mastra/core';
5
5
  import { RuntimeContext } from '@mastra/core/di';
6
- import { Tool } from '@mastra/core/tools';
7
6
  import { Workflow, Run, DefaultExecutionEngine } from '@mastra/core/workflows';
8
7
  import type {
9
8
  ExecuteFunction,
@@ -17,9 +16,7 @@ import type {
17
16
  WorkflowResult,
18
17
  SerializedStepFlowEntry,
19
18
  StepFailure,
20
- Emitter,
21
19
  WatchEvent,
22
- StreamEvent,
23
20
  } from '@mastra/core/workflows';
24
21
  import { EMITTER_SYMBOL } from '@mastra/core/workflows/_constants';
25
22
  import type { Span } from '@opentelemetry/api';
@@ -90,8 +87,7 @@ export class InngestRun<
90
87
 
91
88
  async getRunOutput(eventId: string) {
92
89
  let runs = await this.getRuns(eventId);
93
-
94
- while (runs?.[0]?.status !== 'Completed' || runs?.[0]?.event_id !== eventId) {
90
+ while (runs?.[0]?.status !== 'Completed') {
95
91
  await new Promise(resolve => setTimeout(resolve, 1000));
96
92
  runs = await this.getRuns(eventId);
97
93
  if (runs?.[0]?.status === 'Failed' || runs?.[0]?.status === 'Cancelled') {
@@ -101,13 +97,6 @@ export class InngestRun<
101
97
  return runs?.[0];
102
98
  }
103
99
 
104
- async sendEvent(event: string, data: any) {
105
- await this.inngest.send({
106
- name: `user-event-${event}`,
107
- data,
108
- });
109
- }
110
-
111
100
  async start({
112
101
  inputData,
113
102
  }: {
@@ -159,27 +148,6 @@ export class InngestRun<
159
148
  | string
160
149
  | string[];
161
150
  runtimeContext?: RuntimeContext;
162
- }): Promise<WorkflowResult<TOutput, TSteps>> {
163
- const p = this._resume(params).then(result => {
164
- if (result.status !== 'suspended') {
165
- this.closeStreamAction?.().catch(() => {});
166
- }
167
-
168
- return result;
169
- });
170
-
171
- this.executionResults = p;
172
- return p;
173
- }
174
-
175
- async _resume<TResumeSchema extends z.ZodType<any>>(params: {
176
- resumeData?: z.infer<TResumeSchema>;
177
- step:
178
- | Step<string, any, any, TResumeSchema, any>
179
- | [...Step<string, any, any, any, any>[], Step<string, any, any, TResumeSchema, any>]
180
- | string
181
- | string[];
182
- runtimeContext?: RuntimeContext;
183
151
  }): Promise<WorkflowResult<TOutput, TSteps>> {
184
152
  const steps: string[] = (Array.isArray(params.step) ? params.step : [params.step]).map(step =>
185
153
  typeof step === 'string' ? step : step?.id,
@@ -217,72 +185,28 @@ export class InngestRun<
217
185
  return result;
218
186
  }
219
187
 
220
- watch(cb: (event: WatchEvent) => void, type: 'watch' | 'watch-v2' = 'watch'): () => void {
221
- let active = true;
188
+ watch(cb: (event: any) => void): () => void {
222
189
  const streamPromise = subscribe(
223
190
  {
224
191
  channel: `workflow:${this.workflowId}:${this.runId}`,
225
- topics: [type],
192
+ topics: ['watch'],
226
193
  app: this.inngest,
227
194
  },
228
195
  (message: any) => {
229
- if (active) {
230
- cb(message.data);
231
- }
196
+ cb(message.data);
232
197
  },
233
198
  );
234
199
 
235
200
  return () => {
236
- active = false;
237
201
  streamPromise
238
- .then(async (stream: Awaited<typeof streamPromise>) => {
239
- return stream.cancel();
202
+ .then((stream: any) => {
203
+ stream.cancel();
240
204
  })
241
205
  .catch(err => {
242
206
  console.error(err);
243
207
  });
244
208
  };
245
209
  }
246
-
247
- stream({ inputData, runtimeContext }: { inputData?: z.infer<TInput>; runtimeContext?: RuntimeContext } = {}): {
248
- stream: ReadableStream<StreamEvent>;
249
- getWorkflowState: () => Promise<WorkflowResult<TOutput, TSteps>>;
250
- } {
251
- const { readable, writable } = new TransformStream<StreamEvent, StreamEvent>();
252
-
253
- const writer = writable.getWriter();
254
- const unwatch = this.watch(async event => {
255
- try {
256
- // watch-v2 events are data stream events, so we need to cast them to the correct type
257
- await writer.write(event as any);
258
- } catch {}
259
- }, 'watch-v2');
260
-
261
- this.closeStreamAction = async () => {
262
- unwatch();
263
-
264
- try {
265
- await writer.close();
266
- } catch (err) {
267
- console.error('Error closing stream:', err);
268
- } finally {
269
- writer.releaseLock();
270
- }
271
- };
272
-
273
- this.executionResults = this.start({ inputData, runtimeContext }).then(result => {
274
- if (result.status !== 'suspended') {
275
- this.closeStreamAction?.().catch(() => {});
276
- }
277
-
278
- return result;
279
- });
280
-
281
- return {
282
- stream: readable as ReadableStream<StreamEvent>,
283
- getWorkflowState: () => this.executionResults!,
284
- };
285
- }
286
210
  }
287
211
 
288
212
  export class InngestWorkflow<
@@ -410,49 +334,6 @@ export class InngestWorkflow<
410
334
  return run;
411
335
  }
412
336
 
413
- async createRunAsync(options?: { runId?: string }): Promise<Run<TEngineType, TSteps, TInput, TOutput>> {
414
- const runIdToUse = options?.runId || randomUUID();
415
-
416
- // Return a new Run instance with object parameters
417
- const run: Run<TEngineType, TSteps, TInput, TOutput> =
418
- this.runs.get(runIdToUse) ??
419
- new InngestRun(
420
- {
421
- workflowId: this.id,
422
- runId: runIdToUse,
423
- executionEngine: this.executionEngine,
424
- executionGraph: this.executionGraph,
425
- serializedStepGraph: this.serializedStepGraph,
426
- mastra: this.#mastra,
427
- retryConfig: this.retryConfig,
428
- cleanup: () => this.runs.delete(runIdToUse),
429
- },
430
- this.inngest,
431
- );
432
-
433
- this.runs.set(runIdToUse, run);
434
-
435
- await this.mastra?.getStorage()?.persistWorkflowSnapshot({
436
- workflowName: this.id,
437
- runId: runIdToUse,
438
- snapshot: {
439
- runId: runIdToUse,
440
- status: 'pending',
441
- value: {},
442
- context: {},
443
- activePaths: [],
444
- serializedStepGraph: this.serializedStepGraph,
445
- suspendedPaths: {},
446
- result: undefined,
447
- error: undefined,
448
- // @ts-ignore
449
- timestamp: Date.now(),
450
- },
451
- });
452
-
453
- return run;
454
- }
455
-
456
337
  getFunction() {
457
338
  if (this.function) {
458
339
  return this.function;
@@ -479,22 +360,13 @@ export class InngestWorkflow<
479
360
  try {
480
361
  await publish({
481
362
  channel: `workflow:${this.id}:${runId}`,
482
- topic: event,
363
+ topic: 'watch',
483
364
  data,
484
365
  });
485
366
  } catch (err: any) {
486
367
  this.logger.error('Error emitting event: ' + (err?.stack ?? err?.message ?? err));
487
368
  }
488
369
  },
489
- on: (_event: string, _callback: (data: any) => void) => {
490
- // no-op
491
- },
492
- off: (_event: string, _callback: (data: any) => void) => {
493
- // no-op
494
- },
495
- once: (_event: string, _callback: (data: any) => void) => {
496
- // no-op
497
- },
498
370
  };
499
371
 
500
372
  const engine = new InngestExecutionEngine(this.#mastra, step, attempt);
@@ -536,14 +408,6 @@ export class InngestWorkflow<
536
408
  }
537
409
  }
538
410
 
539
- function isAgent(params: any): params is Agent<any, any, any> {
540
- return params?.component === 'AGENT';
541
- }
542
-
543
- function isTool(params: any): params is Tool<any, any, any> {
544
- return params instanceof Tool;
545
- }
546
-
547
411
  export function createStep<
548
412
  TStepId extends string,
549
413
  TStepInput extends z.ZodType<any>,
@@ -587,6 +451,7 @@ export function createStep<
587
451
  execute: (context: TContext) => Promise<any>;
588
452
  },
589
453
  ): Step<string, TSchemaIn, TSchemaOut, z.ZodType<any>, z.ZodType<any>, InngestEngineType>;
454
+
590
455
  export function createStep<
591
456
  TStepId extends string,
592
457
  TStepInput extends z.ZodType<any>,
@@ -617,7 +482,7 @@ export function createStep<
617
482
  execute: (context: ToolExecutionContext<TStepInput>) => Promise<any>;
618
483
  }),
619
484
  ): Step<TStepId, TStepInput, TStepOutput, TResumeSchema, TSuspendSchema, InngestEngineType> {
620
- if (isAgent(params)) {
485
+ if (params instanceof Agent) {
621
486
  return {
622
487
  id: params.name,
623
488
  // @ts-ignore
@@ -692,7 +557,7 @@ export function createStep<
692
557
  };
693
558
  }
694
559
 
695
- if (isTool(params)) {
560
+ if (params instanceof Tool) {
696
561
  if (!params.inputSchema || !params.outputSchema) {
697
562
  throw new Error('Tool must have input and output schemas defined');
698
563
  }
@@ -766,12 +631,11 @@ export function init(inngest: Inngest) {
766
631
  any,
767
632
  InngestEngineType
768
633
  >[],
769
- TPrevSchema extends z.ZodType<any> = TInput,
770
634
  >(
771
- workflow: Workflow<InngestEngineType, TSteps, string, TInput, TOutput, TPrevSchema>,
635
+ workflow: Workflow<InngestEngineType, TSteps, string, TInput, TOutput, TInput>,
772
636
  opts: { id: TWorkflowId },
773
- ): Workflow<InngestEngineType, TSteps, TWorkflowId, TInput, TOutput, TPrevSchema> {
774
- const wf: Workflow<InngestEngineType, TSteps, TWorkflowId, TInput, TOutput, TPrevSchema> = new Workflow({
637
+ ): Workflow<InngestEngineType, TSteps, TWorkflowId, TInput, TOutput, TInput> {
638
+ const wf = new Workflow({
775
639
  id: opts.id,
776
640
  inputSchema: workflow.inputSchema,
777
641
  outputSchema: workflow.outputSchema,
@@ -796,44 +660,9 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
796
660
  this.inngestAttempts = inngestAttempts;
797
661
  }
798
662
 
799
- async execute<TInput, TOutput>(params: {
800
- workflowId: string;
801
- runId: string;
802
- graph: ExecutionGraph;
803
- serializedStepGraph: SerializedStepFlowEntry[];
804
- input?: TInput;
805
- resume?: {
806
- // TODO: add execute path
807
- steps: string[];
808
- stepResults: Record<string, StepResult<any, any, any, any>>;
809
- resumePayload: any;
810
- resumePath: number[];
811
- };
812
- emitter: Emitter;
813
- retryConfig?: {
814
- attempts?: number;
815
- delay?: number;
816
- };
817
- runtimeContext: RuntimeContext;
818
- }): Promise<TOutput> {
819
- await params.emitter.emit('watch-v2', {
820
- type: 'start',
821
- payload: { runId: params.runId },
822
- });
823
-
824
- const result = await super.execute<TInput, TOutput>(params);
825
-
826
- await params.emitter.emit('watch-v2', {
827
- type: 'finish',
828
- payload: { runId: params.runId },
829
- });
830
-
831
- return result;
832
- }
833
-
834
663
  protected async fmtReturnValue<TOutput>(
835
664
  executionSpan: Span | undefined,
836
- emitter: Emitter,
665
+ emitter: { emit: (event: string, data: any) => Promise<void> },
837
666
  stepResults: Record<string, StepResult<any, any, any, any>>,
838
667
  lastOutput: StepResult<any, any, any, any>,
839
668
  error?: Error | string,
@@ -926,7 +755,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
926
755
  resumePayload: any;
927
756
  };
928
757
  prevOutput: any;
929
- emitter: Emitter;
758
+ emitter: { emit: (event: string, data: any) => Promise<void> };
930
759
  runtimeContext: RuntimeContext;
931
760
  }): Promise<StepResult<any, any, any, any>> {
932
761
  return super.executeStep({
@@ -946,19 +775,6 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
946
775
  await this.inngestStep.sleep(id, duration);
947
776
  }
948
777
 
949
- async executeWaitForEvent({ event, timeout }: { event: string; timeout?: number }): Promise<any> {
950
- const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
951
- event: `user-event-${event}`,
952
- timeout: timeout ?? 5e3,
953
- });
954
-
955
- if (eventData === null) {
956
- throw 'Timeout waiting for event';
957
- }
958
-
959
- return eventData?.data;
960
- }
961
-
962
778
  async executeStep({
963
779
  step,
964
780
  stepResults,
@@ -983,13 +799,12 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
983
799
  runId?: string;
984
800
  };
985
801
  prevOutput: any;
986
- emitter: Emitter;
802
+ emitter: { emit: (event: string, data: any) => Promise<void> };
987
803
  runtimeContext: RuntimeContext;
988
804
  }): Promise<StepResult<any, any, any, any>> {
989
- const startedAt = await this.inngestStep.run(
805
+ await this.inngestStep.run(
990
806
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
991
807
  async () => {
992
- const startedAt = Date.now();
993
808
  await emitter.emit('watch', {
994
809
  type: 'watch',
995
810
  payload: {
@@ -1011,15 +826,6 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1011
826
  },
1012
827
  eventTimestamp: Date.now(),
1013
828
  });
1014
-
1015
- await emitter.emit('watch-v2', {
1016
- type: 'step-start',
1017
- payload: {
1018
- id: step.id,
1019
- },
1020
- });
1021
-
1022
- return startedAt;
1023
829
  },
1024
830
  );
1025
831
 
@@ -1086,14 +892,6 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1086
892
  eventTimestamp: Date.now(),
1087
893
  });
1088
894
 
1089
- await emitter.emit('watch-v2', {
1090
- type: 'step-result',
1091
- payload: {
1092
- id: step.id,
1093
- status: 'failed',
1094
- },
1095
- });
1096
-
1097
895
  return { executionContext, result: { status: 'failed', error: result?.error } };
1098
896
  } else if (result.status === 'suspended') {
1099
897
  const suspendedSteps = Object.entries(result.steps).filter(([_stepName, stepResult]) => {
@@ -1124,13 +922,6 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1124
922
  eventTimestamp: Date.now(),
1125
923
  });
1126
924
 
1127
- await emitter.emit('watch-v2', {
1128
- type: 'step-suspended',
1129
- payload: {
1130
- id: step.id,
1131
- },
1132
- });
1133
-
1134
925
  return {
1135
926
  executionContext,
1136
927
  result: {
@@ -1187,14 +978,6 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1187
978
  eventTimestamp: Date.now(),
1188
979
  });
1189
980
 
1190
- await emitter.emit('watch-v2', {
1191
- type: 'step-finish',
1192
- payload: {
1193
- id: step.id,
1194
- metadata: {},
1195
- },
1196
- });
1197
-
1198
981
  return { executionContext, result: { status: 'success', output: result?.result } };
1199
982
  },
1200
983
  );
@@ -1206,8 +989,6 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1206
989
  const stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
1207
990
  let execResults: any;
1208
991
  let suspended: { payload: any } | undefined;
1209
- let bailed: { payload: any } | undefined;
1210
-
1211
992
  try {
1212
993
  const result = await step.execute({
1213
994
  runId: executionContext.runId,
@@ -1228,9 +1009,6 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1228
1009
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
1229
1010
  suspended = { payload: suspendPayload };
1230
1011
  },
1231
- bail: (result: any) => {
1232
- bailed = { payload: result };
1233
- },
1234
1012
  resume: {
1235
1013
  steps: resume?.steps?.slice(1) || [],
1236
1014
  resumePayload: resume?.resumePayload,
@@ -1242,41 +1020,14 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1242
1020
  step: this.inngestStep,
1243
1021
  },
1244
1022
  });
1245
- const endedAt = Date.now();
1246
-
1247
- execResults = {
1248
- status: 'success',
1249
- output: result,
1250
- startedAt,
1251
- endedAt,
1252
- payload: prevOutput,
1253
- resumedAt: resume?.steps[0] === step.id ? startedAt : undefined,
1254
- resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : undefined,
1255
- };
1023
+
1024
+ execResults = { status: 'success', output: result };
1256
1025
  } catch (e) {
1257
- execResults = {
1258
- status: 'failed',
1259
- payload: prevOutput,
1260
- error: e instanceof Error ? e.message : String(e),
1261
- endedAt: Date.now(),
1262
- startedAt,
1263
- resumedAt: resume?.steps[0] === step.id ? startedAt : undefined,
1264
- resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : undefined,
1265
- };
1026
+ execResults = { status: 'failed', error: e instanceof Error ? e.message : String(e) };
1266
1027
  }
1267
1028
 
1268
1029
  if (suspended) {
1269
- execResults = {
1270
- status: 'suspended',
1271
- suspendedPayload: suspended.payload,
1272
- payload: prevOutput,
1273
- suspendedAt: Date.now(),
1274
- startedAt,
1275
- resumedAt: resume?.steps[0] === step.id ? startedAt : undefined,
1276
- resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : undefined,
1277
- };
1278
- } else if (bailed) {
1279
- execResults = { status: 'bailed', output: bailed.payload, payload: prevOutput, endedAt: Date.now(), startedAt };
1030
+ execResults = { status: 'suspended', payload: suspended.payload };
1280
1031
  }
1281
1032
 
1282
1033
  if (execResults.status === 'failed') {
@@ -1290,11 +1041,12 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1290
1041
  payload: {
1291
1042
  currentStep: {
1292
1043
  id: step.id,
1293
- ...execResults,
1044
+ status: execResults.status,
1045
+ output: execResults.output,
1294
1046
  },
1295
1047
  workflowState: {
1296
1048
  status: 'running',
1297
- steps: { ...stepResults, [step.id]: execResults },
1049
+ steps: stepResults,
1298
1050
  result: null,
1299
1051
  error: null,
1300
1052
  },
@@ -1302,34 +1054,6 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1302
1054
  eventTimestamp: Date.now(),
1303
1055
  });
1304
1056
 
1305
- if (execResults.status === 'suspended') {
1306
- await emitter.emit('watch-v2', {
1307
- type: 'step-suspended',
1308
- payload: {
1309
- id: step.id,
1310
- status: execResults.status,
1311
- output: execResults.status === 'success' ? execResults?.output : undefined,
1312
- },
1313
- });
1314
- } else {
1315
- await emitter.emit('watch-v2', {
1316
- type: 'step-result',
1317
- payload: {
1318
- id: step.id,
1319
- status: execResults.status,
1320
- output: execResults.status === 'success' ? execResults?.output : undefined,
1321
- },
1322
- });
1323
-
1324
- await emitter.emit('watch-v2', {
1325
- type: 'step-finish',
1326
- payload: {
1327
- id: step.id,
1328
- metadata: {},
1329
- },
1330
- });
1331
- }
1332
-
1333
1057
  return { result: execResults, executionContext, stepResults };
1334
1058
  });
1335
1059
 
@@ -1416,7 +1140,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1416
1140
  resumePath: number[];
1417
1141
  };
1418
1142
  executionContext: ExecutionContext;
1419
- emitter: Emitter;
1143
+ emitter: { emit: (event: string, data: any) => Promise<void> };
1420
1144
  runtimeContext: RuntimeContext;
1421
1145
  }): Promise<StepResult<any, any, any, any>> {
1422
1146
  let execResults: any;
@@ -1446,7 +1170,6 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1446
1170
 
1447
1171
  // TODO: this function shouldn't have suspend probably?
1448
1172
  suspend: async (_suspendPayload: any) => {},
1449
- bail: () => {},
1450
1173
  [EMITTER_SYMBOL]: emitter,
1451
1174
  engine: {
1452
1175
  step: this.inngestStep,
@@ -1493,7 +1216,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1493
1216
  if (hasFailed) {
1494
1217
  execResults = { status: 'failed', error: hasFailed.result.error };
1495
1218
  } else if (hasSuspended) {
1496
- execResults = { status: 'suspended', payload: hasSuspended.result.suspendPayload };
1219
+ execResults = { status: 'suspended', payload: hasSuspended.result.payload };
1497
1220
  } else {
1498
1221
  execResults = {
1499
1222
  status: 'success',