@mastra/core
Version:
61 lines (60 loc) • 2.96 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region src/workflows/builder/index.ts
const WORKFLOW_BUILDER_SUPPORTED_STEP_TYPES = [
"agent",
"tool",
"mapping",
"workflow",
"parallel",
"foreach",
"sleep",
"sleepUntil",
"conditional",
"loop"
];
function normalizeJsonValue(value, path, seen) {
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
if (typeof value === "number") {
if (!Number.isFinite(value)) throw new TypeError(`${path} must contain only finite numbers.`);
return value;
}
if (typeof value !== "object") throw new TypeError(`${path} must be JSON-safe.`);
if (seen.has(value)) throw new TypeError(`${path} must not contain cycles.`);
seen.add(value);
try {
if (Array.isArray(value)) return value.map((item, index) => normalizeJsonValue(item, `${path}.${index}`, seen));
if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) throw new TypeError(`${path} must contain only plain objects.`);
const normalized = {};
for (const [key, item] of Object.entries(value)) if (item !== void 0) normalized[key] = normalizeJsonValue(item, `${path}.${key}`, seen);
return normalized;
} finally {
seen.delete(value);
}
}
function normalizeEntry(entry) {
const normalized = normalizeJsonValue(entry, "graph entry", /* @__PURE__ */ new Set());
if (normalized.type === "agent" && typeof normalized.agentId !== "string" && typeof normalized.agent === "string") {
normalized.agentId = normalized.agent;
delete normalized.agent;
}
if (normalized.type === "mapping" && typeof normalized.mapConfig !== "string") {
const mapConfig = normalized.mapConfig ?? (normalized.output === void 0 ? void 0 : { output: normalized.output });
if (mapConfig !== void 0) normalized.mapConfig = JSON.stringify(mapConfig);
delete normalized.output;
}
if ((normalized.type === "parallel" || normalized.type === "conditional") && Array.isArray(normalized.steps)) normalized.steps = normalized.steps.map((step) => normalizeEntry(step));
if ((normalized.type === "foreach" || normalized.type === "loop") && normalized.step) normalized.step = normalizeEntry(normalized.step);
return normalized;
}
function normalizeWorkflowBuilderDefinition(input) {
const normalized = normalizeJsonValue(input, "workflow definition", /* @__PURE__ */ new Set());
if (normalized.stateSchema === null) delete normalized.stateSchema;
if (normalized.requestContextSchema === null) delete normalized.requestContextSchema;
if (!Array.isArray(normalized.graph)) throw new TypeError("Workflow definition graph must be an array.");
normalized.graph = normalized.graph.map((entry) => normalizeEntry(entry));
return normalized;
}
//#endregion
exports.WORKFLOW_BUILDER_SUPPORTED_STEP_TYPES = WORKFLOW_BUILDER_SUPPORTED_STEP_TYPES;
exports.normalizeWorkflowBuilderDefinition = normalizeWorkflowBuilderDefinition;
//# sourceMappingURL=index.cjs.map