prompt.ts 20.8 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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
// ============================================
// Common Prompt Sections
// ============================================

import { getProjectProfile, renderProfileConstraint } from '../../utils/project-profile';
import { buildComponentUsageGuidance, buildStretchGuidance } from './strategies/component-usage';

/**
 * Renders the target-project profile (components / tokens / utils / conventions)
 * as a constraint block for injection into the system instructions. Returns an
 * empty string when no profile is active, so generation stays generic.
 */
function profileConstraintSection(): string {
    const p = getProjectProfile();
    return p ? renderProfileConstraint(p) : '';
}

const STYLING_GUIDELINES = `
      - **Style Consistency**: Implement styles using the technical stack and libraries listed in <styling>.
      - **Strict Restriction**: Absolutely ONLY use the technical stack and libraries listed in <styling>. Do NOT use any other styling methods, libraries, or frameworks (e.g., if clsx is not listed, do not use clsx).
      - **Default Styling**: If <styling> is empty or does not contain specific libraries, DEFAULT to standard vanilla CSS.
      
      - **Tailwind CSS + CSS Modules (CRITICAL)**:
        - If the stack includes BOTH Tailwind and CSS Modules (Less/SCSS), use them correctly:
          1. **Tailwind utilities**: Use DIRECTLY in JSX className (e.g., \`className="flex items-center gap-4"\`)
          2. **CSS Modules**: Use ONLY for complex styles that can't be expressed with Tailwind utilities (e.g., gradients, animations, pseudo-elements)
          3. **NEVER use \`@apply\` in CSS Module files** - it's a Tailwind-specific directive that doesn't work in Less/SCSS
          4. Example correct usage:
             TSX: \`<div className={\`flex \${styles.customGradient}\`}>\`
             Less: \`.customGradient { background: linear-gradient(...); }\`
      
      - **CSS Modules Only**: If the tech stack specifies CSS Modules without Tailwind:
        1. Create a corresponding style file (e.g., \`index.module.less\`, \`index.module.scss\`, or \`index.module.css\`)
        2. Import it as \`import styles from './index.module.[ext]';\` in the TSX
        3. Define all styles in the style file using standard CSS/Less/SCSS syntax
        4. Use \`styles.className\` in JSX`;

const ASSETS_HANDLING = `
      - **CRITICAL**: For any image URL starting with \`@/assets\`, you MUST import it at the top of the file.
      - **Asset Name Matching**: 
        - Check the \`<available_assets>\` list for actual filenames in the project.
        - Asset filenames follow the pattern: \`kebab-case-name-id1-id2.ext\` (e.g., "Star 2.svg" → "star-2-1-2861.svg")
        - Match the base name (ignoring spaces, case, and ID suffix): "@/assets/arXiv.svg" → look for "arxiv-*.svg" in the list
        - Use the EXACT filename from the available assets list in your import.
      - Example: If available_assets contains "arxiv-1-2956.svg", use:
        \`import ArXivIcon from '@/assets/arxiv-1-2956.svg';\`
      - **Usage**: \`<img src={ArXivIcon} />\`, do not use backgroundImage property.
      - **NEVER** use the string path directly in JSX or styles.`;

const ICON_PROP_GUIDANCE = `
      - **CRITICAL — Icon props are React nodes/components, NOT image URLs (a wrong usage crashes the render)**:
        - An imported asset file (\`import X from '@/assets/foo.svg'\`) is a **string URL** at runtime (in dev it may be a \`data:image/svg+xml,...\` data URI). It is NEVER a React component.
        - A URL string may ONLY be used as: (1) \`<img src={X} />\`, or (2) a CSS \`backgroundImage: url(...)\` value (use a relative/absolute path there, NOT the \`@\` alias).
        - NEVER pass it to a prop that expects a **React node/component**, such as Ant Design \`Input\`/\`Select\` \`suffix\`/\`prefix\`, \`Button\`'s \`icon\`, or MUI \`startIcon\`/\`endIcon\`. Passing a URL there makes React call \`createElement('data:image/svg+xml,...')\` → \`InvalidCharacterError\`.
        - ✅ To show a custom arrow/icon: import an ICON COMPONENT from the kit's icon package (e.g. Ant Design: \`import { ArrowDownOutlined } from '@ant-design/icons';\` then \`<Select suffixIcon={<ArrowDownOutlined />}>\`) or use a plain \`<img>\`/CSS background for decorative icons.
        - ❌ NEVER do \`<Select suffixIcon={importedSvgUrl}>\` where \`importedSvgUrl\` is an imported \`.svg\`.`;

