@mastra/inngest 1.0.0-beta.1 → 1.0.0-beta.11

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,41 +1,511 @@
1
+ import { Agent } from '@mastra/core/agent';
2
+ import { Tool } from '@mastra/core/tools';
3
+ import { DefaultExecutionEngine, createTimeTravelExecutionParams, Run, hydrateSerializedStepErrors, Workflow } from '@mastra/core/workflows';
4
+ import { PUBSUB_SYMBOL, STREAM_FORMAT_SYMBOL } from '@mastra/core/workflows/_constants';
5
+ import { z } from 'zod';
1
6
  import { randomUUID } from 'crypto';
2
- import { ReadableStream } from 'stream/web';
3
- import { subscribe } from '@inngest/realtime';
4
7
  import { RequestContext } from '@mastra/core/di';
5
- import { wrapMastra, SpanType } from '@mastra/core/observability';
8
+ import { NonRetriableError } from 'inngest';
9
+ import { getErrorFromUnknown } from '@mastra/core/error';
10
+ import { subscribe } from '@inngest/realtime';
11
+ import { PubSub } from '@mastra/core/events';
12
+ import { ReadableStream } from 'stream/web';
6
13
  import { ChunkFrom, WorkflowRunOutput } from '@mastra/core/stream';
7
- import { ToolStream, Tool } from '@mastra/core/tools';
8
- import { Run, Workflow, DefaultExecutionEngine, createDeprecationProxy, getStepResult, runCountDeprecationMessage, validateStepInput } from '@mastra/core/workflows';
9
- import { EMITTER_SYMBOL, STREAM_FORMAT_SYMBOL } from '@mastra/core/workflows/_constants';
10
- import { NonRetriableError, RetryAfterError } from 'inngest';
11
14
  import { serve as serve$1 } from 'inngest/hono';
12
- import { z } from 'zod';
13
15
 
14
16
  // src/index.ts
