utils.ts 16.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
import type { FigmaFrameInfo, Protocol, FrameData } from '../../../types';
import type { SimplifiedFigmaNode, ExtendedFrameStructNode, ParsedDataListResponse } from './types';
import path from 'node:path';
import { toKebabCase } from '../../../utils/naming';
import { extractJSON } from '../../../utils/parser';
import { callModel } from '../../../utils/call-model';
import { logger } from '../../../utils/logger';

// ============= Figma Node Utilities =============
/**
 * Simplifies Figma nodes for content extraction, retaining essential fields for AI processing
 * Removes heavy vector data while keeping text content, images, and layout information
 *
 * @param node - The Figma frame node to simplify
 * @returns Simplified node with only essential fields
 */
export function simplifyFigmaNodeForContent(node: FigmaFrameInfo): SimplifiedFigmaNode {
    const simple: SimplifiedFigmaNode = {
        id: node.id,
        name: node.name,
        type: node.type,
    };

    // Check both url (set by Asset node) and thumbnailUrl (original Figma field)
    const imageUrl = (node as FigmaFrameInfo & { url?: string }).url || node.thumbnailUrl;
    if (imageUrl) {
        simple.url = imageUrl;
    }

    if (node.cornerRadius !== undefined) {
        simple.cornerRadius = node.cornerRadius;
    }

    if (node.characters !== undefined && node.characters !== null) {
        simple.characters = node.characters;
    }

    if (node.visible !== undefined) simple.visible = node.visible;

    if (node.absoluteBoundingBox) simple.absoluteBoundingBox = node.absoluteBoundingBox;

    if (node.children && Array.isArray(node.children)) {
        simple.children = node.children.map(simplifyFigmaNodeForContent);
    }

    if (node.inlineStyles) {
        simple.inlineStyles = node.inlineStyles as Record<string, unknown>;
    }

    if (node.style) {
        simple.style = node.style as unknown as Record<string, unknown>;
    }

    if (node.strokes && Array.isArray(node.strokes) && node.strokes.length > 0) {
        simple.hasStrokes = true;
    }

    return simple;
}

/**
 * Extract node positions with hierarchical structure preserved
 * Returns nested position data maintaining parent-child relationships
 *
 * @param data - Figma frame data (single frame or array of frames)
 * @returns Hierarchical position data with node information
 */
export function extractNodePositionsHierarchical(data: FigmaFrameInfo[] | FigmaFrameInfo | undefined): Record<string, unknown> {
    const result: Record<string, unknown> = {};

    if (!data) {
        return result;
    }

    const list = Array.isArray(data) ? data : [data];

    for (const item of list) {
        if (item && typeof item === 'object' && item.id) {
            const nodeData: Record<string, unknown> = {};

            // Extract position information
            const bounds = item.absoluteBoundingBox || item.absoluteRenderBounds;
            if (bounds) {
                nodeData.x = bounds.x;
                nodeData.y = bounds.y;
                nodeData.w = bounds.width;
                nodeData.h = bounds.height;
            }

            // Recursively process children
            if (item.children && Array.isArray(item.children) && item.children.length > 0) {
                nodeData.children = extractNodePositionsHierarchical(item.children);
            }

            result[item.id] = nodeData;
        }
    }

    return result;
}

/**
 * Extract nodes by IDs, including their full subtrees (all children)
 * This allows inspecting the full content of specific component instances
 */
function extractNodesWithSubtreeByIds(tree: FigmaFrameInfo | FigmaFrameInfo[], idList: string[]): FigmaFrameInfo[] {
    const idSet = new Set(idList);
    const result: FigmaFrameInfo[] = [];

    const findNodes = (nodes: FigmaFrameInfo[]) => {
        for (const node of nodes) {
            if (idSet.has(node.id)) {
                // Deep clone the node to ensure all fields (including url) are preserved
                const clonedNode = JSON.parse(JSON.stringify(node)) as FigmaFrameInfo;
                result.push(clonedNode);
                // Do not recurse into children of a match, because the match already contains them.
            } else if (node.children && Array.isArray(node.children)) {
                findNodes(node.children);
            }
        }
    };

    const nodeArray = Array.isArray(tree) ? tree : [tree];
    findNodes(nodeArray);
    return result;
}

/**
 * Extract nodes by IDs while preserving hierarchical structure
 * Nodes in idList are kept; if a deep child is in idList but parent isn't, the child is still extracted
 *
 * @param tree - The Figma frame tree to search
 * @param idList - Array of node IDs to extract
 * @param options - Optional settings
 * @param options.includeSubtree - If true, includes all descendants of matched nodes
 * @returns Array of extracted nodes with hierarchy preserved
 */