const DOM_IDS_REQUIREMENT = `
      - Assign \`id\` attributes to the main container and any internal elements, matching \`frame_details\`.`;

const LAYOUT_GUIDANCE = `
      - **Layout & Shell (CRITICAL — no overlapping regions)**:
        - When a frame contains a navigation/menu/sidebar region AND a content region meant to sit side-by-side, render them as flex children of a row container:
          \`display: flex; min-height: 100vh;\`
        - Give the menu/sidebar a FIXED width and \`flex-shrink: 0\` (e.g. \`width: 240px;\`), and the content area \`flex: 1; min-width: 0;\`.
        - Add spacing between them (e.g. \`gap: 16px;\` or padding) — the menu and content must NEVER touch or overlap.
        - NEVER use \`position: absolute\` to overlay the menu on top of the content. They must be separate flex children laid out left-to-right.
        - If the design is a top bar + content (vertical), use \`flex-direction: column\` with the header \`flex-shrink: 0\` and content \`flex: 1; overflow: auto;\`.
        - The content area must be allowed to scroll independently; never squeeze the menu width to 0.
        - Honor the explicit \`layout\` from \`frame_details\` (flex/grid/absolute); only apply the rules above when the design actually places regions side-by-side.`;

const CONTENT_REGION_GUIDANCE = `
      - **Content Region — NOT the whole app shell**:
        - If the design shows a persistent application chrome — a LEFT sidebar/menu, a TOP navigation bar, and/or a BREADCRUMB — treat those three areas as the host application's fixed shell, which is ALREADY provided by the parent app.
        - Generate ONLY the **main content region**: the area to the RIGHT of the left menu and BELOW the top nav and breadcrumb. Do NOT re-implement the left menu, top navigation, or breadcrumb.
        - The component you emit MUST BE the content area itself: a self-contained panel that fills the available space. When it sits beside a sidebar, give the content root \`flex: 1; min-width: 0; overflow: auto;\` and do NOT wrap it inside another app shell (no second sidebar, no second top bar).
        - If you must render the full frame (e.g. for a standalone preview), keep the menu / top nav / breadcrumb as minimal STATIC, NON-interactive placeholders (plain labeled containers) and spend all visual fidelity on the content region. NEVER build a real working menu / router / breadcrumb for them.
        - Rule of thumb: your output = "what lives inside the content area", not "the entire application chrome".`;

const REACT_IMPORT_RULE = `
      - Do **NOT** include \`import React from 'react';\` at the top of the file.`;

const FILE_NAMING_CONVENTION = `
      - ALWAYS name the main component file \`index.tsx\`.
      - ALWAYS name the style file (if applicable) \`index.module.[css|less|scss]\`.
      - NEVER use PascalCase or other names for filenames (e.g., DO NOT use \`MainFrame.tsx\` or \`Button.tsx\`).`;

const OUTPUT_FORMAT = `
  <output_format>
    **CRITICAL - Output Format Requirements:**
    
    **CASE 1: Single File (TSX only)**
    - Return code wrapped in triple backticks with language identifier
    - NO file name header needed
    - Example:
    \`\`\`tsx
    export default function Component() {
      return <div>...</div>;
    }
    \`\`\`

    **CASE 2: Multiple Files (TSX + Styles)**
    - **REQUIRED**: Each file MUST start with EXACTLY \`## \` (two hash symbols + one space) followed by filename
    - **REQUIRED**: Filename must be complete with extension (e.g., \`index.tsx\`, \`index.module.css\`)
    - **FORBIDDEN**: Do NOT use single \`#\`, do NOT omit filename, do NOT use other markers
    - Follow this exact structure:
    
    ## index.tsx
    \`\`\`tsx
    export default function Component() {
      return <div>...</div>;
    }
    \`\`\`

    ## index.module.[css|less|scss]
    \`\`\`[css|less|scss]
    .container {
      /* styles */
    }
    \`\`\`
    
    **VALIDATION CHECKLIST (for multiple files):**
    ✓ Each file section starts with \`## \` (two hashes + space)
    ✓ Filename includes full extension
    ✓ Code wrapped in triple backticks with language
    ✗ DO NOT use \`# filename\` (single hash)
    ✗ DO NOT omit file headers
    ✗ DO NOT use other separators
  </output_format>`;

