{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://cms.kuaiz.net/contracts/customer-service-connector-v1.json",
  "title": "Kuaiz Customer Service Connector v1",
  "oneOf": [
    {"$ref": "#/$defs/testRequest"},
    {"$ref": "#/$defs/testResponse"},
    {"$ref": "#/$defs/connectRequest"},
    {"$ref": "#/$defs/connectResponse"},
    {"$ref": "#/$defs/messageRequest"},
    {"$ref": "#/$defs/messageResponse"},
    {"$ref": "#/$defs/disconnectRequest"},
    {"$ref": "#/$defs/disconnectResponse"}
  ],
  "$defs": {
    "base": {
      "type": "object",
      "properties": {
        "schema": {"const": "kuaiz-customer-service-connector/v1"}
      },
      "required": ["schema"]
    },
    "site": {
      "type": "object",
      "additionalProperties": false,
      "required": ["url", "name", "language"],
      "properties": {
        "url": {"type": "string", "format": "uri", "maxLength": 500},
        "name": {"type": "string", "minLength": 1, "maxLength": 120},
        "language": {"type": "string", "minLength": 2, "maxLength": 35}
      }
    },
    "testRequest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "action", "site"],
      "properties": {
        "schema": {"const": "kuaiz-customer-service-connector/v1"},
        "action": {"const": "test"},
        "site": {"$ref": "#/$defs/site"}
      }
    },
    "testResponse": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "action", "ok", "engine_version"],
      "properties": {
        "schema": {"const": "kuaiz-customer-service-connector/v1"},
        "action": {"const": "test"},
        "ok": {"const": true},
        "engine_version": {"type": "string", "minLength": 1, "maxLength": 80}
      }
    },
    "connectRequest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "action", "site", "knowledge", "widget"],
      "properties": {
        "schema": {"const": "kuaiz-customer-service-connector/v1"},
        "action": {"const": "connect"},
        "site": {"$ref": "#/$defs/site"},
        "knowledge": {
          "type": "object",
          "additionalProperties": false,
          "required": ["endpoint", "token", "audience", "drafts"],
          "properties": {
            "endpoint": {"type": "string", "format": "uri", "maxLength": 800},
            "token": {"type": "string", "pattern": "^kzai_[A-Za-z0-9_-]{43}$"},
            "audience": {"const": "service"},
            "drafts": {"const": false}
          }
        },
        "widget": {"$ref": "#/$defs/widget"}
      }
    },
    "connectResponse": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "action", "ok", "site_id"],
      "properties": {
        "schema": {"const": "kuaiz-customer-service-connector/v1"},
        "action": {"const": "connect"},
        "ok": {"const": true},
        "site_id": {"type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,159}$"}
      }
    },
    "widget": {
      "type": "object",
      "additionalProperties": false,
      "required": ["assistant_name", "welcome_message", "fallback_message"],
      "properties": {
        "assistant_name": {"type": "string", "minLength": 1, "maxLength": 40},
        "welcome_message": {"type": "string", "minLength": 1, "maxLength": 300},
        "fallback_message": {"type": "string", "minLength": 1, "maxLength": 300}
      }
    },
    "messageRequest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "action", "site_id", "session_id", "message", "page_path"],
      "properties": {
        "schema": {"const": "kuaiz-customer-service-connector/v1"},
        "action": {"const": "message"},
        "site_id": {"type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,159}$"},
        "session_id": {"type": "string", "minLength": 20, "maxLength": 100},
        "message": {"type": "string", "minLength": 1, "maxLength": 2000},
        "page_path": {"type": "string", "pattern": "^/", "maxLength": 500}
      }
    },
    "messageResponse": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "action", "ok", "reply", "conversation_id", "handoff"],
      "properties": {
        "schema": {"const": "kuaiz-customer-service-connector/v1"},
        "action": {"const": "message"},
        "ok": {"const": true},
        "reply": {"type": "string", "minLength": 1, "maxLength": 4000},
        "conversation_id": {"type": ["string", "null"], "maxLength": 160},
        "handoff": {
          "oneOf": [
            {"type": "null"},
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["label", "url"],
              "properties": {
                "label": {"type": "string", "minLength": 1, "maxLength": 80},
                "url": {"type": "string", "minLength": 1, "maxLength": 500}
              }
            }
          ]
        }
      }
    },
    "disconnectRequest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "action", "site_id"],
      "properties": {
        "schema": {"const": "kuaiz-customer-service-connector/v1"},
        "action": {"const": "disconnect"},
        "site_id": {"type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,159}$"}
      }
    },
    "disconnectResponse": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "action", "ok"],
      "properties": {
        "schema": {"const": "kuaiz-customer-service-connector/v1"},
        "action": {"const": "disconnect"},
        "ok": {"const": true}
      }
    }
  }
}
