/** * Patch Lowering * * Transforms PatchFragment[] (MEL IR) to ConditionalPatchOp[] (Core IR). * * @see SPEC v0.4.0 §17.4, §17.5 */ import type { ExprNode as CoreExprNode } from "@manifesto-ai/core"; import type { PatchLoweringContext } from "./context.js"; import { MelExprNode } from "./lower-expr.js"; /** * MEL TypeExpr (Translator output). */ export type MelTypeExpr = { kind: "primitive"; name: "string" | "number" | "boolean" | "null"; } | { kind: "array"; element: MelTypeExpr; } | { kind: "object"; fields: MelTypeField[]; } | { kind: "union"; members: MelTypeExpr[]; } | { kind: "literal"; value: string | number | boolean | null; } | { kind: "ref"; name: string; }; /** * MEL TypeField. */ export type MelTypeField = { name: string; type: MelTypeExpr; optional?: boolean; }; /** * MEL PatchOp (Translator output - schema operations). * * @see SPEC v0.4.0 §17.4 */ export type MelPatchOp = { kind: "addType"; typeName: string; typeExpr: MelTypeExpr; } | { kind: "addField"; typeName: string; field: MelTypeField & { defaultValue?: unknown; }; } | { kind: "setFieldType"; path: string; typeExpr: MelTypeExpr; } | { kind: "setDefaultValue"; path: string; value: unknown; } | { kind: "addConstraint"; targetPath: string; rule: MelExprNode; message?: string; } | { kind: "addComputed"; name: string; expr: MelExprNode; deps?: string[]; } | { kind: "addActionAvailable"; actionName: string; expr: MelExprNode; }; /** * MEL PatchFragment (Translator output). * * Contains MEL IR expressions that need lowering. * * @see SPEC v0.4.0 §17.4 */ export interface MelPatchFragment { /** * Unique fragment identifier (content-addressed). */ fragmentId: string; /** * Source intent identifier. */ sourceIntentId: string; /** * Fragment operation with MEL IR expressions. */ op: MelPatchOp; /** * Optional condition (MEL IR). * Preserved in output as Core IR. */ condition?: MelExprNode; /** * Confidence score (0-1). */ confidence: number; /** * Evidence strings. */ evidence: string[]; /** * Creation timestamp (ISO 8601). */ createdAt: string; } /** * Lowered TypeExpr (Core format). */ export type LoweredTypeExpr = { kind: "primitive"; name: "string" | "number" | "boolean" | "null"; } | { kind: "array"; element: LoweredTypeExpr; } | { kind: "object"; fields: LoweredTypeField[]; } | { kind: "union"; members: LoweredTypeExpr[]; } | { kind: "literal"; value: string | number | boolean | null; } | { kind: "ref"; name: string; }; /** * Lowered TypeField. */ export type LoweredTypeField = { name: string; type: LoweredTypeExpr; optional?: boolean; }; /** * Lowered PatchOp (Core IR expressions). * * Same structure as MelPatchOp but with Core IR expressions. */ export type LoweredPatchOp = { kind: "addType"; typeName: string; typeExpr: LoweredTypeExpr; } | { kind: "addField"; typeName: string; field: LoweredTypeField & { defaultValue?: unknown; }; } | { kind: "setFieldType"; path: string; typeExpr: LoweredTypeExpr; } | { kind: "setDefaultValue"; path: string; value: unknown; } | { kind: "addConstraint"; targetPath: string; rule: CoreExprNode; message?: string; } | { kind: "addComputed"; name: string; expr: CoreExprNode; deps?: string[]; } | { kind: "addActionAvailable"; actionName: string; expr: CoreExprNode; }; /** * Schema conditional patch operation (intermediate IR for Translator → Host). * * Used for schema evolution operations (addType, addField, addComputed, etc.). * Preserves fragment condition for later evaluation. * * @see SPEC v0.4.0 §17.5 */ export interface SchemaConditionalPatchOp { /** * Fragment identifier (for tracing). */ fragmentId: string; /** * Condition expression (Core IR). * If present, op is only applied when condition evaluates to true. * * @see FDR-MEL-073 */ condition?: CoreExprNode; /** * Lowered patch operation. */ op: LoweredPatchOp; /** * Confidence (preserved from fragment). */ confidence: number; } /** * @deprecated Use SchemaConditionalPatchOp instead. */ export type ConditionalPatchOp = SchemaConditionalPatchOp; /** * Lower PatchFragment[] to SchemaConditionalPatchOp[]. * * Transforms MEL IR expressions to Core IR expressions. * Preserves fragment conditions for evaluation phase. * * @param fragments - MEL IR patch fragments from Translator * @param ctx - Patch lowering context * @returns Core IR schema conditional patch operations * * @see SPEC v0.4.0 §17.5 */ export declare function lowerPatchFragments(fragments: MelPatchFragment[], ctx: PatchLoweringContext): SchemaConditionalPatchOp[];