// ============================================
// Prompt Functions
// ============================================

/**
 * Generate children rendering instructions based on detected modes
 */
function generateChildrenPropsInstructions(modes: { hasStates: boolean; hasIndependentChildren: boolean }): string {
    const instructions: string[] = [];

    if (modes.hasStates) {
        instructions.push(`
      - **List Rendering (States-based)**:
        - Check if \`<frame_details>\` contains a \`states\` property (array).
        - Each state entry has: \`state\` (data array), \`componentName\`, \`componentPath\`.
        - Implementation:
          \`\`\`tsx
          import ComponentName from 'path';
          
          {states[0].state.map((item, index) => (
            <ComponentName key={index} {...item} />
          ))}
          \`\`\`
        - **CRITICAL - Only Use State Data**:
          - **ONLY** pass props that exist in the state data objects.
          - **DO NOT** add extra props like \`content\`, \`className\`, or any other fields not present in state.
          - **DO NOT** create or invent additional data - use exactly what's in the state array.
          - Example: If state has \`{iconSrc, title, description}\`, only pass those three props.
        - **Asset Imports**: If state data contains image paths (e.g., \`imageSrc\`, \`iconSrc\`), 
          import them at the top and pass as values.`);
    }

    if (modes.hasIndependentChildren) {
        instructions.push(`
      - **Independent Components (No Props) - CRITICAL**:
        - If a child has NO \`componentName\` and NO \`properties\`, render as \`<ComponentName />\` without any props.
        - These components use default values or hardcoded content internally.`);
    }

    return instructions.join('\n');
}

export const generateFramePrompt = ({
    childrenImports,
    frameDetails,
    assetFiles,
    styling,
    renderingModes,
    fillContainer,
}: {
    childrenImports: string;
    frameDetails: string;
    assetFiles?: string;
    styling: string;
    renderingModes: {
        hasStates: boolean;
        hasIndependentChildren: boolean;
    };
    fillContainer?: boolean;
}) => {
    return `
<system_instructions>
  <role>
    You are a React Architect.
    Your task is to assemble a "Frame" component that composes multiple child components based on Figma layout data.
  </role>

  <input_context>
    <frame_details>${frameDetails}</frame_details>
    <children>${childrenImports}</children>
    <styling>${styling}</styling>
    ${assetFiles ? `<available_assets>Available asset files: ${assetFiles}</available_assets>` : ''}

    <frame_structure>
      The \`frame_details\` parameter contains:
      - \`layout\`: Layout information for the frame (flex/grid/absolute)
      - \`elements\`: Array of CSS styles and asset URLs for all elements in this component (colors, spacing, fonts, backgrounds, etc.)
      - \`states\` (optional): If present, this frame contains reusable components. Each state entry includes:
        * \`state\`: Array of data objects to be used as props for component instances
        * \`componentName\`: Name of the reusable component
        * \`componentPath\`: Import path for the component
        Use \`states\` data to render multiple instances of reusable components via \`.map()\`.
    </frame_structure>
  </input_context>

  <requirements>
    <req_1>
      **Children Components & Props (CRITICAL)**:
      - The \`<children>\` field describes child components with their import paths and prop types.
${generateChildrenPropsInstructions(renderingModes)}
      
      - **Component Imports (CRITICAL)**:
        - You MUST use the exact import path provided in the \`<children>\` list for each component.
        - **Example**:
          - Provided: \`{"name": "TaskGrid", "path": "@/components/tasks-section/task-grid"}\`
          - CORRECT: \`import TaskGrid from "@/components/tasks-section/task-grid";\`
          - WRONG: \`import TaskGrid from "@/components/task-grid";\` (Do NOT do this!)
    </req_1>
    
    <req_2>
      **Layout & Styling**:
      - Use \`layout_data\` for dimensions, spacing, and flow (flex/grid).
${STYLING_GUIDELINES}
      - Use responsive utilities provided by the chosen libraries to ensure the component is adaptive.
      - Use \`css_context\` for exact background styles, gradients, and shadows.
      - Use \`relative\` positioning for the container.
      - Use \`spacing\` field in <frame_details> to set the spacing between elements
    </req_2>

    <req_7>
      **App Layout & Shell**:
${LAYOUT_GUIDANCE}
    </req_7>

    <req_3>
      **Images & Assets**:
${ASSETS_HANDLING}
    </req_3>

    <req_13>
      **Icon Props ≠ Image URLs**:
${ICON_PROP_GUIDANCE}
    </req_13>

    <req_4>
      **DOM IDs**:
${DOM_IDS_REQUIREMENT}
    </req_4>
    
    <req_5>
      **React Import**:
${REACT_IMPORT_RULE}
    </req_5>
    
    <req_6>
      **File Naming**:
${FILE_NAMING_CONVENTION}
    </req_6>

    <req_8>
      **Content Region (dashboard / app pages)**:
${CONTENT_REGION_GUIDANCE}
    </req_8>

    <req_11>
      **Strict Tech-Stack Compliance (UI components)**:
${buildComponentUsageGuidance()}
    </req_11>

    <req_12>
      **Fill Container**:
${buildStretchGuidance(fillContainer !== false)}
    </req_12>
  </requirements>

${OUTPUT_FORMAT}
${profileConstraintSection()}
</system_instructions>
`.trim();
};

