{
  "version": "3.16",
  "name": "AddService",
  "description": "Add a new backend service domain to an existing project. Creates service .vs file, updates database schema if needed, and updates ProjectMeta.",
  "sectionMerge": true,
  "registry": {
    "params": [
      "serviceDescription(STRING)",
      "currentMeta(OBJECT)"
    ],
    "services": [],
    "apis": [],
    "components": [],
    "vars": [
      "$updatedMeta(OBJECT)",
      "$updatedMetaStr(STRING)",
      "$servicePlan(OBJECT)",
      "$dbChanges(OBJECT)"
    ],
    "files": {
      "inputs": [
        ".vl-code/ProjectMeta.json",
        "Database/*"
      ],
      "artifacts": [
        ".vl-code/ProjectMeta.json",
        "Database/*",
        "Services/*"
      ]
    },
    "docs": {
      "1": "VL Syntax Rules",
      "100": "Agent-GenDatabase-Prompt",
      "102": "Agent-GenServiceDomain-Prompt"
    }
  },
  "steps": [
    {
      "id": "LLM_010_PlanService",
      "meta": {
        "title": "1. Plan new service structure"
      },
      "in": {
        "docs": [
          "1"
        ],
        "max_tokens": 32768,
        "messages": [
          {
            "role": "user",
            "content": "Current ProjectMeta:"
          },
          {
            "role": "user",
            "content": "=currentMeta"
          },
          {
            "role": "user",
            "content": "New service request:"
          },
          {
            "role": "user",
            "content": "=serviceDescription"
          },
          {
            "role": "user",
            "content": "Plan the new service domain. Determine: 1) Service name and methods (CRUD + custom), 2) Database table changes needed (new tables or new columns), 3) API routes. Return JSON with: service(OBJECT with name, methods[], routes[]), dbChanges(OBJECT with newTables[], modifiedTables[]), updatedMeta(full updated ProjectMeta)."
          }
        ],
        "output_config": {
          "format": {
            "type": "json_object"
          }
        }
      },
      "out": {
        "$servicePlan": "=_result.service",
        "$dbChanges": "=_result.dbChanges",
        "$updatedMeta": "=_result.updatedMeta",
        "$updatedMetaStr": "=_result.updatedMeta",
        "/.vl-code/ProjectMeta.json": "=_result.updatedMeta"
      },
      "next": "Branch_NeedDBUpdate"
    },
    {
      "id": "Branch_NeedDBUpdate",
      "meta": {
        "title": "2. Check if DB update needed"
      },
      "cases": [
        {
          "condition": "=$dbChanges.newTables.length > 0 || =$dbChanges.modifiedTables.length > 0",
          "next": "LLM_020_UpdateDB"
        }
      ],
      "default": "LLM_030_GenService"
    },
    {
      "id": "LLM_020_UpdateDB",
      "meta": {
        "title": "2a. Update database schema"
      },
      "in": {
        "docs": [
          "1",
          "100"
        ],
        "max_tokens": 32768,
        "stream": true,
        "readFiles": [
          "=currentMeta.database.file || ('Database/' + currentMeta.projectName + '.vdb')"
        ],
        "messages": [
          {
            "role": "user",
            "content": "ProjectMeta:"
          },
          {
            "role": "user",
            "content": "=$updatedMetaStr"
          },
          {
            "role": "user",
            "content": "Database changes needed:"
          },
          {
            "role": "user",
            "content": "=$dbChanges"
          },
          {
            "role": "user",
            "content": "Update the .vdb database schema file. Use sectionMerge — only add/modify the affected tables, preserve all existing tables.\n\nIMPORTANT: The file MUST start with '// VL_VERSION:3.7' as the first non-empty line."
          }
        ]
      },
      "out": {
        "/{currentMeta.database.file || ('Database/' + currentMeta.projectName + '.vdb')}": "=_result"
      },
      "next": "LLM_030_GenService"
    },
    {
      "id": "LLM_030_GenService",
      "meta": {
        "title": "3. Generate service domain file"
      },
      "in": {
        "docs": [
          "1",
          "102"
        ],
        "max_tokens": 32768,
        "stream": true,
        "messages": [
          {
            "role": "user",
            "content": "ProjectMeta:"
          },
          {
            "role": "user",
            "content": "=$updatedMetaStr"
          },
          {
            "role": "user",
            "content": "Generate the .vs service domain file for:"
          },
          {
            "role": "user",
            "content": "=$servicePlan\n\nIMPORTANT: The file MUST start with '// VL_VERSION:3.7' as the first non-empty line.\n\nBackend 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": {
        "/{$servicePlan.filePath}": "=_result"
      },
      "next": "Stop_Done"
    },
    {
      "id": "Stop_Done",
      "meta": {
        "title": "Done — New service added"
      }
    }
  ]
}
