@mastra/temporal 0.5.0 → 0.6.0-alpha.0

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/worker.js CHANGED
@@ -403,24 +403,55 @@ function hasLocalMastraBinding(ast) {
403
403
  }
404
404
  function createTemporalActivitiesHelperStatements(mastraImportPath, hasMastraBinding, requestContextClassName) {
405
405
  const helperSource = `
406
- function withRequestContext(params) {
406
+ function withRequestContext(params, args) {
407
407
  const { requestContext, initData, ...rest } = params;
408
+ let suspended;
408
409
  return {
409
410
  ...rest,
410
411
  requestContext: new ${requestContextClassName}(requestContext),
411
412
  getInitData: () => initData,
413
+ suspend: async (suspendPayload, options) => {
414
+ const parsedSuspendPayload = await parseWithSchema(args.suspendSchema, suspendPayload, 'Suspend payload');
415
+ suspended = {
416
+ __temporalSuspension: true,
417
+ suspendPayload: parsedSuspendPayload,
418
+ ...(options?.resumeLabel !== undefined && { resumeLabel: options.resumeLabel }),
419
+ };
420
+ },
421
+ __getTemporalSuspension: () => suspended,
412
422
  };
413
423
  }
414
424
 
425
+ async function parseWithSchema(schema, value, label) {
426
+ if (!schema) return value;
427
+ if (schema['~standard']?.validate) {
428
+ const result = await schema['~standard'].validate(value);
429
+ if ('issues' in result && result.issues?.length) {
430
+ throw new Error(label + ' validation failed: ' + result.issues.map(issue => issue.message).join(', '));
431
+ }
432
+ return result.value;
433
+ }
434
+ if (typeof schema.parseAsync === 'function') return schema.parseAsync(value);
435
+ if (typeof schema.parse === 'function') return schema.parse(value);
436
+ return value;
437
+ }
438
+
439
+ async function executeTemporalStep(args, context) {
440
+ const result = await args.execute(context);
441
+ return context.__getTemporalSuspension() ?? result;
442
+ }
443
+
415
444
  function createStep(args) {
416
445
  return {
417
446
  inputSchema: args.inputSchema,
418
447
  requestContextSchema: args.requestContextSchema,
448
+ suspendSchema: args.suspendSchema,
449
+ resumeSchema: args.resumeSchema,
419
450
  execute: async (params) => {
420
451
  ${mastraImportPath ? `
421
452
  const { mastra } = await import(${JSON.stringify(mastraImportPath)});
422
- return args.execute({ ...withRequestContext(params), mastra });
423
- ` : hasMastraBinding ? "return args.execute({ ...withRequestContext(params), mastra });" : "return args.execute(withRequestContext(params));"}
453
+ return executeTemporalStep(args, { ...withRequestContext(params, args), mastra });
454
+ ` : hasMastraBinding ? "return executeTemporalStep(args, { ...withRequestContext(params, args), mastra });" : "return executeTemporalStep(args, withRequestContext(params, args));"}
424
455
  },
425
456
  };
426
457
  }