validation-types.ts 6.91 KB
/**
 * Shared validation types.
 *
 * Types used across multiple subsystems in the validation pipeline:
 * - Validation node (src/nodes/validation/)
 * - Position tool (src/tools/position-tool/)
 * - Report tool (src/tools/report-tool/)
 * - History tool (src/tools/history-tool/)
 * - Judger agent (src/agents/judger-agent/)
 *
 * Per type organization hierarchy: shared types go in src/types/
 */

import type { FigmaFrameInfo } from './figma-types.js';

// ============================================================================
// Utility Types
// ============================================================================

/**
 * Generic dictionary/object type with string keys and unknown values.
 * Used for flexible data structures like Figma JSON trees, structure trees, etc.
 */
export type Dict = Record<string, unknown>;

// ============================================================================
// Validation Context Types (extracted from protocol)
// ============================================================================

/**
 * Figma position with offset already applied (CSS coordinates).
 */
export interface FigmaPosition {
    x: number;
    y: number;
    w: number;
    h: number;
}

/**
 * Component information from protocol structure.
 */
export interface ComponentInfo {
    id: string;
    name: string;
    path: string;
}

/**
 * Complete metadata for a single element (Figma node) in context.
 */
export interface ElementInfo {
    id: string;
    name: string;
    type: string;
    /** Position with offset already applied (CSS coordinates) */
    position: FigmaPosition;
    /** Raw Figma layout properties */
    layoutMode?: string;
    primaryAxisAlignItems?: string;
    counterAxisAlignItems?: string;
    itemSpacing?: number;
    paddingTop?: number;
    paddingRight?: number;
    paddingBottom?: number;
    paddingLeft?: number;
    constraints?: Record<string, unknown>;
    /** Parent component ID */
    parentComponentId: string;
    /** Parent component name */
    parentComponentName: string;
    /** Parent component path */
    parentComponentPath: string;
    /** Parent item type */
    parentItemType: 'frame' | 'component';
}

/**
 * Extended Figma frame information for validation.
 * Includes layout properties needed for position validation.
 */
export interface ValidationFrameInfo extends FigmaFrameInfo {
    layoutMode?: string; // 'HORIZONTAL' | 'VERTICAL' | 'NONE'
    primaryAxisAlignItems?: string; // Auto-layout: main axis alignment
    counterAxisAlignItems?: string; // Auto-layout: cross axis alignment
    itemSpacing?: number; // Auto-layout: gap between items
    paddingTop?: number; // Auto-layout padding
    paddingRight?: number;
    paddingBottom?: number;
    paddingLeft?: number;
}

/**
 * Unified validation context extracted from protocol.
 * Built once at validation start and reused across all iterations.
 */
export interface ValidationContext {
    /** Coordinate offset from Figma canvas to CSS (0,0) origin */
    offset: { x: number; y: number };

    /** All elements indexed by Figma node ID */
    elements: Map<string, ElementInfo>;

    /** All components indexed by component ID */
    components: Map<string, ComponentInfo>;

    /** Element ID to parent component mapping (for compatibility) */
    elementToComponent: Map<string, ComponentInfo>;

    /** All Figma positions indexed by node ID (offset already applied) */
    figmaPositions: Record<string, FigmaPosition | undefined>;
}

/**
 * Element metadata registry for compatibility with existing APIs.
 */
export interface ElementMetadataRegistry {
    elements: Map<
        string,
        {
            id: string;
            name: string;
            parentComponentId: string;
            parentComponentName: string;
            parentComponentPath: string;
            parentItemType: 'frame' | 'component';
        }
    >;
    components: Map<string, ComponentInfo>;
}

// ============================================================================
// Validation Reports
// ============================================================================

/**
 * User-facing validation report with annotated screenshots and analysis
 * Generated by ValidationNode after position capture and annotation
 */
export interface UserReport {
    design: {
        snap: string; // Figma thumbnail URL
        markedSnap: string; // Annotated design image (base64 data URI)
    };
    page: {
        url: string; // Local server URL
        snap: string; // Page screenshot (base64 data URI)
        markedSnap: string; // Annotated page screenshot (base64 data URI)
    };
    report: {
        heatmap: string; // Reserved for future heatmap implementation (empty for now)
        detail: {
            metrics: {
                mae: number;
                sae: number;
                misalignedCount: number;
            };
            // Grouped component type for report visualization
            components: {
                componentId: string;
                componentPath: string;
                elementId?: string;
                validationInfo?: {
                    x: number;
                    y: number;
                };
                elements?: {
                    elementId: string;
                    elementIndex: number;
                    validationInfo: {
                        x: number; // Horizontal deviation in px
                        y: number; // Vertical deviation in px
                    };
                }[];
            }[];
        };
    };
}

/**
 * Structured validation error report
 * Contains position comparison data for a single component
 */
export interface ValidationReport {
    /** [x, y] of rendered position */
    currentPosition: [number, number];
    /** [x, y] of expected Figma position */
    targetPosition: [number, number];
    /** [abs_error_x, abs_error_y] absolute values of position error */
    absoluteError: [number, number];
}

/**
 * Misaligned component data
 * Contains all information about a component that needs fixing
 */
export interface MisalignedComponent {
    name: string;
    componentId: string;
    elementIds: string[];
    path: string;
    validationReport: ValidationReport;
    currentX: number;
    currentY: number;
    targetX: number;
    targetY: number;
    currentWidth: number;
    currentHeight: number;
    targetWidth: number;
    targetHeight: number;
    distance: number;
}

// ============================================================================
// Component History
// ============================================================================

/**
 * Component history entry
 * Tracks position and fix across iterations for HistoryTool
 */
export interface ComponentHistoryEntry {
    iteration: number;
    position: [number, number];
    error: [number, number];
    fixApplied: string[] | null;
}

/**
 * Component history map
 * Maps component IDs to their history entries
 */
export type ComponentHistory = Record<string, ComponentHistoryEntry[]>;