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