{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.baijimu.com/contracts/workflow-definition/5.0.0/schema.json",
  "title": "Baijimu Workflow Definition Authoring Contract",
  "description": "baijimu bundle workflow create/update 的 5.0.0 强类型 definition 合同。Agent 节点使用 message、result 和 executionPolicy，并拒绝 4.x 的 promptTemplate、outputFormat 及作者提供的身份/会话字段。",
  "type": "object",
  "additionalProperties": false,
  "required": ["code", "name", "startNodeId", "nodes", "edges"],
  "properties": {
    "workspaceId": { "type": "string" },
    "code": { "$ref": "../4.1.0/schema.json#/$defs/nonEmptyString" },
    "name": { "$ref": "../4.1.0/schema.json#/$defs/nonEmptyString" },
    "description": { "type": ["string", "null"] },
    "status": { "enum": ["draft", "active", "disabled"] },
    "startNodeId": { "$ref": "../4.1.0/schema.json#/$defs/nonEmptyString" },
    "inputSchema": {
      "type": "array",
      "items": { "$ref": "../4.1.0/schema.json#/$defs/paramDefinition" },
      "default": []
    },
    "outputSchema": { "$ref": "../4.1.0/schema.json#/$defs/lowcodeType" },
    "nodes": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/workflowNode" }
    },
    "edges": {
      "type": "array",
      "items": { "$ref": "../4.1.0/schema.json#/$defs/workflowEdge" }
    },
    "graphLayout": true,
    "tags": {
      "type": "array",
      "items": { "type": "string" },
      "default": []
    }
  },
  "$defs": {
    "agentResultType": {
      "oneOf": [
        { "$ref": "#/$defs/agentDataType" },
        { "$ref": "#/$defs/agentRefType" }
      ]
    },
    "agentDataType": {
      "type": "object",
      "additionalProperties": false,
      "required": ["@type"],
      "properties": {
        "@type": { "const": "DataType" },
        "type": { "$ref": "../4.1.0/schema.json#/$defs/nonEmptyString" },
        "ref": { "$ref": "../4.1.0/schema.json#/$defs/nonEmptyString" },
        "oneOf": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/agentResultType" }
        },
        "anyOf": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/agentResultType" }
        },
        "allOf": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/agentResultType" }
        },
        "properties": {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/agentResultType" }
        },
        "items": { "$ref": "#/$defs/agentResultType" },
        "additionalProperties": true,
        "required": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string" }
        },
        "nullable": { "type": "boolean" },
        "enum": { "type": "array" }
      },
      "anyOf": [
        { "required": ["type"], "properties": { "type": true } },
        { "required": ["ref"], "properties": { "ref": true } },
        { "required": ["oneOf"], "properties": { "oneOf": true } },
        { "required": ["anyOf"], "properties": { "anyOf": true } },
        { "required": ["allOf"], "properties": { "allOf": true } }
      ]
    },
    "agentRefType": {
      "type": "object",
      "additionalProperties": false,
      "required": ["@type", "ref"],
      "properties": {
        "@type": { "const": "RefType" },
        "ref": { "$ref": "../4.1.0/schema.json#/$defs/nonEmptyString" },
        "referenceUri": { "type": "string" }
      }
    },
    "agentResult": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type"],
      "properties": {
        "path": { "$ref": "../4.1.0/schema.json#/$defs/nonEmptyString" },
        "type": { "$ref": "#/$defs/agentResultType" }
      }
    },
    "agentExecutionPolicy": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "completionTimeoutSecs": {
          "type": "integer",
          "minimum": 1,
          "maximum": 86400,
          "default": 300
        }
      }
    },
    "agentNode": {
      "type": "object",
      "allOf": [
        { "$ref": "../4.1.0/schema.json#/$defs/nodeBase" },
        {
          "type": "object",
          "required": ["agentResourceLocator", "message", "result"],
          "properties": {
            "type": { "const": "agent" },
            "agentResourceLocator": {
              "type": "string",
              "pattern": "^[^/\\s]+/[^/\\s]+/AGENT/[^/\\s]+$"
            },
            "message": { "$ref": "../4.1.0/schema.json#/$defs/nonEmptyString" },
            "result": { "$ref": "#/$defs/agentResult" },
            "executionPolicy": { "$ref": "#/$defs/agentExecutionPolicy" },
            "outputPath": false
          }
        }
      ],
      "unevaluatedProperties": false
    },
    "forEachNode": {
      "type": "object",
      "allOf": [
        { "$ref": "../4.1.0/schema.json#/$defs/nodeBase" },
        {
          "type": "object",
          "required": ["itemsPath"],
          "properties": {
            "type": { "const": "for_each" },
            "itemsPath": { "$ref": "../4.1.0/schema.json#/$defs/nonEmptyString" },
            "itemName": { "type": ["string", "null"] },
            "indexName": { "type": ["string", "null"] },
            "mode": { "const": "sequential", "default": "sequential" },
            "maxIterations": { "type": ["integer", "null"], "minimum": 1, "maximum": 10000 },
            "onItemError": { "enum": ["fail", "continue"], "default": "fail" },
            "body": {
              "type": "array",
              "items": { "$ref": "#/$defs/workflowNode" },
              "default": []
            },
            "itemOutput": true
          }
        }
      ],
      "unevaluatedProperties": false
    },
    "workflowNode": {
      "oneOf": [
        { "$ref": "../4.1.0/schema.json#/$defs/startNode" },
        { "$ref": "../4.1.0/schema.json#/$defs/endNode" },
        { "$ref": "../4.1.0/schema.json#/$defs/httpNode" },
        { "$ref": "../4.1.0/schema.json#/$defs/waitNode" },
        { "$ref": "../4.1.0/schema.json#/$defs/humanNode" },
        { "$ref": "../4.1.0/schema.json#/$defs/joinNode" },
        { "$ref": "#/$defs/forEachNode" },
        { "$ref": "../4.1.0/schema.json#/$defs/moduleMethodNode" },
        { "$ref": "../4.1.0/schema.json#/$defs/emitEventNode" },
        { "$ref": "#/$defs/agentNode" },
        { "$ref": "../4.1.0/schema.json#/$defs/customNode" }
      ]
    }
  }
}
