@mastra/temporal 0.0.0 → 0.1.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.
Files changed (55) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/LICENSE.md +30 -0
  3. package/README.md +104 -2
  4. package/dist/chunk-BF6TR7JX.js +9 -0
  5. package/dist/chunk-BF6TR7JX.js.map +1 -0
  6. package/dist/chunk-DYBSPLCJ.cjs +11 -0
  7. package/dist/chunk-DYBSPLCJ.cjs.map +1 -0
  8. package/dist/index.cjs +3 -946
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.ts +0 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +2 -920
  13. package/dist/index.js.map +1 -1
  14. package/dist/mastra-deployer-UCBGECM5.js +39 -0
  15. package/dist/mastra-deployer-UCBGECM5.js.map +1 -0
  16. package/dist/mastra-deployer-YVT5GB5G.cjs +41 -0
  17. package/dist/mastra-deployer-YVT5GB5G.cjs.map +1 -0
  18. package/dist/mastra-deployer.d.ts +16 -0
  19. package/dist/mastra-deployer.d.ts.map +1 -0
  20. package/dist/plugin.d.ts +13 -15
  21. package/dist/plugin.d.ts.map +1 -1
  22. package/dist/temporal-workflow-runtime.mjs +295 -0
  23. package/dist/transforms/activities.d.ts +4 -3
  24. package/dist/transforms/activities.d.ts.map +1 -1
  25. package/dist/transforms/shared.d.ts +2 -7
  26. package/dist/transforms/shared.d.ts.map +1 -1
  27. package/dist/transforms/temporal-workflow-runtime.d.mts +7 -0
  28. package/dist/transforms/temporal-workflow-runtime.d.mts.map +1 -1
  29. package/dist/transforms/workflows.d.ts +3 -11
  30. package/dist/transforms/workflows.d.ts.map +1 -1
  31. package/dist/worker.cjs +1336 -0
  32. package/dist/worker.cjs.map +1 -0
  33. package/dist/worker.d.ts +2 -0
  34. package/dist/worker.d.ts.map +1 -0
  35. package/dist/worker.js +1310 -0
  36. package/dist/worker.js.map +1 -0
  37. package/package.json +30 -24
  38. package/dist/__tests__/__fixtures__/before/index.d.ts +0 -11
  39. package/dist/__tests__/__fixtures__/before/index.d.ts.map +0 -1
  40. package/dist/__tests__/__fixtures__/before/weather-workflow.d.ts +0 -8
  41. package/dist/__tests__/__fixtures__/before/weather-workflow.d.ts.map +0 -1
  42. package/dist/transforms/__fixtures__/activities/weather-workflow/input.d.ts +0 -8
  43. package/dist/transforms/__fixtures__/activities/weather-workflow/input.d.ts.map +0 -1
  44. package/dist/transforms/__fixtures__/activities/weather-workflow/output.d.ts +0 -3
  45. package/dist/transforms/__fixtures__/activities/weather-workflow/output.d.ts.map +0 -1
  46. package/dist/transforms/__fixtures__/workflow/weather-workflow/input.d.ts +0 -8
  47. package/dist/transforms/__fixtures__/workflow/weather-workflow/input.d.ts.map +0 -1
  48. package/dist/transforms/__fixtures__/workflow/weather-workflow/output.d.ts +0 -2
  49. package/dist/transforms/__fixtures__/workflow/weather-workflow/output.d.ts.map +0 -1
  50. package/dist/webpack-loader.cjs +0 -856
  51. package/dist/webpack-loader.cjs.map +0 -1
  52. package/dist/webpack-loader.d.ts +0 -8
  53. package/dist/webpack-loader.d.ts.map +0 -1
  54. package/dist/webpack-plugin.d.ts +0 -18
  55. package/dist/webpack-plugin.d.ts.map +0 -1