export function extractHierarchicalNodesByIds(
    tree: FigmaFrameInfo | FigmaFrameInfo[],
    idList: string[],
    options?: { includeSubtree?: boolean }
): FigmaFrameInfo[] {
    if (options?.includeSubtree) {
        return extractNodesWithSubtreeByIds(tree, idList);
    }

    const idSet = new Set(idList);
    const result: FigmaFrameInfo[] = [];

    // Helper function to check if a node or any of its descendants are in idList
    const hasDescendantInList = (node: FigmaFrameInfo): boolean => {
        if (idSet.has(node.id)) return true;

        if (node.children && Array.isArray(node.children)) {
            for (const child of node.children) {
                if (typeof child === 'object' && child !== null && 'id' in child) {
                    if (hasDescendantInList(child)) {
                        return true;
                    }
                }
            }
        }

        return false;
    };

    // Helper function to recursively process a single node
    const processNode = (node: FigmaFrameInfo): FigmaFrameInfo[] => {
        // First check if this node or any descendant is in the list
        if (!hasDescendantInList(node)) {
            return [];
        }

        // If current node is in the list, keep it with filtered children
        if (idSet.has(node.id)) {
            const clonedNode: FigmaFrameInfo = { ...node };

            // Process children if they exist
            if (node.children && Array.isArray(node.children)) {
                const filteredChildren: FigmaFrameInfo[] = [];

                for (const child of node.children) {
                    if (typeof child === 'object' && child !== null && 'id' in child) {
                        const processedChildren = processNode(child);
                        filteredChildren.push(...processedChildren);
                    }
                }

                clonedNode.children = filteredChildren.length > 0 ? filteredChildren : [];
            } else {
                clonedNode.children = [];
            }

            return [clonedNode];
        } else {
            // Current node is not in the list, but some descendants are
            // Collect all matching descendants and return them (flattened)
            const matchingDescendants: FigmaFrameInfo[] = [];

            if (node.children && Array.isArray(node.children)) {
                for (const child of node.children) {
                    if (typeof child === 'object' && child !== null && 'id' in child) {
                        const processedChildren = processNode(child);
                        matchingDescendants.push(...processedChildren);
                    }
                }
            }

            return matchingDescendants;
        }
    };

    // Process tree (handle both single node and array)
    const nodeArray = Array.isArray(tree) ? tree : [tree];

    for (const node of nodeArray) {
        const processedNodes = processNode(node);
        result.push(...processedNodes);
    }

    return result;
}

// ============= Structure Processing Utilities =============

/**
 * Post-processes the structure tree in a single traversal
 * Performs three operations simultaneously:
 * 1. Normalizes componentName (moves from top-level to data field)
 * 2. Populates elements data from elementIds
 * 3. Annotates with file system paths (path, componentPath, kebabName)
 *
 * @param structure - The parsed structure from AI model
 * @param frames - The Figma frames tree for element extraction
 */
export function postProcessStructure(structure?: Protocol | Protocol[] | null, frames?: FigmaFrameInfo[]): void {
    if (!structure) {
        return;
    }

    // Utility to join alias path segments (always POSIX '/')
    const joinSegments = (...segments: (string | undefined)[]): string =>
        path.posix.join(...segments.filter((segment): segment is string => Boolean(segment && segment.length)));

    const nodes = Array.isArray(structure) ? structure : [structure];
    let rootPath = '@/components';

    // Convert component name to kebab-case for file naming
    const toKebabName = (node: Protocol): string => {
        const source = node.data.kebabName || node.data.name || node.id || 'component';
        const kebabName = toKebabCase(source);
        if (!node.data.kebabName) {
            node.data.kebabName = kebabName;
        }
        return kebabName;
    };

    const traverse = (node?: Protocol | null, parentPath?: string, level = 0): void => {
        if (!node || !node.data) {
            return;
        }

        // 1. Normalize componentName (from top-level to data field)
        const extendedNode = node as ExtendedFrameStructNode;
        const topLevelComponentName = extendedNode.componentName;
        if (topLevelComponentName && !node.data.componentName) {
            node.data.componentName = topLevelComponentName;
            delete extendedNode.componentName;
        }

        // 2. Populate elements data from elementIds
        if (frames) {
            const nodeData = node.data as FrameData & { elementIds?: string[] };
            const elementIds = nodeData.elementIds;
            if (elementIds && Array.isArray(elementIds)) {
                if (elementIds.length > 0) {
                    node.data.elements = extractHierarchicalNodesByIds(frames, elementIds, { includeSubtree: true });
                } else {
                    node.data.elements = [];
                }
                delete nodeData.elementIds;
            } else {
                node.data.elements = node.data.elements || [];
            }
        }

        // 3. Annotate with file system paths
        const segment = toKebabName(node);
        let currentPath: string;

        if (level === 0) {
            // Root node always uses base path
            currentPath = rootPath;
            rootPath = currentPath;
        } else {
            const ancestorPath = parentPath || rootPath;
            currentPath = joinSegments(ancestorPath, segment);
        }

        // For reusable components, generate flat componentPath (non-hierarchical)
        if (node.data.componentName) {
            const componentKebabName = toKebabCase(node.data.componentName);
            node.data.componentPath = joinSegments(rootPath, componentKebabName);
            node.data.path = node.data.componentPath;
        }

        node.data.path = currentPath;

        // Recursively process children
        if (Array.isArray(node.children) && node.children.length > 0) {
            node.children.forEach(child => traverse(child, node.data.path, level + 1));
        }
    };

    nodes.forEach(node => {
        if (!node || !node.data) {
            return;
        }
        traverse(node, undefined, 0);
    });
}