15
- function serve({
16
- mastra,
17
- inngest,
18
- functions: userFunctions = [],
19
- registerOptions
20
- }) {
21
- const wfs = mastra.listWorkflows();
22
- const workflowFunctions = Array.from(
23
- new Set(
24
- Object.values(wfs).flatMap((wf) => {
25
- if (wf instanceof InngestWorkflow) {
26
- wf.__registerMastra(mastra);
27
- return wf.getFunctions();
17
+ var InngestExecutionEngine = class extends DefaultExecutionEngine {
18
+ inngestStep;
19
+ inngestAttempts;
20
+ constructor(mastra, inngestStep, inngestAttempts = 0, options) {
21
+ super({ mastra, options });
22
+ this.inngestStep = inngestStep;
23
+ this.inngestAttempts = inngestAttempts;
24
+ }
25
+ // =============================================================================
26
+ // Hook Overrides
27
+ // =============================================================================
28
+ /**
29
+ * Format errors while preserving Error instances and their custom properties.
30
+ * Uses getErrorFromUnknown to ensure all error properties are preserved.
31
+ */
32
+ formatResultError(error, lastOutput) {
33
+ const outputError = lastOutput?.error;
34
+ const errorSource = error || outputError;
35
+ const errorInstance = getErrorFromUnknown(errorSource, {
36
+ serializeStack: true,
37
+ // Include stack in JSON for better debugging in Inngest
38
+ fallbackMessage: "Unknown workflow error"
39
+ });
40
+ return errorInstance.toJSON();
41
+ }
42
+ /**
43
+ * Detect InngestWorkflow instances for special nested workflow handling
44
+ */
45
+ isNestedWorkflowStep(step) {
46
+ return step instanceof InngestWorkflow;
47
+ }
48
+ /**
49
+ * Inngest requires requestContext serialization for memoization.
50
+ * When steps are replayed, the original function doesn't re-execute,
51
+ * so requestContext modifications must be captured and restored.
52
+ */
53
+ requiresDurableContextSerialization() {
54
+ return true;
55
+ }
56
+ /**
57
+ * Execute a step with retry logic for Inngest.
58
+ * Retries are handled via step-level retry (RetryAfterError thrown INSIDE step.run()).
59
+ * After retries exhausted, error propagates here and we return a failed result.
60
+ */
61
+ async executeStepWithRetry(stepId, runStep, params) {
62
+ for (let i = 0; i < params.retries + 1; i++) {
63
+ if (i > 0 && params.delay) {
64
+ await new Promise((resolve) => setTimeout(resolve, params.delay));
65
+ }
66
+ try {
67
+ const result = await this.wrapDurableOperation(stepId, runStep);
68
+ return { ok: true, result };
69
+ } catch (e) {
70
+ if (i === params.retries) {
71
+ const cause = e?.cause;
72
+ if (cause?.status === "failed") {
73
+ params.stepSpan?.error({
74
+ error: e,
75
+ attributes: { status: "failed" }
76
+ });
77
+ if (cause.error && !(cause.error instanceof Error)) {
78
+ cause.error = getErrorFromUnknown(cause.error, { serializeStack: false });
79
+ }
80
+ return { ok: false, error: cause };
81
+ }
82
+ const errorInstance = getErrorFromUnknown(e, {
83
+ serializeStack: false,
84
+ fallbackMessage: "Unknown step execution error"
85
+ });
86
+ params.stepSpan?.error({
87
+ error: errorInstance,
88
+ attributes: { status: "failed" }
89
+ });
90
+ return {
91
+ ok: false,
92
+ error: {
93
+ status: "failed",
94
+ error: errorInstance,
95
+ endedAt: Date.now()
96
+ }
97
+ };
28
98
  }
29
- return [];
30
- })
31
- )
32
- );
33
- return serve$1({
34
- ...registerOptions,
35
- client: inngest,
36
- functions: [...workflowFunctions, ...userFunctions]
37
- });
38
- }
99
+ }
100
+ }
101
+ return { ok: false, error: { status: "failed", error: new Error("Unknown error"), endedAt: Date.now() } };
102
+ }
103
+ /**
104
+ * Use Inngest's sleep primitive for durability
105
+ */
106
+ async executeSleepDuration(duration, sleepId, workflowId) {
107
+ await this.inngestStep.sleep(`workflow.${workflowId}.sleep.${sleepId}`, duration < 0 ? 0 : duration);
108
+ }
109
+ /**
110
+ * Use Inngest's sleepUntil primitive for durability
111
+ */
112
+ async executeSleepUntilDate(date, sleepUntilId, workflowId) {
113
+ await this.inngestStep.sleepUntil(`workflow.${workflowId}.sleepUntil.${sleepUntilId}`, date);
114
+ }
115
+ /**
116
+ * Wrap durable operations in Inngest step.run() for durability.
117
+ * If retryConfig is provided, throws RetryAfterError INSIDE step.run() to trigger
118
+ * Inngest's step-level retry mechanism (not function-level retry).
119
+ */
120
+ async wrapDurableOperation(operationId, operationFn) {
121
+ return this.inngestStep.run(operationId, async () => {
122
+ try {
123
+ return await operationFn();
124
+ } catch (e) {
125
+ throw e;
126
+ }
127
+ });
128
+ }
129
+ /**
130
+ * Provide Inngest step primitive in engine context
131
+ */
132
+ getEngineContext() {
133
+ return { step: this.inngestStep };
134
+ }
135
+ /**
136
+ * For Inngest, lifecycle callbacks are invoked in the workflow's finalize step
137
+ * (wrapped in step.run for durability), not in execute(). Override to skip.
138
+ */
139
+ async invokeLifecycleCallbacks(_result) {
140
+ }
141
+ /**
142
+ * Actually invoke the lifecycle callbacks. Called from workflow.ts finalize step.
143
+ */
144
+ async invokeLifecycleCallbacksInternal(result) {
145
+ return super.invokeLifecycleCallbacks(result);
146
+ }
147
+ /**
148
+ * Execute nested InngestWorkflow using inngestStep.invoke() for durability.
149
+ * This MUST be called directly (not inside step.run()) due to Inngest constraints.
150
+ */
151
+ async executeWorkflowStep(params) {
152
+ if (!(params.step instanceof InngestWorkflow)) {
153
+ return null;
154
+ }
155
+ const {
156
+ step,
157
+ stepResults,
158
+ executionContext,
159
+ resume,
160
+ timeTravel,
161
+ prevOutput,
162
+ inputData,
163
+ pubsub,
164
+ startedAt,
165
+ perStep
166
+ } = params;
167
+ const isResume = !!resume?.steps?.length;
168
+ let result;
169
+ let runId;
170
+ const isTimeTravel = !!(timeTravel && timeTravel.steps?.length > 1 && timeTravel.steps[0] === step.id);
171
+ try {
172
+ if (isResume) {
173
+ runId = stepResults[resume?.steps?.[0] ?? ""]?.suspendPayload?.__workflow_meta?.runId ?? randomUUID();
174
+ const workflowsStore = await this.mastra?.getStorage()?.getStore("workflows");
175
+ const snapshot = await workflowsStore?.loadWorkflowSnapshot({
176
+ workflowName: step.id,
177
+ runId
178
+ });
179
+ const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
180
+ function: step.getFunction(),
181
+ data: {
182
+ inputData,
183
+ initialState: executionContext.state ?? snapshot?.value ?? {},
184
+ runId,
185
+ resume: {
186
+ runId,
187
+ steps: resume.steps.slice(1),
188
+ stepResults: snapshot?.context,
189
+ resumePayload: resume.resumePayload,
190
+ resumePath: resume.steps?.[1] ? snapshot?.suspendedPaths?.[resume.steps?.[1]] : void 0
191
+ },
192
+ outputOptions: { includeState: true },
193
+ perStep
194
+ }
195
+ });
196
+ result = invokeResp.result;
197
+ runId = invokeResp.runId;
198
+ executionContext.state = invokeResp.result.state;
199
+ } else if (isTimeTravel) {
200
+ const workflowsStoreForTimeTravel = await this.mastra?.getStorage()?.getStore("workflows");
201
+ const snapshot = await workflowsStoreForTimeTravel?.loadWorkflowSnapshot({
202
+ workflowName: step.id,
203
+ runId: executionContext.runId
204
+ }) ?? { context: {} };
205
+ const timeTravelParams = createTimeTravelExecutionParams({
206
+ steps: timeTravel.steps.slice(1),
207
+ inputData: timeTravel.inputData,
208
+ resumeData: timeTravel.resumeData,
209
+ context: timeTravel.nestedStepResults?.[step.id] ?? {},
210
+ nestedStepsContext: timeTravel.nestedStepResults ?? {},
211
+ snapshot,
212
+ graph: step.buildExecutionGraph()
213
+ });
214
+ const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
215
+ function: step.getFunction(),
216
+ data: {
217
+ timeTravel: timeTravelParams,
218
+ initialState: executionContext.state ?? {},
219
+ runId: executionContext.runId,
220
+ outputOptions: { includeState: true },
221
+ perStep
222
+ }
223
+ });
224
+ result = invokeResp.result;
225
+ runId = invokeResp.runId;
226
+ executionContext.state = invokeResp.result.state;
227
+ } else {
228
+ const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
229
+ function: step.getFunction(),
230
+ data: {
231
+ inputData,
232
+ initialState: executionContext.state ?? {},
233
+ outputOptions: { includeState: true },
234
+ perStep
235
+ }
236
+ });
237
+ result = invokeResp.result;
238
+ runId = invokeResp.runId;
239
+ executionContext.state = invokeResp.result.state;
240
+ }
241
+ } catch (e) {
242
+ const errorCause = e?.cause;
243
+ if (errorCause && typeof errorCause === "object") {
244
+ result = errorCause;
245
+ runId = errorCause.runId || randomUUID();
246
+ } else {
247
+ runId = randomUUID();
248
+ result = {
249
+ status: "failed",
250
+ error: e instanceof Error ? e : new Error(String(e)),
251
+ steps: {},
252
+ input: inputData
253
+ };
254
+ }
255
+ }
256
+ const res = await this.inngestStep.run(
257
+ `workflow.${executionContext.workflowId}.step.${step.id}.nestedwf-results`,
258
+ async () => {
259
+ if (result.status === "failed") {
260
+ await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
261
+ type: "watch",
262
+ runId: executionContext.runId,
263
+ data: {
264
+ type: "workflow-step-result",
265
+ payload: {
266
+ id: step.id,
267
+ status: "failed",
268
+ error: result?.error,
269
+ payload: prevOutput
270
+ }
271
+ }
272
+ });
273
+ return { executionContext, result: { status: "failed", error: result?.error, endedAt: Date.now() } };
274
+ } else if (result.status === "suspended") {
275
+ const suspendedSteps = Object.entries(result.steps).filter(([_stepName, stepResult]) => {
276
+ const stepRes = stepResult;
277
+ return stepRes?.status === "suspended";
278
+ });
279
+ for (const [stepName, stepResult] of suspendedSteps) {
280
+ const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
281
+ executionContext.suspendedPaths[step.id] = executionContext.executionPath;
282
+ await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
283
+ type: "watch",
284
+ runId: executionContext.runId,
285
+ data: {
286
+ type: "workflow-step-suspended",
287
+ payload: {
288
+ id: step.id,
289
+ status: "suspended"
290
+ }
291
+ }
292
+ });
293
+ return {
294
+ executionContext,
295
+ result: {
296
+ status: "suspended",
297
+ suspendedAt: Date.now(),
298
+ payload: stepResult.payload,
299
+ suspendPayload: {
300
+ ...stepResult?.suspendPayload,
301
+ __workflow_meta: { runId, path: suspendPath }
302
+ }
303
+ }
304
+ };
305
+ }
306
+ return {
307
+ executionContext,
308
+ result: {
309
+ status: "suspended",
310
+ suspendedAt: Date.now(),
311
+ payload: {}
312
+ }
313
+ };
314
+ } else if (result.status === "tripwire") {
315
+ await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
316
+ type: "watch",
317
+ runId: executionContext.runId,
318
+ data: {
319
+ type: "workflow-step-result",
320
+ payload: {
321
+ id: step.id,
322
+ status: "tripwire",
323
+ error: result?.tripwire?.reason,
324
+ payload: prevOutput
325
+ }
326
+ }
327
+ });
328
+ return {
329
+ executionContext,
330
+ result: {
331
+ status: "tripwire",
332
+ tripwire: result?.tripwire,
333
+ endedAt: Date.now()
334
+ }
335
+ };
336
+ } else if (perStep || result.status === "paused") {
337
+ await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
338
+ type: "watch",
339
+ runId: executionContext.runId,
340
+ data: {
341
+ type: "workflow-step-result",
342
+ payload: {
343
+ id: step.id,
344
+ status: "paused"
345
+ }
346
+ }
347
+ });
348
+ await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
349
+ type: "watch",
350
+ runId: executionContext.runId,
351
+ data: {
352
+ type: "workflow-step-finish",
353
+ payload: {
354
+ id: step.id,
355
+ metadata: {}
356
+ }
357
+ }
358
+ });
359
+ return { executionContext, result: { status: "paused" } };
360
+ }
361
+ await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
362
+ type: "watch",
363
+ runId: executionContext.runId,
364
+ data: {
365
+ type: "workflow-step-result",
366
+ payload: {
367
+ id: step.id,
368
+ status: "success",
369
+ output: result?.result
370
+ }
371
+ }
372
+ });
373
+ await pubsub.publish(`workflow.events.v2.${executionContext.runId}`, {
374
+ type: "watch",
375
+ runId: executionContext.runId,
376
+ data: {
377
+ type: "workflow-step-finish",
378
+ payload: {
379
+ id: step.id,
380
+ metadata: {}
381
+ }
382
+ }
383
+ });
384
+ return { executionContext, result: { status: "success", output: result?.result, endedAt: Date.now() } };
385
+ }
386
+ );
387
+ Object.assign(executionContext, res.executionContext);
388
+ return {
389
+ ...res.result,
390
+ startedAt,
391
+ payload: inputData,
392
+ resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
393
+ resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
394
+ };
395
+ }
396
+ };
397
+ var InngestPubSub = class extends PubSub {
398
+ inngest;
399
+ workflowId;
400
+ publishFn;
401
+ subscriptions = /* @__PURE__ */ new Map();
402
+ constructor(inngest, workflowId, publishFn) {
403
+ super();
404
+ this.inngest = inngest;
405
+ this.workflowId = workflowId;
406
+ this.publishFn = publishFn;
407
+ }
408
+ /**
409
+ * Publish an event to Inngest's realtime system.
410
+ *
411
+ * Topic format: "workflow.events.v2.{runId}"
412
+ * Maps to Inngest channel: "workflow:{workflowId}:{runId}"
413
+ */
414
+ async publish(topic, event) {
415
+ if (!this.publishFn) {
416
+ return;
417
+ }
418
+ const match = topic.match(/^workflow\.events\.v2\.(.+)$/);
419
+ if (!match) {
420
+ return;
421
+ }
422
+ const runId = match[1];
423
+ try {
424
+ await this.publishFn({
425
+ channel: `workflow:${this.workflowId}:${runId}`,
426
+ topic: "watch",
427
+ data: event.data
428
+ });
429
+ } catch (err) {
430
+ console.error("InngestPubSub publish error:", err?.message ?? err);
431
+ }
432
+ }
433
+ /**
434
+ * Subscribe to events from Inngest's realtime system.
435
+ *
436
+ * Topic format: "workflow.events.v2.{runId}"
437
+ * Maps to Inngest channel: "workflow:{workflowId}:{runId}"
438
+ */
439
+ async subscribe(topic, cb) {
440
+ const match = topic.match(/^workflow\.events\.v2\.(.+)$/);
441
+ if (!match || !match[1]) {
442
+ return;
443
+ }
444
+ const runId = match[1];
445
+ if (this.subscriptions.has(topic)) {
446
+ this.subscriptions.get(topic).callbacks.add(cb);
447
+ return;
448
+ }
449
+ const callbacks = /* @__PURE__ */ new Set([cb]);
450
+ const channel = `workflow:${this.workflowId}:${runId}`;
451
+ const streamPromise = subscribe(
452
+ {
453
+ channel,
454
+ topics: ["watch"],
455
+ app: this.inngest
456
+ },
457
+ (message) => {
458
+ const event = {
459
+ id: crypto.randomUUID(),
460
+ type: "watch",
461
+ runId,
462
+ data: message.data,
463
+ createdAt: /* @__PURE__ */ new Date()
464
+ };
465
+ for (const callback of callbacks) {
466
+ callback(event);
467
+ }
468
+ }
469
+ );
470
+ this.subscriptions.set(topic, {
471
+ unsubscribe: () => {
472
+ streamPromise.then((stream) => stream.cancel()).catch((err) => {
473
+ console.error("InngestPubSub unsubscribe error:", err);
474
+ });
475
+ },
476
+ callbacks
477
+ });
478
+ }
479
+ /**
480
+ * Unsubscribe a callback from a topic.
481
+ * If no callbacks remain, the underlying Inngest subscription is cancelled.
482
+ */
483
+ async unsubscribe(topic, cb) {
484
+ const sub = this.subscriptions.get(topic);
485
+ if (!sub) {
486
+ return;
487
+ }
488
+ sub.callbacks.delete(cb);
489
+ if (sub.callbacks.size === 0) {
490
+ sub.unsubscribe();
491
+ this.subscriptions.delete(topic);
492
+ }
493
+ }
494
+ /**
495
+ * Flush any pending operations. No-op for Inngest.
496
+ */
497
+ async flush() {
498
+ }
499
+ /**
500
+ * Clean up all subscriptions during graceful shutdown.
501
+ */
502
+ async close() {
503
+ for (const [, sub] of this.subscriptions) {
504
+ sub.unsubscribe();
505
+ }
506
+ this.subscriptions.clear();
507
+ }
508
+ };
39
509
  var InngestRun = class extends Run {
40
510
  inngest;
41
511
  serializedStepGraph;
@@ -47,38 +517,90 @@ var InngestRun = class extends Run {
47
517
  this.#mastra = params.mastra;
48
518
  }
49
519
  async getRuns(eventId) {
50
- const response = await fetch(`${this.inngest.apiBaseUrl ?? "https://api.inngest.com"}/v1/events/${eventId}/runs`, {
51
- headers: {
52
- Authorization: `Bearer ${process.env.INNGEST_SIGNING_KEY}`
520
+ const maxRetries = 3;
521
+ let lastError = null;
522
+ for (let attempt = 0; attempt < maxRetries; attempt++) {
523
+ try {
524
+ const response = await fetch(
525
+ `${this.inngest.apiBaseUrl ?? "https://api.inngest.com"}/v1/events/${eventId}/runs`,
526
+ {
527
+ headers: {
528
+ Authorization: `Bearer ${process.env.INNGEST_SIGNING_KEY}`
529
+ }
530
+ }
531
+ );
532
+ if (response.status === 429) {
533
+ const retryAfter = parseInt(response.headers.get("retry-after") || "2", 10);
534
+ await new Promise((resolve) => setTimeout(resolve, retryAfter * 1e3));
535
+ continue;
536
+ }
537
+ if (!response.ok) {
538
+ throw new Error(`Inngest API error: ${response.status} ${response.statusText}`);
539
+ }
540
+ const text = await response.text();
541
+ if (!text) {
542
+ await new Promise((resolve) => setTimeout(resolve, 1e3 * (attempt + 1)));
543
+ continue;
544
+ }
545
+ const json = JSON.parse(text);
546
+ return json.data;
547
+ } catch (error) {
548
+ lastError = error;
549
+ if (attempt < maxRetries - 1) {
550
+ await new Promise((resolve) => setTimeout(resolve, 1e3 * Math.pow(2, attempt)));
551
+ }
53
552
  }
54
- });
55
- const json = await response.json();
56
- return json.data;
553
+ }
554
+ throw new NonRetriableError(`Failed to get runs after ${maxRetries} attempts: ${lastError?.message}`);
57
555
  }
58
- async getRunOutput(eventId) {
59
- let runs = await this.getRuns(eventId);
556
+ async getRunOutput(eventId, maxWaitMs = 3e5) {
557
+ const startTime = Date.now();
60
558
  const storage = this.#mastra?.getStorage();
61
- while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
62
- await new Promise((resolve) => setTimeout(resolve, 1e3));
63
- runs = await this.getRuns(eventId);
559
+ const workflowsStore = await storage?.getStore("workflows");
560
+ while (Date.now() - startTime < maxWaitMs) {
561
+ let runs;
562
+ try {
563
+ runs = await this.getRuns(eventId);
564
+ } catch (error) {
565
+ if (error instanceof NonRetriableError) {
566
+ throw error;
567
+ }
568
+ throw new NonRetriableError(
569
+ `Failed to poll workflow status: ${error instanceof Error ? error.message : String(error)}`
570
+ );
571
+ }
572
+ if (runs?.[0]?.status === "Completed" && runs?.[0]?.event_id === eventId) {
573
+ return runs[0];
574
+ }
64
575
  if (runs?.[0]?.status === "Failed") {
65
- const snapshot = await storage?.loadWorkflowSnapshot({
576
+ const snapshot = await workflowsStore?.loadWorkflowSnapshot({
66
577
  workflowName: this.workflowId,
67
578
  runId: this.runId
68
579
  });
580
+ if (snapshot?.context) {
581
+ snapshot.context = hydrateSerializedStepErrors(snapshot.context);
582
+ }
69
583
  return {
70
- output: { result: { steps: snapshot?.context, status: "failed", error: runs?.[0]?.output?.message } }
584
+ output: {
585
+ result: {
586
+ steps: snapshot?.context,
587
+ status: "failed",
588
+ // Get the original error from NonRetriableError's cause (which contains the workflow result)
589
+ error: getErrorFromUnknown(runs?.[0]?.output?.cause?.error, { serializeStack: false })
590
+ }
591
+ }
71
592
  };
72
593
  }
73
594
  if (runs?.[0]?.status === "Cancelled") {
74
- const snapshot = await storage?.loadWorkflowSnapshot({
595
+ const snapshot = await workflowsStore?.loadWorkflowSnapshot({
75
596
  workflowName: this.workflowId,
76
597
  runId: this.runId
77
598
  });
78
599
  return { output: { result: { steps: snapshot?.context, status: "canceled" } } };
79
600
  }
601
+ await new Promise((resolve) => setTimeout(resolve, 1e3 + Math.random() * 1e3));
80
602
  }
81
- return runs?.[0];
603
+ throw new NonRetriableError(`Workflow did not complete within ${maxWaitMs}ms`);
82
604
  }
83
605
  async cancel() {
84
606
  const storage = this.#mastra?.getStorage();
@@ -88,18 +610,20 @@ var InngestRun = class extends Run {
88
610
  runId: this.runId
89
611
  }
90
612
  });
91
- const snapshot = await storage?.loadWorkflowSnapshot({
613
+ const workflowsStore = await storage?.getStore("workflows");
614
+ const snapshot = await workflowsStore?.loadWorkflowSnapshot({
92
615
  workflowName: this.workflowId,
93
616
  runId: this.runId
94
617
  });
95
618
  if (snapshot) {
96
- await storage?.persistWorkflowSnapshot({
619
+ await workflowsStore?.persistWorkflowSnapshot({
97
620
  workflowName: this.workflowId,
98
621
  runId: this.runId,
99
622
  resourceId: this.resourceId,
100
623
  snapshot: {
101
624
  ...snapshot,
102
- status: "canceled"
625
+ status: "canceled",
626
+ value: snapshot.value
103
627
  }
104
628
  });
105
629
  }
@@ -107,28 +631,79 @@ var InngestRun = class extends Run {
107
631
  async start(params) {
108
632
  return this._start(params);
109
633
  }
634
+ /**
635
+ * Starts the workflow execution without waiting for completion (fire-and-forget).
636
+ * Returns immediately with the runId after sending the event to Inngest.
637
+ * The workflow executes independently in Inngest.
638
+ * Use this when you don't need to wait for the result or want to avoid polling failures.
639
+ */
640
+ async startAsync(params) {
641
+ const workflowsStore = await this.#mastra.getStorage()?.getStore("workflows");
642
+ await workflowsStore?.persistWorkflowSnapshot({
643
+ workflowName: this.workflowId,
644
+ runId: this.runId,
645
+ resourceId: this.resourceId,
646
+ snapshot: {
647
+ runId: this.runId,
648
+ serializedStepGraph: this.serializedStepGraph,
649
+ status: "running",
650
+ value: {},
651
+ context: {},
652
+ activePaths: [],
653
+ suspendedPaths: {},
654
+ activeStepsPath: {},
655
+ resumeLabels: {},
656
+ waitingPaths: {},
657
+ timestamp: Date.now()
658
+ }
659
+ });
660
+ const inputDataToUse = await this._validateInput(params.inputData);
661
+ const initialStateToUse = await this._validateInitialState(params.initialState ?? {});
662
+ const eventOutput = await this.inngest.send({
663
+ name: `workflow.${this.workflowId}`,
664
+ data: {
665
+ inputData: inputDataToUse,
666
+ initialState: initialStateToUse,
667
+ runId: this.runId,
668
+ resourceId: this.resourceId,
669
+ outputOptions: params.outputOptions,
670
+ tracingOptions: params.tracingOptions,
671
+ requestContext: params.requestContext ? Object.fromEntries(params.requestContext.entries()) : {},
672
+ perStep: params.perStep
673
+ }
674
+ });
675
+ const eventId = eventOutput.ids[0];
676
+ if (!eventId) {
677
+ throw new Error("Event ID is not set");
678
+ }
679
+ return { runId: this.runId };
680
+ }
110
681
  async _start({
111
682
  inputData,
112
683
  initialState,
113
684
  outputOptions,
114
685
  tracingOptions,
115
- format
686
+ format,
687
+ requestContext,
688
+ perStep
116
689
  }) {
117
- await this.#mastra.getStorage()?.persistWorkflowSnapshot({
690
+ const workflowsStore = await this.#mastra.getStorage()?.getStore("workflows");
691
+ await workflowsStore?.persistWorkflowSnapshot({
118
692
  workflowName: this.workflowId,
119
693
  runId: this.runId,
120
694
  resourceId: this.resourceId,
121
695
  snapshot: {
122
696
  runId: this.runId,
123
697
  serializedStepGraph: this.serializedStepGraph,
698
+ status: "running",
124
699
  value: {},
125
700
  context: {},
126
701
  activePaths: [],
127
702
  suspendedPaths: {},
703
+ activeStepsPath: {},
128
704
  resumeLabels: {},
129
705
  waitingPaths: {},
130
- timestamp: Date.now(),
131
- status: "running"
706
+ timestamp: Date.now()
132
707
  }
133
708
  });
134
709
  const inputDataToUse = await this._validateInput(inputData);
@@ -142,7 +717,9 @@ var InngestRun = class extends Run {
142
717
  resourceId: this.resourceId,
143
718
  outputOptions,
144
719
  tracingOptions,
145
- format
720
+ format,
721
+ requestContext: requestContext ? Object.fromEntries(requestContext.entries()) : {},
722
+ perStep
146
723
  }
147
724
  });
148
725
  const eventId = eventOutput.ids[0];
@@ -151,9 +728,7 @@ var InngestRun = class extends Run {
151
728
  }
152
729
  const runOutput = await this.getRunOutput(eventId);
153
730
  const result = runOutput?.output?.result;
154
- if (result.status === "failed") {
155
- result.error = new Error(result.error);
156
- }
731
+ this.hydrateFailedResult(result);
157
732
  if (result.status !== "suspended") {
158
733
  this.cleanup?.();
159
734
  }
@@ -172,15 +747,24 @@ var InngestRun = class extends Run {
172
747
  }
173
748
  async _resume(params) {
174
749
  const storage = this.#mastra?.getStorage();
175
- const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
176
- (step) => typeof step === "string" ? step : step?.id
177
- );
178
- const snapshot = await storage?.loadWorkflowSnapshot({
750
+ let steps = [];
751
+ if (typeof params.step === "string") {
752
+ steps = params.step.split(".");
753
+ } else {
754
+ steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
755
+ (step) => typeof step === "string" ? step : step?.id
756
+ );
757
+ }
758
+ const workflowsStore = await storage?.getStore("workflows");
759
+ const snapshot = await workflowsStore?.loadWorkflowSnapshot({
179
760
  workflowName: this.workflowId,
180
761
  runId: this.runId
181
762
  });
182
763
  const suspendedStep = this.workflowSteps[steps?.[0] ?? ""];
183
764
  const resumeDataToUse = await this._validateResumeData(params.resumeData, suspendedStep);
765
+ const persistedRequestContext = snapshot?.requestContext ?? {};
766
+ const newRequestContext = params.requestContext ? Object.fromEntries(params.requestContext.entries()) : {};
767
+ const mergedRequestContext = { ...persistedRequestContext, ...newRequestContext };
184
768
  const eventOutput = await this.inngest.send({
185
769
  name: `workflow.${this.workflowId}`,
186
770
  data: {
@@ -193,9 +777,10 @@ var InngestRun = class extends Run {
193
777
  steps,
194
778
  stepResults: snapshot?.context,
195
779
  resumePayload: resumeDataToUse,
196
- // @ts-ignore
197
- resumePath: snapshot?.suspendedPaths?.[steps?.[0]]
198
- }
780
+ resumePath: steps?.[0] ? snapshot?.suspendedPaths?.[steps?.[0]] : void 0
781
+ },
782
+ requestContext: mergedRequestContext,
783
+ perStep: params.perStep
199
784
  }
200
785
  });
201
786
  const eventId = eventOutput.ids[0];
@@ -204,9 +789,100 @@ var InngestRun = class extends Run {
204
789
  }
205
790
  const runOutput = await this.getRunOutput(eventId);
206
791
  const result = runOutput?.output?.result;
207
- if (result.status === "failed") {
208
- result.error = new Error(result.error);
792
+ this.hydrateFailedResult(result);
793
+ return result;
794
+ }
795
+ async timeTravel(params) {
796
+ const p = this._timeTravel(params).then((result) => {
797
+ if (result.status !== "suspended") {
798
+ this.closeStreamAction?.().catch(() => {
799
+ });
800
+ }
801
+ return result;
802
+ });
803
+ this.executionResults = p;
804
+ return p;
805
+ }
806
+ async _timeTravel(params) {
807
+ if (!params.step || Array.isArray(params.step) && params.step?.length === 0) {
808
+ throw new Error("Step is required and must be a valid step or array of steps");
809
+ }
810
+ let steps = [];
811
+ if (typeof params.step === "string") {
812
+ steps = params.step.split(".");
813
+ } else {
814
+ steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
815
+ (step) => typeof step === "string" ? step : step?.id
816
+ );
817
+ }
818
+ if (steps.length === 0) {
819
+ throw new Error("No steps provided to timeTravel");
820
+ }
821
+ const storage = this.#mastra?.getStorage();
822
+ const workflowsStore = await storage?.getStore("workflows");
823
+ const snapshot = await workflowsStore?.loadWorkflowSnapshot({
824
+ workflowName: this.workflowId,
825
+ runId: this.runId
826
+ });
827
+ if (!snapshot) {
828
+ await workflowsStore?.persistWorkflowSnapshot({
829
+ workflowName: this.workflowId,
830
+ runId: this.runId,
831
+ resourceId: this.resourceId,
832
+ snapshot: {
833
+ runId: this.runId,
834
+ serializedStepGraph: this.serializedStepGraph,
835
+ status: "pending",
836
+ value: {},
837
+ context: {},
838
+ activePaths: [],
839
+ suspendedPaths: {},
840
+ activeStepsPath: {},
841
+ resumeLabels: {},
842
+ waitingPaths: {},
843
+ timestamp: Date.now()
844
+ }
845
+ });
846
+ }
847
+ if (snapshot?.status === "running") {
848
+ throw new Error("This workflow run is still running, cannot time travel");
849
+ }
850
+ let inputDataToUse = params.inputData;
851
+ if (inputDataToUse && steps.length === 1) {
852
+ inputDataToUse = await this._validateTimetravelInputData(params.inputData, this.workflowSteps[steps[0]]);
853
+ }
854
+ const timeTravelData = createTimeTravelExecutionParams({
855
+ steps,
856
+ inputData: inputDataToUse,
857
+ resumeData: params.resumeData,
858
+ context: params.context,
859
+ nestedStepsContext: params.nestedStepsContext,
860
+ snapshot: snapshot ?? { context: {} },
861
+ graph: this.executionGraph,
862
+ initialState: params.initialState,
863
+ perStep: params.perStep
864
+ });
865
+ const eventOutput = await this.inngest.send({
866
+ name: `workflow.${this.workflowId}`,
867
+ data: {
868
+ initialState: timeTravelData.state,
869
+ runId: this.runId,
870
+ workflowId: this.workflowId,
871
+ stepResults: timeTravelData.stepResults,
872
+ timeTravel: timeTravelData,
873
+ tracingOptions: params.tracingOptions,
874
+ outputOptions: params.outputOptions,
875
+ requestContext: params.requestContext ? Object.fromEntries(params.requestContext.entries()) : {},
876
+ perStep: params.perStep
877
+ }
878
+ });
879
+ const eventId = eventOutput.ids[0];
880
+ if (!eventId) {
881
+ throw new Error("Event ID is not set");
209
882
  }
883
+ const runOutput = await this.getRunOutput(eventId);
884
+ const result = runOutput?.output?.result;
885
+ this.hydrateFailedResult(result);
210
886
  return result;
211
887
  }
212
888
  watch(cb) {
@@ -235,14 +911,14 @@ var InngestRun = class extends Run {
235
911
  streamLegacy({ inputData, requestContext } = {}) {
236
912
  const { readable, writable } = new TransformStream();
237
913
  const writer = writable.getWriter();
914
+ void writer.write({
915
+ // @ts-ignore
916
+ type: "start",
917
+ // @ts-ignore
918
+ payload: { runId: this.runId }
919
+ });
238
920
  const unwatch = this.watch(async (event) => {
239
921
  try {
240
- await writer.write({
241
- // @ts-ignore
242
- type: "start",
243
- // @ts-ignore
244
- payload: { runId: this.runId }
245
- });
246
922
  const e = {
247
923
  ...event,
248
924
  type: event.type.replace("workflow-", "")
@@ -288,7 +964,8 @@ var InngestRun = class extends Run {
288
964
  tracingOptions,
289
965
  closeOnSuspend = true,
290
966
  initialState,
291
- outputOptions
967
+ outputOptions,
968
+ perStep
292
969
  } = {}) {
293
970
  if (this.closeStreamAction && this.streamOutput) {
294
971
  return this.streamOutput;
@@ -324,7 +1001,8 @@ var InngestRun = class extends Run {
324
1001
  initialState,
325
1002
  tracingOptions,
326
1003
  outputOptions,
327
- format: "vnext"
1004
+ format: "vnext",
1005
+ perStep
328
1006
  });
329
1007
  let executionResults;
330
1008
  try {
@@ -355,24 +1033,112 @@ var InngestRun = class extends Run {
355
1033
  });
356
1034
  return this.streamOutput;
357
1035
  }
358
- streamVNext(args = {}) {
359
- return this.stream(args);
1036
+ timeTravelStream({
1037
+ inputData,
1038
+ resumeData,
1039
+ initialState,
1040
+ step,
1041
+ context,
1042
+ nestedStepsContext,
1043
+ requestContext,
1044
+ tracingOptions,
1045
+ outputOptions,
1046
+ perStep
1047
+ }) {
1048
+ this.closeStreamAction = async () => {
1049
+ };
1050
+ const self = this;
1051
+ const stream = new ReadableStream({
1052
+ async start(controller) {
1053
+ const unwatch = self.watch(async ({ type, from = ChunkFrom.WORKFLOW, payload }) => {
1054
+ controller.enqueue({
1055
+ type,
1056
+ runId: self.runId,
1057
+ from,
1058
+ payload: {
1059
+ stepName: payload?.id,
1060
+ ...payload
1061
+ }
1062
+ });
1063
+ });
1064
+ self.closeStreamAction = async () => {
1065
+ unwatch();
1066
+ try {
1067
+ controller.close();
1068
+ } catch (err) {
1069
+ console.error("Error closing stream:", err);
1070
+ }
1071
+ };
1072
+ const executionResultsPromise = self._timeTravel({
1073
+ inputData,
1074
+ step,
1075
+ context,
1076
+ nestedStepsContext,
1077
+ resumeData,
1078
+ initialState,
1079
+ requestContext,
1080
+ tracingOptions,
1081
+ outputOptions,
1082
+ perStep
1083
+ });
1084
+ self.executionResults = executionResultsPromise;
1085
+ let executionResults;
1086
+ try {
1087
+ executionResults = await executionResultsPromise;
1088
+ self.closeStreamAction?.().catch(() => {
1089
+ });
1090
+ if (self.streamOutput) {
1091
+ self.streamOutput.updateResults(executionResults);
1092
+ }
1093
+ } catch (err) {
1094
+ self.streamOutput?.rejectResults(err);
1095
+ self.closeStreamAction?.().catch(() => {
1096
+ });
1097
+ }
1098
+ }
1099
+ });
1100
+ this.streamOutput = new WorkflowRunOutput({
1101
+ runId: this.runId,
1102
+ workflowId: this.workflowId,
1103
+ stream
1104
+ });
1105
+ return this.streamOutput;
1106
+ }
1107
+ /**
1108
+ * Hydrates errors in a failed workflow result back to proper Error instances.
1109
+ * This ensures error.cause chains and custom properties are preserved.
1110
+ */
1111
+ hydrateFailedResult(result) {
1112
+ if (result.status === "failed") {
1113
+ result.error = getErrorFromUnknown(result.error, { serializeStack: false });
1114
+ if (result.steps) {
1115
+ hydrateSerializedStepErrors(result.steps);
1116
+ }
1117
+ }
360
1118
  }
361
1119
  };
1120
+
1121
+ // src/workflow.ts
362
1122
  var InngestWorkflow = class _InngestWorkflow extends Workflow {
363
1123
  #mastra;
364
1124
  inngest;
365
1125
  function;
1126
+ cronFunction;
366
1127
  flowControlConfig;
1128
+ cronConfig;
367
1129
  constructor(params, inngest) {
368
- const { concurrency, rateLimit, throttle, debounce, priority, ...workflowParams } = params;
1130
+ const { concurrency, rateLimit, throttle, debounce, priority, cron, inputData, initialState, ...workflowParams } = params;
369
1131
  super(workflowParams);
1132
+ this.engineType = "inngest";
370
1133
  const flowControlEntries = Object.entries({ concurrency, rateLimit, throttle, debounce, priority }).filter(
371
1134
  ([_, value]) => value !== void 0
372
1135
  );
373
1136
  this.flowControlConfig = flowControlEntries.length > 0 ? Object.fromEntries(flowControlEntries) : void 0;
374
1137
  this.#mastra = params.mastra;
375
1138
  this.inngest = inngest;
1139
+ if (cron) {
1140
+ this.cronConfig = { cron, inputData, initialState };
1141
+ }
376
1142
  }
377
1143
  async listWorkflowRuns(args) {
378
1144
  const storage = this.#mastra?.getStorage();
@@ -380,18 +1146,14 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
380
1146
  this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
381
1147
  return { runs: [], total: 0 };
382
1148
  }
383
- return storage.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
384
- }
385
- async getWorkflowRunById(runId) {
386
- const storage = this.#mastra?.getStorage();
387
- if (!storage) {
388
- this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
389
- return this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null;
1149
+ const workflowsStore = await storage.getStore("workflows");
1150
+ if (!workflowsStore) {
1151
+ return { runs: [], total: 0 };
390
1152
  }
391
- const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
392
- return run ?? (this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null);
1153
+ return workflowsStore.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
393
1154
  }
394
1155
  __registerMastra(mastra) {
1156
+ super.__registerMastra(mastra);
395
1157
  this.#mastra = mastra;
396
1158
  this.executionEngine.__registerMastra(mastra);
397
1159
  const updateNested = (step) => {
@@ -422,7 +1184,9 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
422
1184
  mastra: this.#mastra,
423
1185
  retryConfig: this.retryConfig,
424
1186
  cleanup: () => this.runs.delete(runIdToUse),
425
- workflowSteps: this.steps
1187
+ workflowSteps: this.steps,
1188
+ workflowEngineType: this.engineType,
1189
+ validateInputs: this.options.validateInputs
426
1190
  },
427
1191
  this.inngest
428
1192
  );
@@ -431,9 +1195,13 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
431
1195
  workflowStatus: run.workflowRunStatus,
432
1196
  stepResults: {}
433
1197
  });
434
- const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse, false);
435
- if (!workflowSnapshotInStorage && shouldPersistSnapshot) {
436
- await this.mastra?.getStorage()?.persistWorkflowSnapshot({
1198
+ const existingRun = await this.getWorkflowRunById(runIdToUse, {
1199
+ withNestedWorkflows: false
1200
+ });
1201
+ const existsInStorage = existingRun && !existingRun.isFromInMemory;
1202
+ if (!existsInStorage && shouldPersistSnapshot) {
1203
+ const workflowsStore = await this.mastra?.getStorage()?.getStore("workflows");
1204
+ await workflowsStore?.persistWorkflowSnapshot({
437
1205
  workflowName: this.id,
438
1206
  runId: runIdToUse,
439
1207
  resourceId: options?.resourceId,
@@ -443,19 +1211,43 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
443
1211
  value: {},
444
1212
  context: {},
445
1213
  activePaths: [],
1214
+ activeStepsPath: {},
446
1215
  waitingPaths: {},
447
1216
  serializedStepGraph: this.serializedStepGraph,
448
1217
  suspendedPaths: {},
449
1218
  resumeLabels: {},
450
1219
  result: void 0,
451
1220
  error: void 0,
452
- // @ts-ignore
453
1221
  timestamp: Date.now()
454
1222
  }
455
1223
  });
456
1224
  }
457
1225
  return run;
458
1226
  }
1227
+ //createCronFunction is only called if cronConfig.cron is defined.
1228
+ createCronFunction() {
1229
+ if (this.cronFunction) {
1230
+ return this.cronFunction;
1231
+ }
1232
+ this.cronFunction = this.inngest.createFunction(
1233
+ {
1234
+ id: `workflow.${this.id}.cron`,
1235
+ retries: 0,
1236
+ cancelOn: [{ event: `cancel.workflow.${this.id}` }],
1237
+ ...this.flowControlConfig
1238
+ },
1239
+ { cron: this.cronConfig?.cron ?? "" },
1240
+ async () => {
1241
+ const run = await this.createRun();
1242
+ const result = await run.start({
1243
+ inputData: this.cronConfig?.inputData,
1244
+ initialState: this.cronConfig?.initialState
1245
+ });
1246
+ return { result, runId: run.runId };
1247
+ }
1248
+ );
1249
+ return this.cronFunction;
1250
+ }
459
1251
  getFunction() {
460
1252
  if (this.function) {
461
1253
  return this.function;
@@ -463,42 +1255,20 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
463
1255
  this.function = this.inngest.createFunction(
464
1256
  {
465
1257
  id: `workflow.${this.id}`,
466
- // @ts-ignore
467
- retries: this.retryConfig?.attempts ?? 0,
1258
+ retries: 0,
468
1259
  cancelOn: [{ event: `cancel.workflow.${this.id}` }],
469
1260
  // Spread flow control configuration
470
1261
  ...this.flowControlConfig
471
1262
  },
472
1263
  { event: `workflow.${this.id}` },
473
1264
  async ({ event, step, attempt, publish }) => {
474
- let { inputData, initialState, runId, resourceId, resume, outputOptions, format } = event.data;
1265
+ let { inputData, initialState, runId, resourceId, resume, outputOptions, format, timeTravel, perStep } = event.data;
475
1266
  if (!runId) {
476
1267
  runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
477
1268
  return randomUUID();
478
1269
  });
479
1270
  }
480
- const emitter = {
481
- emit: async (event2, data) => {
482
- if (!publish) {
483
- return;
484
- }
485
- try {
486
- await publish({
487
- channel: `workflow:${this.id}:${runId}`,
488
- topic: event2,
489
- data
490
- });
491
- } catch (err) {
492
- this.logger.error("Error emitting event: " + (err?.stack ?? err?.message ?? err));
493
- }
494
- },
495
- on: (_event, _callback) => {
496
- },
497
- off: (_event, _callback) => {
498
- },
499
- once: (_event, _callback) => {
500
- }
501
- };
1271
+ const pubsub = new InngestPubSub(this.inngest, this.id, publish);
502
1272
  const engine = new InngestExecutionEngine(this.#mastra, step, attempt, this.options);
503
1273
  const result = await engine.execute({
504
1274
  workflowId: this.id,
@@ -508,23 +1278,32 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
508
1278
  serializedStepGraph: this.serializedStepGraph,
509
1279
  input: inputData,
510
1280
  initialState,
511
- emitter,
1281
+ pubsub,
512
1282
  retryConfig: this.retryConfig,
513
- requestContext: new RequestContext(),
514
- // TODO
1283
+ requestContext: new RequestContext(Object.entries(event.data.requestContext ?? {})),
515
1284
  resume,
1285
+ timeTravel,
1286
+ perStep,
516
1287
  format,
517
1288
  abortController: new AbortController(),
518
1289
  // currentSpan: undefined, // TODO: Pass actual parent Span from workflow execution context
519
1290
  outputOptions,
520
- writableStream: new WritableStream({
521
- write(chunk) {
522
- void emitter.emit("watch", chunk).catch(() => {
1291
+ outputWriter: async (chunk) => {
1292
+ try {
1293
+ await pubsub.publish(`workflow.events.v2.${runId}`, {
1294
+ type: "watch",
1295
+ runId,
1296
+ data: chunk
523
1297
  });
1298
+ } catch (err) {
1299
+ this.logger.debug?.("Failed to publish watch event:", err);
524
1300
  }
525
- })
1301
+ }
526
1302
  });
527
1303
  await step.run(`workflow.${this.id}.finalize`, async () => {
1304
+ if (result.status !== "paused") {
1305
+ await engine.invokeLifecycleCallbacksInternal(result);
1306
+ }
528
1307
  if (result.status === "failed") {
529
1308
  throw new NonRetriableError(`Workflow failed`, {
530
1309
  cause: result
@@ -551,33 +1330,65 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
551
1330
  });
552
1331
  }
553
1332
  getFunctions() {
554
- return [this.getFunction(), ...this.getNestedFunctions(this.executionGraph.steps)];
1333
+ return [
1334
+ this.getFunction(),
1335
+ ...this.cronConfig?.cron ? [this.createCronFunction()] : [],
1336
+ ...this.getNestedFunctions(this.executionGraph.steps)
1337
+ ];
555
1338
  }
556
1339
  };
557
- function isAgent(params) {
558
- return params?.component === "AGENT";
559
- }
560
- function isTool(params) {
561
- return params instanceof Tool;
1340
+ function serve({
1341
+ mastra,
1342
+ inngest,
1343
+ functions: userFunctions = [],
1344
+ registerOptions
1345
+ }) {
1346
+ const wfs = mastra.listWorkflows();
1347
+ const workflowFunctions = Array.from(
1348
+ new Set(
1349
+ Object.values(wfs).flatMap((wf) => {
1350
+ if (wf instanceof InngestWorkflow) {
1351
+ wf.__registerMastra(mastra);
1352
+ return wf.getFunctions();
1353
+ }
1354
+ return [];
1355
+ })
1356
+ )
1357
+ );
1358
+ return serve$1({
1359
+ ...registerOptions,
1360
+ client: inngest,
1361
+ functions: [...workflowFunctions, ...userFunctions]
1362
+ });
562
1363
  }
563
- function createStep(params, agentOptions) {
564
- if (isAgent(params)) {
1364
+
1365
+ // src/types.ts
1366
+ var _compatibilityCheck = true;
1367
+
1368
+ // src/index.ts
1369
+ function createStep(params, agentOrToolOptions) {
1370
+ if (params instanceof InngestWorkflow) {
1371
+ return params;
1372
+ }
1373
+ if (params instanceof Agent) {
1374
+ const options = agentOrToolOptions;
1375
+ const outputSchema = options?.structuredOutput?.schema ?? z.object({ text: z.string() });
1376
+ const { retries, scorers, ...agentOptions } = options ?? {};
565
1377
  return {
566
1378
  id: params.name,
567
1379
  description: params.getDescription(),
568
- // @ts-ignore
569
1380
  inputSchema: z.object({
570
1381
  prompt: z.string()
571
1382
  // resourceId: z.string().optional(),
572
1383
  // threadId: z.string().optional(),
573
1384
  }),
574
- // @ts-ignore
575
- outputSchema: z.object({
576
- text: z.string()
577
- }),
1385
+ outputSchema,
1386
+ retries,
1387
+ scorers,
578
1388
  execute: async ({
579
1389
  inputData,
580
- [EMITTER_SYMBOL]: emitter,
1390
+ runId,
1391
+ [PUBSUB_SYMBOL]: pubsub,
581
1392
  [STREAM_FORMAT_SYMBOL]: streamFormat,
582
1393
  requestContext,
583
1394
  tracingContext,
@@ -590,6 +1401,7 @@ function createStep(params, agentOptions) {
590
1401
  streamPromise.resolve = resolve;
591
1402
  streamPromise.reject = reject;
592
1403
  });
1404
+ let structuredResult = null;
593
1405
  const toolData = {
594
1406
  name: params.name,
595
1407
  args: inputData
@@ -603,6 +1415,10 @@ function createStep(params, agentOptions) {
603
1415
  requestContext,
604
1416
  tracingContext,
605
1417
  onFinish: (result) => {
1418
+ const resultWithObject = result;
1419
+ if (agentOptions?.structuredOutput?.schema && resultWithObject.object) {
1420
+ structuredResult = resultWithObject.object;
1421
+ }
606
1422
  streamPromise.resolve(result.text);
607
1423
  void agentOptions?.onFinish?.(result);
608
1424
  },
@@ -615,6 +1431,10 @@ function createStep(params, agentOptions) {
615
1431
  requestContext,
616
1432
  tracingContext,
617
1433
  onFinish: (result) => {
1434
+ const resultWithObject = result;
1435
+ if (agentOptions?.structuredOutput?.schema && resultWithObject.object) {
1436
+ structuredResult = resultWithObject.object;
1437
+ }
618
1438
  streamPromise.resolve(result.text);
619
1439
  void agentOptions?.onFinish?.(result);
620
1440
  },
@@ -623,22 +1443,24 @@ function createStep(params, agentOptions) {
623
1443
  stream = modelOutput.fullStream;
624
1444
  }
625
1445
  if (streamFormat === "legacy") {
626
- await emitter.emit("watch", {
627
- type: "tool-call-streaming-start",
628
- ...toolData ?? {}
1446
+ await pubsub.publish(`workflow.events.v2.${runId}`, {
1447
+ type: "watch",
1448
+ runId,
1449
+ data: { type: "tool-call-streaming-start", ...toolData ?? {} }
629
1450
  });
630
1451
  for await (const chunk of stream) {
631
1452
  if (chunk.type === "text-delta") {
632
- await emitter.emit("watch", {
633
- type: "tool-call-delta",
634
- ...toolData ?? {},
635
- argsTextDelta: chunk.textDelta
1453
+ await pubsub.publish(`workflow.events.v2.${runId}`, {
1454
+ type: "watch",
1455
+ runId,
1456
+ data: { type: "tool-call-delta", ...toolData ?? {}, argsTextDelta: chunk.textDelta }
636
1457
  });
637
1458
  }
638
1459
  }
639
- await emitter.emit("watch", {
640
- type: "tool-call-streaming-finish",
641
- ...toolData ?? {}
1460
+ await pubsub.publish(`workflow.events.v2.${runId}`, {
1461
+ type: "watch",
1462
+ runId,
1463
+ data: { type: "tool-call-streaming-finish", ...toolData ?? {} }
642
1464
  });
643
1465
  } else {
644
1466
  for await (const chunk of stream) {
@@ -648,6 +1470,9 @@ function createStep(params, agentOptions) {
648
1470
  if (abortSignal.aborted) {
649
1471
  return abort();
650
1472
  }
1473
+ if (structuredResult !== null) {
1474
+ return structuredResult;
1475
+ }
651
1476
  return {
652
1477
  text: await streamPromise.promise
653
1478
  };
@@ -655,26 +1480,47 @@ function createStep(params, agentOptions) {
655
1480
  component: params.component
656
1481
  };
657
1482
  }
658
- if (isTool(params)) {
1483
+ if (params instanceof Tool) {
1484
+ const toolOpts = agentOrToolOptions;
659
1485
  if (!params.inputSchema || !params.outputSchema) {
660
1486
  throw new Error("Tool must have input and output schemas defined");
661
1487
  }
662
1488
  return {
663
1489
  // TODO: tool probably should have strong id type
664
- // @ts-ignore
665
1490
  id: params.id,
666
1491
  description: params.description,
667
1492
  inputSchema: params.inputSchema,
668
1493
  outputSchema: params.outputSchema,
669
- execute: async ({ inputData, mastra, requestContext, tracingContext, suspend, resumeData }) => {
670
- return params.execute({
671
- context: inputData,
672
- mastra: wrapMastra(mastra, tracingContext),
1494
+ suspendSchema: params.suspendSchema,
1495
+ resumeSchema: params.resumeSchema,
1496
+ retries: toolOpts?.retries,
1497
+ scorers: toolOpts?.scorers,
1498
+ execute: async ({
1499
+ inputData,
1500
+ mastra,
1501
+ requestContext,
1502
+ tracingContext,
1503
+ suspend,
1504
+ resumeData,
1505
+ runId,
1506
+ workflowId,
1507
+ state,
1508
+ setState
1509
+ }) => {
1510
+ const toolContext = {
1511
+ mastra,
673
1512
  requestContext,
674
1513
  tracingContext,
675
- suspend,
676
- resumeData
677
- });
1514
+ workflow: {
1515
+ runId,
1516
+ resumeData,
1517
+ suspend,
1518
+ workflowId,
1519
+ state,
1520
+ setState
1521
+ }
1522
+ };
1523
+ return params.execute(inputData, toolContext);
678
1524
  },
679
1525
  component: "TOOL"
680
1526
  };
@@ -686,6 +1532,8 @@ function createStep(params, agentOptions) {
686
1532
  outputSchema: params.outputSchema,
687
1533
  resumeSchema: params.resumeSchema,
688
1534
  suspendSchema: params.suspendSchema,
1535
+ retries: params.retries,
1536
+ scorers: params.scorers,
689
1537
  execute: params.execute
690
1538
  };
691
1539
  }
@@ -708,6 +1556,8 @@ function init(inngest) {
708
1556
  suspendSchema: step.suspendSchema,
709
1557
  stateSchema: step.stateSchema,
710
1558
  execute: step.execute,
1559
+ retries: step.retries,
1560
+ scorers: step.scorers,
711
1561
  component: step.component
712
1562
  };
713
1563
  },
@@ -717,7 +1567,8 @@ function init(inngest) {
717
1567
  inputSchema: workflow.inputSchema,
718
1568
  outputSchema: workflow.outputSchema,
719
1569
  steps: workflow.stepDefs,
720
- mastra: workflow.mastra
1570
+ mastra: workflow.mastra,
1571
+ options: workflow.options
721
1572
  });
722
1573
  wf.setStepFlow(workflow.stepGraph);
723
1574
  wf.commit();
@@ -725,800 +1576,7 @@ function init(inngest) {
725
1576
  }
726
1577
  };
727
1578
  }
728
- var InngestExecutionEngine = class extends DefaultExecutionEngine {
729
- inngestStep;
730
- inngestAttempts;
731
- constructor(mastra, inngestStep, inngestAttempts = 0, options) {
732
- super({ mastra, options });
733
- this.inngestStep = inngestStep;
734
- this.inngestAttempts = inngestAttempts;
735
- }
736
- async fmtReturnValue(emitter, stepResults, lastOutput, error) {
737
- const base = {
738
- status: lastOutput.status,
739
- steps: stepResults
740
- };
741
- if (lastOutput.status === "success") {
742
- base.result = lastOutput.output;
743
- } else if (lastOutput.status === "failed") {
744
- base.error = error instanceof Error ? error?.stack ?? error.message : lastOutput?.error instanceof Error ? lastOutput.error.message : lastOutput.error ?? error ?? "Unknown error";
745
- } else if (lastOutput.status === "suspended") {
746
- const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
747
- if (stepResult?.status === "suspended") {
748
- const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
749
- return nestedPath ? [[stepId, ...nestedPath]] : [[stepId]];
750
- }
751
- return [];
752
- });
753
- base.suspended = suspendedStepIds;
754
- }
755
- return base;
756
- }
757
- // async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
758
- // await this.inngestStep.sleep(id, duration);
759
- // }
760
- async executeSleep({
761
- workflowId,
762
- runId,
763
- entry,
764
- prevOutput,
765
- stepResults,
766
- emitter,
767
- abortController,
768
- requestContext,
769
- executionContext,
770
- writableStream,
771
- tracingContext
772
- }) {
773
- let { duration, fn } = entry;
774
- const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
775
- type: SpanType.WORKFLOW_SLEEP,
776
- name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
777
- attributes: {
778
- durationMs: duration,
779
- sleepType: fn ? "dynamic" : "fixed"
780
- },
781
- tracingPolicy: this.options?.tracingPolicy
782
- });
783
- if (fn) {
784
- const stepCallId = randomUUID();
785
- duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
786
- return await fn(
787
- createDeprecationProxy(
788
- {
789
- runId,
790
- workflowId,
791
- mastra: this.mastra,
792
- requestContext,
793
- inputData: prevOutput,
794
- state: executionContext.state,
795
- setState: (state) => {
796
- executionContext.state = state;
797
- },
798
- retryCount: -1,
799
- tracingContext: {
800
- currentSpan: sleepSpan
801
- },
802
- getInitData: () => stepResults?.input,
803
- getStepResult: getStepResult.bind(this, stepResults),
804
- // TODO: this function shouldn't have suspend probably?
805
- suspend: async (_suspendPayload) => {
806
- },
807
- bail: () => {
808
- },
809
- abort: () => {
810
- abortController?.abort();
811
- },
812
- [EMITTER_SYMBOL]: emitter,
813
- [STREAM_FORMAT_SYMBOL]: executionContext.format,
814
- engine: { step: this.inngestStep },
815
- abortSignal: abortController?.signal,
816
- writer: new ToolStream(
817
- {
818
- prefix: "workflow-step",
819
- callId: stepCallId,
820
- name: "sleep",
821
- runId
822
- },
823
- writableStream
824
- )
825
- },
826
- {
827
- paramName: "runCount",
828
- deprecationMessage: runCountDeprecationMessage,
829
- logger: this.logger
830
- }
831
- )
832
- );
833
- });
834
- sleepSpan?.update({
835
- attributes: {
836
- durationMs: duration
837
- }
838
- });
839
- }
840
- try {
841
- await this.inngestStep.sleep(entry.id, !duration || duration < 0 ? 0 : duration);
842
- sleepSpan?.end();
843
- } catch (e) {
844
- sleepSpan?.error({ error: e });
845
- throw e;
846
- }
847
- }
848
- async executeSleepUntil({
849
- workflowId,
850
- runId,
851
- entry,
852
- prevOutput,
853
- stepResults,
854
- emitter,
855
- abortController,
856
- requestContext,
857
- executionContext,
858
- writableStream,
859
- tracingContext
860
- }) {
861
- let { date, fn } = entry;
862
- const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
863
- type: SpanType.WORKFLOW_SLEEP,
864
- name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
865
- attributes: {
866
- untilDate: date,
867
- durationMs: date ? Math.max(0, date.getTime() - Date.now()) : void 0,
868
- sleepType: fn ? "dynamic" : "fixed"
869
- },
870
- tracingPolicy: this.options?.tracingPolicy
871
- });
872
- if (fn) {
873
- date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
874
- const stepCallId = randomUUID();
875
- return await fn(
876
- createDeprecationProxy(
877
- {
878
- runId,
879
- workflowId,
880
- mastra: this.mastra,
881
- requestContext,
882
- inputData: prevOutput,
883
- state: executionContext.state,
884
- setState: (state) => {
885
- executionContext.state = state;
886
- },
887
- retryCount: -1,
888
- tracingContext: {
889
- currentSpan: sleepUntilSpan
890
- },
891
- getInitData: () => stepResults?.input,
892
- getStepResult: getStepResult.bind(this, stepResults),
893
- // TODO: this function shouldn't have suspend probably?
894
- suspend: async (_suspendPayload) => {
895
- },
896
- bail: () => {
897
- },
898
- abort: () => {
899
- abortController?.abort();
900
- },
901
- [EMITTER_SYMBOL]: emitter,
902
- [STREAM_FORMAT_SYMBOL]: executionContext.format,
903
- engine: { step: this.inngestStep },
904
- abortSignal: abortController?.signal,
905
- writer: new ToolStream(
906
- {
907
- prefix: "workflow-step",
908
- callId: stepCallId,
909
- name: "sleep",
910
- runId
911
- },
912
- writableStream
913
- )
914
- },
915
- {
916
- paramName: "runCount",
917
- deprecationMessage: runCountDeprecationMessage,
918
- logger: this.logger
919
- }
920
- )
921
- );
922
- });
923
- if (date && !(date instanceof Date)) {
924
- date = new Date(date);
925
- }
926
- const time = !date ? 0 : date.getTime() - Date.now();
927
- sleepUntilSpan?.update({
928
- attributes: {
929
- durationMs: Math.max(0, time)
930
- }
931
- });
932
- }
933
- if (!(date instanceof Date)) {
934
- sleepUntilSpan?.end();
935
- return;
936
- }
937
- try {
938
- await this.inngestStep.sleepUntil(entry.id, date);
939
- sleepUntilSpan?.end();
940
- } catch (e) {
941
- sleepUntilSpan?.error({ error: e });
942
- throw e;
943
- }
944
- }
945
- async executeStep({
946
- step,
947
- stepResults,
948
- executionContext,
949
- resume,
950
- prevOutput,
951
- emitter,
952
- abortController,
953
- requestContext,
954
- tracingContext,
955
- writableStream,
956
- disableScorers
957
- }) {
958
- const stepSpan = tracingContext?.currentSpan?.createChildSpan({
959
- name: `workflow step: '${step.id}'`,
960
- type: SpanType.WORKFLOW_STEP,
961
- input: prevOutput,
962
- attributes: {
963
- stepId: step.id
964
- },
965
- tracingPolicy: this.options?.tracingPolicy
966
- });
967
- const { inputData, validationError } = await validateStepInput({
968
- prevOutput,
969
- step,
970
- validateInputs: this.options?.validateInputs ?? false
971
- });
972
- const startedAt = await this.inngestStep.run(
973
- `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
974
- async () => {
975
- const startedAt2 = Date.now();
976
- await emitter.emit("watch", {
977
- type: "workflow-step-start",
978
- payload: {
979
- id: step.id,
980
- status: "running",
981
- payload: inputData,
982
- startedAt: startedAt2
983
- }
984
- });
985
- return startedAt2;
986
- }
987
- );
988
- if (step instanceof InngestWorkflow) {
989
- const isResume = !!resume?.steps?.length;
990
- let result;
991
- let runId;
992
- try {
993
- if (isResume) {
994
- runId = stepResults[resume?.steps?.[0]]?.suspendPayload?.__workflow_meta?.runId ?? randomUUID();
995
- const snapshot = await this.mastra?.getStorage()?.loadWorkflowSnapshot({
996
- workflowName: step.id,
997
- runId
998
- });
999
- const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
1000
- function: step.getFunction(),
1001
- data: {
1002
- inputData,
1003
- initialState: executionContext.state ?? snapshot?.value ?? {},
1004
- runId,
1005
- resume: {
1006
- runId,
1007
- steps: resume.steps.slice(1),
1008
- stepResults: snapshot?.context,
1009
- resumePayload: resume.resumePayload,
1010
- // @ts-ignore
1011
- resumePath: snapshot?.suspendedPaths?.[resume.steps?.[1]]
1012
- },
1013
- outputOptions: { includeState: true }
1014
- }
1015
- });
1016
- result = invokeResp.result;
1017
- runId = invokeResp.runId;
1018
- executionContext.state = invokeResp.result.state;
1019
- } else {
1020
- const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
1021
- function: step.getFunction(),
1022
- data: {
1023
- inputData,
1024
- initialState: executionContext.state ?? {},
1025
- outputOptions: { includeState: true }
1026
- }
1027
- });
1028
- result = invokeResp.result;
1029
- runId = invokeResp.runId;
1030
- executionContext.state = invokeResp.result.state;
1031
- }
1032
- } catch (e) {
1033
- const errorCause = e?.cause;
1034
- if (errorCause && typeof errorCause === "object") {
1035
- result = errorCause;
1036
- runId = errorCause.runId || randomUUID();
1037
- } else {
1038
- runId = randomUUID();
1039
- result = {
1040
- status: "failed",
1041
- error: e instanceof Error ? e : new Error(String(e)),
1042
- steps: {},
1043
- input: inputData
1044
- };
1045
- }
1046
- }
1047
- const res = await this.inngestStep.run(
1048
- `workflow.${executionContext.workflowId}.step.${step.id}.nestedwf-results`,
1049
- async () => {
1050
- if (result.status === "failed") {
1051
- await emitter.emit("watch", {
1052
- type: "workflow-step-result",
1053
- payload: {
1054
- id: step.id,
1055
- status: "failed",
1056
- error: result?.error,
1057
- payload: prevOutput
1058
- }
1059
- });
1060
- return { executionContext, result: { status: "failed", error: result?.error } };
1061
- } else if (result.status === "suspended") {
1062
- const suspendedSteps = Object.entries(result.steps).filter(([_stepName, stepResult]) => {
1063
- const stepRes2 = stepResult;
1064
- return stepRes2?.status === "suspended";
1065
- });
1066
- for (const [stepName, stepResult] of suspendedSteps) {
1067
- const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
1068
- executionContext.suspendedPaths[step.id] = executionContext.executionPath;
1069
- await emitter.emit("watch", {
1070
- type: "workflow-step-suspended",
1071
- payload: {
1072
- id: step.id,
1073
- status: "suspended"
1074
- }
1075
- });
1076
- return {
1077
- executionContext,
1078
- result: {
1079
- status: "suspended",
1080
- payload: stepResult.payload,
1081
- suspendPayload: {
1082
- ...stepResult?.suspendPayload,
1083
- __workflow_meta: { runId, path: suspendPath }
1084
- }
1085
- }
1086
- };
1087
- }
1088
- return {
1089
- executionContext,
1090
- result: {
1091
- status: "suspended",
1092
- payload: {}
1093
- }
1094
- };
1095
- }
1096
- await emitter.emit("watch", {
1097
- type: "workflow-step-result",
1098
- payload: {
1099
- id: step.id,
1100
- status: "success",
1101
- output: result?.result
1102
- }
1103
- });
1104
- await emitter.emit("watch", {
1105
- type: "workflow-step-finish",
1106
- payload: {
1107
- id: step.id,
1108
- metadata: {}
1109
- }
1110
- });
1111
- return { executionContext, result: { status: "success", output: result?.result } };
1112
- }
1113
- );
1114
- Object.assign(executionContext, res.executionContext);
1115
- return {
1116
- ...res.result,
1117
- startedAt,
1118
- endedAt: Date.now(),
1119
- payload: inputData,
1120
- resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1121
- resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1122
- };
1123
- }
1124
- const stepCallId = randomUUID();
1125
- let stepRes;
1126
- try {
1127
- stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
1128
- let execResults;
1129
- let suspended;
1130
- let bailed;
1131
- try {
1132
- if (validationError) {
1133
- throw validationError;
1134
- }
1135
- const result = await step.execute({
1136
- runId: executionContext.runId,
1137
- mastra: this.mastra,
1138
- requestContext,
1139
- writer: new ToolStream(
1140
- {
1141
- prefix: "workflow-step",
1142
- callId: stepCallId,
1143
- name: step.id,
1144
- runId: executionContext.runId
1145
- },
1146
- writableStream
1147
- ),
1148
- state: executionContext?.state ?? {},
1149
- setState: (state) => {
1150
- executionContext.state = state;
1151
- },
1152
- inputData,
1153
- resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
1154
- tracingContext: {
1155
- currentSpan: stepSpan
1156
- },
1157
- getInitData: () => stepResults?.input,
1158
- getStepResult: getStepResult.bind(this, stepResults),
1159
- suspend: async (suspendPayload, suspendOptions) => {
1160
- executionContext.suspendedPaths[step.id] = executionContext.executionPath;
1161
- if (suspendOptions?.resumeLabel) {
1162
- const resumeLabel = Array.isArray(suspendOptions.resumeLabel) ? suspendOptions.resumeLabel : [suspendOptions.resumeLabel];
1163
- for (const label of resumeLabel) {
1164
- executionContext.resumeLabels[label] = {
1165
- stepId: step.id,
1166
- foreachIndex: executionContext.foreachIndex
1167
- };
1168
- }
1169
- }
1170
- suspended = { payload: suspendPayload };
1171
- },
1172
- bail: (result2) => {
1173
- bailed = { payload: result2 };
1174
- },
1175
- resume: {
1176
- steps: resume?.steps?.slice(1) || [],
1177
- resumePayload: resume?.resumePayload,
1178
- // @ts-ignore
1179
- runId: stepResults[step.id]?.suspendPayload?.__workflow_meta?.runId
1180
- },
1181
- [EMITTER_SYMBOL]: emitter,
1182
- [STREAM_FORMAT_SYMBOL]: executionContext.format,
1183
- engine: {
1184
- step: this.inngestStep
1185
- },
1186
- abortSignal: abortController.signal
1187
- });
1188
- const endedAt = Date.now();
1189
- execResults = {
1190
- status: "success",
1191
- output: result,
1192
- startedAt,
1193
- endedAt,
1194
- payload: inputData,
1195
- resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1196
- resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1197
- };
1198
- } catch (e) {
1199
- const stepFailure = {
1200
- status: "failed",
1201
- payload: inputData,
1202
- error: e instanceof Error ? e.message : String(e),
1203
- endedAt: Date.now(),
1204
- startedAt,
1205
- resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1206
- resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1207
- };
1208
- execResults = stepFailure;
1209
- const fallbackErrorMessage = `Step ${step.id} failed`;
1210
- stepSpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
1211
- throw new RetryAfterError(execResults.error ?? fallbackErrorMessage, executionContext.retryConfig.delay, {
1212
- cause: execResults
1213
- });
1214
- }
1215
- if (suspended) {
1216
- execResults = {
1217
- status: "suspended",
1218
- suspendPayload: suspended.payload,
1219
- ...execResults.output ? { suspendOutput: execResults.output } : {},
1220
- payload: inputData,
1221
- suspendedAt: Date.now(),
1222
- startedAt,
1223
- resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1224
- resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1225
- };
1226
- } else if (bailed) {
1227
- execResults = {
1228
- status: "bailed",
1229
- output: bailed.payload,
1230
- payload: inputData,
1231
- endedAt: Date.now(),
1232
- startedAt
1233
- };
1234
- }
1235
- if (execResults.status === "suspended") {
1236
- await emitter.emit("watch", {
1237
- type: "workflow-step-suspended",
1238
- payload: {
1239
- id: step.id,
1240
- ...execResults
1241
- }
1242
- });
1243
- } else {
1244
- await emitter.emit("watch", {
1245
- type: "workflow-step-result",
1246
- payload: {
1247
- id: step.id,
1248
- ...execResults
1249
- }
1250
- });
1251
- await emitter.emit("watch", {
1252
- type: "workflow-step-finish",
1253
- payload: {
1254
- id: step.id,
1255
- metadata: {}
1256
- }
1257
- });
1258
- }
1259
- stepSpan?.end({ output: execResults });
1260
- return { result: execResults, executionContext, stepResults };
1261
- });
1262
- } catch (e) {
1263
- const stepFailure = e instanceof Error ? e?.cause : {
1264
- status: "failed",
1265
- error: e instanceof Error ? e.message : String(e),
1266
- payload: inputData,
1267
- startedAt,
1268
- endedAt: Date.now()
1269
- };
1270
- stepRes = {
1271
- result: stepFailure,
1272
- executionContext,
1273
- stepResults: {
1274
- ...stepResults,
1275
- [step.id]: stepFailure
1276
- }
1277
- };
1278
- }
1279
- if (disableScorers !== false && stepRes.result.status === "success") {
1280
- await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}.score`, async () => {
1281
- if (step.scorers) {
1282
- await this.runScorers({
1283
- scorers: step.scorers,
1284
- runId: executionContext.runId,
1285
- input: inputData,
1286
- output: stepRes.result,
1287
- workflowId: executionContext.workflowId,
1288
- stepId: step.id,
1289
- requestContext,
1290
- disableScorers,
1291
- tracingContext: { currentSpan: stepSpan }
1292
- });
1293
- }
1294
- });
1295
- }
1296
- Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
1297
- Object.assign(stepResults, stepRes.stepResults);
1298
- executionContext.state = stepRes.executionContext.state;
1299
- return stepRes.result;
1300
- }
1301
- async persistStepUpdate({
1302
- workflowId,
1303
- runId,
1304
- stepResults,
1305
- resourceId,
1306
- executionContext,
1307
- serializedStepGraph,
1308
- workflowStatus,
1309
- result,
1310
- error
1311
- }) {
1312
- await this.inngestStep.run(
1313
- `workflow.${workflowId}.run.${runId}.path.${JSON.stringify(executionContext.executionPath)}.stepUpdate`,
1314
- async () => {
1315
- const shouldPersistSnapshot = this.options.shouldPersistSnapshot({ stepResults, workflowStatus });
1316
- if (!shouldPersistSnapshot) {
1317
- return;
1318
- }
1319
- await this.mastra?.getStorage()?.persistWorkflowSnapshot({
1320
- workflowName: workflowId,
1321
- runId,
1322
- resourceId,
1323
- snapshot: {
1324
- runId,
1325
- value: executionContext.state,
1326
- context: stepResults,
1327
- activePaths: [],
1328
- suspendedPaths: executionContext.suspendedPaths,
1329
- resumeLabels: executionContext.resumeLabels,
1330
- waitingPaths: {},
1331
- serializedStepGraph,
1332
- status: workflowStatus,
1333
- result,
1334
- error,
1335
- // @ts-ignore
1336
- timestamp: Date.now()
1337
- }
1338
- });
1339
- }
1340
- );
1341
- }
1342
- async executeConditional({
1343
- workflowId,
1344
- runId,
1345
- entry,
1346
- prevOutput,
1347
- stepResults,
1348
- resume,
1349
- executionContext,
1350
- emitter,
1351
- abortController,
1352
- requestContext,
1353
- writableStream,
1354
- disableScorers,
1355
- tracingContext
1356
- }) {
1357
- const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
1358
- type: SpanType.WORKFLOW_CONDITIONAL,
1359
- name: `conditional: '${entry.conditions.length} conditions'`,
1360
- input: prevOutput,
1361
- attributes: {
1362
- conditionCount: entry.conditions.length
1363
- },
1364
- tracingPolicy: this.options?.tracingPolicy
1365
- });
1366
- let execResults;
1367
- const truthyIndexes = (await Promise.all(
1368
- entry.conditions.map(
1369
- (cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
1370
- const evalSpan = conditionalSpan?.createChildSpan({
1371
- type: SpanType.WORKFLOW_CONDITIONAL_EVAL,
1372
- name: `condition: '${index}'`,
1373
- input: prevOutput,
1374
- attributes: {
1375
- conditionIndex: index
1376
- },
1377
- tracingPolicy: this.options?.tracingPolicy
1378
- });
1379
- try {
1380
- const result = await cond(
1381
- createDeprecationProxy(
1382
- {
1383
- runId,
1384
- workflowId,
1385
- mastra: this.mastra,
1386
- requestContext,
1387
- retryCount: -1,
1388
- inputData: prevOutput,
1389
- state: executionContext.state,
1390
- setState: (state) => {
1391
- executionContext.state = state;
1392
- },
1393
- tracingContext: {
1394
- currentSpan: evalSpan
1395
- },
1396
- getInitData: () => stepResults?.input,
1397
- getStepResult: getStepResult.bind(this, stepResults),
1398
- // TODO: this function shouldn't have suspend probably?
1399
- suspend: async (_suspendPayload) => {
1400
- },
1401
- bail: () => {
1402
- },
1403
- abort: () => {
1404
- abortController.abort();
1405
- },
1406
- [EMITTER_SYMBOL]: emitter,
1407
- [STREAM_FORMAT_SYMBOL]: executionContext.format,
1408
- engine: {
1409
- step: this.inngestStep
1410
- },
1411
- abortSignal: abortController.signal,
1412
- writer: new ToolStream(
1413
- {
1414
- prefix: "workflow-step",
1415
- callId: randomUUID(),
1416
- name: "conditional",
1417
- runId
1418
- },
1419
- writableStream
1420
- )
1421
- },
1422
- {
1423
- paramName: "runCount",
1424
- deprecationMessage: runCountDeprecationMessage,
1425
- logger: this.logger
1426
- }
1427
- )
1428
- );
1429
- evalSpan?.end({
1430
- output: result,
1431
- attributes: {
1432
- result: !!result
1433
- }
1434
- });
1435
- return result ? index : null;
1436
- } catch (e) {
1437
- evalSpan?.error({
1438
- error: e instanceof Error ? e : new Error(String(e)),
1439
- attributes: {
1440
- result: false
1441
- }
1442
- });
1443
- return null;
1444
- }
1445
- })
1446
- )
1447
- )).filter((index) => index !== null);
1448
- const stepsToRun = entry.steps.filter((_, index) => truthyIndexes.includes(index));
1449
- conditionalSpan?.update({
1450
- attributes: {
1451
- truthyIndexes,
1452
- selectedSteps: stepsToRun.map((s) => s.type === "step" ? s.step.id : `control-${s.type}`)
1453
- }
1454
- });
1455
- const results = await Promise.all(
1456
- stepsToRun.map(async (step, index) => {
1457
- const currStepResult = stepResults[step.step.id];
1458
- if (currStepResult && currStepResult.status === "success") {
1459
- return currStepResult;
1460
- }
1461
- const result = await this.executeStep({
1462
- step: step.step,
1463
- prevOutput,
1464
- stepResults,
1465
- resume,
1466
- executionContext: {
1467
- workflowId,
1468
- runId,
1469
- executionPath: [...executionContext.executionPath, index],
1470
- suspendedPaths: executionContext.suspendedPaths,
1471
- resumeLabels: executionContext.resumeLabels,
1472
- retryConfig: executionContext.retryConfig,
1473
- state: executionContext.state
1474
- },
1475
- emitter,
1476
- abortController,
1477
- requestContext,
1478
- writableStream,
1479
- disableScorers,
1480
- tracingContext: {
1481
- currentSpan: conditionalSpan
1482
- }
1483
- });
1484
- stepResults[step.step.id] = result;
1485
- return result;
1486
- })
1487
- );
1488
- const hasFailed = results.find((result) => result.status === "failed");
1489
- const hasSuspended = results.find((result) => result.status === "suspended");
1490
- if (hasFailed) {
1491
- execResults = { status: "failed", error: hasFailed.error };
1492
- } else if (hasSuspended) {
1493
- execResults = {
1494
- status: "suspended",
1495
- suspendPayload: hasSuspended.suspendPayload,
1496
- ...hasSuspended.suspendOutput ? { suspendOutput: hasSuspended.suspendOutput } : {}
1497
- };
1498
- } else {
1499
- execResults = {
1500
- status: "success",
1501
- output: results.reduce((acc, result, index) => {
1502
- if (result.status === "success") {
1503
- acc[stepsToRun[index].step.id] = result.output;
1504
- }
1505
- return acc;
1506
- }, {})
1507
- };
1508
- }
1509
- if (execResults.status === "failed") {
1510
- conditionalSpan?.error({
1511
- error: new Error(execResults.error)
1512
- });
1513
- } else {
1514
- conditionalSpan?.end({
1515
- output: execResults.output || execResults
1516
- });
1517
- }
1518
- return execResults;
1519
- }
1520
- };
1521
1579
 
1522
- export { InngestExecutionEngine, InngestRun, InngestWorkflow, createStep, init, serve };
1580
+ export { InngestExecutionEngine, InngestPubSub, InngestRun, InngestWorkflow, _compatibilityCheck, createStep, init, serve };
1523
1581
  //# sourceMappingURL=index.js.map
1524
1582
  //# sourceMappingURL=index.js.map