{
  "version": "3.16",
  "name": "MetaDirect-CodeGen",
  "description": "Small-project code generation: ProjectMeta -> default Theme/Theme.vth + project database -> all VL files in parallel.",
  "registry": {
    "params": [
      "userRequest(STRING)",
      "targetLang(STRING)"
    ],
    "services": [],
    "apis": [],
    "components": [],
    "vars": [
      "$projectMeta(OBJECT)",
      "$projectMetaStr(STRING)",
      "$vdbContent(STRING)",
      "$vthContent(STRING)",
      "$codeValidation(OBJECT)",
      "$repairPlan(OBJECT)"
    ],
    "files": {
      "inputs": [],
      "artifacts": [
        ".vl-code/ProjectMeta.json",
        "Database/*",
        "Theme/*",
        "Services/*",
        "ExtComponents/*",
        "Sections/*",
        "Apps/*"
      ]
    },
    "docs": {
      "1": "VL Syntax Rules",
      "100": "Agent-GenDatabase: .vdb generation",
      "101": "Agent-GenTheme: .vth generation",
      "102": "Agent-GenServiceDomain: .vs generation",
      "103": "Agent-GenComponent: .cp generation",
      "104": "Agent-GenSection: .sc generation",
      "105": "Agent-GenApp: .vx generation",
      "107": "Agent-RepairVLFile: Targeted repair for failed VL files",
      "111": "Agent-MetaDirect: Generate ProjectMeta directly from user request"
    }
  },
  "steps": [
    {
      "id": "ClearFiles_005_ClearExisting",
      "meta": {
        "title": "0. Clear existing VL files before regeneration"
      },
      "in": {},
      "out": {},
      "next": "LLM_010_GenMeta"
    },
    {
      "id": "LLM_010_GenMeta",
      "meta": {
        "title": "1. Generate ProjectMeta directly from user request"
      },
      "in": {
        "docs": [
          "1",
          "111"
        ],
        "max_tokens": 16000,
        "stream": true,
        "messages": [
          {
            "role": "user",
            "content": "User Request:"
          },
          {
            "role": "user",
            "content": "=userRequest"
          },
          {
            "role": "user",
            "content": "Target Language: =targetLang"
          },
          {
            "role": "user",
            "content": "Generate the complete ProjectMeta JSON. REQUIRED structure:\n{\n  \"specVersion\": \"ProjectMeta/1.0\",\n  \"projectName\": \"<PascalCase name>\",\n  \"projectDescription\": \"...\",\n  \"vlVersion\": \"3.5\",\n  \"config\": { \"defaultDevice\": \"Phone\", \"defaultResolution\": \"375x812\", \"themeFile\": \"Theme/Theme.vth\", \"colorScheme\": \"light\" },\n  \"database\": { \"file\": \"Database/<ProjectName>.vdb\" },\n  \"theme\": { \"file\": \"Theme/Theme.vth\" },\n  \"dataSchema\": { \"tables\": [{ \"id\": \"<TableName>\", \"fields\": [{ \"name\": \"...\", \"type\": \"INT|STRING|BOOL|TIMESTAMP|FLOAT\", \"constraints\": [\"PK\",\"autoIncrement\",\"notNull\"] }], \"indexes\": [] }], \"relations\": [] },\n  \"services\": [{ \"domainId\": \"<ServiceName>\", \"filePath\": \"Services/<ServiceName>.vs\", \"methods\": [{ \"id\": \"<MethodName>\", \"isPublic\": false, \"params\": \"paramName(TYPE)\", \"description\": \"...\" }], \"virtualTables\": [{ \"id\": \"<TableName>\", \"alias\": \"<alias>\" }] }],\n  \"components\": [{ \"id\": \"<CompName>\", \"filePath\": \"ExtComponents/<CompName>.cp\", \"props\": [{ \"name\": \"...\", \"type\": \"...\", \"default\": \"...\" }], \"slots\": [] }],\n  \"sections\": [{ \"id\": \"<SectionName>\", \"filePath\": \"Sections/<SectionName>.sc\", \"consumesServices\": [\"<ServiceName>\"], \"usesComponents\": [\"<CompName>\"], \"bindings\": [{ \"var\": \"$varName\", \"source\": \"<ServiceName>.<MethodName>\" }], \"events\": [{ \"trigger\": \"...\", \"action\": \"<ServiceName>.<MethodName>\", \"params\": \"...\" }] }],\n  \"apps\": [{ \"id\": \"<AppName>\", \"filePath\": \"Apps/<AppName>.vx\", \"routes\": [{ \"path\": \"/<path>\", \"section\": \"<SectionName>\" }], \"layout\": { \"type\": \"tabs|stack|drawer\", \"items\": [\"<SectionName>\"] } }]\n}\nCRITICAL: Every section MUST have consumesServices and usesComponents arrays linking to actual services/components. Every entity MUST have filePath. Output ONLY valid JSON."
          }
        ],
        "output_config": {
          "format": {
            "type": "json_object"
          }
        }
      },
      "out": {
        "$projectMeta": "=_result",
        "$projectMetaStr": "=_result",
        "/.vl-code/ProjectMeta.json": "=_result"
      },
      "next": "Fork_DBTheme"
    },
    {
      "id": "Fork_DBTheme",
      "meta": {
        "title": "2. Generate Database + seed default Theme in parallel"
      },
      "children": [
        "LLM_020_GenDB",
        "SeedTheme_030_DefaultTheme"
      ],
      "next": "Loop_Services"
    },
    {
      "id": "LLM_020_GenDB",
      "meta": {
        "title": "2a. Generate Database Schema (.vdb)"
      },
      "in": {
        "docs": [
          "1",
          "100"
        ],
        "max_tokens": 8192,
        "stream": true,
        "messages": [
          {
            "role": "user",
            "content": "ProjectMeta:"
          },
          {
            "role": "user",
            "content": "=$projectMetaStr"
          },
          {
            "role": "user",
            "content": "Generate the .vdb database schema file based on the databases defined in ProjectMeta."
          }
        ]
      },
      "out": {
        "$vdbContent": "=_result",
        "/{$projectMeta.database.file || ('Database/' + $projectMeta.projectName + '.vdb')}": "=_result"
      }
    },
    {
      "id": "SeedTheme_030_DefaultTheme",
      "meta": {
        "title": "2b. Seed default Theme/Theme.vth"
      },
      "in": {
        "path": "Theme/Theme.vth"
      },
      "out": {
        "$vthContent": "=_result",
        "/Theme/Theme.vth": "=_result"
      },
      "next": "RETURN"
    },
    {
      "id": "Loop_Services",
      "meta": {
        "title": "3. Generate Service Domains (parallel)"
      },
      "source": "=$projectMeta.services",
      "mode": "parallel",
      "children": [
        "LLM_040_GenService"
      ],
      "next": "Loop_Components"
    },
    {
      "id": "LLM_040_GenService",
      "meta": {
        "title": "Generate Service: =_item.domainId"
      },
      "in": {
        "docs": [
          "1",
          "102"
        ],
        "max_tokens": 8192,
        "stream": true,
        "messages": [
          {
            "role": "user",
            "content": "ProjectMeta:"
          },
          {
            "role": "user",
            "content": "=$projectMetaStr"
          },
          {
            "role": "user",
            "content": "Database Schema:"
          },
          {
            "role": "user",
            "content": "=$vdbContent"
          },
          {
            "role": "user",
            "content": "Generate the .vs service domain file for:"
          },
          {
            "role": "user",
            "content": "=_item"
          },
          {
            "role": "user",
            "content": "Backend query-safety rules: build filter arrays incrementally. If an optional STRING/INT filter is empty, null, 0, or -1 (used as 'all'), DO NOT include that condition in select/count queries. Never generate conditions like [\"field\",\"*eq\",\"\"] or [\"field\",\"*contains\",\"\"] for optional filters. Prefer `_conditions([]) = []` plus IF checks and `_conditions.push(...)`."
          }
        ]
      },
      "out": {
        "/{_item.filePath}": "=_result"
      },
      "onError": "skip"
    },
    {
      "id": "Loop_Components",
      "meta": {
        "title": "4. Generate Components"
      },
      "source": "=$projectMeta.components",
      "mode": "parallel",
      "children": [
        "LLM_050_GenComponent"
      ],
      "next": "Loop_Sections"
    },
    {
      "id": "LLM_050_GenComponent",
      "meta": {
        "title": "Generate Component: =_item.id"
      },
      "in": {
        "docs": [
          "1",
          "103"
        ],
        "max_tokens": 8192,
        "stream": true,
        "messages": [
          {
            "role": "user",
            "content": "ProjectMeta:"
          },
          {
            "role": "user",
            "content": "=$projectMetaStr"
          },
          {
            "role": "user",
            "content": "Theme:"
          },
          {
            "role": "user",
            "content": "=$vthContent"
          },
          {
            "role": "user",
            "content": "Generate the .cp component file for:"
          },
          {
            "role": "user",
            "content": "=_item"
          }
        ]
      },
      "out": {
        "/{_item.filePath}": "=_result"
      },
      "onError": "skip"
    },
    {
      "id": "Loop_Sections",
      "meta": {
        "title": "5. Generate Sections (parallel)"
      },
      "source": "=$projectMeta.sections",
      "mode": "parallel",
      "children": [
        "LLM_060_GenSection"
      ],
      "next": "Loop_Apps"
    },
    {
      "id": "LLM_060_GenSection",
      "meta": {
        "title": "Generate Section: =_item.id"
      },
      "in": {
        "docs": [
          "1",
          "104"
        ],
        "max_tokens": 16000,
        "stream": true,
        "messages": [
          {
            "role": "user",
            "content": "ProjectMeta:"
          },
          {
            "role": "user",
            "content": "=$projectMetaStr"
          },
          {
            "role": "user",
            "content": "Database Schema:"
          },
          {
            "role": "user",
            "content": "=$vdbContent"
          },
          {
            "role": "user",
            "content": "Theme:"
          },
          {
            "role": "user",
            "content": "=$vthContent"
          },
          {
            "role": "user",
            "content": "Generate the .sc section file for:"
          },
          {
            "role": "user",
            "content": "=_item"
          },
          {
            "role": "user",
            "content": "Compile-safety rules: 1) style: values must be static string literals only; NEVER use ternary, concat, variables, or any expression inside style:. If appearance depends on data, split the node with If blocks or use StateStyle conditions; never write style:($cond ? \"a\" : \"b\"). 2) NEVER emit CSS-only skin properties such as border-collapse. 3) Use only services that actually exist in ProjectMeta / generated services, and keep ServiceDomain / Service names aligned with declared ids. 4) Prefer VL-safe layout props over raw CSS. 5) NEVER bind events directly on <For-*> nodes; bind the event on the interactive child inside the loop instead. 6) NEVER emit deprecated <StateStyle-*> trigger:\"hover\" blocks. Output only valid .sc source."
          }
        ]
      },
      "out": {
        "/{_item.filePath}": "=_result"
      },
      "onError": "retry",
      "retryCount": 1
    },
    {
      "id": "Loop_Apps",
      "meta": {
        "title": "6. Generate Apps (parallel)"
      },
      "source": "=$projectMeta.apps",
      "mode": "parallel",
      "children": [
        "LLM_070_GenApp"
      ],
      "next": "Set_080_SkipValidation"
    },
    {
      "id": "LLM_070_GenApp",
      "meta": {
        "title": "Generate App: =_item.id"
      },
      "in": {
        "docs": [
          "1",
          "105"
        ],
        "max_tokens": 8192,
        "stream": true,
        "messages": [
          {
            "role": "user",
            "content": "ProjectMeta:"
          },
          {
            "role": "user",
            "content": "=$projectMetaStr"
          },
          {
            "role": "user",
            "content": "Generate the .vx app file for:"
          },
          {
            "role": "user",
            "content": "=_item"
          }
        ]
      },
      "out": {
        "/{_item.filePath}": "=_result"
      },
      "onError": "retry",
      "retryCount": 1
    },
    {
      "id": "Set_080_SkipValidation",
      "meta": {
        "title": "7. Skip validation (generation-only mode)"
      },
      "target": "$codeValidation",
      "value": "={\"success\":true,\"skipped\":true,\"reason\":\"validation disabled\"}",
      "next": "Stop_Done"
    },
    {
      "id": "Stop_Done",
      "meta": {
        "title": "Done"
      }
    }
  ]
}