/**
 * Extract component groups from protocol children
 * Groups components by their componentName for batch processing
 *
 * @param protocol - The protocol node to analyze
 * @returns Map of componentName to array of instances
 */
export function extractComponentGroups(node: Protocol): Map<string, Protocol[]> {
    if (!node || !node.children || node.children.length === 0) return new Map();
    const componentGroups = new Map<string, Protocol[]>();
    const validChildren = node.children.filter(c => c && c.data);

    validChildren.forEach(child => {
        const name = child.data.componentName;
        if (name) {
            if (!componentGroups.has(name)) {
                componentGroups.set(name, []);
            }
            componentGroups.get(name)!.push(child);
        }
    });

    return componentGroups;
}

/**
 * Applies props and state to the protocol node
 * @param parsed - The parsed data list response
 * @param node - The protocol node to apply the props and state to
 * @param compName - The name of the component
 * @param group - The group of components
 * @param isList - Whether the component is a list
 */
export function applyPropsAndStateToProtocol(
    parsed: ParsedDataListResponse,
    node: Protocol,
    compName: string,
    group: Protocol[],
    isList: boolean
): void {
    if (parsed && parsed.state && Array.isArray(parsed.state)) {
        if (isList) {
            if (!node.data.states) {
                node.data.states = [];
            }

            node.data.states.push({
                state: parsed.state,
                componentName: compName,
                componentPath: group[0]?.data.componentPath || '',
            });

            const originalChildren: Protocol[] = node.children || [];
            const newChildren: Protocol[] = [];
            const processedComponentNames = new Set<string>();

            for (const child of originalChildren) {
                const childName = child.data.componentName;
                if (childName === compName) {
                    if (!processedComponentNames.has(childName)) {
                        child.data.name = childName;
                        child.id = childName;
                        const cleanKebabName = toKebabCase(childName);
                        child.data.kebabName = cleanKebabName;
                        delete child.data.path;

                        if (parsed.props && Array.isArray(parsed.props)) {
                            child.data.props = parsed.props;
                        }

                        newChildren.push(child);
                        processedComponentNames.add(childName);
                    }
                } else {
                    newChildren.push(child);
                }
            }

            node.children = newChildren;
        }
    }
}

/**
 * Extracts component properties and states from repeated component instances
 * For components that appear multiple times (e.g., cards in a grid), this function:
 * 1. Groups instances by componentName
 * 2. Uses AI to extract data variations (text, images, etc.)
 * 3. Generates props schema for the component
 * 4. Collapses duplicate instances into a single template + state array
 *
 * @param node - Current structure node to process
 * @param frames - Figma frames for reference
 * @param thumbnailUrl - Design thumbnail URL for AI visual context
 */
export async function populateComponentProps(node: Protocol, frames: FigmaFrameInfo[], thumbnailUrl?: string): Promise<void> {
    if (!node || !node.children || node.children.length === 0) return;

    const componentGroups = extractComponentGroups(node);

    // Process each component group to extract props and data
    for (const [compName, group] of componentGroups) {
        if (group.length === 0) continue;

        const isList = group.length > 1;
        const allElements = group.flatMap(g => g.data.elements || []);
        const simplifiedNodes = allElements
            .filter((n): n is FigmaFrameInfo => typeof n === 'object' && n !== null)
            .map(n => simplifyFigmaNodeForContent(n));
        const figmaDataJson = JSON.stringify(simplifiedNodes);
        const containerName = node.data.name || 'Container';

        try {
            const { extractDataListPrompt } = await import('./prompt');
            const prompt = extractDataListPrompt({
                containerName,
                childComponentName: compName,
                figmaData: figmaDataJson,
            });

            const result = await callModel({
                question: prompt,
                imageUrls: thumbnailUrl,
                responseFormat: { type: 'json_object' },
            });

            const json = extractJSON(result);
            const parsed = JSON.parse(json) as ParsedDataListResponse;
            applyPropsAndStateToProtocol(parsed, node, compName, group, isList);
        } catch (e) {
            logger.printErrorLog(
                `Failed to extract data list for ${compName} in ${containerName}: ${e instanceof Error ? e.message : String(e)}`
            );
        }
    }

    // Recursively process children
    for (const child of node.children) {
        await populateComponentProps(child, frames, thumbnailUrl);
    }
}