{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://mere.run/schemas/workflow-graph-v1.schema.json",
  "title": "mere.run Workflow Graph V1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema_version", "kind", "name", "inputs", "nodes", "outputs"],
  "properties": {
    "schema_version": { "const": 1 },
    "kind": { "const": "mere.run/workflow-graph" },
    "name": { "type": "string", "minLength": 1 },
    "inputs": {
      "type": "object",
      "propertyNames": { "$ref": "#/$defs/id" },
      "additionalProperties": { "$ref": "#/$defs/input" }
    },
    "nodes": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/node" }
    },
    "outputs": {
      "type": "object",
      "propertyNames": { "$ref": "#/$defs/id" },
      "additionalProperties": { "$ref": "#/$defs/reference" }
    },
    "execution": { "$ref": "#/$defs/graph_execution" },
    "metadata": {
      "type": "object",
      "additionalProperties": { "$ref": "#/$defs/value" }
    }
  },
  "$defs": {
    "id": { "type": "string", "pattern": "^[a-z][a-z0-9-]{0,63}$" },
    "reference": {
      "type": "object",
      "additionalProperties": false,
      "required": ["$ref"],
      "properties": {
        "$ref": {
          "type": "string",
          "pattern": "^(inputs\\.[a-z][a-z0-9-]{0,63}|nodes\\.[a-z][a-z0-9-]{0,63}\\.outputs\\.[a-z][a-z0-9_]{0,63})$"
        }
      }
    },
    "secret_reference": {
      "type": "object",
      "additionalProperties": false,
      "required": ["$secret"],
      "properties": { "$secret": { "$ref": "#/$defs/id" } }
    },
    "value": {
      "anyOf": [
        { "type": "null" },
        { "type": "string" },
        { "type": "integer" },
        { "type": "number" },
        { "type": "boolean" },
        { "type": "array", "items": { "$ref": "#/$defs/value" } },
        { "$ref": "#/$defs/reference" },
        { "$ref": "#/$defs/secret_reference" },
        { "type": "object", "additionalProperties": { "$ref": "#/$defs/value" } }
      ]
    },
    "input": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type"],
      "properties": {
        "type": { "$ref": "#/$defs/port_type" },
        "required": { "type": "boolean" },
        "default": { "$ref": "#/$defs/value" },
        "values": { "type": "array", "items": { "type": "string" }, "minItems": 1, "uniqueItems": true },
        "content_types": { "type": "array", "items": { "type": "string", "minLength": 1 }, "minItems": 1, "uniqueItems": true }
      }
    },
    "node": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "arguments"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "kind": { "type": "string", "pattern": "^[a-z][a-z0-9-]{0,63}(\\.[a-z][a-z0-9-]{0,63})+$" },
        "provider": { "type": "string", "pattern": "^(mere\\.run|mere-[a-z0-9-]+)$" },
        "arguments": { "type": "object", "additionalProperties": { "$ref": "#/$defs/value" } },
        "depends_on": { "type": "array", "items": { "$ref": "#/$defs/id" }, "uniqueItems": true },
        "execution": { "$ref": "#/$defs/execution" }
      }
    },
    "execution": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "max_attempts": { "type": "integer", "minimum": 1, "maximum": 10 },
        "timeout_seconds": { "type": "integer", "minimum": 1, "maximum": 604800 },
        "cache": { "enum": ["auto", "never", "refresh"] }
      }
    },
    "graph_execution": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "max_parallel_nodes": { "type": "integer", "minimum": 1, "maximum": 32 }
      }
    },
    "port_type": {
      "enum": ["string", "integer", "number", "boolean", "enum", "json", "asset", "asset_directory", "asset_collection", "asset_array"]
    }
  }
}
