{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://xerg.ai/schemas/xerg-event-payload-v4.schema.json",
  "title": "Xerg Event Payload v4",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "version",
    "source",
    "events"
  ],
  "properties": {
    "version": {
      "const": 4
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "environment": {
          "type": "string"
        }
      }
    },
    "events": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/event"
      }
    }
  },
  "$defs": {
    "fingerprint": {
      "type": "string",
      "pattern": "^(?:(?:sha1|sha256):)?[a-fA-F0-9]{16,128}$"
    },
    "nonnegativeNumber": {
      "type": "number",
      "minimum": 0
    },
    "nonnegativeInteger": {
      "type": "integer",
      "minimum": 0
    },
    "event": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "type",
        "timestamp",
        "runId"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "type": {
          "enum": [
            "model-call",
            "agent-invocation",
            "tool-execution",
            "delegation"
          ]
        },
        "timestamp": {
          "type": "string",
          "format": "date-time"
        },
        "runId": {
          "type": "string",
          "minLength": 1
        },
        "workflow": {
          "type": "string"
        },
        "agent": {
          "type": "string"
        },
        "parentAgent": {
          "type": "string"
        },
        "tool": {
          "type": "string"
        },
        "modelCallId": {
          "type": "string",
          "minLength": 1
        },
        "toolCallId": {
          "type": "string",
          "minLength": 1
        },
        "requestId": {
          "type": "string",
          "minLength": 1
        },
        "costComponentId": {
          "type": "string",
          "minLength": 1
        },
        "inputFingerprint": {
          "$ref": "#/$defs/fingerprint"
        },
        "resultFingerprint": {
          "$ref": "#/$defs/fingerprint"
        },
        "stateFingerprint": {
          "$ref": "#/$defs/fingerprint"
        },
        "cacheEntryFingerprint": {
          "$ref": "#/$defs/fingerprint"
        },
        "provider": {
          "type": "string"
        },
        "model": {
          "type": "string"
        },
        "inputTokens": {
          "$ref": "#/$defs/nonnegativeInteger"
        },
        "outputTokens": {
          "$ref": "#/$defs/nonnegativeInteger"
        },
        "cacheReadTokens": {
          "$ref": "#/$defs/nonnegativeInteger"
        },
        "cacheWriteTokens": {
          "$ref": "#/$defs/nonnegativeInteger"
        },
        "reasoningTokens": {
          "$ref": "#/$defs/nonnegativeInteger"
        },
        "costUsd": {
          "$ref": "#/$defs/nonnegativeNumber"
        },
        "latencyMs": {
          "$ref": "#/$defs/nonnegativeNumber"
        },
        "retries": {
          "$ref": "#/$defs/nonnegativeInteger"
        },
        "attempt": {
          "$ref": "#/$defs/nonnegativeInteger"
        },
        "iteration": {
          "$ref": "#/$defs/nonnegativeInteger"
        },
        "status": {
          "type": "string"
        },
        "stopReason": {
          "type": "string"
        },
        "taskClass": {
          "type": "string"
        },
        "cacheHit": {
          "type": "boolean"
        },
        "outcome": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "status"
          ],
          "properties": {
            "status": {
              "enum": [
                "success",
                "failure",
                "partial",
                "unknown"
              ]
            }
          }
        },
        "metadata": {
          "type": "object",
          "additionalProperties": {
            "type": [
              "string",
              "number",
              "boolean",
              "null"
            ]
          }
        }
      }
    }
  }
}