export const generateComponentPrompt = ({
    componentName,
    componentDetails,
    styling,
    assetFiles,
    fillContainer,
}: {
    componentName: string;
    componentDetails: string;
    styling: string;
    assetFiles?: string;
    fillContainer?: boolean;
}) => {
    return `
<system_instructions>
  <role>
    You are a Pixel-Perfect React Frontend Engineer.
    Your goal is to implement a specific UI component from Figma design data with 100% visual fidelity while ensuring header scroll to sections on click.
  </role>

  <input_context>
    <component_details>${componentDetails}</component_details>
    ${assetFiles ? `<available_assets>Available asset files: ${assetFiles}</available_assets>` : ''}
    <styling>${styling}</styling>

    <component_structure>
      The \`component_details\` parameter contains:
      - \`elements\`: Array of CSS styles and asset URLs for all elements in this component (colors, spacing, fonts, backgrounds, etc.)
      - \`componentName\` (optional): If present, indicates this is a **reusable component**; if absent, it's a **regular component**
      - \`props\` (optional): Props interface definition, only present when \`componentName\` exists
    </component_structure>

    <global_styles>
      .gradientBorder {
        position: relative;
        z-index: 0;
        &::before {
          content: '';
          position: absolute;
          inset: 0;
          padding: 1px;  // From strokeWidth
          border-radius: inherit;
          background: linear-gradient(278deg, rgba(170, 255, 248, 0.60) 1.63%, rgba(71, 169, 255, 0.60) 104.34%);  // From strokeColor
          -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
          -webkit-mask-composite: destination-out;
          mask-composite: exclude;
          z-index: -1;
        }
      } 
    </global_styles>

    <visual_reference>
      Refer to the conversation history for the page thumbnail and context.
      Use it to verify visual details like shadows, gradients, and spacing.
    </visual_reference>
  </input_context>

  <requirements>
    <req_1>
     **High Fidelity & Responsive**:
${STYLING_GUIDELINES}
      - **CRITICAL**: Use exact design values from \`component_details.elements\` (colors, spacing, font sizes, gradients, shadows, etc.)
      - Use responsive utilities provided by the chosen libraries
      - For complex styles (gradients, shadows), use values from \`elements\` directly in CSS Modules or inline styles
      - For gradient rounded borders, CHECK \`global_styles\` for \`.gradientBorder\` mixin
    </req_1>

    <req_2>
      **DOM Identification**:
      - EVERY DOM element corresponding to a Figma node MUST have an \`id\` attribute.
      - The \`id\` value must match the \`id\` in the Figma Data exactly.
      - This is crucial for automated position validation.
    </req_2>

    <req_3>
      **Images & Assets**:
${ASSETS_HANDLING}
    </req_3>

    <req_13>
      **Icon Props ≠ Image URLs**:
${ICON_PROP_GUIDANCE}
    </req_13>

    <req_4>
      **Semantic HTML**:
      - Use semantic tags: \`<header>\`, \`<footer>\`, \`<nav>\`, \`<article>\`, \`<section>\`, \`<button>\`.
    </req_4>

    <req_5>
      **Layout Strategy**:
      - PREFER relative/flex/grid layout.
      - Use absolute positioning ONLY for decoration/overlays or if Figma structure explicitly implies overlay.
    </req_5>

    <req_6>
      **Naming Conventions**:
      - Component Name: **${componentName}** (PascalCase).
      - Export default.
    </req_6>

    <req_7>
      **Component Type & Props (CRITICAL)**:
      
      **IF \`component_details.componentName\` exists:**
      - This is a **reusable component**
      - Generate props interface from \`component_details.props\`: \`interface ${componentName}Props { ... }\`
      - Reference props in JSX: \`{title}\`, \`<img src={iconSrc} />\` (NOT hardcoded values)
      
      **IF \`component_details.componentName\` does NOT exist:**
      - This is a **regular component**
      - Do NOT generate props interface
      - Directly hardcode content from \`component_details.elements\` into JSX
      
      **Both types**: Do NOT repeat JSX to simulate grids; parent components handle iteration.
    </req_7>

    <req_8>
      **Content Region (dashboard / app pages)**:
${CONTENT_REGION_GUIDANCE}
    </req_8>

    <req_11>
      **Strict Tech-Stack Compliance (UI components)**:
${buildComponentUsageGuidance()}
    </req_11>

    <req_12>
      **Fill Container**:
${buildStretchGuidance(fillContainer !== false)}
    </req_12>

    <req_9>
      **React Import**:
${REACT_IMPORT_RULE}
    </req_9>
    
    <req_10>
      **File Naming**:
${FILE_NAMING_CONVENTION}
    </req_10>
  </requirements>

${OUTPUT_FORMAT}
${profileConstraintSection()}
</system_instructions>
`.trim();
};