@@ -1,856 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/webpack-loader.ts
31
- var webpack_loader_exports = {};
32
- __export(webpack_loader_exports, {
33
- default: () => mastraTemporalWorkflowLoader
34
- });
35
- module.exports = __toCommonJS(webpack_loader_exports);
36
- var import_promises = require("fs/promises");
37
- var import_node_path3 = __toESM(require("path"), 1);
38
-
39
- // src/transforms/workflows.ts
40
- var import_node_fs = require("fs");
41
- var import_node_path2 = __toESM(require("path"), 1);
42
- var import_generator = require("@babel/generator");
43
- var import_parser = require("@babel/parser");
44
- var t2 = __toESM(require("@babel/types"), 1);
45
-
46
- // src/utils.ts
47
- function toWorkflowType(id) {
48
- const camelCased = id.replace(/[-_]+([a-zA-Z0-9])/g, (_, char) => char.toUpperCase());
49
- return camelCased.endsWith("Workflow") ? camelCased : `${camelCased}Workflow`;
50
- }
51
-
52
- // src/transforms/shared.ts
53
- var import_node_path = __toESM(require("path"), 1);
54
- var t = __toESM(require("@babel/types"), 1);
55
- var import_esbuild = require("esbuild");
56
- var parserPlugins = [
57
- "typescript",
58
- "jsx",
59
- "classProperties",
60
- "classPrivateProperties",
61
- "classPrivateMethods",
62
- "topLevelAwait",
63
- "importAttributes",
64
- "decorators-legacy"
65
- ];
66
- function isIdentifierNamed(node, name) {
67
- return t.isIdentifier(node) && node.name === name;
68
- }
69
- function isTemporalHelperModule(source) {
70
- return typeof source === "string" && /(^|\/)temporal\.(ts|tsx|js|jsx|mts|mjs)?$/.test(source);
71
- }
72
- var strippedExternalModules = /* @__PURE__ */ new Set(["@temporalio/client", "@temporalio/envconfig"]);
73
- function isStrippedExternalModule(source) {
74
- return typeof source === "string" && strippedExternalModules.has(source);
75
- }
76
- function collectImportedNames(statement) {
77
- const names = /* @__PURE__ */ new Set();
78
- for (const specifier of statement.specifiers) {
79
- if (t.isImportDefaultSpecifier(specifier) || t.isImportNamespaceSpecifier(specifier) || t.isImportSpecifier(specifier)) {
80
- if (t.isIdentifier(specifier.local)) {
81
- names.add(specifier.local.name);
82
- }
83
- }
84
- }
85
- return names;
86
- }
87
- function nodeReferencesName(node, names) {
88
- let found = false;
89
- walk(node, (current) => {
90
- if (t.isIdentifier(current) && names.has(current.name)) {
91
- found = true;
92
- return false;
93
- }
94
- });
95
- return found;
96
- }
97
- function isWorkflowHelperDestructure(declaration) {
98
- if (!t.isObjectPattern(declaration.id)) {
99
- return false;
100
- }
101
- return declaration.id.properties.some(
102
- (property) => t.isObjectProperty(property) && !property.computed && t.isIdentifier(property.value) && (property.value.name === "createStep" || property.value.name === "createWorkflow")
103
- );
104
- }
105
- function isCreateWorkflowCall(node) {
106
- return t.isCallExpression(node) && isIdentifierNamed(node.callee, "createWorkflow");
107
- }
108
- function isCreateStepCall(node) {
109
- return t.isCallExpression(node) && isIdentifierNamed(node.callee, "createStep");
110
- }
111
- function getObjectPropertyName(property) {
112
- if (property.computed) {
113
- return null;
114
- }
115
- if (t.isIdentifier(property.key)) {
116
- return property.key.name;
117
- }
118
- if (t.isStringLiteral(property.key)) {
119
- return property.key.value;
120
- }
121
- return null;
122
- }
123
- function walk(node, visitor) {
124
- if (!node) {
125
- return;
126
- }
127
- const result = visitor(node);
128
- if (result === false) {
129
- return;
130
- }
131
- const keys = t.VISITOR_KEYS[node.type] ?? [];
132
- for (const key of keys) {
133
- const value = node[key];
134
- if (Array.isArray(value)) {
135
- for (const child of value) {
136
- if (child && typeof child.type === "string") {
137
- walk(child, visitor);
138
- }
139
- }
140
- continue;
141
- }
142
- if (value && typeof value.type === "string") {
143
- walk(value, visitor);
144
- }
145
- }
146
- }
147
- async function transpileModule(sourceText, filePath) {
148
- const loader = import_node_path.default.extname(filePath).slice(1) || "js";
149
- const { code } = await (0, import_esbuild.transform)(sourceText, {
150
- loader,
151
- format: "esm",
152
- platform: "node",
153
- target: "node22",
154
- sourcemap: false,
155
- sourcefile: filePath
156
- });
157
- return code ?? "";
158
- }
159
- function getCreateStepId(node) {
160
- if (!node || !isCreateStepCall(node)) {
161
- return null;
162
- }
163
- const [config] = node.arguments;
164
- if (!t.isObjectExpression(config)) {
165
- return null;
166
- }
167
- for (const property of config.properties) {
168
- if (!t.isObjectProperty(property) && !t.isObjectMethod(property)) {
169
- continue;
170
- }
171
- if (getObjectPropertyName(property) !== "id") {
172
- continue;
173
- }
174
- const value = t.isObjectMethod(property) ? null : property.value;
175
- return t.isStringLiteral(value) ? value.value : null;
176
- }
177
- return null;
178
- }
179
- function shouldCountIdentifierAsReference(parent, key) {
180
- if (!parent) {
181
- return true;
182
- }
183
- if ((t.isObjectProperty(parent) || t.isObjectMethod(parent)) && key === "key" && !parent.computed) {
184
- return false;
185
- }
186
- if (t.isMemberExpression(parent) && key === "property" && !parent.computed) {
187
- return false;
188
- }
189
- if (t.isVariableDeclarator(parent) && key === "id") {
190
- return false;
191
- }
192
- if ((t.isFunctionDeclaration(parent) || t.isFunctionExpression(parent) || t.isArrowFunctionExpression(parent)) && key === "params") {
193
- return false;
194
- }
195
- if ((t.isFunctionDeclaration(parent) || t.isFunctionExpression(parent) || t.isClassDeclaration(parent)) && key === "id") {
196
- return false;
197
- }
198
- if ((t.isImportSpecifier(parent) || t.isImportDefaultSpecifier(parent) || t.isImportNamespaceSpecifier(parent)) && (key === "local" || key === "imported")) {
199
- return false;
200
- }
201
- if (t.isExportSpecifier(parent) && key === "exported") {
202
- return false;
203
- }
204
- if (t.isLabeledStatement(parent) && key === "label") {
205
- return false;
206
- }
207
- if (t.isCatchClause(parent) && key === "param") {
208
- return false;
209
- }
210
- if (t.isRestElement(parent) && key === "argument") {
211
- return false;
212
- }
213
- if (t.isAssignmentPattern(parent) && key === "left") {
214
- return false;
215
- }
216
- if (t.isTSPropertySignature(parent) || t.isTSMethodSignature(parent) || t.isTSExpressionWithTypeArguments(parent)) {
217
- return false;
218
- }
219
- return true;
220
- }
221
- function collectRuntimeReferencedIdentifiers(node) {
222
- const refs = /* @__PURE__ */ new Set();
223
- const visit = (current, parent, key) => {
224
- if (!current) {
225
- return;
226
- }
227
- if (current.type.startsWith("TS")) {
228
- return;
229
- }
230
- if (t.isIdentifier(current)) {
231
- if (shouldCountIdentifierAsReference(parent, key)) {
232
- refs.add(current.name);
233
- }
234
- return;
235
- }
236
- for (const visitorKey of t.VISITOR_KEYS[current.type] ?? []) {
237
- const value = current[visitorKey];
238
- if (Array.isArray(value)) {
239
- value.forEach((child) => {
240
- if (t.isNode(child)) {
241
- visit(child, current, visitorKey);
242
- }
243
- });
244
- continue;
245
- }
246
- if (t.isNode(value)) {
247
- visit(value, current, visitorKey);
248
- }
249
- }
250
- };
251
- visit(node, null, null);
252
- return refs;
253
- }
254
- function pruneUnusedTopLevelBindings(statements) {
255
- const bindings = /* @__PURE__ */ new Map();
256
- const liveStatements = /* @__PURE__ */ new Set();
257
- const queue = [];
258
- const markLive = (statementIndex) => {
259
- if (liveStatements.has(statementIndex)) {
260
- return;
261
- }
262
- liveStatements.add(statementIndex);
263
- queue.push(statementIndex);
264
- };
265
- statements.forEach((statement, statementIndex) => {
266
- if (t.isImportDeclaration(statement)) {
267
- for (const specifier of statement.specifiers) {
268
- bindings.set(specifier.local.name, { refs: /* @__PURE__ */ new Set(), statementIndex });
269
- }
270
- return;
271
- }
272
- if (t.isVariableDeclaration(statement)) {
273
- for (const declaration of statement.declarations) {
274
- if (t.isIdentifier(declaration.id)) {
275
- bindings.set(declaration.id.name, {
276
- refs: declaration.init ? collectRuntimeReferencedIdentifiers(declaration.init) : /* @__PURE__ */ new Set(),
277
- statementIndex
278
- });
279
- }
280
- }
281
- return;
282
- }
283
- if (t.isExportNamedDeclaration(statement) && t.isVariableDeclaration(statement.declaration)) {
284
- for (const declaration of statement.declaration.declarations) {
285
- if (t.isIdentifier(declaration.id)) {
286
- bindings.set(declaration.id.name, {
287
- refs: declaration.init ? collectRuntimeReferencedIdentifiers(declaration.init) : /* @__PURE__ */ new Set(),
288
- statementIndex
289
- });
290
- }
291
- }
292
- markLive(statementIndex);
293
- return;
294
- }
295
- markLive(statementIndex);
296
- });
297
- while (queue.length > 0) {
298
- const statementIndex = queue.pop();
299
- const statement = statements[statementIndex];
300
- if (!statement) {
301
- continue;
302
- }
303
- const refs = /* @__PURE__ */ new Set();
304
- if (t.isImportDeclaration(statement)) {
305
- continue;
306
- }
307
- if (t.isVariableDeclaration(statement)) {
308
- for (const declaration of statement.declarations) {
309
- if (declaration.init) {
310
- for (const ref of collectRuntimeReferencedIdentifiers(declaration.init)) {
311
- refs.add(ref);
312
- }
313
- }
314
- }
315
- } else if (t.isExportNamedDeclaration(statement) && t.isVariableDeclaration(statement.declaration)) {
316
- for (const declaration of statement.declaration.declarations) {
317
- if (declaration.init) {
318
- for (const ref of collectRuntimeReferencedIdentifiers(declaration.init)) {
319
- refs.add(ref);
320
- }
321
- }
322
- }
323
- } else {
324
- for (const ref of collectRuntimeReferencedIdentifiers(statement)) {
325
- refs.add(ref);
326
- }
327
- }
328
- for (const ref of refs) {
329
- const binding = bindings.get(ref);
330
- if (binding) {
331
- markLive(binding.statementIndex);
332
- }
333
- }
334
- }
335
- const prunedStatements = [];
336
- statements.forEach((statement, statementIndex) => {
337
- if (!liveStatements.has(statementIndex)) {
338
- return;
339
- }
340
- if (t.isImportDeclaration(statement)) {
341
- const specifiers = statement.specifiers.filter(
342
- (specifier) => liveStatements.has(bindings.get(specifier.local.name)?.statementIndex ?? -1)
343
- );
344
- if (specifiers.length > 0) {
345
- prunedStatements.push(t.importDeclaration(specifiers, statement.source));
346
- }
347
- return;
348
- }
349
- if (t.isVariableDeclaration(statement)) {
350
- const declarations = statement.declarations.filter(
351
- (declaration) => !t.isIdentifier(declaration.id) || liveStatements.has(bindings.get(declaration.id.name)?.statementIndex ?? -1)
352
- );
353
- if (declarations.length > 0) {
354
- prunedStatements.push(t.variableDeclaration(statement.kind, declarations));
355
- }
356
- return;
357
- }
358
- if (t.isExportNamedDeclaration(statement) && t.isVariableDeclaration(statement.declaration)) {
359
- const declarations = statement.declaration.declarations.filter(
360
- (declaration) => !t.isIdentifier(declaration.id) || liveStatements.has(bindings.get(declaration.id.name)?.statementIndex ?? -1)
361
- );
362
- if (declarations.length > 0) {
363
- prunedStatements.push(
364
- t.exportNamedDeclaration(t.variableDeclaration(statement.declaration.kind, declarations))
365
- );
366
- }
367
- return;
368
- }
369
- prunedStatements.push(statement);
370
- });
371
- return prunedStatements;
372
- }
373
-
374
- // src/transforms/workflows.ts
375
- var import_meta = {};
376
- function getWorkflowIdMetadata(workflowConfig, workflowName, filePath) {
377
- for (const property of workflowConfig.properties) {
378
- if (!t2.isObjectProperty(property) || getObjectPropertyName(property) !== "id") {
379
- continue;
380
- }
381
- if (!t2.isExpression(property.value)) {
382
- break;
383
- }
384
- if (t2.isStringLiteral(property.value)) {
385
- return {
386
- expression: t2.cloneNode(property.value, true),
387
- workflowId: property.value.value
388
- };
389
- }
390
- if (t2.isTemplateLiteral(property.value) && property.value.expressions.length === 0) {
391
- return {
392
- expression: t2.cloneNode(property.value, true),
393
- workflowId: property.value.quasis[0]?.value.cooked ?? ""
394
- };
395
- }
396
- throw new Error(`Workflow id must be a static string for ${workflowName} in ${filePath}`);
397
- }
398
- throw new Error(`Unable to determine workflow id for ${workflowName} in ${filePath}`);
399
- }
400
- var temporalWorkflowRuntimeSource = (0, import_node_fs.readFileSync)(new URL("./temporal-workflow-runtime.mjs", import_meta.url), "utf8");
401
- function createTemporalWorkflowHelperStatements() {
402
- const helperProgram = (0, import_parser.parse)(temporalWorkflowRuntimeSource, {
403
- sourceType: "module",
404
- plugins: parserPlugins
405
- }).program.body;
406
- return helperProgram.flatMap((statement) => {
407
- if (t2.isExportNamedDeclaration(statement) && statement.declaration) {
408
- return [statement.declaration];
409
- }
410
- return [statement];
411
- });
412
- }
413
- function parseWorkflowChain(node) {
414
- const methods = [];
415
- let current = node;
416
- while (t2.isCallExpression(current) && t2.isMemberExpression(current.callee) && !current.callee.computed) {
417
- if (!t2.isIdentifier(current.callee.property)) {
418
- return null;
419
- }
420
- methods.unshift({
421
- name: current.callee.property.name,
422
- args: current.arguments
423
- });
424
- current = current.callee.object;
425
- }
426
- if (!isCreateWorkflowCall(current)) {
427
- return null;
428
- }
429
- return {
430
- createWorkflowCall: current,
431
- methods
432
- };
433
- }
434
- function collectStepBindings(program2) {
435
- const stepBindings = /* @__PURE__ */ new Map();
436
- for (const statement of program2.body) {
437
- if (!t2.isVariableDeclaration(statement) && !(t2.isExportNamedDeclaration(statement) && t2.isVariableDeclaration(statement.declaration))) {
438
- continue;
439
- }
440
- const declarationStatement = t2.isVariableDeclaration(statement) ? statement : statement.declaration;
441
- for (const declaration of declarationStatement.declarations) {
442
- if (!t2.isIdentifier(declaration.id) || !declaration.init) {
443
- continue;
444
- }
445
- const stepId = getCreateStepId(declaration.init);
446
- if (stepId) {
447
- stepBindings.set(declaration.id.name, stepId);
448
- }
449
- }
450
- }
451
- return stepBindings;
452
- }
453
- function getWorkflowStepName(node, stepBindings) {
454
- if (!node) {
455
- return null;
456
- }
457
- if (t2.isIdentifier(node)) {
458
- return stepBindings.get(node.name) ?? node.name;
459
- }
460
- if (t2.isStringLiteral(node)) {
461
- return node.value;
462
- }
463
- return getCreateStepId(node);
464
- }
465
- function rewriteChainMethod(method, filePath, workflowName, stepBindings) {
466
- const argNode = (index) => method.args[index];
467
- switch (method.name) {
468
- case "then": {
469
- const name = getWorkflowStepName(argNode(0), stepBindings);
470
- if (!name) {
471
- throw new Error(
472
- `.then() in ${workflowName} (${filePath}) must take a step identifier (inline createStep calls are not supported)`
473
- );
474
- }
475
- return [t2.stringLiteral(name)];
476
- }
477
- case "sleep": {
478
- const arg = argNode(0);
479
- if (t2.isNumericLiteral(arg)) {
480
- return [t2.cloneNode(arg, true)];
481
- }
482
- const name = getWorkflowStepName(arg, stepBindings);
483
- if (!name) {
484
- throw new Error(`.sleep() in ${workflowName} (${filePath}) must be a numeric literal or an identifier`);
485
- }
486
- return [t2.stringLiteral(name)];
487
- }
488
- case "sleepUntil": {
489
- const arg = argNode(0);
490
- if (t2.isNewExpression(arg) && t2.isIdentifier(arg.callee) && arg.callee.name === "Date") {
491
- return [t2.cloneNode(arg, true)];
492
- }
493
- if (t2.isStringLiteral(arg) || t2.isNumericLiteral(arg)) {
494
- return [t2.cloneNode(arg, true)];
495
- }
496
- const name = getWorkflowStepName(arg, stepBindings);
497
- if (!name) {
498
- throw new Error(
499
- `.sleepUntil() in ${workflowName} (${filePath}) must be a Date, string/number literal, or an identifier`
500
- );
501
- }
502
- return [t2.stringLiteral(name)];
503
- }
504
- case "parallel": {
505
- const arg = argNode(0);
506
- if (!t2.isArrayExpression(arg)) {
507
- throw new Error(`.parallel() in ${workflowName} (${filePath}) requires an array literal argument`);
508
- }
509
- const names = arg.elements.map((el) => getWorkflowStepName(el, stepBindings));
510
- if (names.some((n) => !n)) {
511
- throw new Error(`Unable to determine step names inside .parallel() in ${workflowName} (${filePath})`);
512
- }
513
- return [t2.arrayExpression(names.map((n) => t2.stringLiteral(n)))];
514
- }
515
- case "branch": {
516
- const arg = argNode(0);
517
- if (!t2.isArrayExpression(arg)) {
518
- throw new Error(
519
- `.branch() in ${workflowName} (${filePath}) requires an array literal of [condition, step] pairs`
520
- );
521
- }
522
- const pairs = arg.elements.map((pair) => {
523
- if (!t2.isArrayExpression(pair) || pair.elements.length !== 2) {
524
- throw new Error(
525
- `.branch() pair in ${workflowName} (${filePath}) must be a 2-element array [condition, step]`
526
- );
527
- }
528
- const condName = getWorkflowStepName(pair.elements[0], stepBindings);
529
- const stepName = getWorkflowStepName(pair.elements[1], stepBindings);
530
- if (!condName || !stepName) {
531
- throw new Error(`.branch() condition and step in ${workflowName} (${filePath}) must be identifiers`);
532
- }
533
- return t2.arrayExpression([t2.stringLiteral(condName), t2.stringLiteral(stepName)]);
534
- });
535
- return [t2.arrayExpression(pairs)];
536
- }
537
- case "dowhile":
538
- case "dountil": {
539
- const stepName = getWorkflowStepName(argNode(0), stepBindings);
540
- const condName = getWorkflowStepName(argNode(1), stepBindings);
541
- if (!stepName || !condName) {
542
- throw new Error(`.${method.name}() in ${workflowName} (${filePath}) must take (step, condition) identifiers`);
543
- }
544
- return [t2.stringLiteral(stepName), t2.stringLiteral(condName)];
545
- }
546
- case "foreach": {
547
- const stepName = getWorkflowStepName(argNode(0), stepBindings);
548
- if (!stepName) {
549
- throw new Error(`.foreach() in ${workflowName} (${filePath}) must take a step identifier`);
550
- }
551
- const args = [t2.stringLiteral(stepName)];
552
- const optsArg = method.args[1];
553
- if (optsArg && t2.isExpression(optsArg)) {
554
- args.push(t2.cloneNode(optsArg, true));
555
- }
556
- return args;
557
- }
558
- case "commit":
559
- return [];
560
- default:
561
- throw new Error(`Unsupported workflow chain method .${method.name}() in ${workflowName} (${filePath})`);
562
- }
563
- }
564
- function getExportedName(node) {
565
- return t2.isIdentifier(node) ? node.name : node.value;
566
- }
567
- function createTemporalWorkflowStatements(exportName, workflowId, methods, filePath, includeCommit, exportInline, stepBindings) {
568
- let expression = t2.callExpression(t2.identifier("createWorkflow"), [t2.cloneNode(workflowId, true)]);
569
- for (const method of methods) {
570
- const newArgs = rewriteChainMethod(method, filePath, exportName, stepBindings);
571
- expression = t2.callExpression(t2.memberExpression(expression, t2.identifier(method.name)), newArgs);
572
- }
573
- if (includeCommit && !methods.some((m) => m.name === "commit")) {
574
- expression = t2.callExpression(t2.memberExpression(expression, t2.identifier("commit")), []);
575
- }
576
- const argsParam = t2.identifier("args");
577
- const lambda = t2.arrowFunctionExpression(
578
- [argsParam],
579
- t2.blockStatement([t2.returnStatement(t2.callExpression(expression, [t2.identifier("args")]))])
580
- );
581
- const declaration = t2.variableDeclaration("const", [t2.variableDeclarator(t2.identifier(exportName), lambda)]);
582
- return [exportInline ? t2.exportNamedDeclaration(declaration) : declaration];
583
- }
584
- function parseWorkflowModuleAst(sourceText, filePath) {
585
- return (0, import_parser.parse)(sourceText, {
586
- sourceType: "module",
587
- plugins: parserPlugins,
588
- sourceFilename: filePath
589
- });
590
- }
591
- function getVariableDeclarationFromStatement(statement) {
592
- if (t2.isVariableDeclaration(statement)) {
593
- return statement;
594
- }
595
- if (t2.isExportNamedDeclaration(statement) && t2.isVariableDeclaration(statement.declaration)) {
596
- return statement.declaration;
597
- }
598
- return null;
599
- }
600
- function getCommittedWorkflowName(statement) {
601
- if (!t2.isExpressionStatement(statement)) {
602
- return null;
603
- }
604
- const { expression } = statement;
605
- if (!t2.isCallExpression(expression) || !t2.isMemberExpression(expression.callee) || expression.callee.computed || !isIdentifierNamed(expression.callee.property, "commit") || !t2.isIdentifier(expression.callee.object)) {
606
- return null;
607
- }
608
- return expression.callee.object.name;
609
- }
610
- function createWorkflowTransformState(program2) {
611
- return {
612
- statements: [...createTemporalWorkflowHelperStatements()],
613
- workflowNames: /* @__PURE__ */ new Set(),
614
- generatedWorkflowNames: /* @__PURE__ */ new Set(),
615
- committedWorkflowNames: /* @__PURE__ */ new Set(),
616
- committedGeneratedWorkflowNames: /* @__PURE__ */ new Set(),
617
- inlineExportedWorkflowNames: /* @__PURE__ */ new Set(),
618
- strippedNames: /* @__PURE__ */ new Set(),
619
- stepBindings: collectStepBindings(program2),
620
- workflowExports: []
621
- };
622
- }
623
- function collectWorkflowDeclarationMetadata(statement, state) {
624
- const declarationStatement = getVariableDeclarationFromStatement(statement);
625
- if (!declarationStatement) {
626
- return;
627
- }
628
- for (const declaration of declarationStatement.declarations) {
629
- if (!t2.isIdentifier(declaration.id) || !declaration.init) {
630
- continue;
631
- }
632
- if (!parseWorkflowChain(declaration.init)) {
633
- continue;
634
- }
635
- state.workflowNames.add(declaration.id.name);
636
- if (t2.isExportNamedDeclaration(statement)) {
637
- state.inlineExportedWorkflowNames.add(declaration.id.name);
638
- }
639
- }
640
- }
641
- function collectWorkflowExportMetadata(statement, state) {
642
- if (t2.isExportNamedDeclaration(statement) && statement.declaration == null && statement.source == null) {
643
- for (const specifier of statement.specifiers) {
644
- if (!t2.isExportSpecifier(specifier) || !t2.isIdentifier(specifier.local)) {
645
- continue;
646
- }
647
- if (!state.workflowNames.has(specifier.local.name)) {
648
- continue;
649
- }
650
- if (getExportedName(specifier.exported) === specifier.local.name) {
651
- state.inlineExportedWorkflowNames.add(specifier.local.name);
652
- }
653
- }
654
- }
655
- }
656
- function collectWorkflowTransformMetadata(program2, state) {
657
- for (const statement of program2.body) {
658
- collectWorkflowDeclarationMetadata(statement, state);
659
- const committedWorkflowName = getCommittedWorkflowName(statement);
660
- if (committedWorkflowName) {
661
- state.committedWorkflowNames.add(committedWorkflowName);
662
- }
663
- collectWorkflowExportMetadata(statement, state);
664
- }
665
- }
666
- function rewriteWorkflowImportDeclaration(statement, state) {
667
- if (statement.source.value === "@mastra/core/workflows") {
668
- const retainedSpecifiers = statement.specifiers.filter(
669
- (specifier) => !(t2.isImportSpecifier(specifier) && t2.isIdentifier(specifier.imported) && (specifier.imported.name === "createWorkflow" || specifier.imported.name === "createStep"))
670
- );
671
- if (retainedSpecifiers.length > 0) {
672
- state.statements.push(t2.importDeclaration(retainedSpecifiers, t2.stringLiteral(statement.source.value)));
673
- }
674
- return;
675
- }
676
- if (isTemporalHelperModule(statement.source.value) || isStrippedExternalModule(statement.source.value)) {
677
- for (const name of collectImportedNames(statement)) {
678
- state.strippedNames.add(name);
679
- }
680
- return;
681
- }
682
- state.statements.push(statement);
683
- }
684
- function rewriteWorkflowNamedExport(statement, state) {
685
- if (statement.source != null) {
686
- state.statements.push(statement);
687
- return;
688
- }
689
- const retainedSpecifiers = statement.specifiers.filter((specifier) => {
690
- if (!t2.isExportSpecifier(specifier) || !t2.isIdentifier(specifier.local)) {
691
- return true;
692
- }
693
- if (!state.workflowNames.has(specifier.local.name)) {
694
- return true;
695
- }
696
- return getExportedName(specifier.exported) !== specifier.local.name;
697
- });
698
- if (retainedSpecifiers.length > 0) {
699
- state.statements.push(t2.exportNamedDeclaration(null, retainedSpecifiers));
700
- }
701
- }
702
- function rewriteWorkflowVariableDeclaration(statement, filePath, state) {
703
- const declarationStatement = getVariableDeclarationFromStatement(statement);
704
- if (!declarationStatement) {
705
- return;
706
- }
707
- const declarations = [];
708
- for (const declaration of declarationStatement.declarations) {
709
- if (isWorkflowHelperDestructure(declaration)) {
710
- continue;
711
- }
712
- if (t2.isIdentifier(declaration.id) && state.stepBindings.has(declaration.id.name)) {
713
- state.strippedNames.add(declaration.id.name);
714
- continue;
715
- }
716
- if (!t2.isIdentifier(declaration.id) || !declaration.init) {
717
- declarations.push(declaration);
718
- continue;
719
- }
720
- const workflowChain = parseWorkflowChain(declaration.init);
721
- if (!workflowChain && nodeReferencesName(declaration.init, state.strippedNames)) {
722
- state.strippedNames.add(declaration.id.name);
723
- continue;
724
- }
725
- if (!workflowChain) {
726
- declarations.push(declaration);
727
- continue;
728
- }
729
- const [workflowConfig] = workflowChain.createWorkflowCall.arguments;
730
- if (!workflowConfig || !t2.isObjectExpression(workflowConfig)) {
731
- throw new Error(`Unable to determine workflow config for ${declaration.id.name} in ${filePath}`);
732
- }
733
- const { expression: workflowId, workflowId: workflowIdValue } = getWorkflowIdMetadata(
734
- workflowConfig,
735
- declaration.id.name,
736
- filePath
737
- );
738
- const exportName = toWorkflowType(workflowIdValue);
739
- state.generatedWorkflowNames.add(exportName);
740
- if (state.committedWorkflowNames.has(declaration.id.name)) {
741
- state.committedGeneratedWorkflowNames.add(exportName);
742
- }
743
- state.workflowExports.push({
744
- exportName,
745
- workflowId: workflowIdValue
746
- });
747
- state.statements.push(
748
- ...createTemporalWorkflowStatements(
749
- exportName,
750
- workflowId,
751
- workflowChain.methods,
752
- filePath,
753
- state.committedWorkflowNames.has(declaration.id.name),
754
- t2.isExportNamedDeclaration(statement) || state.inlineExportedWorkflowNames.has(declaration.id.name),
755
- state.stepBindings
756
- )
757
- );
758
- }
759
- if (declarations.length > 0) {
760
- const cloned = declarations.map((declaration) => t2.cloneNode(declaration, true));
761
- state.statements.push(
762
- t2.isVariableDeclaration(statement) ? t2.variableDeclaration(statement.kind, cloned) : t2.exportNamedDeclaration(t2.variableDeclaration(declarationStatement.kind, cloned))
763
- );
764
- }
765
- }
766
- function rewriteWorkflowStatement(statement, filePath, state) {
767
- if (t2.isImportDeclaration(statement)) {
768
- rewriteWorkflowImportDeclaration(statement, state);
769
- return;
770
- }
771
- if (getCommittedWorkflowName(statement)) {
772
- return;
773
- }
774
- if (t2.isExportNamedDeclaration(statement) && statement.declaration == null) {
775
- rewriteWorkflowNamedExport(statement, state);
776
- return;
777
- }
778
- if (t2.isExportDefaultDeclaration(statement) && t2.isIdentifier(statement.declaration)) {
779
- if (state.workflowNames.has(statement.declaration.name)) {
780
- state.statements.push(statement);
781
- }
782
- return;
783
- }
784
- if (getVariableDeclarationFromStatement(statement)) {
785
- rewriteWorkflowVariableDeclaration(statement, filePath, state);
786
- return;
787
- }
788
- state.statements.push(statement);
789
- }
790
- function appendMissingWorkflowCommits(state) {
791
- for (const workflowName of state.generatedWorkflowNames) {
792
- if (state.committedGeneratedWorkflowNames.has(workflowName)) {
793
- continue;
794
- }
795
- state.statements.push(
796
- t2.expressionStatement(
797
- t2.callExpression(t2.memberExpression(t2.identifier(workflowName), t2.identifier("commit")), [])
798
- )
799
- );
800
- }
801
- }
802
- async function finalizeWorkflowModule(filePath, state) {
803
- const transformedSource = (0, import_generator.generate)(t2.file(t2.program(pruneUnusedTopLevelBindings(state.statements), [], "module")), {
804
- sourceMaps: false
805
- }).code;
806
- return {
807
- code: await transpileModule(transformedSource, filePath),
808
- workflows: state.workflowExports
809
- };
810
- }
811
- async function buildTemporalWorkflowModule(sourceText, filePath, _options = {}) {
812
- const ast = parseWorkflowModuleAst(sourceText, filePath);
813
- const state = createWorkflowTransformState(ast.program);
814
- collectWorkflowTransformMetadata(ast.program, state);
815
- for (const statement of ast.program.body) {
816
- rewriteWorkflowStatement(statement, filePath, state);
817
- }
818
- appendMissingWorkflowCommits(state);
819
- return finalizeWorkflowModule(filePath, state);
820
- }
821
-
822
- // src/webpack-loader.ts
823
- async function writeDebugModule(resourcePath, code, options) {
824
- if (!options.debugOutputDir) {
825
- return;
826
- }
827
- const baseDir = options.entryFile ? import_node_path3.default.dirname(options.entryFile) : process.cwd();
828
- const relativePath = import_node_path3.default.relative(baseDir, resourcePath);
829
- const safeRelativePath = relativePath.startsWith("..") ? import_node_path3.default.basename(resourcePath) : relativePath;
830
- const outputPath = import_node_path3.default.join(options.debugOutputDir, "modules", safeRelativePath);
831
- await (0, import_promises.mkdir)(import_node_path3.default.dirname(outputPath), { recursive: true });
832
- await (0, import_promises.writeFile)(outputPath, code, "utf-8");
833
- }
834
- function mastraTemporalWorkflowLoader(source) {
835
- const callback = this.async();
836
- if (!callback) {
837
- throw new Error("mastra-temporal-webpack-loader requires async mode");
838
- }
839
- const options = this.getOptions?.() ?? {};
840
- if (!source.includes("createWorkflow")) {
841
- return callback(null, source);
842
- }
843
- const transform = async () => {
844
- const result = await buildTemporalWorkflowModule(source, this.resourcePath, options);
845
- options.registry?.register(
846
- this.resourcePath,
847
- result.workflows.map((workflow) => workflow.exportName)
848
- );
849
- return result.code;
850
- };
851
- transform().then(async (code) => {
852
- await writeDebugModule(this.resourcePath, code, options);
853
- callback(null, code);
854
- }).catch((err) => callback(err));
855
- }
856
- //# sourceMappingURL=webpack-loader.cjs.map