UNPKG

@mastra/core

Version:
1 lines 9.24 kB
{"version":3,"file":"index.cjs","names":[],"sources":["../../../src/workflows/builder/index.ts"],"sourcesContent":["import type { Predicate } from '../predicate';\nimport type { ValidatableStepFlowEntry, WorkflowValidationInput } from '../stored/validate/types';\nimport type { SerializedSingleStepEntry, SerializedStepOptions } from '../types';\n\nexport type WorkflowBuilderJsonValue =\n | string\n | number\n | boolean\n | null\n | WorkflowBuilderJsonValue[]\n | { [key: string]: WorkflowBuilderJsonValue };\n\nexport type WorkflowBuilderJsonObject = { [key: string]: WorkflowBuilderJsonValue };\n\nexport type WorkflowBuilderStepOptions = SerializedStepOptions;\n\n/**\n * Authoring leaf entries are the canonical serialized leaf union minus\n * code-only `step` descriptors (a persisted definition cannot reference a\n * live Step object). Derived, not duplicated: when the serialized union\n * changes, these change with it.\n */\nexport type WorkflowBuilderSingleStepEntry = Exclude<SerializedSingleStepEntry, { type: 'step' }>;\n\nexport type WorkflowBuilderAgentEntry = Extract<WorkflowBuilderSingleStepEntry, { type: 'agent' }>;\nexport type WorkflowBuilderToolEntry = Extract<WorkflowBuilderSingleStepEntry, { type: 'tool' }>;\nexport type WorkflowBuilderMappingEntry = Extract<WorkflowBuilderSingleStepEntry, { type: 'mapping' }>;\nexport type WorkflowBuilderWorkflowEntry = Extract<WorkflowBuilderSingleStepEntry, { type: 'workflow' }>;\n\nexport type WorkflowBuilderExecutableInnerEntry = Exclude<WorkflowBuilderSingleStepEntry, { type: 'mapping' }>;\n\n/**\n * Container entries are hand-written *narrowings* of the serialized union:\n * declarative predicates are required (closure conditions can't be authored),\n * fluent-only debug labels (`serializedConditions`/`serializedCondition`) are\n * absent, and `sleepUntil.date` is the wire's ISO string rather than a Date.\n * The static assertions at the bottom of this file prove each narrowing stays\n * inside the canonical union — drift is a compile error.\n */\nexport interface WorkflowBuilderParallelEntry {\n type: 'parallel';\n steps: WorkflowBuilderExecutableInnerEntry[];\n}\n\nexport interface WorkflowBuilderForeachEntry {\n type: 'foreach';\n step: WorkflowBuilderExecutableInnerEntry;\n opts?: { concurrency: number };\n}\n\nexport interface WorkflowBuilderSleepEntry {\n type: 'sleep';\n id: string;\n duration: number;\n}\n\nexport interface WorkflowBuilderSleepUntilEntry {\n type: 'sleepUntil';\n id: string;\n date: string;\n}\n\nexport interface WorkflowBuilderConditionalEntry {\n type: 'conditional';\n steps: WorkflowBuilderExecutableInnerEntry[];\n predicates: Predicate[];\n}\n\nexport interface WorkflowBuilderLoopEntry {\n type: 'loop';\n step: WorkflowBuilderExecutableInnerEntry;\n loopType: 'dowhile' | 'dountil';\n predicate: Predicate;\n}\n\nexport type WorkflowBuilderGraphEntry =\n | WorkflowBuilderSingleStepEntry\n | WorkflowBuilderParallelEntry\n | WorkflowBuilderForeachEntry\n | WorkflowBuilderSleepEntry\n | WorkflowBuilderSleepUntilEntry\n | WorkflowBuilderConditionalEntry\n | WorkflowBuilderLoopEntry;\n\nexport interface WorkflowBuilderDefinition {\n id: string;\n description?: string;\n metadata?: Record<string, unknown>;\n inputSchema: WorkflowBuilderJsonObject;\n outputSchema: WorkflowBuilderJsonObject;\n stateSchema?: WorkflowBuilderJsonObject;\n requestContextSchema?: WorkflowBuilderJsonObject;\n graph: WorkflowBuilderGraphEntry[];\n}\n\ntype Extends<A, B> = [A] extends [B] ? true : false;\ntype Expect<T extends true> = T;\n\n/**\n * Compile-time drift guards: the authoring universe must remain a subset of\n * the canonical serialized/wire union the validation core operates on. If a\n * serialized variant gains a required field (or an authoring type drifts),\n * these tuple members stop typechecking and the build fails.\n */\nexport type WorkflowBuilderTypeAssertions = [\n Expect<Extends<WorkflowBuilderGraphEntry, ValidatableStepFlowEntry>>,\n Expect<Extends<WorkflowBuilderDefinition, WorkflowValidationInput>>,\n];\n\nexport const WORKFLOW_BUILDER_SUPPORTED_STEP_TYPES = [\n 'agent',\n 'tool',\n 'mapping',\n 'workflow',\n 'parallel',\n 'foreach',\n 'sleep',\n 'sleepUntil',\n 'conditional',\n 'loop',\n] as const;\n\nexport type WorkflowBuilderSupportedStepType = (typeof WORKFLOW_BUILDER_SUPPORTED_STEP_TYPES)[number];\n\nfunction normalizeJsonValue(value: unknown, path: string, seen: Set<object>): WorkflowBuilderJsonValue {\n if (value === null || typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') {\n if (!Number.isFinite(value)) throw new TypeError(`${path} must contain only finite numbers.`);\n return value;\n }\n if (typeof value !== 'object') throw new TypeError(`${path} must be JSON-safe.`);\n if (seen.has(value)) throw new TypeError(`${path} must not contain cycles.`);\n seen.add(value);\n try {\n if (Array.isArray(value)) return value.map((item, index) => normalizeJsonValue(item, `${path}.${index}`, seen));\n if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) {\n throw new TypeError(`${path} must contain only plain objects.`);\n }\n const normalized: WorkflowBuilderJsonObject = {};\n for (const [key, item] of Object.entries(value)) {\n if (item !== undefined) normalized[key] = normalizeJsonValue(item, `${path}.${key}`, seen);\n }\n return normalized;\n } finally {\n seen.delete(value);\n }\n}\n\nfunction normalizeEntry(entry: Record<string, unknown>): WorkflowBuilderGraphEntry {\n const normalized = normalizeJsonValue(entry, 'graph entry', new Set()) as WorkflowBuilderJsonObject;\n if (normalized.type === 'agent' && typeof normalized.agentId !== 'string' && typeof normalized.agent === 'string') {\n normalized.agentId = normalized.agent;\n delete normalized.agent;\n }\n if (normalized.type === 'mapping' && typeof normalized.mapConfig !== 'string') {\n const mapConfig =\n normalized.mapConfig ?? (normalized.output === undefined ? undefined : { output: normalized.output });\n if (mapConfig !== undefined) normalized.mapConfig = JSON.stringify(mapConfig);\n delete normalized.output;\n }\n if ((normalized.type === 'parallel' || normalized.type === 'conditional') && Array.isArray(normalized.steps)) {\n normalized.steps = normalized.steps.map(step =>\n normalizeEntry(step as Record<string, unknown>),\n ) as unknown as WorkflowBuilderJsonValue[];\n }\n if ((normalized.type === 'foreach' || normalized.type === 'loop') && normalized.step) {\n normalized.step = normalizeEntry(normalized.step as Record<string, unknown>) as unknown as WorkflowBuilderJsonValue;\n }\n return normalized as unknown as WorkflowBuilderGraphEntry;\n}\n\nexport function normalizeWorkflowBuilderDefinition(input: unknown): WorkflowBuilderDefinition {\n const normalized = normalizeJsonValue(input, 'workflow definition', new Set()) as WorkflowBuilderJsonObject;\n if (normalized.stateSchema === null) delete normalized.stateSchema;\n if (normalized.requestContextSchema === null) delete normalized.requestContextSchema;\n if (!Array.isArray(normalized.graph)) throw new TypeError('Workflow definition graph must be an array.');\n normalized.graph = normalized.graph.map(entry =>\n normalizeEntry(entry as Record<string, unknown>),\n ) as unknown as WorkflowBuilderJsonValue[];\n return normalized as unknown as WorkflowBuilderDefinition;\n}\n"],"mappings":";;AA6GA,MAAa,wCAAwC;CACnD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAIA,SAAS,mBAAmB,OAAgB,MAAc,MAA6C;CACrG,IAAI,UAAU,QAAQ,OAAO,UAAU,YAAY,OAAO,UAAU,WAAW,OAAO;CACtF,IAAI,OAAO,UAAU,UAAU;EAC7B,IAAI,CAAC,OAAO,SAAS,KAAK,GAAG,MAAM,IAAI,UAAU,GAAG,KAAK,mCAAmC;EAC5F,OAAO;CACT;CACA,IAAI,OAAO,UAAU,UAAU,MAAM,IAAI,UAAU,GAAG,KAAK,oBAAoB;CAC/E,IAAI,KAAK,IAAI,KAAK,GAAG,MAAM,IAAI,UAAU,GAAG,KAAK,0BAA0B;CAC3E,KAAK,IAAI,KAAK;CACd,IAAI;EACF,IAAI,MAAM,QAAQ,KAAK,GAAG,OAAO,MAAM,KAAK,MAAM,UAAU,mBAAmB,MAAM,GAAG,KAAK,GAAG,SAAS,IAAI,CAAC;EAC9G,IAAI,OAAO,eAAe,KAAK,MAAM,OAAO,aAAa,OAAO,eAAe,KAAK,MAAM,MACxF,MAAM,IAAI,UAAU,GAAG,KAAK,kCAAkC;EAEhE,MAAM,aAAwC,CAAC;EAC/C,KAAK,MAAM,CAAC,KAAK,SAAS,OAAO,QAAQ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