{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://github.com/andrewhuot/headless-chat-sdk/schemas/a2ui.schema.json",
  "title": "A2UI v0.9 SDK envelopes",
  "description": "SDK-owned A2UI envelope schemas. Component-level catalog validation belongs to A2UI catalogs/renderers.",
  "definitions": {
    "A2UIComponentFrame": {
      "type": "object",
      "required": ["id", "component"],
      "properties": {
        "id": { "type": "string" },
        "component": { "type": "string" }
      },
      "additionalProperties": true
    },
    "A2UICreateSurfaceFrame": {
      "type": "object",
      "required": ["version", "createSurface"],
      "properties": {
        "version": { "const": "v0.9" },
        "createSurface": {
          "type": "object",
          "required": ["surfaceId", "catalogId"],
          "properties": {
            "surfaceId": { "type": "string" },
            "catalogId": { "type": "string" },
            "theme": { "type": "object" },
            "sendDataModel": { "type": "boolean" }
          }
        }
      }
    },
    "A2UIUpdateComponentsFrame": {
      "type": "object",
      "required": ["version", "updateComponents"],
      "properties": {
        "version": { "const": "v0.9" },
        "updateComponents": {
          "type": "object",
          "required": ["surfaceId", "components"],
          "properties": {
            "surfaceId": { "type": "string" },
            "components": {
              "type": "array",
              "items": { "$ref": "#/definitions/A2UIComponentFrame" }
            }
          }
        }
      }
    },
    "A2UIUpdateDataModelFrame": {
      "type": "object",
      "required": ["version", "updateDataModel"],
      "properties": {
        "version": { "const": "v0.9" },
        "updateDataModel": {
          "type": "object",
          "required": ["surfaceId"],
          "properties": {
            "surfaceId": { "type": "string" },
            "path": { "type": "string" },
            "value": {}
          }
        }
      }
    },
    "A2UIDeleteSurfaceFrame": {
      "type": "object",
      "required": ["version", "deleteSurface"],
      "properties": {
        "version": { "const": "v0.9" },
        "deleteSurface": {
          "type": "object",
          "required": ["surfaceId"],
          "properties": {
            "surfaceId": { "type": "string" }
          }
        }
      }
    },
    "A2UIv09Frame": {
      "oneOf": [
        { "$ref": "#/definitions/A2UICreateSurfaceFrame" },
        { "$ref": "#/definitions/A2UIUpdateComponentsFrame" },
        { "$ref": "#/definitions/A2UIUpdateDataModelFrame" },
        { "$ref": "#/definitions/A2UIDeleteSurfaceFrame" }
      ]
    },
    "A2UISurfacePart": {
      "type": "object",
      "required": ["id", "type", "surfaceId", "protocolVersion", "catalogId", "components", "dataModel", "status"],
      "properties": {
        "id": { "type": "string" },
        "type": { "const": "a2ui-surface" },
        "surfaceId": { "type": "string" },
        "protocolVersion": { "const": "v0.9" },
        "catalogId": { "type": "string" },
        "theme": { "type": "object" },
        "components": {
          "type": "array",
          "items": { "$ref": "#/definitions/A2UIComponentFrame" }
        },
        "dataModel": { "type": "object" },
        "status": { "type": "string", "enum": ["streaming", "ready", "deleted", "error"] },
        "error": {
          "type": "object",
          "required": ["code", "message"],
          "properties": {
            "code": { "type": "string" },
            "message": { "type": "string" }
          }
        },
        "metadata": {
          "type": "object",
          "properties": {
            "frameCount": { "type": "integer", "minimum": 0 },
            "lastFrameAt": { "type": "string", "format": "date-time" },
            "source": { "const": "rich-payload" }
          }
        }
      }
    },
    "A2UIActionEnvelope": {
      "type": "object",
      "required": ["name", "surfaceId", "timestamp"],
      "properties": {
        "name": { "type": "string" },
        "surfaceId": { "type": "string" },
        "sourceComponentId": { "type": "string" },
        "timestamp": { "type": "string", "format": "date-time" },
        "context": { "type": "object" },
        "dataModel": { "type": "object" }
      }
    }
  },
  "$ref": "#/definitions/A2UIv09Frame"
}
