{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "/schema/glam_scribe.page.v2.json",
  "title": "glam_scribe.page/2",
  "description": "One GlamScribe record per finished page of a IIIF item. Published as JSON Lines (one record per line). Coordinates are [x, y, w, h] integers in full-resolution pixels of image_service (equal to the canvas's pixel space), origin top-left.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema", "record_id", "item_id", "item_label", "manifest_url",
    "canvas_id", "canvas_index", "canvas_label", "image_service", "width", "height",
    "text", "text_sha256", "languages", "language_name", "scripts",
    "regions", "provenance", "created_at", "updated_at"
  ],
  "properties": {
    "schema": {"const": "glam_scribe.page/2", "description": "Format name and version."},
    "record_id": {"type": "string", "pattern": "^.+/[0-9]+$", "description": "\"<item_id>/<canvas_index>\"; stable for a page."},
    "item_id": {"type": "string", "minLength": 1, "description": "GlamScribe item id (the Figgy resource UUID for Figgy manifests)."},
    "item_label": {"type": ["string", "null"], "description": "The item's title from its manifest."},
    "manifest_url": {"type": "string", "minLength": 1, "description": "The source IIIF manifest."},
    "canvas_id": {"type": ["string", "null"], "description": "The canvas id in the source manifest."},
    "canvas_index": {"type": "integer", "minimum": 0, "description": "0-based position of the canvas in the source manifest."},
    "canvas_label": {"type": ["string", "null"], "description": "The canvas label (e.g. a folio or page number)."},
    "image_service": {"type": ["string", "null"], "description": "IIIF Image API service base URL for the page image."},
    "width": {"type": ["integer", "null"], "minimum": 0, "description": "Full-resolution width in pixels."},
    "height": {"type": ["integer", "null"], "minimum": 0, "description": "Full-resolution height in pixels."},
    "text": {"type": "string", "description": "Reading text: region texts in reading order joined by blank lines, page numbers removed. Empty for pages with no text."},
    "text_sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$", "description": "Lowercase hex SHA-256 of text (UTF-8)."},
    "languages": {"type": "array", "items": {"type": "string", "pattern": "^[a-z]{2,3}(-[A-Za-z0-9]+)*$"}, "description": "BCP 47 tags derived from the catalog language, in catalog order; empty when unknown."},
    "language_name": {"type": ["string", "null"], "description": "The catalog's language string as written (e.g. \"Greek, Modern (1453-)\")."},
    "scripts": {"type": "array", "items": {"type": "string", "pattern": "^[A-Z][a-z]{3}$"}, "description": "ISO 15924 script codes expected or detected on the page."},
    "regions": {"type": "array", "items": {"$ref": "#/$defs/region"}, "description": "Layout regions in reading order."},
    "provenance": {"$ref": "#/$defs/provenance"},
    "created_at": {"type": ["string", "null"], "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$", "description": "When the page entered GlamScribe (UTC)."},
    "updated_at": {"type": ["string", "null"], "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$", "description": "When the page's result last changed (UTC)."}
  },
  "$defs": {
    "region": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "bbox", "label", "order", "text", "confidence", "layout_score", "model"],
      "properties": {
        "id": {"type": "string", "pattern": "^r[0-9]+$", "description": "\"r<order>\"; unique within the record."},
        "bbox": {
          "type": "array",
          "items": {"type": "integer", "minimum": 0},
          "minItems": 4,
          "maxItems": 4,
          "description": "[x, y, w, h]: top-left corner, width and height in full-resolution pixels. Fetch with <image_service>/x,y,w,h/full/0/default.jpg; target with <canvas>#xywh=x,y,w,h."
        },
        "label": {"type": ["string", "null"], "description": "Layout class, e.g. text, title, header, footer, page_number, table, figure, caption, page."},
        "order": {"type": "integer", "minimum": 0, "description": "0-based reading order."},
        "text": {"type": ["string", "null"], "description": "The region's text, or null when the page was read whole (see the record's text) or the region has no text (figures)."},
        "confidence": {"type": ["number", "null"], "minimum": 0, "maximum": 1, "description": "Recognition confidence: exp(mean token log-probability) of the kept reading, when the model reports it."},
        "layout_score": {"type": ["number", "null"], "description": "The layout detector's score for this region."},
        "model": {"type": ["string", "null"], "description": "The model that read this region."}
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["pipeline", "worker_type", "models", "layout_model", "runtime", "client", "route", "contributions", "elapsed_ms"],
      "description": "What produced the result. Never contains worker names, tokens or other personal data.",
      "properties": {
        "pipeline": {"type": ["string", "null"], "description": "Pipeline that produced the page; \"volunteer:<pipeline>\" when read by a volunteer."},
        "worker_type": {"enum": ["browser", "cli", "coordinator", null], "description": "browser: the volunteer web page; cli: the command-line worker; coordinator: the server; null: no reading needed (no text)."},
        "models": {"type": "array", "items": {"type": "string"}, "description": "Recognition models used, sorted."},
        "layout_model": {"type": ["string", "null"]},
        "runtime": {"type": ["string", "null"], "description": "Browser inference runtime, when reported."},
        "client": {"type": ["string", "null"], "description": "Command-line worker version, e.g. glam_scribe/0.1.0."},
        "route": {
          "type": ["object", "null"],
          "description": "The page router's decision (reason and the evidence it used).",
          "additionalProperties": false,
          "properties": {
            "pipeline": {"type": ["string", "null"]},
            "reason": {"type": ["string", "null"]},
            "scripts": {"type": ["array", "null"], "items": {"type": "string"}},
            "language": {"type": ["string", "null"]},
            "rotate": {"type": ["integer", "null"]},
            "layout": {"type": ["string", "null"]},
            "table_kind": {"type": ["string", "null"]},
            "material": {"type": ["string", "null"]},
            "has_text": {"type": ["boolean", "number", "null"]},
            "orthography": {"type": ["string", "null"]}
          }
        },
        "contributions": {"type": ["integer", "null"], "minimum": 1, "description": "Agreeing volunteer readings behind the result."},
        "elapsed_ms": {"type": ["integer", "null"], "minimum": 0}
      }
    }
  }
}