export const injectRootComponentPrompt = ({
    appContent,
    componentName,
    componentPath,
}: {
    appContent: string;
    componentName: string;
    componentPath: string;
}) => {
    return `
<system_instructions>
  <role>
    You are a React code refactoring expert.
    Your task is to inject a root component into an existing App.tsx file with minimal changes.
  </role>

  <input_context>
    <current_app_content>
${appContent}
    </current_app_content>
    <component_to_inject>
      - Component Name: ${componentName}
      - Import Path: ${componentPath}
    </component_to_inject>
  </input_context>

  <requirements>
    <req_1>
      **Import Statement**:
      - Add the import statement at the top of the file: \`import ${componentName} from '${componentPath}';\`
      - Place it after existing imports, before the component definition.
      - Do NOT remove or modify existing imports.
    </req_1>

    <req_2>
      **Component Rendering**:
      - Inside the App component's return statement, replace the existing content with \`<${componentName} />\`.
      - If the return contains a wrapper div or other container, keep it and place the component inside.
      - Preserve any existing className, styles, or other attributes on wrapper elements.
    </req_2>

    <req_3>
      **Preserve Existing Code**:
      - Keep all existing imports (CSS, Less, etc.)
      - Preserve any hooks, state, or logic inside the App component
      - Maintain the component structure and export statement
      - Do NOT add comments or explanatory text
    </req_3>

    <req_4>
      **Minimal Changes**:
      - Only add the necessary import and render the component
      - Do NOT refactor or optimize existing code
      - Do NOT change formatting or styling unless necessary
      - Do NOT add TypeScript types unless they already exist
    </req_4>

    <req_5>
      **React Import**:
${REACT_IMPORT_RULE}
    </req_5>
  </requirements>

  <output_format>
    Return ONLY the complete updated App.tsx code without markdown code blocks or explanation.
    The output should be valid TypeScript/React code that can be directly written to the file.
  </output_format>
${profileConstraintSection()}
</system_instructions>
`.trim();
};