@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.
- package/CHANGELOG.md +33 -0
- package/LICENSE.md +30 -0
- package/README.md +104 -2
- package/dist/chunk-BF6TR7JX.js +9 -0
- package/dist/chunk-BF6TR7JX.js.map +1 -0
- package/dist/chunk-DYBSPLCJ.cjs +11 -0
- package/dist/chunk-DYBSPLCJ.cjs.map +1 -0
- package/dist/index.cjs +3 -946
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -920
- package/dist/index.js.map +1 -1
- package/dist/mastra-deployer-UCBGECM5.js +39 -0
- package/dist/mastra-deployer-UCBGECM5.js.map +1 -0
- package/dist/mastra-deployer-YVT5GB5G.cjs +41 -0
- package/dist/mastra-deployer-YVT5GB5G.cjs.map +1 -0
- package/dist/mastra-deployer.d.ts +16 -0
- package/dist/mastra-deployer.d.ts.map +1 -0
- package/dist/plugin.d.ts +13 -15
- package/dist/plugin.d.ts.map +1 -1
- package/dist/temporal-workflow-runtime.mjs +295 -0
- package/dist/transforms/activities.d.ts +4 -3
- package/dist/transforms/activities.d.ts.map +1 -1
- package/dist/transforms/shared.d.ts +2 -7
- package/dist/transforms/shared.d.ts.map +1 -1
- package/dist/transforms/temporal-workflow-runtime.d.mts +7 -0
- package/dist/transforms/temporal-workflow-runtime.d.mts.map +1 -1
- package/dist/transforms/workflows.d.ts +3 -11
- package/dist/transforms/workflows.d.ts.map +1 -1
- package/dist/worker.cjs +1336 -0
- package/dist/worker.cjs.map +1 -0
- package/dist/worker.d.ts +2 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +1310 -0
- package/dist/worker.js.map +1 -0
- package/package.json +30 -24
- package/dist/__tests__/__fixtures__/before/index.d.ts +0 -11
- package/dist/__tests__/__fixtures__/before/index.d.ts.map +0 -1
- package/dist/__tests__/__fixtures__/before/weather-workflow.d.ts +0 -8
- package/dist/__tests__/__fixtures__/before/weather-workflow.d.ts.map +0 -1
- package/dist/transforms/__fixtures__/activities/weather-workflow/input.d.ts +0 -8
- package/dist/transforms/__fixtures__/activities/weather-workflow/input.d.ts.map +0 -1
- package/dist/transforms/__fixtures__/activities/weather-workflow/output.d.ts +0 -3
- package/dist/transforms/__fixtures__/activities/weather-workflow/output.d.ts.map +0 -1
- package/dist/transforms/__fixtures__/workflow/weather-workflow/input.d.ts +0 -8
- package/dist/transforms/__fixtures__/workflow/weather-workflow/input.d.ts.map +0 -1
- package/dist/transforms/__fixtures__/workflow/weather-workflow/output.d.ts +0 -2
- package/dist/transforms/__fixtures__/workflow/weather-workflow/output.d.ts.map +0 -1
- package/dist/webpack-loader.cjs +0 -856
- package/dist/webpack-loader.cjs.map +0 -1
- package/dist/webpack-loader.d.ts +0 -8
- package/dist/webpack-loader.d.ts.map +0 -1
- package/dist/webpack-plugin.d.ts +0 -18
- package/dist/webpack-plugin.d.ts.map +0 -1
package/dist/worker.js
ADDED
|
@@ -0,0 +1,1310 @@
|
|
|
1
|
+
import { toWorkflowType } from './chunk-BF6TR7JX.js';
|
|
2
|
+
import { readFileSync } from 'fs';
|
|
3
|
+
import { writeFile } from 'fs/promises';
|
|
4
|
+
import path, { basename, join } from 'path';
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
6
|
+
import { generate } from '@babel/generator';
|
|
7
|
+
import { parse } from '@babel/parser';
|
|
8
|
+
import * as t3 from '@babel/types';
|
|
9
|
+
import { rollup } from 'rollup';
|
|
10
|
+
|
|
11
|
+
var parserPlugins = [
|
|
12
|
+
"typescript",
|
|
13
|
+
"jsx",
|
|
14
|
+
"classProperties",
|
|
15
|
+
"classPrivateProperties",
|
|
16
|
+
"classPrivateMethods",
|
|
17
|
+
"topLevelAwait",
|
|
18
|
+
"importAttributes",
|
|
19
|
+
"decorators-legacy"
|
|
20
|
+
];
|
|
21
|
+
function parseModule(filePath, sourceText) {
|
|
22
|
+
if (!sourceText) {
|
|
23
|
+
sourceText = readFileSync(filePath, "utf8");
|
|
24
|
+
}
|
|
25
|
+
return parse(sourceText, {
|
|
26
|
+
sourceType: "module",
|
|
27
|
+
plugins: parserPlugins,
|
|
28
|
+
sourceFilename: filePath
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function isIdentifierNamed(node, name) {
|
|
32
|
+
return t3.isIdentifier(node) && node.name === name;
|
|
33
|
+
}
|
|
34
|
+
function isTemporalHelperModule(source) {
|
|
35
|
+
return typeof source === "string" && /(^|\/)temporal\.(ts|tsx|js|jsx|mts|mjs)$/.test(source);
|
|
36
|
+
}
|
|
37
|
+
var strippedExternalModules = /* @__PURE__ */ new Set(["@temporalio/client", "@temporalio/envconfig"]);
|
|
38
|
+
function isStrippedExternalModule(source) {
|
|
39
|
+
return typeof source === "string" && strippedExternalModules.has(source);
|
|
40
|
+
}
|
|
41
|
+
function collectImportedNames(statement) {
|
|
42
|
+
const names = /* @__PURE__ */ new Set();
|
|
43
|
+
for (const specifier of statement.specifiers) {
|
|
44
|
+
if (t3.isImportDefaultSpecifier(specifier) || t3.isImportNamespaceSpecifier(specifier) || t3.isImportSpecifier(specifier)) {
|
|
45
|
+
if (t3.isIdentifier(specifier.local)) {
|
|
46
|
+
names.add(specifier.local.name);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return names;
|
|
51
|
+
}
|
|
52
|
+
function nodeReferencesName(node, names) {
|
|
53
|
+
let found = false;
|
|
54
|
+
walk(node, (current) => {
|
|
55
|
+
if (t3.isIdentifier(current) && names.has(current.name)) {
|
|
56
|
+
found = true;
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return found;
|
|
61
|
+
}
|
|
62
|
+
function isWorkflowHelperDestructure(declaration) {
|
|
63
|
+
if (!t3.isObjectPattern(declaration.id)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return declaration.id.properties.some(
|
|
67
|
+
(property) => t3.isObjectProperty(property) && !property.computed && t3.isIdentifier(property.value) && (property.value.name === "createStep" || property.value.name === "createWorkflow")
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
function isCreateWorkflowCall(node) {
|
|
71
|
+
return t3.isCallExpression(node) && isIdentifierNamed(node.callee, "createWorkflow");
|
|
72
|
+
}
|
|
73
|
+
function isCreateStepCall(node) {
|
|
74
|
+
return t3.isCallExpression(node) && isIdentifierNamed(node.callee, "createStep");
|
|
75
|
+
}
|
|
76
|
+
function getObjectPropertyName(property) {
|
|
77
|
+
if (property.computed) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
if (t3.isIdentifier(property.key)) {
|
|
81
|
+
return property.key.name;
|
|
82
|
+
}
|
|
83
|
+
if (t3.isStringLiteral(property.key)) {
|
|
84
|
+
return property.key.value;
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
function walk(node, visitor) {
|
|
89
|
+
if (!node) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const result = visitor(node);
|
|
93
|
+
if (result === false) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const keys = t3.VISITOR_KEYS[node.type] ?? [];
|
|
97
|
+
for (const key of keys) {
|
|
98
|
+
const value = node[key];
|
|
99
|
+
if (Array.isArray(value)) {
|
|
100
|
+
for (const child of value) {
|
|
101
|
+
if (child && typeof child.type === "string") {
|
|
102
|
+
walk(child, visitor);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (value && typeof value.type === "string") {
|
|
108
|
+
walk(value, visitor);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function hasCreateWorkflowCall(node) {
|
|
113
|
+
let found = false;
|
|
114
|
+
walk(node, (current) => {
|
|
115
|
+
if (isCreateWorkflowCall(current)) {
|
|
116
|
+
found = true;
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
return found;
|
|
121
|
+
}
|
|
122
|
+
function getStepNameFromCall(node) {
|
|
123
|
+
const stepId = getCreateStepId(node);
|
|
124
|
+
if (!stepId) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
return stepId.replace(/[^a-zA-Z0-9]+(.)/g, (_match, char) => char.toUpperCase()).replace(/^[^a-zA-Z_$]+/, "").replace(/^(.)/, (char) => char.toLowerCase());
|
|
128
|
+
}
|
|
129
|
+
function createExportedStepStatement(name, initializer) {
|
|
130
|
+
return t3.exportNamedDeclaration(
|
|
131
|
+
t3.variableDeclaration("const", [t3.variableDeclarator(t3.identifier(name), t3.cloneNode(initializer, true))])
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
function collectInlineCreateSteps(node, seenNames, statements, onStep) {
|
|
135
|
+
walk(node, (current) => {
|
|
136
|
+
if (!isCreateStepCall(current)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const stepName = getStepNameFromCall(current);
|
|
140
|
+
if (!stepName || seenNames.has(stepName)) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
seenNames.add(stepName);
|
|
144
|
+
onStep?.(stepName, current);
|
|
145
|
+
statements.push(createExportedStepStatement(stepName, current));
|
|
146
|
+
return false;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
function getCreateStepId(node) {
|
|
150
|
+
if (!node || !isCreateStepCall(node)) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
const [config] = node.arguments;
|
|
154
|
+
if (!t3.isObjectExpression(config)) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
for (const property of config.properties) {
|
|
158
|
+
if (!t3.isObjectProperty(property) && !t3.isObjectMethod(property)) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (getObjectPropertyName(property) !== "id") {
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const value = t3.isObjectMethod(property) ? null : property.value;
|
|
165
|
+
return t3.isStringLiteral(value) ? value.value : null;
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
function shouldCountIdentifierAsReference(parent, key) {
|
|
170
|
+
if (!parent) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
if ((t3.isObjectProperty(parent) || t3.isObjectMethod(parent)) && key === "key" && !parent.computed) {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
if (t3.isMemberExpression(parent) && key === "property" && !parent.computed) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
if (t3.isVariableDeclarator(parent) && key === "id") {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
if ((t3.isFunctionDeclaration(parent) || t3.isFunctionExpression(parent) || t3.isArrowFunctionExpression(parent)) && key === "params") {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
if ((t3.isFunctionDeclaration(parent) || t3.isFunctionExpression(parent) || t3.isClassDeclaration(parent)) && key === "id") {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
if ((t3.isImportSpecifier(parent) || t3.isImportDefaultSpecifier(parent) || t3.isImportNamespaceSpecifier(parent)) && (key === "local" || key === "imported")) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
if (t3.isExportSpecifier(parent) && key === "exported") {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
if (t3.isLabeledStatement(parent) && key === "label") {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
if (t3.isCatchClause(parent) && key === "param") {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
if (t3.isRestElement(parent) && key === "argument") {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
if (t3.isAssignmentPattern(parent) && key === "left") {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
if (t3.isTSPropertySignature(parent) || t3.isTSMethodSignature(parent) || t3.isTSExpressionWithTypeArguments(parent)) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
function collectRuntimeReferencedIdentifiers(node) {
|
|
212
|
+
const refs = /* @__PURE__ */ new Set();
|
|
213
|
+
const visit = (current, parent, key) => {
|
|
214
|
+
if (!current) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (current.type.startsWith("TS")) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (t3.isIdentifier(current)) {
|
|
221
|
+
if (shouldCountIdentifierAsReference(parent, key)) {
|
|
222
|
+
refs.add(current.name);
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
for (const visitorKey of t3.VISITOR_KEYS[current.type] ?? []) {
|
|
227
|
+
const value = current[visitorKey];
|
|
228
|
+
if (Array.isArray(value)) {
|
|
229
|
+
value.forEach((child) => {
|
|
230
|
+
if (t3.isNode(child)) {
|
|
231
|
+
visit(child, current, visitorKey);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
if (t3.isNode(value)) {
|
|
237
|
+
visit(value, current, visitorKey);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
visit(node, null, null);
|
|
242
|
+
return refs;
|
|
243
|
+
}
|
|
244
|
+
function pruneUnusedTopLevelBindings(statements) {
|
|
245
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
246
|
+
const liveStatements = /* @__PURE__ */ new Set();
|
|
247
|
+
const queue = [];
|
|
248
|
+
const markLive = (statementIndex) => {
|
|
249
|
+
if (liveStatements.has(statementIndex)) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
liveStatements.add(statementIndex);
|
|
253
|
+
queue.push(statementIndex);
|
|
254
|
+
};
|
|
255
|
+
statements.forEach((statement, statementIndex) => {
|
|
256
|
+
if (t3.isImportDeclaration(statement)) {
|
|
257
|
+
for (const specifier of statement.specifiers) {
|
|
258
|
+
bindings.set(specifier.local.name, { refs: /* @__PURE__ */ new Set(), statementIndex });
|
|
259
|
+
}
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (t3.isVariableDeclaration(statement)) {
|
|
263
|
+
for (const declaration of statement.declarations) {
|
|
264
|
+
if (t3.isIdentifier(declaration.id)) {
|
|
265
|
+
bindings.set(declaration.id.name, {
|
|
266
|
+
refs: declaration.init ? collectRuntimeReferencedIdentifiers(declaration.init) : /* @__PURE__ */ new Set(),
|
|
267
|
+
statementIndex
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (t3.isExportNamedDeclaration(statement) && t3.isVariableDeclaration(statement.declaration)) {
|
|
274
|
+
for (const declaration of statement.declaration.declarations) {
|
|
275
|
+
if (t3.isIdentifier(declaration.id)) {
|
|
276
|
+
bindings.set(declaration.id.name, {
|
|
277
|
+
refs: declaration.init ? collectRuntimeReferencedIdentifiers(declaration.init) : /* @__PURE__ */ new Set(),
|
|
278
|
+
statementIndex
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
markLive(statementIndex);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
markLive(statementIndex);
|
|
286
|
+
});
|
|
287
|
+
while (queue.length > 0) {
|
|
288
|
+
const statementIndex = queue.pop();
|
|
289
|
+
const statement = statements[statementIndex];
|
|
290
|
+
if (!statement) {
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
const refs = /* @__PURE__ */ new Set();
|
|
294
|
+
if (t3.isImportDeclaration(statement)) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
if (t3.isVariableDeclaration(statement)) {
|
|
298
|
+
for (const declaration of statement.declarations) {
|
|
299
|
+
if (declaration.init) {
|
|
300
|
+
for (const ref of collectRuntimeReferencedIdentifiers(declaration.init)) {
|
|
301
|
+
refs.add(ref);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
} else if (t3.isExportNamedDeclaration(statement) && t3.isVariableDeclaration(statement.declaration)) {
|
|
306
|
+
for (const declaration of statement.declaration.declarations) {
|
|
307
|
+
if (declaration.init) {
|
|
308
|
+
for (const ref of collectRuntimeReferencedIdentifiers(declaration.init)) {
|
|
309
|
+
refs.add(ref);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
} else {
|
|
314
|
+
for (const ref of collectRuntimeReferencedIdentifiers(statement)) {
|
|
315
|
+
refs.add(ref);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
for (const ref of refs) {
|
|
319
|
+
const binding = bindings.get(ref);
|
|
320
|
+
if (binding) {
|
|
321
|
+
markLive(binding.statementIndex);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
const prunedStatements = [];
|
|
326
|
+
statements.forEach((statement, statementIndex) => {
|
|
327
|
+
if (!liveStatements.has(statementIndex)) {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
if (t3.isImportDeclaration(statement)) {
|
|
331
|
+
const specifiers = statement.specifiers.filter(
|
|
332
|
+
(specifier) => liveStatements.has(bindings.get(specifier.local.name)?.statementIndex ?? -1)
|
|
333
|
+
);
|
|
334
|
+
if (specifiers.length > 0) {
|
|
335
|
+
prunedStatements.push(t3.importDeclaration(specifiers, statement.source));
|
|
336
|
+
}
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (t3.isVariableDeclaration(statement)) {
|
|
340
|
+
const declarations = statement.declarations.filter(
|
|
341
|
+
(declaration) => !t3.isIdentifier(declaration.id) || liveStatements.has(bindings.get(declaration.id.name)?.statementIndex ?? -1)
|
|
342
|
+
);
|
|
343
|
+
if (declarations.length > 0) {
|
|
344
|
+
prunedStatements.push(t3.variableDeclaration(statement.kind, declarations));
|
|
345
|
+
}
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (t3.isExportNamedDeclaration(statement) && t3.isVariableDeclaration(statement.declaration)) {
|
|
349
|
+
const declarations = statement.declaration.declarations.filter(
|
|
350
|
+
(declaration) => !t3.isIdentifier(declaration.id) || liveStatements.has(bindings.get(declaration.id.name)?.statementIndex ?? -1)
|
|
351
|
+
);
|
|
352
|
+
if (declarations.length > 0) {
|
|
353
|
+
prunedStatements.push(
|
|
354
|
+
t3.exportNamedDeclaration(t3.variableDeclaration(statement.declaration.kind, declarations))
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
prunedStatements.push(statement);
|
|
360
|
+
});
|
|
361
|
+
return prunedStatements;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// src/transforms/activities.ts
|
|
365
|
+
function normalizeImportPath(importPath, extension) {
|
|
366
|
+
const normalizedPath = importPath.split(path.sep).join("/");
|
|
367
|
+
const pathWithExtension = extension === ".mjs" || extension === ".cjs" ? normalizedPath : normalizedPath.replace(/\.[cm]?[jt]sx?$/, "");
|
|
368
|
+
return pathWithExtension.startsWith(".") ? pathWithExtension : `./${pathWithExtension}`;
|
|
369
|
+
}
|
|
370
|
+
function rebaseModulePath(modulePath, sourceFilePath, outputFilePath) {
|
|
371
|
+
if (!modulePath.startsWith(".")) {
|
|
372
|
+
return modulePath;
|
|
373
|
+
}
|
|
374
|
+
const resolvedPath = path.resolve(path.dirname(sourceFilePath), modulePath);
|
|
375
|
+
const relativePath = path.relative(path.dirname(outputFilePath), resolvedPath);
|
|
376
|
+
return normalizeImportPath(relativePath, path.extname(resolvedPath));
|
|
377
|
+
}
|
|
378
|
+
function collectWorkflowBindingNames(ast) {
|
|
379
|
+
const workflowNames = /* @__PURE__ */ new Set();
|
|
380
|
+
for (const statement of ast.program.body) {
|
|
381
|
+
if (!t3.isVariableDeclaration(statement) && !(t3.isExportNamedDeclaration(statement) && t3.isVariableDeclaration(statement.declaration))) {
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
const declarationStatement = t3.isVariableDeclaration(statement) ? statement : statement.declaration;
|
|
385
|
+
for (const declaration of declarationStatement.declarations) {
|
|
386
|
+
if (t3.isIdentifier(declaration.id) && declaration.init && hasCreateWorkflowCall(declaration.init)) {
|
|
387
|
+
workflowNames.add(declaration.id.name);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return workflowNames;
|
|
392
|
+
}
|
|
393
|
+
function isMastraDeclaration(declaration) {
|
|
394
|
+
return t3.isIdentifier(declaration.id) && declaration.id.name === "mastra";
|
|
395
|
+
}
|
|
396
|
+
function removeStrippedReferencesFromMastraInitializer(init, strippedNames) {
|
|
397
|
+
const clonedInit = t3.cloneNode(init, true);
|
|
398
|
+
if (!t3.isNewExpression(clonedInit) && !t3.isCallExpression(clonedInit)) {
|
|
399
|
+
return clonedInit;
|
|
400
|
+
}
|
|
401
|
+
const config = clonedInit.arguments[0];
|
|
402
|
+
if (!t3.isObjectExpression(config)) {
|
|
403
|
+
return clonedInit;
|
|
404
|
+
}
|
|
405
|
+
config.properties = config.properties.filter((property) => !nodeReferencesName(property, strippedNames));
|
|
406
|
+
return clonedInit;
|
|
407
|
+
}
|
|
408
|
+
function createPreservedDeclaration(declaration, strippedNames) {
|
|
409
|
+
if (!isMastraDeclaration(declaration) || !declaration.init) {
|
|
410
|
+
return t3.cloneNode(declaration, true);
|
|
411
|
+
}
|
|
412
|
+
return t3.variableDeclarator(
|
|
413
|
+
t3.cloneNode(declaration.id, true),
|
|
414
|
+
removeStrippedReferencesFromMastraInitializer(declaration.init, strippedNames)
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
function hasLocalMastraBinding(ast) {
|
|
418
|
+
return ast.program.body.some((statement) => {
|
|
419
|
+
const declaration = t3.isExportNamedDeclaration(statement) ? statement.declaration : statement;
|
|
420
|
+
if (!t3.isVariableDeclaration(declaration)) {
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
return declaration.declarations.some((declarator) => t3.isIdentifier(declarator.id, { name: "mastra" }));
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
function createTemporalActivitiesHelperStatements(mastraImportPath, hasMastraBinding) {
|
|
427
|
+
const helperSource = hasMastraBinding ? `
|
|
428
|
+
function createStep(args) {
|
|
429
|
+
return async (params) => {
|
|
430
|
+
return args.execute({ ...params, mastra });
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
` : `
|
|
434
|
+
function createStep(args) {
|
|
435
|
+
return async (params) => {
|
|
436
|
+
return args.execute(params);
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
`;
|
|
440
|
+
return parse(helperSource, {
|
|
441
|
+
sourceType: "module",
|
|
442
|
+
plugins: parserPlugins
|
|
443
|
+
}).program.body;
|
|
444
|
+
}
|
|
445
|
+
async function buildTemporalActivitiesModule(entryFile, outputDirectory, outputFileName) {
|
|
446
|
+
const activityBindings = [];
|
|
447
|
+
const seenActivityBindingNames = /* @__PURE__ */ new Set();
|
|
448
|
+
const addActivityBinding = (exportName, call) => {
|
|
449
|
+
const stepId = getCreateStepId(call);
|
|
450
|
+
if (!stepId || seenActivityBindingNames.has(exportName)) {
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
seenActivityBindingNames.add(exportName);
|
|
454
|
+
activityBindings.push({ exportName, stepId });
|
|
455
|
+
};
|
|
456
|
+
const bundle = await rollup({
|
|
457
|
+
input: entryFile,
|
|
458
|
+
treeshake: "smallest",
|
|
459
|
+
logLevel: "silent",
|
|
460
|
+
plugins: [
|
|
461
|
+
{
|
|
462
|
+
name: "temporal-workflow-transform",
|
|
463
|
+
transform(code, id) {
|
|
464
|
+
const ast = parse(code, {
|
|
465
|
+
sourceType: "module",
|
|
466
|
+
plugins: parserPlugins,
|
|
467
|
+
sourceFilename: id
|
|
468
|
+
});
|
|
469
|
+
const statements = [];
|
|
470
|
+
const seenNames = /* @__PURE__ */ new Set();
|
|
471
|
+
const strippedNames = /* @__PURE__ */ new Set();
|
|
472
|
+
const workflowBindingNames = collectWorkflowBindingNames(ast);
|
|
473
|
+
const sourceFilePath = id;
|
|
474
|
+
const hasMastraBinding = hasLocalMastraBinding(ast);
|
|
475
|
+
let helperInserted = false;
|
|
476
|
+
const ensureHelperInserted = () => {
|
|
477
|
+
if (helperInserted) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
statements.push(...createTemporalActivitiesHelperStatements(null, hasMastraBinding));
|
|
481
|
+
helperInserted = true;
|
|
482
|
+
};
|
|
483
|
+
for (const statement of ast.program.body) {
|
|
484
|
+
if (t3.isImportDeclaration(statement)) {
|
|
485
|
+
if (statement.source.value === "@mastra/core/workflows") {
|
|
486
|
+
const retainedSpecifiers = statement.specifiers.filter(
|
|
487
|
+
(specifier) => !(t3.isImportSpecifier(specifier) && t3.isIdentifier(specifier.imported) && (specifier.imported.name === "createStep" || specifier.imported.name === "createWorkflow"))
|
|
488
|
+
);
|
|
489
|
+
if (retainedSpecifiers.length > 0) {
|
|
490
|
+
statements.push(t3.importDeclaration(retainedSpecifiers, t3.stringLiteral(statement.source.value)));
|
|
491
|
+
}
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
if (isTemporalHelperModule(statement.source.value) || isStrippedExternalModule(statement.source.value)) {
|
|
495
|
+
for (const name of collectImportedNames(statement)) {
|
|
496
|
+
strippedNames.add(name);
|
|
497
|
+
}
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
const rewrittenSource = rebaseModulePath(statement.source.value, sourceFilePath, id);
|
|
501
|
+
if (rewrittenSource === statement.source.value) {
|
|
502
|
+
statements.push(statement);
|
|
503
|
+
} else {
|
|
504
|
+
statements.push(
|
|
505
|
+
t3.importDeclaration(
|
|
506
|
+
statement.specifiers.map((specifier) => t3.cloneNode(specifier, true)),
|
|
507
|
+
t3.stringLiteral(rewrittenSource)
|
|
508
|
+
)
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
if (t3.isFunctionDeclaration(statement) || t3.isClassDeclaration(statement) || t3.isTSTypeAliasDeclaration(statement) || t3.isTSInterfaceDeclaration(statement) || t3.isTSEnumDeclaration(statement)) {
|
|
514
|
+
ensureHelperInserted();
|
|
515
|
+
statements.push(statement);
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
if (t3.isExpressionStatement(statement) && nodeReferencesName(statement, strippedNames)) {
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
ensureHelperInserted();
|
|
522
|
+
if (t3.isVariableDeclaration(statement)) {
|
|
523
|
+
const declarations = [];
|
|
524
|
+
for (const declaration of statement.declarations) {
|
|
525
|
+
if (isWorkflowHelperDestructure(declaration)) {
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
if (declaration.init && nodeReferencesName(declaration.init, strippedNames) && !isMastraDeclaration(declaration)) {
|
|
529
|
+
if (t3.isIdentifier(declaration.id)) {
|
|
530
|
+
strippedNames.add(declaration.id.name);
|
|
531
|
+
}
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (!t3.isIdentifier(declaration.id) || !declaration.init) {
|
|
535
|
+
declarations.push(createPreservedDeclaration(declaration, strippedNames));
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
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;
|
|
543
|
+
}
|
|
544
|
+
if (hasCreateWorkflowCall(declaration.init)) {
|
|
545
|
+
workflowBindingNames.add(declaration.id.name);
|
|
546
|
+
strippedNames.add(declaration.id.name);
|
|
547
|
+
collectInlineCreateSteps(declaration.init, seenNames, statements, addActivityBinding);
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
declarations.push(createPreservedDeclaration(declaration, strippedNames));
|
|
551
|
+
}
|
|
552
|
+
if (declarations.length > 0) {
|
|
553
|
+
statements.push(
|
|
554
|
+
t3.variableDeclaration(
|
|
555
|
+
statement.kind,
|
|
556
|
+
declarations.map((declaration) => t3.cloneNode(declaration, true))
|
|
557
|
+
)
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
if (t3.isExportNamedDeclaration(statement) && t3.isVariableDeclaration(statement.declaration)) {
|
|
563
|
+
const exportedDeclarations = [];
|
|
564
|
+
const localDeclarations = [];
|
|
565
|
+
for (const declaration of statement.declaration.declarations) {
|
|
566
|
+
if (isWorkflowHelperDestructure(declaration)) {
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
if (declaration.init && nodeReferencesName(declaration.init, strippedNames) && !isMastraDeclaration(declaration)) {
|
|
570
|
+
if (t3.isIdentifier(declaration.id)) {
|
|
571
|
+
strippedNames.add(declaration.id.name);
|
|
572
|
+
}
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
if (!t3.isIdentifier(declaration.id) || !declaration.init) {
|
|
576
|
+
exportedDeclarations.push(createPreservedDeclaration(declaration, strippedNames));
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
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;
|
|
584
|
+
}
|
|
585
|
+
if (hasCreateWorkflowCall(declaration.init)) {
|
|
586
|
+
workflowBindingNames.add(declaration.id.name);
|
|
587
|
+
strippedNames.add(declaration.id.name);
|
|
588
|
+
collectInlineCreateSteps(declaration.init, seenNames, statements, addActivityBinding);
|
|
589
|
+
continue;
|
|
590
|
+
}
|
|
591
|
+
if (declaration.id.name === "mastra") {
|
|
592
|
+
localDeclarations.push(createPreservedDeclaration(declaration, strippedNames));
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
exportedDeclarations.push(createPreservedDeclaration(declaration, strippedNames));
|
|
596
|
+
}
|
|
597
|
+
if (localDeclarations.length > 0) {
|
|
598
|
+
statements.push(
|
|
599
|
+
t3.variableDeclaration(
|
|
600
|
+
statement.declaration.kind,
|
|
601
|
+
localDeclarations.map((declaration) => t3.cloneNode(declaration, true))
|
|
602
|
+
)
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
if (exportedDeclarations.length > 0) {
|
|
606
|
+
statements.push(
|
|
607
|
+
t3.exportNamedDeclaration(
|
|
608
|
+
t3.variableDeclaration(
|
|
609
|
+
statement.declaration.kind,
|
|
610
|
+
exportedDeclarations.map((declaration) => t3.cloneNode(declaration, true))
|
|
611
|
+
)
|
|
612
|
+
)
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
if (t3.isExpressionStatement(statement)) {
|
|
618
|
+
if (nodeReferencesName(statement, workflowBindingNames) || nodeReferencesName(statement, strippedNames)) {
|
|
619
|
+
collectInlineCreateSteps(statement, seenNames, statements, addActivityBinding);
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
collectInlineCreateSteps(statement, seenNames, statements, addActivityBinding);
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
if (t3.isExportNamedDeclaration(statement)) {
|
|
626
|
+
if (statement.declaration == null && statement.source == null) {
|
|
627
|
+
const retainedSpecifiers = statement.specifiers.filter(
|
|
628
|
+
(specifier) => t3.isExportSpecifier(specifier) && t3.isIdentifier(specifier.local) && specifier.local.name !== "mastra" && !workflowBindingNames.has(specifier.local.name) && !seenNames.has(specifier.local.name)
|
|
629
|
+
);
|
|
630
|
+
if (retainedSpecifiers.length > 0) {
|
|
631
|
+
statements.push(t3.exportNamedDeclaration(null, retainedSpecifiers));
|
|
632
|
+
}
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
635
|
+
if (statement.declaration == null && statement.source) {
|
|
636
|
+
const mastraSpecifiers = statement.specifiers.filter(
|
|
637
|
+
(specifier) => t3.isExportSpecifier(specifier) && t3.isIdentifier(specifier.exported, { name: "mastra" }) && t3.isIdentifier(specifier.local, { name: "mastra" })
|
|
638
|
+
);
|
|
639
|
+
if (mastraSpecifiers.length > 0) {
|
|
640
|
+
statements.push(
|
|
641
|
+
t3.importDeclaration(
|
|
642
|
+
[t3.importSpecifier(t3.identifier("mastra"), t3.identifier("mastra"))],
|
|
643
|
+
t3.stringLiteral(rebaseModulePath(statement.source.value, sourceFilePath, id))
|
|
644
|
+
)
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
collectInlineCreateSteps(statement, seenNames, statements, addActivityBinding);
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
if (t3.isExportDefaultDeclaration(statement)) {
|
|
653
|
+
if (t3.isIdentifier(statement.declaration) && workflowBindingNames.has(statement.declaration.name)) {
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
collectInlineCreateSteps(statement, seenNames, statements, addActivityBinding);
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
statements.push(statement);
|
|
660
|
+
}
|
|
661
|
+
ensureHelperInserted();
|
|
662
|
+
const transformedSource = generate(t3.file(t3.program(pruneUnusedTopLevelBindings(statements), [], "module")), {
|
|
663
|
+
sourceMaps: true
|
|
664
|
+
});
|
|
665
|
+
return transformedSource;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
]
|
|
669
|
+
});
|
|
670
|
+
try {
|
|
671
|
+
const baseName = basename(outputFileName);
|
|
672
|
+
const { output } = await bundle.write({
|
|
673
|
+
dir: outputDirectory,
|
|
674
|
+
entryFileNames: outputFileName,
|
|
675
|
+
chunkFileNames: `${baseName}-[hash].mjs`,
|
|
676
|
+
format: "esm",
|
|
677
|
+
sourcemap: "inline"
|
|
678
|
+
});
|
|
679
|
+
return {
|
|
680
|
+
outputPath: join(outputDirectory, output.find((chunk) => chunk.type === "chunk" && chunk.isEntry).fileName),
|
|
681
|
+
activityBindings
|
|
682
|
+
};
|
|
683
|
+
} finally {
|
|
684
|
+
await bundle.close();
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
function getWorkflowIdMetadata(workflowConfig, workflowName, filePath) {
|
|
688
|
+
for (const property of workflowConfig.properties) {
|
|
689
|
+
if (!t3.isObjectProperty(property) || getObjectPropertyName(property) !== "id") {
|
|
690
|
+
continue;
|
|
691
|
+
}
|
|
692
|
+
if (!t3.isExpression(property.value)) {
|
|
693
|
+
break;
|
|
694
|
+
}
|
|
695
|
+
if (t3.isStringLiteral(property.value)) {
|
|
696
|
+
return {
|
|
697
|
+
expression: t3.cloneNode(property.value, true),
|
|
698
|
+
workflowId: property.value.value
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
if (t3.isTemplateLiteral(property.value) && property.value.expressions.length === 0) {
|
|
702
|
+
return {
|
|
703
|
+
expression: t3.cloneNode(property.value, true),
|
|
704
|
+
workflowId: property.value.quasis[0]?.value.cooked ?? ""
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
throw new Error(`Workflow id must be a static string for ${workflowName} in ${filePath}`);
|
|
708
|
+
}
|
|
709
|
+
throw new Error(`Unable to determine workflow id for ${workflowName} in ${filePath}`);
|
|
710
|
+
}
|
|
711
|
+
function createTemporalWorkflowHelperStatements() {
|
|
712
|
+
const temporalWorkflowRuntimeSource = readFileSync(
|
|
713
|
+
new URL("./temporal-workflow-runtime.mjs", import.meta.url),
|
|
714
|
+
"utf8"
|
|
715
|
+
);
|
|
716
|
+
const helperProgram = parse(temporalWorkflowRuntimeSource, {
|
|
717
|
+
sourceType: "module",
|
|
718
|
+
plugins: parserPlugins
|
|
719
|
+
}).program.body;
|
|
720
|
+
return helperProgram.flatMap((statement) => {
|
|
721
|
+
if (t3.isExportNamedDeclaration(statement) && statement.declaration) {
|
|
722
|
+
return [statement.declaration];
|
|
723
|
+
}
|
|
724
|
+
return [statement];
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
function parseWorkflowChain(node) {
|
|
728
|
+
const methods = [];
|
|
729
|
+
let current = node;
|
|
730
|
+
while (t3.isCallExpression(current) && t3.isMemberExpression(current.callee) && !current.callee.computed) {
|
|
731
|
+
if (!t3.isIdentifier(current.callee.property)) {
|
|
732
|
+
return null;
|
|
733
|
+
}
|
|
734
|
+
methods.unshift({
|
|
735
|
+
name: current.callee.property.name,
|
|
736
|
+
args: current.arguments
|
|
737
|
+
});
|
|
738
|
+
current = current.callee.object;
|
|
739
|
+
}
|
|
740
|
+
if (!isCreateWorkflowCall(current)) {
|
|
741
|
+
return null;
|
|
742
|
+
}
|
|
743
|
+
return {
|
|
744
|
+
createWorkflowCall: current,
|
|
745
|
+
methods
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
function collectStepBindings(program3) {
|
|
749
|
+
const stepBindings = /* @__PURE__ */ new Map();
|
|
750
|
+
for (const statement of program3.body) {
|
|
751
|
+
if (!t3.isVariableDeclaration(statement) && !(t3.isExportNamedDeclaration(statement) && t3.isVariableDeclaration(statement.declaration))) {
|
|
752
|
+
continue;
|
|
753
|
+
}
|
|
754
|
+
const declarationStatement = t3.isVariableDeclaration(statement) ? statement : statement.declaration;
|
|
755
|
+
for (const declaration of declarationStatement.declarations) {
|
|
756
|
+
if (!t3.isIdentifier(declaration.id) || !declaration.init) {
|
|
757
|
+
continue;
|
|
758
|
+
}
|
|
759
|
+
const stepId = getCreateStepId(declaration.init);
|
|
760
|
+
if (stepId) {
|
|
761
|
+
stepBindings.set(declaration.id.name, stepId);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
return stepBindings;
|
|
766
|
+
}
|
|
767
|
+
function collectWorkflowBindings(program3, filePath) {
|
|
768
|
+
const workflowBindings = /* @__PURE__ */ new Map();
|
|
769
|
+
for (const statement of program3.body) {
|
|
770
|
+
if (!t3.isVariableDeclaration(statement) && !(t3.isExportNamedDeclaration(statement) && t3.isVariableDeclaration(statement.declaration))) {
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
const declarationStatement = t3.isVariableDeclaration(statement) ? statement : statement.declaration;
|
|
774
|
+
for (const declaration of declarationStatement.declarations) {
|
|
775
|
+
if (!t3.isIdentifier(declaration.id) || !declaration.init) {
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
const workflowChain = parseWorkflowChain(declaration.init);
|
|
779
|
+
const [workflowConfig] = workflowChain?.createWorkflowCall.arguments ?? [];
|
|
780
|
+
if (!workflowChain || !workflowConfig || !t3.isObjectExpression(workflowConfig)) {
|
|
781
|
+
continue;
|
|
782
|
+
}
|
|
783
|
+
const { workflowId } = getWorkflowIdMetadata(workflowConfig, declaration.id.name, filePath);
|
|
784
|
+
workflowBindings.set(declaration.id.name, toWorkflowType(workflowId));
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
return workflowBindings;
|
|
788
|
+
}
|
|
789
|
+
function getWorkflowStepName(node, stepBindings) {
|
|
790
|
+
if (!node) {
|
|
791
|
+
return null;
|
|
792
|
+
}
|
|
793
|
+
if (t3.isIdentifier(node)) {
|
|
794
|
+
return stepBindings.get(node.name) ?? node.name;
|
|
795
|
+
}
|
|
796
|
+
if (t3.isStringLiteral(node)) {
|
|
797
|
+
return node.value;
|
|
798
|
+
}
|
|
799
|
+
return getCreateStepId(node);
|
|
800
|
+
}
|
|
801
|
+
function rewriteChainMethod(method, filePath, workflowName, stepBindings, workflowBindings) {
|
|
802
|
+
const argNode = (index) => method.args[index];
|
|
803
|
+
const rewritten = (args, name = method.name) => ({ name, args });
|
|
804
|
+
switch (method.name) {
|
|
805
|
+
case "then": {
|
|
806
|
+
const arg = argNode(0);
|
|
807
|
+
if (t3.isIdentifier(arg)) {
|
|
808
|
+
const workflowType = workflowBindings.get(arg.name);
|
|
809
|
+
if (workflowType) {
|
|
810
|
+
return rewritten([t3.stringLiteral(workflowType)], "thenWorkflow");
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
const name = getWorkflowStepName(arg, stepBindings);
|
|
814
|
+
if (!name) {
|
|
815
|
+
throw new Error(
|
|
816
|
+
`.then() in ${workflowName} (${filePath}) must take a step or workflow identifier (inline createStep calls are not supported)`
|
|
817
|
+
);
|
|
818
|
+
}
|
|
819
|
+
return rewritten([t3.stringLiteral(name)]);
|
|
820
|
+
}
|
|
821
|
+
case "sleep": {
|
|
822
|
+
const arg = argNode(0);
|
|
823
|
+
if (t3.isNumericLiteral(arg)) {
|
|
824
|
+
return rewritten([t3.cloneNode(arg, true)]);
|
|
825
|
+
}
|
|
826
|
+
const name = getWorkflowStepName(arg, stepBindings);
|
|
827
|
+
if (!name) {
|
|
828
|
+
throw new Error(`.sleep() in ${workflowName} (${filePath}) must be a numeric literal or an identifier`);
|
|
829
|
+
}
|
|
830
|
+
return rewritten([t3.stringLiteral(name)]);
|
|
831
|
+
}
|
|
832
|
+
case "sleepUntil": {
|
|
833
|
+
const arg = argNode(0);
|
|
834
|
+
if (t3.isNewExpression(arg) && t3.isIdentifier(arg.callee) && arg.callee.name === "Date") {
|
|
835
|
+
return rewritten([t3.cloneNode(arg, true)]);
|
|
836
|
+
}
|
|
837
|
+
if (t3.isStringLiteral(arg) || t3.isNumericLiteral(arg)) {
|
|
838
|
+
return rewritten([t3.cloneNode(arg, true)]);
|
|
839
|
+
}
|
|
840
|
+
const name = getWorkflowStepName(arg, stepBindings);
|
|
841
|
+
if (!name) {
|
|
842
|
+
throw new Error(
|
|
843
|
+
`.sleepUntil() in ${workflowName} (${filePath}) must be a Date, string/number literal, or an identifier`
|
|
844
|
+
);
|
|
845
|
+
}
|
|
846
|
+
return rewritten([t3.stringLiteral(name)]);
|
|
847
|
+
}
|
|
848
|
+
case "parallel": {
|
|
849
|
+
const arg = argNode(0);
|
|
850
|
+
if (!t3.isArrayExpression(arg)) {
|
|
851
|
+
throw new Error(`.parallel() in ${workflowName} (${filePath}) requires an array literal argument`);
|
|
852
|
+
}
|
|
853
|
+
const names = arg.elements.map((el) => getWorkflowStepName(el, stepBindings));
|
|
854
|
+
if (names.some((n) => !n)) {
|
|
855
|
+
throw new Error(`Unable to determine step names inside .parallel() in ${workflowName} (${filePath})`);
|
|
856
|
+
}
|
|
857
|
+
return rewritten([t3.arrayExpression(names.map((n) => t3.stringLiteral(n)))]);
|
|
858
|
+
}
|
|
859
|
+
case "branch": {
|
|
860
|
+
const arg = argNode(0);
|
|
861
|
+
if (!t3.isArrayExpression(arg)) {
|
|
862
|
+
throw new Error(
|
|
863
|
+
`.branch() in ${workflowName} (${filePath}) requires an array literal of [condition, step] pairs`
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
const pairs = arg.elements.map((pair) => {
|
|
867
|
+
if (!t3.isArrayExpression(pair) || pair.elements.length !== 2) {
|
|
868
|
+
throw new Error(
|
|
869
|
+
`.branch() pair in ${workflowName} (${filePath}) must be a 2-element array [condition, step]`
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
const condName = getWorkflowStepName(pair.elements[0], stepBindings);
|
|
873
|
+
const stepName = getWorkflowStepName(pair.elements[1], stepBindings);
|
|
874
|
+
if (!condName || !stepName) {
|
|
875
|
+
throw new Error(`.branch() condition and step in ${workflowName} (${filePath}) must be identifiers`);
|
|
876
|
+
}
|
|
877
|
+
return t3.arrayExpression([t3.stringLiteral(condName), t3.stringLiteral(stepName)]);
|
|
878
|
+
});
|
|
879
|
+
return rewritten([t3.arrayExpression(pairs)]);
|
|
880
|
+
}
|
|
881
|
+
case "dowhile":
|
|
882
|
+
case "dountil": {
|
|
883
|
+
const stepName = getWorkflowStepName(argNode(0), stepBindings);
|
|
884
|
+
const condName = getWorkflowStepName(argNode(1), stepBindings);
|
|
885
|
+
if (!stepName || !condName) {
|
|
886
|
+
throw new Error(`.${method.name}() in ${workflowName} (${filePath}) must take (step, condition) identifiers`);
|
|
887
|
+
}
|
|
888
|
+
return rewritten([t3.stringLiteral(stepName), t3.stringLiteral(condName)]);
|
|
889
|
+
}
|
|
890
|
+
case "foreach": {
|
|
891
|
+
const stepName = getWorkflowStepName(argNode(0), stepBindings);
|
|
892
|
+
if (!stepName) {
|
|
893
|
+
throw new Error(`.foreach() in ${workflowName} (${filePath}) must take a step identifier`);
|
|
894
|
+
}
|
|
895
|
+
const args = [t3.stringLiteral(stepName)];
|
|
896
|
+
const optsArg = method.args[1];
|
|
897
|
+
if (optsArg && t3.isExpression(optsArg)) {
|
|
898
|
+
args.push(t3.cloneNode(optsArg, true));
|
|
899
|
+
}
|
|
900
|
+
return rewritten(args);
|
|
901
|
+
}
|
|
902
|
+
case "commit":
|
|
903
|
+
return rewritten([]);
|
|
904
|
+
default:
|
|
905
|
+
throw new Error(`Unsupported workflow chain method .${method.name}() in ${workflowName} (${filePath})`);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
function getExportedName(node) {
|
|
909
|
+
return t3.isIdentifier(node) ? node.name : node.value;
|
|
910
|
+
}
|
|
911
|
+
function createTemporalWorkflowStatements(exportName, workflowId, methods, filePath, includeCommit, stepBindings, workflowBindings) {
|
|
912
|
+
let expression = t3.callExpression(t3.identifier("createWorkflow"), [t3.cloneNode(workflowId, true)]);
|
|
913
|
+
for (const method of methods) {
|
|
914
|
+
const rewrittenMethod = rewriteChainMethod(method, filePath, exportName, stepBindings, workflowBindings);
|
|
915
|
+
expression = t3.callExpression(
|
|
916
|
+
t3.memberExpression(expression, t3.identifier(rewrittenMethod.name)),
|
|
917
|
+
rewrittenMethod.args
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
if (includeCommit && !methods.some((m) => m.name === "commit")) {
|
|
921
|
+
expression = t3.callExpression(t3.memberExpression(expression, t3.identifier("commit")), []);
|
|
922
|
+
}
|
|
923
|
+
const argsParam = t3.identifier("args");
|
|
924
|
+
const lambda = t3.arrowFunctionExpression(
|
|
925
|
+
[argsParam],
|
|
926
|
+
t3.blockStatement([t3.returnStatement(t3.callExpression(expression, [t3.identifier("args")]))])
|
|
927
|
+
);
|
|
928
|
+
const declaration = t3.variableDeclaration("const", [t3.variableDeclarator(t3.identifier(exportName), lambda)]);
|
|
929
|
+
return [t3.exportNamedDeclaration(declaration)];
|
|
930
|
+
}
|
|
931
|
+
function getTemporalWorkflowExportFromDeclaration(declaration, filePath) {
|
|
932
|
+
if (!t3.isIdentifier(declaration.id) || !declaration.init) {
|
|
933
|
+
return null;
|
|
934
|
+
}
|
|
935
|
+
const workflowChain = parseWorkflowChain(declaration.init);
|
|
936
|
+
if (!workflowChain) {
|
|
937
|
+
return null;
|
|
938
|
+
}
|
|
939
|
+
const [workflowConfig] = workflowChain.createWorkflowCall.arguments;
|
|
940
|
+
if (!workflowConfig || !t3.isObjectExpression(workflowConfig)) {
|
|
941
|
+
throw new Error(`Unable to determine workflow config for ${declaration.id.name} in ${filePath}`);
|
|
942
|
+
}
|
|
943
|
+
const { workflowId } = getWorkflowIdMetadata(workflowConfig, declaration.id.name, filePath);
|
|
944
|
+
return {
|
|
945
|
+
exportName: toWorkflowType(workflowId),
|
|
946
|
+
workflowId
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
function getVariableDeclarationFromStatement(statement) {
|
|
950
|
+
if (t3.isVariableDeclaration(statement)) {
|
|
951
|
+
return statement;
|
|
952
|
+
}
|
|
953
|
+
if (t3.isExportNamedDeclaration(statement) && t3.isVariableDeclaration(statement.declaration)) {
|
|
954
|
+
return statement.declaration;
|
|
955
|
+
}
|
|
956
|
+
return null;
|
|
957
|
+
}
|
|
958
|
+
function getCommittedWorkflowName(statement) {
|
|
959
|
+
if (!t3.isExpressionStatement(statement)) {
|
|
960
|
+
return null;
|
|
961
|
+
}
|
|
962
|
+
const { expression } = statement;
|
|
963
|
+
if (!t3.isCallExpression(expression) || !t3.isMemberExpression(expression.callee) || expression.callee.computed || !isIdentifierNamed(expression.callee.property, "commit") || !t3.isIdentifier(expression.callee.object)) {
|
|
964
|
+
return null;
|
|
965
|
+
}
|
|
966
|
+
return expression.callee.object.name;
|
|
967
|
+
}
|
|
968
|
+
function createWorkflowTransformState(program3, filePath) {
|
|
969
|
+
return {
|
|
970
|
+
statements: [...createTemporalWorkflowHelperStatements()],
|
|
971
|
+
workflowNames: /* @__PURE__ */ new Set(),
|
|
972
|
+
committedWorkflowNames: /* @__PURE__ */ new Set(),
|
|
973
|
+
inlineExportedWorkflowNames: /* @__PURE__ */ new Set(),
|
|
974
|
+
strippedNames: /* @__PURE__ */ new Set(),
|
|
975
|
+
stepBindings: collectStepBindings(program3),
|
|
976
|
+
workflowBindings: collectWorkflowBindings(program3, filePath),
|
|
977
|
+
workflowExports: []
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
function collectWorkflowDeclarationMetadata(statement, state) {
|
|
981
|
+
const declarationStatement = getVariableDeclarationFromStatement(statement);
|
|
982
|
+
if (!declarationStatement) {
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
for (const declaration of declarationStatement.declarations) {
|
|
986
|
+
if (!t3.isIdentifier(declaration.id) || !declaration.init) {
|
|
987
|
+
continue;
|
|
988
|
+
}
|
|
989
|
+
if (!parseWorkflowChain(declaration.init)) {
|
|
990
|
+
continue;
|
|
991
|
+
}
|
|
992
|
+
state.workflowNames.add(declaration.id.name);
|
|
993
|
+
if (t3.isExportNamedDeclaration(statement)) {
|
|
994
|
+
state.inlineExportedWorkflowNames.add(declaration.id.name);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
function collectWorkflowExportMetadata(statement, state) {
|
|
999
|
+
if (t3.isExportNamedDeclaration(statement) && statement.declaration == null && statement.source == null) {
|
|
1000
|
+
for (const specifier of statement.specifiers) {
|
|
1001
|
+
if (!t3.isExportSpecifier(specifier) || !t3.isIdentifier(specifier.local)) {
|
|
1002
|
+
continue;
|
|
1003
|
+
}
|
|
1004
|
+
if (!state.workflowNames.has(specifier.local.name)) {
|
|
1005
|
+
continue;
|
|
1006
|
+
}
|
|
1007
|
+
if (getExportedName(specifier.exported) === specifier.local.name) {
|
|
1008
|
+
state.inlineExportedWorkflowNames.add(specifier.local.name);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
function collectWorkflowTransformMetadata(program3, state) {
|
|
1014
|
+
for (const statement of program3.body) {
|
|
1015
|
+
collectWorkflowDeclarationMetadata(statement, state);
|
|
1016
|
+
const committedWorkflowName = getCommittedWorkflowName(statement);
|
|
1017
|
+
if (committedWorkflowName) {
|
|
1018
|
+
state.committedWorkflowNames.add(committedWorkflowName);
|
|
1019
|
+
}
|
|
1020
|
+
collectWorkflowExportMetadata(statement, state);
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
function rewriteWorkflowImportDeclaration(statement, state) {
|
|
1024
|
+
if (statement.source.value === "@mastra/core/workflows") {
|
|
1025
|
+
const retainedSpecifiers = statement.specifiers.filter(
|
|
1026
|
+
(specifier) => !(t3.isImportSpecifier(specifier) && t3.isIdentifier(specifier.imported) && (specifier.imported.name === "createWorkflow" || specifier.imported.name === "createStep"))
|
|
1027
|
+
);
|
|
1028
|
+
if (retainedSpecifiers.length > 0) {
|
|
1029
|
+
state.statements.push(t3.importDeclaration(retainedSpecifiers, t3.stringLiteral(statement.source.value)));
|
|
1030
|
+
}
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
1033
|
+
if (isTemporalHelperModule(statement.source.value) || isStrippedExternalModule(statement.source.value)) {
|
|
1034
|
+
for (const name of collectImportedNames(statement)) {
|
|
1035
|
+
state.strippedNames.add(name);
|
|
1036
|
+
}
|
|
1037
|
+
return;
|
|
1038
|
+
}
|
|
1039
|
+
state.statements.push(statement);
|
|
1040
|
+
}
|
|
1041
|
+
function getNormalizedWorkflowBindingName(name, state) {
|
|
1042
|
+
if (!state.workflowNames.has(name)) {
|
|
1043
|
+
return null;
|
|
1044
|
+
}
|
|
1045
|
+
return state.workflowBindings.get(name) ?? name;
|
|
1046
|
+
}
|
|
1047
|
+
function rewriteWorkflowNamedExport(statement, state) {
|
|
1048
|
+
if (statement.source != null) {
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
const retainedSpecifiers = statement.specifiers.flatMap((specifier) => {
|
|
1052
|
+
if (!t3.isExportSpecifier(specifier) || !t3.isIdentifier(specifier.local)) {
|
|
1053
|
+
return [];
|
|
1054
|
+
}
|
|
1055
|
+
const normalizedLocalName = getNormalizedWorkflowBindingName(specifier.local.name, state);
|
|
1056
|
+
if (!normalizedLocalName || getExportedName(specifier.exported) === specifier.local.name) {
|
|
1057
|
+
return [];
|
|
1058
|
+
}
|
|
1059
|
+
return [t3.exportSpecifier(t3.identifier(normalizedLocalName), t3.cloneNode(specifier.exported))];
|
|
1060
|
+
});
|
|
1061
|
+
if (retainedSpecifiers.length > 0) {
|
|
1062
|
+
state.statements.push(t3.exportNamedDeclaration(null, retainedSpecifiers));
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
function rewriteWorkflowVariableDeclaration(statement, filePath, state) {
|
|
1066
|
+
const declarationStatement = getVariableDeclarationFromStatement(statement);
|
|
1067
|
+
if (!declarationStatement) {
|
|
1068
|
+
return;
|
|
1069
|
+
}
|
|
1070
|
+
const declarations = [];
|
|
1071
|
+
for (const declaration of declarationStatement.declarations) {
|
|
1072
|
+
if (isWorkflowHelperDestructure(declaration)) {
|
|
1073
|
+
continue;
|
|
1074
|
+
}
|
|
1075
|
+
if (t3.isIdentifier(declaration.id) && state.stepBindings.has(declaration.id.name)) {
|
|
1076
|
+
state.strippedNames.add(declaration.id.name);
|
|
1077
|
+
continue;
|
|
1078
|
+
}
|
|
1079
|
+
if (!t3.isIdentifier(declaration.id) || !declaration.init) {
|
|
1080
|
+
declarations.push(declaration);
|
|
1081
|
+
continue;
|
|
1082
|
+
}
|
|
1083
|
+
const workflowChain = parseWorkflowChain(declaration.init);
|
|
1084
|
+
if (!workflowChain && nodeReferencesName(declaration.init, state.strippedNames)) {
|
|
1085
|
+
state.strippedNames.add(declaration.id.name);
|
|
1086
|
+
continue;
|
|
1087
|
+
}
|
|
1088
|
+
if (!workflowChain) {
|
|
1089
|
+
declarations.push(declaration);
|
|
1090
|
+
continue;
|
|
1091
|
+
}
|
|
1092
|
+
const [workflowConfig] = workflowChain.createWorkflowCall.arguments;
|
|
1093
|
+
if (!workflowConfig || !t3.isObjectExpression(workflowConfig)) {
|
|
1094
|
+
throw new Error(`Unable to determine workflow config for ${declaration.id.name} in ${filePath}`);
|
|
1095
|
+
}
|
|
1096
|
+
const { expression: workflowId } = getWorkflowIdMetadata(workflowConfig, declaration.id.name, filePath);
|
|
1097
|
+
const workflowExport = getTemporalWorkflowExportFromDeclaration(declaration, filePath);
|
|
1098
|
+
if (!workflowExport) {
|
|
1099
|
+
throw new Error(`Unable to determine workflow export for ${declaration.id.name} in ${filePath}`);
|
|
1100
|
+
}
|
|
1101
|
+
const { exportName } = workflowExport;
|
|
1102
|
+
state.workflowExports.push(workflowExport);
|
|
1103
|
+
state.statements.push(
|
|
1104
|
+
...createTemporalWorkflowStatements(
|
|
1105
|
+
exportName,
|
|
1106
|
+
workflowId,
|
|
1107
|
+
workflowChain.methods,
|
|
1108
|
+
filePath,
|
|
1109
|
+
state.committedWorkflowNames.has(declaration.id.name),
|
|
1110
|
+
state.stepBindings,
|
|
1111
|
+
state.workflowBindings
|
|
1112
|
+
)
|
|
1113
|
+
);
|
|
1114
|
+
}
|
|
1115
|
+
if (declarations.length > 0) {
|
|
1116
|
+
const cloned = declarations.map((declaration) => t3.cloneNode(declaration, true));
|
|
1117
|
+
state.statements.push(t3.variableDeclaration(declarationStatement.kind, cloned));
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
function rewriteWorkflowStatement(statement, filePath, state) {
|
|
1121
|
+
if (t3.isImportDeclaration(statement)) {
|
|
1122
|
+
rewriteWorkflowImportDeclaration(statement, state);
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
if (getCommittedWorkflowName(statement)) {
|
|
1126
|
+
return;
|
|
1127
|
+
}
|
|
1128
|
+
if (t3.isExportNamedDeclaration(statement)) {
|
|
1129
|
+
if (statement.declaration == null) {
|
|
1130
|
+
rewriteWorkflowNamedExport(statement, state);
|
|
1131
|
+
return;
|
|
1132
|
+
}
|
|
1133
|
+
if (t3.isVariableDeclaration(statement.declaration)) {
|
|
1134
|
+
rewriteWorkflowVariableDeclaration(statement, filePath, state);
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
state.statements.push(statement.declaration);
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
if (t3.isExportDefaultDeclaration(statement) && t3.isIdentifier(statement.declaration)) {
|
|
1141
|
+
const normalizedLocalName = getNormalizedWorkflowBindingName(statement.declaration.name, state);
|
|
1142
|
+
if (normalizedLocalName) {
|
|
1143
|
+
state.statements.push(t3.exportDefaultDeclaration(t3.identifier(normalizedLocalName)));
|
|
1144
|
+
}
|
|
1145
|
+
return;
|
|
1146
|
+
}
|
|
1147
|
+
if (getVariableDeclarationFromStatement(statement)) {
|
|
1148
|
+
rewriteWorkflowVariableDeclaration(statement, filePath, state);
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
1151
|
+
state.statements.push(statement);
|
|
1152
|
+
}
|
|
1153
|
+
async function finalizeWorkflowModule(state) {
|
|
1154
|
+
const transformedSource = generate(t3.file(t3.program(pruneUnusedTopLevelBindings(state.statements), [], "module")), {
|
|
1155
|
+
sourceMaps: true
|
|
1156
|
+
});
|
|
1157
|
+
return {
|
|
1158
|
+
...transformedSource,
|
|
1159
|
+
workflows: state.workflowExports
|
|
1160
|
+
};
|
|
1161
|
+
}
|
|
1162
|
+
async function buildTemporalWorkflowModule(entryFile, outputDirectory, outputFileName) {
|
|
1163
|
+
const bundle = await rollup({
|
|
1164
|
+
input: entryFile,
|
|
1165
|
+
treeshake: "smallest",
|
|
1166
|
+
logLevel: "silent",
|
|
1167
|
+
plugins: [
|
|
1168
|
+
{
|
|
1169
|
+
name: "temporal-workflow-transform",
|
|
1170
|
+
transform(code, id) {
|
|
1171
|
+
const ast = parseModule(id, code);
|
|
1172
|
+
const state = createWorkflowTransformState(ast.program, id);
|
|
1173
|
+
collectWorkflowTransformMetadata(ast.program, state);
|
|
1174
|
+
for (const statement of ast.program.body) {
|
|
1175
|
+
rewriteWorkflowStatement(statement, id, state);
|
|
1176
|
+
}
|
|
1177
|
+
return finalizeWorkflowModule(state);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
]
|
|
1181
|
+
});
|
|
1182
|
+
try {
|
|
1183
|
+
const baseName = basename(outputFileName);
|
|
1184
|
+
const { output } = await bundle.write({
|
|
1185
|
+
dir: outputDirectory,
|
|
1186
|
+
entryFileNames: outputFileName,
|
|
1187
|
+
chunkFileNames: `${baseName}-[hash].mjs`,
|
|
1188
|
+
format: "esm",
|
|
1189
|
+
sourcemap: "inline"
|
|
1190
|
+
});
|
|
1191
|
+
return {
|
|
1192
|
+
outputPath: join(outputDirectory, output.find((chunk) => chunk.type === "chunk" && chunk.isEntry).fileName)
|
|
1193
|
+
};
|
|
1194
|
+
} finally {
|
|
1195
|
+
await bundle.close();
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
// src/plugin.ts
|
|
1200
|
+
var CACHE_PATH = "node_modules/.mastra";
|
|
1201
|
+
var WORKFLOW_FILE_NAME = "workflow.mjs";
|
|
1202
|
+
var ACTIVITIES_FILE_NAME = "activities.mjs";
|
|
1203
|
+
var ACTIVITY_BINDINGS_FILE_NAME = "activity-bindings.json";
|
|
1204
|
+
function getGeneratedWorkflowModulePath(outputDir) {
|
|
1205
|
+
return path.join(outputDir, WORKFLOW_FILE_NAME);
|
|
1206
|
+
}
|
|
1207
|
+
function getGeneratedActivitiesModulePath(outputDir) {
|
|
1208
|
+
return path.join(outputDir, ACTIVITIES_FILE_NAME);
|
|
1209
|
+
}
|
|
1210
|
+
function getActivityBindingsPath(outputDir) {
|
|
1211
|
+
return path.join(outputDir, ACTIVITY_BINDINGS_FILE_NAME);
|
|
1212
|
+
}
|
|
1213
|
+
var MastraPlugin = class {
|
|
1214
|
+
#prebuildPath = null;
|
|
1215
|
+
#compiledActivitiesModules = /* @__PURE__ */ new Map();
|
|
1216
|
+
name = "Mastra";
|
|
1217
|
+
constructor() {
|
|
1218
|
+
}
|
|
1219
|
+
async #bundleMastra(entryFile, projectRoot, outputDirectory) {
|
|
1220
|
+
const { BuildBundler } = await import('./mastra-deployer-UCBGECM5.js');
|
|
1221
|
+
const normalizedEntryFile = entryFile.startsWith("file:/") ? fileURLToPath(entryFile) : entryFile;
|
|
1222
|
+
const mastraBundler = new BuildBundler();
|
|
1223
|
+
await mastraBundler.prepare(outputDirectory);
|
|
1224
|
+
await mastraBundler.bundle(normalizedEntryFile, outputDirectory, {
|
|
1225
|
+
toolsPaths: [],
|
|
1226
|
+
projectRoot
|
|
1227
|
+
});
|
|
1228
|
+
return path.join(outputDirectory, "output", "index.mjs");
|
|
1229
|
+
}
|
|
1230
|
+
async prebuild({
|
|
1231
|
+
entryFile,
|
|
1232
|
+
projectRoot = process.cwd()
|
|
1233
|
+
}) {
|
|
1234
|
+
const temporalOutputDir = path.resolve(projectRoot, CACHE_PATH);
|
|
1235
|
+
const compiledEntryPath = await this.#bundleMastra(entryFile, projectRoot, temporalOutputDir);
|
|
1236
|
+
await buildTemporalWorkflowModule(compiledEntryPath, temporalOutputDir, WORKFLOW_FILE_NAME);
|
|
1237
|
+
const { activityBindings } = await buildTemporalActivitiesModule(
|
|
1238
|
+
compiledEntryPath,
|
|
1239
|
+
temporalOutputDir,
|
|
1240
|
+
ACTIVITIES_FILE_NAME
|
|
1241
|
+
);
|
|
1242
|
+
await writeFile(getActivityBindingsPath(temporalOutputDir), JSON.stringify(activityBindings, null, 2), "utf8");
|
|
1243
|
+
this.#prebuildPath = temporalOutputDir;
|
|
1244
|
+
return this.getTemporalWorkerOptions(temporalOutputDir);
|
|
1245
|
+
}
|
|
1246
|
+
#loadActivityBindings(activityBindingsPath) {
|
|
1247
|
+
try {
|
|
1248
|
+
const bindings = JSON.parse(readFileSync(activityBindingsPath, "utf8"));
|
|
1249
|
+
return bindings;
|
|
1250
|
+
} catch (error) {
|
|
1251
|
+
throw new Error(`MastraPlugin.prebuild() must be called before use, or ${activityBindingsPath} must exist`, {
|
|
1252
|
+
cause: error
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
#loadCompiledActivitiesModule(activitiesModulePath) {
|
|
1257
|
+
const cachedModule = this.#compiledActivitiesModules.get(activitiesModulePath);
|
|
1258
|
+
if (cachedModule) {
|
|
1259
|
+
return cachedModule;
|
|
1260
|
+
}
|
|
1261
|
+
const modulePromise = import(`${pathToFileURL(activitiesModulePath).href}?t=${Date.now()}`);
|
|
1262
|
+
this.#compiledActivitiesModules.set(activitiesModulePath, modulePromise);
|
|
1263
|
+
return modulePromise;
|
|
1264
|
+
}
|
|
1265
|
+
#generateActivityBindings(activityBindings, compiledActivitiesPath) {
|
|
1266
|
+
const generatedActivities = {};
|
|
1267
|
+
for (const binding of activityBindings) {
|
|
1268
|
+
if (generatedActivities[binding.stepId]) {
|
|
1269
|
+
continue;
|
|
1270
|
+
}
|
|
1271
|
+
generatedActivities[binding.stepId] = async (...args) => {
|
|
1272
|
+
const activityModule = await this.#loadCompiledActivitiesModule(compiledActivitiesPath);
|
|
1273
|
+
const activity = activityModule[binding.exportName];
|
|
1274
|
+
if (typeof activity !== "function") {
|
|
1275
|
+
throw new Error(`Unable to load activity '${binding.exportName}' from ${compiledActivitiesPath}`);
|
|
1276
|
+
}
|
|
1277
|
+
return activity(...args);
|
|
1278
|
+
};
|
|
1279
|
+
}
|
|
1280
|
+
return generatedActivities;
|
|
1281
|
+
}
|
|
1282
|
+
getTemporalWorkerOptions(temporalOutputDir) {
|
|
1283
|
+
const workflowOutputPath = getGeneratedWorkflowModulePath(temporalOutputDir);
|
|
1284
|
+
const activitiesOutputPath = getGeneratedActivitiesModulePath(temporalOutputDir);
|
|
1285
|
+
const activityBindings = this.#loadActivityBindings(getActivityBindingsPath(temporalOutputDir));
|
|
1286
|
+
return {
|
|
1287
|
+
workflowsPath: workflowOutputPath,
|
|
1288
|
+
// workflowBundle: {
|
|
1289
|
+
// codePath: workflowOutputPath,
|
|
1290
|
+
// sourceMapPath: `${workflowOutputPath}.map`,
|
|
1291
|
+
// },
|
|
1292
|
+
activities: this.#generateActivityBindings(activityBindings, activitiesOutputPath)
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
configureWorker(options) {
|
|
1296
|
+
const augmentedOptions = Object.assign({}, options);
|
|
1297
|
+
if (this.#prebuildPath) {
|
|
1298
|
+
Object.assign(augmentedOptions, this.getTemporalWorkerOptions(this.#prebuildPath));
|
|
1299
|
+
} else {
|
|
1300
|
+
if (!options.workflowsPath || !options.activities) {
|
|
1301
|
+
throw new Error("MastraPlugin.prebuild() must be called before use");
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
return augmentedOptions;
|
|
1305
|
+
}
|
|
1306
|
+
};
|
|
1307
|
+
|
|
1308
|
+
export { MastraPlugin };
|
|
1309
|
+
//# sourceMappingURL=worker.js.map
|
|
1310
|
+
//# sourceMappingURL=worker.js.map
|