@mastra/temporal 0.4.6-alpha.8 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/temporal-workflow-runtime.mjs +30 -10
- package/dist/transforms/activities.d.ts.map +1 -1
- package/dist/transforms/temporal-workflow-runtime.d.mts +13 -0
- package/dist/transforms/temporal-workflow-runtime.d.mts.map +1 -1
- package/dist/transforms/workflows.d.ts.map +1 -1
- package/dist/worker.cjs +70 -33
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.js +70 -33
- package/dist/worker.js.map +1 -1
- package/package.json +5 -5
package/dist/worker.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { t as toWorkflowType } from "./utils-B_iPTfDi.js";
|
|
2
|
+
import { builtinModules } from "module";
|
|
2
3
|
import { readFileSync } from "fs";
|
|
3
4
|
import { writeFile } from "fs/promises";
|
|
4
5
|
import path, { basename, join } from "path";
|
|
@@ -324,7 +325,7 @@ function collectTopLevelBindingNames(program) {
|
|
|
324
325
|
for (const statement of program.body) for (const name of Object.keys(t.getBindingIdentifiers(statement))) bindings.add(name);
|
|
325
326
|
return bindings;
|
|
326
327
|
}
|
|
327
|
-
function createMappingActivityStatements(workflowExpression, filePath, staticFunctionBindings, usedExportNames, addBinding) {
|
|
328
|
+
function createMappingActivityStatements(workflowExpression, filePath, staticFunctionBindings, usedExportNames, requestContextClassName, addBinding) {
|
|
328
329
|
const chain = parseWorkflowChain$1(workflowExpression);
|
|
329
330
|
if (!chain) return [];
|
|
330
331
|
const workflowId = getStaticWorkflowId(chain.createWorkflowCall, filePath);
|
|
@@ -341,8 +342,23 @@ function createMappingActivityStatements(workflowExpression, filePath, staticFun
|
|
|
341
342
|
let exportSuffix = 1;
|
|
342
343
|
while (usedExportNames.has(exportName)) exportName = `${baseExportName}${exportSuffix++}`;
|
|
343
344
|
usedExportNames.add(exportName);
|
|
344
|
-
const
|
|
345
|
-
|
|
345
|
+
const contextNames = [
|
|
346
|
+
"requestContext",
|
|
347
|
+
"runId",
|
|
348
|
+
"resourceId",
|
|
349
|
+
"workflowId"
|
|
350
|
+
];
|
|
351
|
+
const params = t.objectPattern([
|
|
352
|
+
t.objectProperty(t.identifier("inputData"), t.identifier("inputData"), false, true),
|
|
353
|
+
t.objectProperty(t.identifier("initData"), t.identifier("initData"), false, true),
|
|
354
|
+
...contextNames.map((name) => t.objectProperty(t.identifier(name), t.identifier(name), false, true))
|
|
355
|
+
]);
|
|
356
|
+
const callbackContext = t.objectExpression([
|
|
357
|
+
t.objectProperty(t.identifier("inputData"), t.identifier("inputData"), false, true),
|
|
358
|
+
t.objectProperty(t.identifier("getInitData"), t.arrowFunctionExpression([], t.identifier("initData"))),
|
|
359
|
+
t.objectProperty(t.identifier("requestContext"), t.newExpression(t.identifier(requestContextClassName), [t.identifier("requestContext")])),
|
|
360
|
+
...contextNames.filter((name) => name !== "requestContext").map((name) => t.objectProperty(t.identifier(name), t.identifier(name), false, true))
|
|
361
|
+
]);
|
|
346
362
|
const callbackExpression = t.cloneNode(callback, true);
|
|
347
363
|
const body = t.callExpression(callbackExpression, [callbackContext]);
|
|
348
364
|
statements.push(t.exportNamedDeclaration(t.variableDeclaration("const", [t.variableDeclarator(t.identifier(exportName), t.arrowFunctionExpression([params], body, true))])));
|
|
@@ -381,27 +397,38 @@ function hasLocalMastraBinding(ast) {
|
|
|
381
397
|
return declaration.declarations.some((declarator) => t.isIdentifier(declarator.id, { name: "mastra" }));
|
|
382
398
|
});
|
|
383
399
|
}
|
|
384
|
-
function createTemporalActivitiesHelperStatements(mastraImportPath, hasMastraBinding) {
|
|
385
|
-
return parse(
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
400
|
+
function createTemporalActivitiesHelperStatements(mastraImportPath, hasMastraBinding, requestContextClassName) {
|
|
401
|
+
return parse(`
|
|
402
|
+
function withRequestContext(params) {
|
|
403
|
+
const { requestContext, initData, ...rest } = params;
|
|
404
|
+
return {
|
|
405
|
+
...rest,
|
|
406
|
+
requestContext: new ${requestContextClassName}(requestContext),
|
|
407
|
+
getInitData: () => initData,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
${mastraImportPath ? `
|
|
412
|
+
function createStep(args) {
|
|
413
|
+
return async (params) => {
|
|
414
|
+
const { mastra } = await import(${JSON.stringify(mastraImportPath)});
|
|
415
|
+
return args.execute({ ...withRequestContext(params), mastra });
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
` : hasMastraBinding ? `
|
|
419
|
+
function createStep(args) {
|
|
420
|
+
return async (params) => {
|
|
421
|
+
return args.execute({ ...withRequestContext(params), mastra });
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
` : `
|
|
425
|
+
function createStep(args) {
|
|
426
|
+
return async (params) => {
|
|
427
|
+
return args.execute(withRequestContext(params));
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
`}
|
|
431
|
+
`, {
|
|
405
432
|
sourceType: "module",
|
|
406
433
|
plugins: parserPlugins
|
|
407
434
|
}).program.body;
|
|
@@ -433,19 +460,23 @@ async function buildTemporalActivitiesModule(entryFile, outputDirectory, outputF
|
|
|
433
460
|
plugins: parserPlugins,
|
|
434
461
|
sourceFilename: id
|
|
435
462
|
});
|
|
436
|
-
const
|
|
463
|
+
const usedExportNames = collectTopLevelBindingNames(ast.program);
|
|
464
|
+
let requestContextClassName = "TemporalRequestContext";
|
|
465
|
+
let requestContextSuffix = 1;
|
|
466
|
+
while (usedExportNames.has(requestContextClassName)) requestContextClassName = `TemporalRequestContext${requestContextSuffix++}`;
|
|
467
|
+
usedExportNames.add(requestContextClassName);
|
|
468
|
+
const statements = [t.importDeclaration([t.importSpecifier(t.identifier(requestContextClassName), t.identifier("RequestContext"))], t.stringLiteral("@mastra/core/di"))];
|
|
437
469
|
const seenNames = /* @__PURE__ */ new Set();
|
|
438
470
|
const strippedNames = /* @__PURE__ */ new Set();
|
|
439
471
|
const workflowBindingNames = collectWorkflowBindingNames(ast);
|
|
440
472
|
const stepFactoryBindings = collectCreateStepFactoryBindings(ast.program);
|
|
441
473
|
const staticFunctionBindings = collectStaticFunctionBindings(ast.program);
|
|
442
|
-
const usedExportNames = collectTopLevelBindingNames(ast.program);
|
|
443
474
|
const sourceFilePath = id;
|
|
444
475
|
const hasMastraBinding = hasLocalMastraBinding(ast);
|
|
445
476
|
let helperInserted = false;
|
|
446
477
|
const ensureHelperInserted = () => {
|
|
447
478
|
if (helperInserted) return;
|
|
448
|
-
statements.push(...createTemporalActivitiesHelperStatements(null, hasMastraBinding));
|
|
479
|
+
statements.push(...createTemporalActivitiesHelperStatements(null, hasMastraBinding, requestContextClassName));
|
|
449
480
|
helperInserted = true;
|
|
450
481
|
};
|
|
451
482
|
for (const statement of ast.program.body) {
|
|
@@ -501,7 +532,7 @@ async function buildTemporalActivitiesModule(entryFile, outputDirectory, outputF
|
|
|
501
532
|
if (hasCreateWorkflowCall(declaration.init)) {
|
|
502
533
|
workflowBindingNames.add(declaration.id.name);
|
|
503
534
|
strippedNames.add(declaration.id.name);
|
|
504
|
-
statements.push(...createMappingActivityStatements(declaration.init, sourceFilePath, staticFunctionBindings, usedExportNames, addGeneratedActivityBinding));
|
|
535
|
+
statements.push(...createMappingActivityStatements(declaration.init, sourceFilePath, staticFunctionBindings, usedExportNames, requestContextClassName, addGeneratedActivityBinding));
|
|
505
536
|
collectInlineCreateSteps(declaration.init, seenNames, statements, addActivityBinding);
|
|
506
537
|
continue;
|
|
507
538
|
}
|
|
@@ -541,7 +572,7 @@ async function buildTemporalActivitiesModule(entryFile, outputDirectory, outputF
|
|
|
541
572
|
if (hasCreateWorkflowCall(declaration.init)) {
|
|
542
573
|
workflowBindingNames.add(declaration.id.name);
|
|
543
574
|
strippedNames.add(declaration.id.name);
|
|
544
|
-
statements.push(...createMappingActivityStatements(declaration.init, sourceFilePath, staticFunctionBindings, usedExportNames, addGeneratedActivityBinding));
|
|
575
|
+
statements.push(...createMappingActivityStatements(declaration.init, sourceFilePath, staticFunctionBindings, usedExportNames, requestContextClassName, addGeneratedActivityBinding));
|
|
545
576
|
collectInlineCreateSteps(declaration.init, seenNames, statements, addActivityBinding);
|
|
546
577
|
continue;
|
|
547
578
|
}
|
|
@@ -876,6 +907,10 @@ function getCommittedWorkflowName(statement) {
|
|
|
876
907
|
if (!t.isCallExpression(expression) || !t.isMemberExpression(expression.callee) || expression.callee.computed || !isIdentifierNamed(expression.callee.property, "commit") || !t.isIdentifier(expression.callee.object)) return null;
|
|
877
908
|
return expression.callee.object.name;
|
|
878
909
|
}
|
|
910
|
+
const nodeBuiltinModules = new Set(builtinModules.flatMap((moduleName) => [moduleName, `node:${moduleName}`]));
|
|
911
|
+
function isNodeBuiltinModule(moduleId) {
|
|
912
|
+
return nodeBuiltinModules.has(moduleId);
|
|
913
|
+
}
|
|
879
914
|
function createWorkflowTransformState(program, filePath) {
|
|
880
915
|
return {
|
|
881
916
|
statements: [...createTemporalWorkflowHelperStatements()],
|
|
@@ -1035,14 +1070,16 @@ async function buildTemporalWorkflowModule(entryFile, outputDirectory, outputFil
|
|
|
1035
1070
|
}]
|
|
1036
1071
|
});
|
|
1037
1072
|
try {
|
|
1038
|
-
const
|
|
1039
|
-
const { output } = await bundle.write({
|
|
1073
|
+
const outputOptions = {
|
|
1040
1074
|
dir: outputDirectory,
|
|
1041
1075
|
entryFileNames: outputFileName,
|
|
1042
|
-
chunkFileNames: `${
|
|
1076
|
+
chunkFileNames: `${basename(outputFileName)}-[hash].mjs`,
|
|
1043
1077
|
format: "esm",
|
|
1044
1078
|
sourcemap: "inline"
|
|
1045
|
-
}
|
|
1079
|
+
};
|
|
1080
|
+
const forbiddenImport = (await bundle.generate(outputOptions)).output.filter((output) => output.type === "chunk").flatMap((chunk) => [...chunk.imports, ...chunk.dynamicImports]).find(isNodeBuiltinModule);
|
|
1081
|
+
if (forbiddenImport) throw new Error(`Temporal workflow bundle cannot depend on Node.js builtin '${forbiddenImport}'. Move the dependency into an activity or remove it from workflow initialization.`);
|
|
1082
|
+
const { output } = await bundle.write(outputOptions);
|
|
1046
1083
|
return { outputPath: join(outputDirectory, output.find((chunk) => chunk.type === "chunk" && chunk.isEntry).fileName) };
|
|
1047
1084
|
} finally {
|
|
1048
1085
|
await bundle.close();
|