@mastra/temporal 0.1.16-alpha.1 → 0.2.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
@@ -146,6 +146,70 @@ function collectInlineCreateSteps(node, seenNames, statements, onStep) {
146
146
  return false;
147
147
  });
148
148
  }
149
+ function getReturnedCreateStepCall(node) {
150
+ if (!node) {
151
+ return null;
152
+ }
153
+ if (t3.isArrowFunctionExpression(node) && !t3.isBlockStatement(node.body)) {
154
+ return t3.isCallExpression(node.body) && isIdentifierNamed(node.body.callee, "createStep") ? node.body : null;
155
+ }
156
+ if (!t3.isFunctionDeclaration(node) && !t3.isFunctionExpression(node) && !t3.isArrowFunctionExpression(node)) {
157
+ return null;
158
+ }
159
+ if (!t3.isBlockStatement(node.body)) {
160
+ return null;
161
+ }
162
+ for (const statement of node.body.body) {
163
+ if (t3.isReturnStatement(statement) && t3.isCallExpression(statement.argument) && isIdentifierNamed(statement.argument.callee, "createStep")) {
164
+ return statement.argument;
165
+ }
166
+ }
167
+ return null;
168
+ }
169
+ function collectCreateStepFactoryBindings(program3) {
170
+ const factories = /* @__PURE__ */ new Map();
171
+ for (const statement of program3.body) {
172
+ const declaration = t3.isExportNamedDeclaration(statement) ? statement.declaration : statement;
173
+ if (t3.isFunctionDeclaration(declaration) && declaration.id) {
174
+ const createStepCall = getReturnedCreateStepCall(declaration);
175
+ if (createStepCall) {
176
+ factories.set(declaration.id.name, createStepCall);
177
+ }
178
+ continue;
179
+ }
180
+ if (!t3.isVariableDeclaration(declaration)) {
181
+ continue;
182
+ }
183
+ for (const declarator of declaration.declarations) {
184
+ if (!t3.isIdentifier(declarator.id) || !declarator.init) {
185
+ continue;
186
+ }
187
+ const createStepCall = getReturnedCreateStepCall(declarator.init);
188
+ if (createStepCall) {
189
+ factories.set(declarator.id.name, createStepCall);
190
+ }
191
+ }
192
+ }
193
+ return factories;
194
+ }
195
+ function getCreateStepCallFromExpression(node, factoryBindings) {
196
+ if (!node) {
197
+ return null;
198
+ }
199
+ if (t3.isCallExpression(node)) {
200
+ if (isIdentifierNamed(node.callee, "createStep")) {
201
+ return node;
202
+ }
203
+ if (t3.isIdentifier(node.callee)) {
204
+ return factoryBindings.get(node.callee.name) ?? null;
205
+ }
206
+ }
207
+ const returnedCreateStepCall = getReturnedCreateStepCall(node);
208
+ if (returnedCreateStepCall) {
209
+ return returnedCreateStepCall;
210
+ }
211
+ return null;
212
+ }
149
213
  function getCreateStepId(node) {
150
214
  if (!node || !isCreateStepCall(node)) {
151
215
  return null;
@@ -470,6 +534,7 @@ async function buildTemporalActivitiesModule(entryFile, outputDirectory, outputF
470
534
  const seenNames = /* @__PURE__ */ new Set();
471
535
  const strippedNames = /* @__PURE__ */ new Set();
472
536
  const workflowBindingNames = collectWorkflowBindingNames(ast);
537
+ const stepFactoryBindings = collectCreateStepFactoryBindings(ast.program);
473
538
  const sourceFilePath = id;
474
539
  const hasMastraBinding = hasLocalMastraBinding(ast);
475
540
  let helperInserted = false;
@@ -535,11 +600,23 @@ async function buildTemporalActivitiesModule(entryFile, outputDirectory, outputF
535
600
  declarations.push(createPreservedDeclaration(declaration, strippedNames));
536
601
  continue;
537
602
  }
538
- if (isCreateStepCall(declaration.init)) {
539
- seenNames.add(declaration.id.name);
540
- addActivityBinding(declaration.id.name, declaration.init);
541
- statements.push(createExportedStepStatement(declaration.id.name, declaration.init));
542
- continue;
603
+ const createStepCall = getCreateStepCallFromExpression(declaration.init, stepFactoryBindings);
604
+ if (createStepCall) {
605
+ const isFactoryCall = t3.isCallExpression(declaration.init) && t3.isIdentifier(declaration.init.callee) && stepFactoryBindings.has(declaration.init.callee.name);
606
+ if (isFactoryCall) {
607
+ seenNames.add(declaration.id.name);
608
+ addActivityBinding(declaration.id.name, createStepCall);
609
+ statements.push(
610
+ t3.exportNamedDeclaration(t3.variableDeclaration(statement.kind, [t3.cloneNode(declaration, true)]))
611
+ );
612
+ continue;
613
+ }
614
+ if (isCreateStepCall(declaration.init)) {
615
+ seenNames.add(declaration.id.name);
616
+ addActivityBinding(declaration.id.name, createStepCall);
617
+ statements.push(createExportedStepStatement(declaration.id.name, createStepCall));
618
+ continue;
619
+ }
543
620
  }
544
621
  if (hasCreateWorkflowCall(declaration.init)) {
545
622
  workflowBindingNames.add(declaration.id.name);
@@ -576,11 +653,21 @@ async function buildTemporalActivitiesModule(entryFile, outputDirectory, outputF
576
653
  exportedDeclarations.push(createPreservedDeclaration(declaration, strippedNames));
577
654
  continue;
578
655
  }
579
- if (isCreateStepCall(declaration.init)) {
580
- seenNames.add(declaration.id.name);
581
- addActivityBinding(declaration.id.name, declaration.init);
582
- statements.push(createExportedStepStatement(declaration.id.name, declaration.init));
583
- continue;
656
+ const createStepCall = getCreateStepCallFromExpression(declaration.init, stepFactoryBindings);
657
+ if (createStepCall) {
658
+ const isFactoryCall = t3.isCallExpression(declaration.init) && t3.isIdentifier(declaration.init.callee) && stepFactoryBindings.has(declaration.init.callee.name);
659
+ if (isFactoryCall) {
660
+ seenNames.add(declaration.id.name);
661
+ addActivityBinding(declaration.id.name, createStepCall);
662
+ exportedDeclarations.push(createPreservedDeclaration(declaration, strippedNames));
663
+ continue;
664
+ }
665
+ if (isCreateStepCall(declaration.init)) {
666
+ seenNames.add(declaration.id.name);
667
+ addActivityBinding(declaration.id.name, createStepCall);
668
+ statements.push(createExportedStepStatement(declaration.id.name, createStepCall));
669
+ continue;
670
+ }
584
671
  }
585
672
  if (hasCreateWorkflowCall(declaration.init)) {
586
673
  workflowBindingNames.add(declaration.id.name);
@@ -623,6 +710,11 @@ async function buildTemporalActivitiesModule(entryFile, outputDirectory, outputF
623
710
  continue;
624
711
  }
625
712
  if (t3.isExportNamedDeclaration(statement)) {
713
+ if (t3.isFunctionDeclaration(statement.declaration) || t3.isClassDeclaration(statement.declaration) || t3.isTSTypeAliasDeclaration(statement.declaration) || t3.isTSInterfaceDeclaration(statement.declaration) || t3.isTSEnumDeclaration(statement.declaration)) {
714
+ ensureHelperInserted();
715
+ statements.push(statement);
716
+ continue;
717
+ }
626
718
  if (statement.declaration == null && statement.source == null) {
627
719
  const retainedSpecifiers = statement.specifiers.filter(
628
720
  (specifier) => t3.isExportSpecifier(specifier) && t3.isIdentifier(specifier.local) && specifier.local.name !== "mastra" && !workflowBindingNames.has(specifier.local.name) && !seenNames.has(specifier.local.name)
@@ -747,6 +839,13 @@ function parseWorkflowChain(node) {
747
839
  }
748
840
  function collectStepBindings(program3) {
749
841
  const stepBindings = /* @__PURE__ */ new Map();
842
+ const stepFactoryBindings = collectCreateStepFactoryBindings(program3);
843
+ for (const [factoryName, createStepCall] of stepFactoryBindings) {
844
+ const stepId = getCreateStepId(createStepCall);
845
+ if (stepId) {
846
+ stepBindings.set(factoryName, stepId);
847
+ }
848
+ }
750
849
  for (const statement of program3.body) {
751
850
  if (!t3.isVariableDeclaration(statement) && !(t3.isExportNamedDeclaration(statement) && t3.isVariableDeclaration(statement.declaration))) {
752
851
  continue;
@@ -756,7 +855,8 @@ function collectStepBindings(program3) {
756
855
  if (!t3.isIdentifier(declaration.id) || !declaration.init) {
757
856
  continue;
758
857
  }
759
- const stepId = getCreateStepId(declaration.init);
858
+ const createStepCall = getCreateStepCallFromExpression(declaration.init, stepFactoryBindings);
859
+ const stepId = getCreateStepId(createStepCall);
760
860
  if (stepId) {
761
861
  stepBindings.set(declaration.id.name, stepId);
762
862
  }
@@ -796,6 +896,9 @@ function getWorkflowStepName(node, stepBindings) {
796
896
  if (t3.isStringLiteral(node)) {
797
897
  return node.value;
798
898
  }
899
+ if (t3.isCallExpression(node) && t3.isIdentifier(node.callee)) {
900
+ return stepBindings.get(node.callee.name) ?? getCreateStepId(node);
901
+ }
799
902
  return getCreateStepId(node);
800
903
  }
801
904
  function rewriteChainMethod(method, filePath, workflowName, stepBindings, workflowBindings) {