@mastra/inngest 0.0.0-fix-generate-title-20250616171351 → 0.0.0-fix-fetch-workflow-runs-20250624231457

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
@@ -1,7 +1,7 @@
1
1
  import { randomUUID } from 'crypto';
2
2
  import { subscribe } from '@inngest/realtime';
3
- import { Agent, Tool } from '@mastra/core';
4
3
  import { RuntimeContext } from '@mastra/core/di';
4
+ import { Tool } from '@mastra/core/tools';
5
5
  import { Run, Workflow, DefaultExecutionEngine } from '@mastra/core/workflows';
6
6
  import { EMITTER_SYMBOL } from '@mastra/core/workflows/_constants';
7
7
  import { serve as serve$1 } from 'inngest/hono';
@@ -43,7 +43,7 @@ var InngestRun = class extends Run {
43
43
  }
44
44
  async getRunOutput(eventId) {
45
45
  let runs = await this.getRuns(eventId);
46
- while (runs?.[0]?.status !== "Completed") {
46
+ while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
47
47
  await new Promise((resolve) => setTimeout(resolve, 1e3));
48
48
  runs = await this.getRuns(eventId);
49
49
  if (runs?.[0]?.status === "Failed" || runs?.[0]?.status === "Cancelled") {
@@ -52,6 +52,12 @@ var InngestRun = class extends Run {
52
52
  }
53
53
  return runs?.[0];
54
54
  }
55
+ async sendEvent(event, data) {
56
+ await this.inngest.send({
57
+ name: `user-event-${event}`,
58
+ data
59
+ });
60
+ }
55
61
  async start({
56
62
  inputData
57
63
  }) {
@@ -89,6 +95,17 @@ var InngestRun = class extends Run {
89
95
  return result;
90
96
  }
91
97
  async resume(params) {
98
+ const p = this._resume(params).then((result) => {
99
+ if (result.status !== "suspended") {
100
+ this.closeStreamAction?.().catch(() => {
101
+ });
102
+ }
103
+ return result;
104
+ });
105
+ this.executionResults = p;
106
+ return p;
107
+ }
108
+ async _resume(params) {
92
109
  const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
93
110
  (step) => typeof step === "string" ? step : step?.id
94
111
  );
@@ -122,25 +139,60 @@ var InngestRun = class extends Run {
122
139
  }
123
140
  return result;
124
141
  }
125
- watch(cb) {
142
+ watch(cb, type = "watch") {
143
+ let active = true;
126
144
  const streamPromise = subscribe(
127
145
  {
128
146
  channel: `workflow:${this.workflowId}:${this.runId}`,
129
- topics: ["watch"],
147
+ topics: [type],
130
148
  app: this.inngest
131
149
  },
132
150
  (message) => {
133
- cb(message.data);
151
+ if (active) {
152
+ cb(message.data);
153
+ }
134
154
  }
135
155
  );
136
156
  return () => {
137
- streamPromise.then((stream) => {
138
- stream.cancel();
157
+ active = false;
158
+ streamPromise.then(async (stream) => {
159
+ return stream.cancel();
139
160
  }).catch((err) => {
140
161
  console.error(err);
141
162
  });
142
163
  };
143
164
  }
165
+ stream({ inputData, runtimeContext } = {}) {
166
+ const { readable, writable } = new TransformStream();
167
+ const writer = writable.getWriter();
168
+ const unwatch = this.watch(async (event) => {
169
+ try {
170
+ await writer.write(event);
171
+ } catch {
172
+ }
173
+ }, "watch-v2");
174
+ this.closeStreamAction = async () => {
175
+ unwatch();
176
+ try {
177
+ await writer.close();
178
+ } catch (err) {
179
+ console.error("Error closing stream:", err);
180
+ } finally {
181
+ writer.releaseLock();
182
+ }
183
+ };
184
+ this.executionResults = this.start({ inputData, runtimeContext }).then((result) => {
185
+ if (result.status !== "suspended") {
186
+ this.closeStreamAction?.().catch(() => {
187
+ });
188
+ }
189
+ return result;
190
+ });
191
+ return {
192
+ stream: readable,
193
+ getWorkflowState: () => this.executionResults
194
+ };
195
+ }
144
196
  };
145
197
  var InngestWorkflow = class _InngestWorkflow extends Workflow {
146
198
  #mastra;
@@ -225,6 +277,41 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
225
277
  this.runs.set(runIdToUse, run);
226
278
  return run;
227
279
  }
280
+ async createRunAsync(options) {
281
+ const runIdToUse = options?.runId || randomUUID();
282
+ const run = this.runs.get(runIdToUse) ?? new InngestRun(
283
+ {
284
+ workflowId: this.id,
285
+ runId: runIdToUse,
286
+ executionEngine: this.executionEngine,
287
+ executionGraph: this.executionGraph,
288
+ serializedStepGraph: this.serializedStepGraph,
289
+ mastra: this.#mastra,
290
+ retryConfig: this.retryConfig,
291
+ cleanup: () => this.runs.delete(runIdToUse)
292
+ },
293
+ this.inngest
294
+ );
295
+ this.runs.set(runIdToUse, run);
296
+ await this.mastra?.getStorage()?.persistWorkflowSnapshot({
297
+ workflowName: this.id,
298
+ runId: runIdToUse,
299
+ snapshot: {
300
+ runId: runIdToUse,
301
+ status: "pending",
302
+ value: {},
303
+ context: {},
304
+ activePaths: [],
305
+ serializedStepGraph: this.serializedStepGraph,
306
+ suspendedPaths: {},
307
+ result: void 0,
308
+ error: void 0,
309
+ // @ts-ignore
310
+ timestamp: Date.now()
311
+ }
312
+ });
313
+ return run;
314
+ }
228
315
  getFunction() {
229
316
  if (this.function) {
230
317
  return this.function;
@@ -248,12 +335,18 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
248
335
  try {
249
336
  await publish({
250
337
  channel: `workflow:${this.id}:${runId}`,
251
- topic: "watch",
338
+ topic: event2,
252
339
  data
253
340
  });
254
341
  } catch (err) {
255
342
  this.logger.error("Error emitting event: " + (err?.stack ?? err?.message ?? err));
256
343
  }
344
+ },
345
+ on: (_event, _callback) => {
346
+ },
347
+ off: (_event, _callback) => {
348
+ },
349
+ once: (_event, _callback) => {
257
350
  }
258
351
  };
259
352
  const engine = new InngestExecutionEngine(this.#mastra, step, attempt);
@@ -291,8 +384,14 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
291
384
  return [this.getFunction(), ...this.getNestedFunctions(this.executionGraph.steps)];
292
385
  }
293
386
  };
387
+ function isAgent(params) {
388
+ return params?.component === "AGENT";
389
+ }
390
+ function isTool(params) {
391
+ return params instanceof Tool;
392
+ }
294
393
  function createStep(params) {
295
- if (params instanceof Agent) {
394
+ if (isAgent(params)) {
296
395
  return {
297
396
  id: params.name,
298
397
  // @ts-ignore
@@ -357,7 +456,7 @@ function createStep(params) {
357
456
  }
358
457
  };
359
458
  }
360
- if (params instanceof Tool) {
459
+ if (isTool(params)) {
361
460
  if (!params.inputSchema || !params.outputSchema) {
362
461
  throw new Error("Tool must have input and output schemas defined");
363
462
  }
@@ -423,6 +522,18 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
423
522
  this.inngestStep = inngestStep;
424
523
  this.inngestAttempts = inngestAttempts;
425
524
  }
525
+ async execute(params) {
526
+ await params.emitter.emit("watch-v2", {
527
+ type: "start",
528
+ payload: { runId: params.runId }
529
+ });
530
+ const result = await super.execute(params);
531
+ await params.emitter.emit("watch-v2", {
532
+ type: "finish",
533
+ payload: { runId: params.runId }
534
+ });
535
+ return result;
536
+ }
426
537
  async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
427
538
  const base = {
428
539
  status: lastOutput.status,
@@ -506,6 +617,16 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
506
617
  async executeSleep({ id, duration }) {
507
618
  await this.inngestStep.sleep(id, duration);
508
619
  }
620
+ async executeWaitForEvent({ event, timeout }) {
621
+ const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
622
+ event: `user-event-${event}`,
623
+ timeout: timeout ?? 5e3
624
+ });
625
+ if (eventData === null) {
626
+ throw "Timeout waiting for event";
627
+ }
628
+ return eventData?.data;
629
+ }
509
630
  async executeStep({
510
631
  step,
511
632
  stepResults,
@@ -515,9 +636,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
515
636
  emitter,
516
637
  runtimeContext
517
638
  }) {
518
- await this.inngestStep.run(
639
+ const startedAt = await this.inngestStep.run(
519
640
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
520
641
  async () => {
642
+ const startedAt2 = Date.now();
521
643
  await emitter.emit("watch", {
522
644
  type: "watch",
523
645
  payload: {
@@ -539,6 +661,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
539
661
  },
540
662
  eventTimestamp: Date.now()
541
663
  });
664
+ await emitter.emit("watch-v2", {
665
+ type: "step-start",
666
+ payload: {
667
+ id: step.id
668
+ }
669
+ });
670
+ return startedAt2;
542
671
  }
543
672
  );
544
673
  if (step instanceof InngestWorkflow) {
@@ -599,6 +728,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
599
728
  },
600
729
  eventTimestamp: Date.now()
601
730
  });
731
+ await emitter.emit("watch-v2", {
732
+ type: "step-result",
733
+ payload: {
734
+ id: step.id,
735
+ status: "failed"
736
+ }
737
+ });
602
738
  return { executionContext, result: { status: "failed", error: result?.error } };
603
739
  } else if (result.status === "suspended") {
604
740
  const suspendedSteps = Object.entries(result.steps).filter(([_stepName, stepResult]) => {
@@ -625,6 +761,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
625
761
  },
626
762
  eventTimestamp: Date.now()
627
763
  });
764
+ await emitter.emit("watch-v2", {
765
+ type: "step-suspended",
766
+ payload: {
767
+ id: step.id
768
+ }
769
+ });
628
770
  return {
629
771
  executionContext,
630
772
  result: {
@@ -675,6 +817,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
675
817
  },
676
818
  eventTimestamp: Date.now()
677
819
  });
820
+ await emitter.emit("watch-v2", {
821
+ type: "step-finish",
822
+ payload: {
823
+ id: step.id,
824
+ metadata: {}
825
+ }
826
+ });
678
827
  return { executionContext, result: { status: "success", output: result?.result } };
679
828
  }
680
829
  );
@@ -684,6 +833,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
684
833
  const stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
685
834
  let execResults;
686
835
  let suspended;
836
+ let bailed;
687
837
  try {
688
838
  const result = await step.execute({
689
839
  runId: executionContext.runId,
@@ -703,6 +853,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
703
853
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
704
854
  suspended = { payload: suspendPayload };
705
855
  },
856
+ bail: (result2) => {
857
+ bailed = { payload: result2 };
858
+ },
706
859
  resume: {
707
860
  steps: resume?.steps?.slice(1) || [],
708
861
  resumePayload: resume?.resumePayload,
@@ -714,12 +867,39 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
714
867
  step: this.inngestStep
715
868
  }
716
869
  });
717
- execResults = { status: "success", output: result };
870
+ const endedAt = Date.now();
871
+ execResults = {
872
+ status: "success",
873
+ output: result,
874
+ startedAt,
875
+ endedAt,
876
+ payload: prevOutput,
877
+ resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
878
+ resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
879
+ };
718
880
  } catch (e) {
719
- execResults = { status: "failed", error: e instanceof Error ? e.message : String(e) };
881
+ execResults = {
882
+ status: "failed",
883
+ payload: prevOutput,
884
+ error: e instanceof Error ? e.message : String(e),
885
+ endedAt: Date.now(),
886
+ startedAt,
887
+ resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
888
+ resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
889
+ };
720
890
  }
