@mastra/inngest 0.0.0-cloud-storage-adapter-20251106204059 → 0.0.0-cloud-604-map-nested-flow-details-to-side-panel-20251212192149

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