{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aancer.ai/dev/schemas/template.schema.json",
  "title": "AANCER workflow template (pack item kind template, schemaVersion 1)",
  "description": "The spec of a template item. A template is a workflow with every installation-specific resource replaced by a typed parameter. Rules this schema cannot express: exactly one trigger step, rootStepId points at it; bindings must name existing parameters and steps and follow the binding allow-list; templates with AI steps need aiAct.riskClass minimal, limited or high plus aiAct.intendedPurpose; no credential-like text anywhere.",
  "type": "object",
  "additionalProperties": false,
  "required": ["id", "version", "record"],
  "properties": {
    "id": { "type": "string", "minLength": 1, "maxLength": 64 },
    "version": { "type": "integer", "minimum": 1 },
    "workspace": { "type": "string", "pattern": "^\\{\\{param\\.[a-z][a-z0-9_]{0,63}\\}\\}$", "description": "A workspace pack parameter. Leave it out to publish installation-wide." },
    "record": {
      "type": "object",
      "required": ["name", "description", "icon", "industry", "complexity", "locale", "aiAct", "compliance", "parameters", "bindings", "translations", "flow", "createdAt"],
      "properties": {
        "name": { "type": "string", "minLength": 1, "maxLength": 80 },
        "description": { "type": "string", "minLength": 1, "maxLength": 300 },
        "longDescription": { "type": "string" },
        "icon": { "type": "string", "minLength": 1, "maxLength": 40 },
        "industry": { "enum": ["healthcare", "legal", "financial", "hr", "manufacturing", "retail", "saas", "marketing", "operations", "devops"] },
        "industries": { "type": "array", "items": { "type": "string" } },
        "complexity": { "enum": ["starter", "intermediate", "advanced"] },
        "estimatedTimeSaved": { "type": "string" },
        "locale": { "type": "string", "maxLength": 20 },
        "dataClassification": { "type": "string" },
        "aiAct": {
          "type": "object",
          "required": ["riskClass"],
          "properties": {
            "riskClass": { "enum": ["unclassified", "minimal", "limited", "high", "prohibited"] },
            "annexIiiCategory": { "type": "string" },
            "intendedPurpose": { "type": "string" },
            "humanOversight": { "type": "string" },
            "transparencyNote": { "type": "string" }
          }
        },
        "compliance": {
          "type": "object",
          "required": ["dataCategories", "dataSubjects"],
          "properties": {
            "dataCategories": { "type": "array", "items": { "type": "string" } },
            "dataSubjects": { "type": "array", "items": { "type": "string" } },
            "lawfulBasis": { "enum": ["", "contract", "consent", "legitimate-interest", "legal-obligation"] },
            "retentionDays": { "type": ["integer", "null"], "minimum": 0, "maximum": 36500 }
          }
        },
        "parameters": {
          "type": "array",
          "maxItems": 50,
          "items": {
            "type": "object",
            "required": ["id", "type", "label"],
            "properties": {
              "id": { "type": "string", "pattern": "^[a-z][a-z0-9_]{0,39}$" },
              "type": { "enum": ["connection", "knowledgeSet", "agent", "persona", "dataset", "approvers", "secret", "string", "number", "boolean", "select"] },
              "label": { "type": "string", "minLength": 1 },
              "help": { "type": "string" },
              "required": { "type": "boolean" },
              "default": { "type": ["string", "number", "boolean"], "description": "Not allowed for connection, knowledgeSet, agent, persona, dataset, approvers or secret." },
              "options": { "type": "array", "minItems": 1, "items": { "type": "string", "minLength": 1 } },
              "connector": { "type": "string" }
            }
          }
        },
        "bindings": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["parameterId", "stepId", "path"],
            "properties": {
              "parameterId": { "type": "string" },
              "stepId": { "type": "string" },
              "path": { "type": "string" }
            }
          }
        },
        "translations": {
          "type": "object",
          "additionalProperties": {
            "type": "object",
            "properties": {
              "name": { "type": "string" },
              "description": { "type": "string" },
              "longDescription": { "type": "string" },
              "parameters": {
                "type": "object",
                "additionalProperties": {
                  "type": "object",
                  "properties": { "label": { "type": "string" }, "help": { "type": "string" } }
                }
              }
            }
          }
        },
        "flow": {
          "type": "object",
          "required": ["rootStepId", "steps"],
          "properties": {
            "name": { "type": "string" },
            "description": { "type": "string" },
            "rootStepId": { "type": "string" },
            "steps": { "type": "array", "minItems": 2, "maxItems": 200, "items": { "$ref": "#/$defs/step" } }
          }
        },
        "builtAgainst": {
          "type": "object",
          "properties": { "catalogHash": { "type": "string" }, "platformVersion": { "type": "string" } }
        },
        "createdAt": { "type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}" }
      }
    }
  },
  "$defs": {
    "step": {
      "type": "object",
      "description": "One workflow step. Resource fields (connectorAuth, knowledgeSetId, agentRef, agentId, personaId, datasetId, approvers, triggerAuthFields, webhookPath, agentApprovers, capturedOutput) must not appear: fill them through bindings.",
      "required": ["id", "type", "name", "position"],
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
        "description": { "type": "string" },
        "type": { "enum": ["trigger", "aancer-ai", "rag", "agent", "connector", "approval", "condition", "router", "loop", "code", "output", "guardrails", "file-search", "publish", "ai-router"] },
        "position": { "type": "object", "required": ["x", "y"], "properties": { "x": { "type": "number" }, "y": { "type": "number" } } },
        "nextStepId": { "type": "string" },
        "triggerType": { "enum": ["webhook", "schedule", "event", "piece"] },
        "schedule": { "type": "string" },
        "triggerTimezone": { "type": "string" },
        "triggerAuthType": { "enum": ["none", "basic", "header", "hmac"] },
        "triggerPiece": { "type": "string" },
        "triggerName": { "type": "string" },
        "triggerInputs": { "type": "object" },
        "triggerPieceVersion": { "type": "string" },
        "modelTier": { "enum": ["local-standard", "local-advanced", "cloud-reasoning"] },
        "dataClassification": { "enum": ["sovereign", "internal", "permitted"] },
        "systemPrompt": { "type": "string" },
        "userPromptTemplate": { "type": "string" },
        "maxTokens": { "type": "number" },
        "connectorPiece": { "type": "string" },
        "connectorAction": { "type": "string" },
        "connectorInputs": { "type": "object" },
        "approvalMessage": { "type": "string" },
        "approvalReasoning": { "type": "string" },
        "approvalTimeout": { "type": "number" },
        "approvalOnReject": { "enum": ["stop", "continue"] },
        "conditionExpression": { "type": "string" },
        "branches": { "type": "array", "items": { "type": "object", "required": ["id", "label"], "properties": { "id": { "type": "string" }, "label": { "type": "string" }, "nextStepId": { "type": "string" } } } },
        "routingInstructions": { "type": "string" },
        "routerBranches": { "type": "array", "items": { "type": "object", "required": ["id", "label"], "properties": { "id": { "type": "string" }, "label": { "type": "string" }, "nextStepId": { "type": "string" }, "isDefault": { "type": "boolean" } } } },
        "routerInput": { "type": "string" },
        "routerQuestion": { "type": "string" },
        "routerRoutes": { "type": "array", "minItems": 2, "maxItems": 12, "items": { "type": "object", "required": ["id", "label", "description"], "properties": { "id": { "type": "string" }, "label": { "type": "string" }, "description": { "type": "string" } } } },
        "routerMode": { "enum": ["single", "multi"] },
        "routerConfidenceFloor": { "type": "number", "minimum": 0, "maximum": 1 },
        "agentGoal": { "type": "string" },
        "agentToolRefs": { "type": "array", "items": { "type": "string" } },
        "agentMaxSteps": { "type": "integer", "minimum": 1, "maximum": 50 },
        "agentMaxBudgetUsd": { "type": "number" },
        "agentRequireApproval": { "type": "boolean" },
        "loopExpression": { "type": "string" },
        "loopBodyStepId": { "type": "string" },
        "codeLanguage": { "enum": ["javascript", "python"] },
        "codeSource": { "type": "string" },
        "codeInputs": { "type": "object", "additionalProperties": { "type": "string" } },
        "guardTarget": { "type": "string" },
        "guardOnFail": { "enum": ["block", "annotate"] },
        "searchQuery": { "type": "string" },
        "searchMode": { "enum": ["hybrid", "vector"] },
        "searchMaxHits": { "type": "integer", "minimum": 1, "maximum": 20 },
        "publishMode": { "enum": ["append", "upsert", "replace"] },
        "whenFull": { "enum": ["remove_oldest", "fail"] },
        "fieldModes": { "type": "object", "additionalProperties": { "enum": ["manual", "auto", "prompt", "none"] } },
        "continueOnFailure": { "type": "boolean" },
        "retryOnFailure": { "type": "boolean" },
        "retryCount": { "type": "number" },
        "retryDelayMs": { "type": "number" }
      },
      "not": {
        "anyOf": [
          { "required": ["connectorAuth"] }, { "required": ["knowledgeSetId"] }, { "required": ["agentRef"] }, { "required": ["agentId"] },
          { "required": ["personaId"] }, { "required": ["datasetId"] }, { "required": ["approvers"] }, { "required": ["triggerAuthFields"] },
          { "required": ["webhookPath"] }, { "required": ["agentApprovers"] }, { "required": ["capturedOutput"] }
        ]
      }
    }
  }
}