721
891
  if (suspended) {
722
- execResults = { status: "suspended", payload: suspended.payload };
892
+ execResults = {
893
+ status: "suspended",
894
+ suspendedPayload: suspended.payload,
895
+ payload: prevOutput,
896
+ suspendedAt: Date.now(),
897
+ startedAt,
898
+ resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
899
+ resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
900
+ };
901
+ } else if (bailed) {
902
+ execResults = { status: "bailed", output: bailed.payload, payload: prevOutput, endedAt: Date.now(), startedAt };
723
903
  }
724
904
  if (execResults.status === "failed") {
725
905
  if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
@@ -731,18 +911,43 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
731
911
  payload: {
732
912
  currentStep: {
733
913
  id: step.id,
734
- status: execResults.status,
735
- output: execResults.output
914
+ ...execResults
736
915
  },
737
916
  workflowState: {
738
917
  status: "running",
739
- steps: stepResults,
918
+ steps: { ...stepResults, [step.id]: execResults },
740
919
  result: null,
741
920
  error: null
742
921
  }
743
922
  },
744
923
  eventTimestamp: Date.now()
745
924
  });
925
+ if (execResults.status === "suspended") {
926
+ await emitter.emit("watch-v2", {
927
+ type: "step-suspended",
928
+ payload: {
929
+ id: step.id,
930
+ status: execResults.status,
931
+ output: execResults.status === "success" ? execResults?.output : void 0
932
+ }
933
+ });
934
+ } else {
935
+ await emitter.emit("watch-v2", {
936
+ type: "step-result",
937
+ payload: {
938
+ id: step.id,
939
+ status: execResults.status,
940
+ output: execResults.status === "success" ? execResults?.output : void 0
941
+ }
942
+ });
943
+ await emitter.emit("watch-v2", {
944
+ type: "step-finish",
945
+ payload: {
946
+ id: step.id,
947
+ metadata: {}
948
+ }
949
+ });
950
+ }
746
951
  return { result: execResults, executionContext, stepResults };
747
952
  });
748
953
  Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
@@ -819,6 +1024,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
819
1024
  // TODO: this function shouldn't have suspend probably?
820
1025
  suspend: async (_suspendPayload) => {
821
1026
  },
1027
+ bail: () => {
1028
+ },
822
1029
  [EMITTER_SYMBOL]: emitter,
823
1030
  engine: {
824
1031
  step: this.inngestStep
@@ -860,7 +1067,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
860
1067
  if (hasFailed) {
861
1068
  execResults = { status: "failed", error: hasFailed.result.error };
862
1069
  } else if (hasSuspended) {
863
- execResults = { status: "suspended", payload: hasSuspended.result.payload };
1070
+ execResults = { status: "suspended", payload: hasSuspended.result.suspendPayload };
864
1071
  } else {
865
1072
  execResults = {
866
1073
  status: "success",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/inngest",
3
- "version": "0.0.0-fix-generate-title-20250616171351",
3
+ "version": "0.0.0-fix-fetch-workflow-runs-20250624231457",
4
4
  "description": "Mastra Inngest integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -22,7 +22,7 @@
22
22
  "@inngest/realtime": "^0.3.1",
23
23
  "@opentelemetry/api": "^1.9.0",
24
24
  "inngest": "^3.39.1",
25
- "zod": "^3.25.57"
25
+ "zod": "^3.25.67"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@ai-sdk/openai": "^1.3.22",
@@ -30,20 +30,20 @@
30
30
  "@microsoft/api-extractor": "^7.52.8",
31
31
  "@types/node": "^20.19.0",
32
32
  "ai": "^4.3.16",
33
- "eslint": "^9.28.0",
33
+ "eslint": "^9.29.0",
34
34
  "execa": "^9.6.0",
35
35
  "get-port": "7.1.0",
36
36
  "hono": "^4.7.11",
37
37
  "tsup": "^8.5.0",
38
38
  "typescript": "^5.8.3",
39
39
  "vitest": "^2.1.9",
40
- "@internal/lint": "0.0.0-fix-generate-title-20250616171351",
41
- "@mastra/libsql": "0.0.0-fix-generate-title-20250616171351",
42
- "@mastra/core": "0.0.0-fix-generate-title-20250616171351",
43
- "@mastra/deployer": "0.0.0-fix-generate-title-20250616171351"
40
+ "@mastra/libsql": "0.0.0-fix-fetch-workflow-runs-20250624231457",
41
+ "@mastra/core": "0.0.0-fix-fetch-workflow-runs-20250624231457",
42
+ "@mastra/deployer": "0.0.0-fix-fetch-workflow-runs-20250624231457",
43
+ "@internal/lint": "0.0.0-fix-fetch-workflow-runs-20250624231457"
44
44
  },
45
45
  "peerDependencies": {
46
- "@mastra/core": "0.0.0-fix-generate-title-20250616171351"
46
+ "@mastra/core": "0.0.0-fix-fetch-workflow-runs-20250624231457"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",