{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://theruliad.ai/.well-known/ruliad-extraction-contract-v1.schema.json",
  "title": "Ruliad agent extraction intake v1",
  "description": "Discovery/intake envelope, not an API request or execution authorization. Field and integrity claims require independently verifiable receipts. No connector availability, consent or production readiness is implied.",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "contractVersion": {
      "const": "ruliad.extraction.v1"
    },
    "inputs": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "brand": {
          "$ref": "#/$defs/sourcedText"
        },
        "product": {
          "$ref": "#/$defs/sourcedText"
        },
        "concept": {
          "$ref": "#/$defs/sourcedText"
        },
        "brandVoice": {
          "$ref": "#/$defs/sourcedText"
        },
        "offers": {
          "type": "array",
          "maxItems": 30,
          "items": {
            "$ref": "#/$defs/sourcedText"
          }
        },
        "scriptInputs": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "durationSeconds": {
              "$ref": "#/$defs/sourcedDuration"
            },
            "aspectRatio": {
              "$ref": "#/$defs/sourcedAspect"
            },
            "language": {
              "$ref": "#/$defs/sourcedText"
            },
            "callToAction": {
              "$ref": "#/$defs/sourcedText"
            },
            "spokenText": {
              "$ref": "#/$defs/sourcedText"
            },
            "creativeDirection": {
              "$ref": "#/$defs/sourcedText"
            }
          },
          "required": [
            "durationSeconds",
            "aspectRatio"
          ]
        }
      },
      "required": [
        "brand",
        "product",
        "concept",
        "scriptInputs"
      ]
    },
    "references": {
      "type": "array",
      "maxItems": 50,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256
          },
          "role": {
            "enum": [
              "talent",
              "product",
              "location",
              "storyboard"
            ]
          },
          "source": {
            "$ref": "#/$defs/source"
          },
          "mimeType": {
            "enum": [
              "image/png",
              "image/jpeg",
              "image/webp"
            ]
          },
          "sourceUrl": {
            "type": "string",
            "format": "uri",
            "pattern": "^https://",
            "maxLength": 4096
          },
          "integrity": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "state": {
                "enum": [
                  "unverified",
                  "stored-body-verified"
                ]
              },
              "sha256": {
                "type": "string",
                "pattern": "^[a-f0-9]{64}$"
              },
              "byteLength": {
                "type": "integer",
                "minimum": 1
              },
              "storageGeneration": {
                "type": "string",
                "pattern": "^[0-9]+$"
              },
              "receiptId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 256
              }
            },
            "required": [
              "state"
            ],
            "allOf": [
              {
                "if": {
                  "properties": {
                    "state": {
                      "const": "stored-body-verified"
                    }
                  },
                  "type": "object"
                },
                "then": {
                  "required": [
                    "sha256",
                    "byteLength",
                    "storageGeneration",
                    "receiptId"
                  ],
                  "type": "object",
                  "properties": {
                    "sha256": true,
                    "byteLength": true,
                    "storageGeneration": true,
                    "receiptId": true
                  }
                },
                "else": {
                  "not": {
                    "anyOf": [
                      {
                        "required": [
                          "sha256"
                        ],
                        "type": "object",
                        "properties": {
                          "sha256": true
                        }
                      },
                      {
                        "required": [
                          "byteLength"
                        ],
                        "type": "object",
                        "properties": {
                          "byteLength": true
                        }
                      },
                      {
                        "required": [
                          "storageGeneration"
                        ],
                        "type": "object",
                        "properties": {
                          "storageGeneration": true
                        }
                      },
                      {
                        "required": [
                          "receiptId"
                        ],
                        "type": "object",
                        "properties": {
                          "receiptId": true
                        }
                      }
                    ]
                  }
                }
              }
            ]
          }
        },
        "required": [
          "id",
          "role",
          "source",
          "mimeType",
          "sourceUrl",
          "integrity"
        ]
      }
    },
    "completeness": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "state": {
          "enum": [
            "partial",
            "ready-for-script"
          ]
        },
        "missingFields": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256
          }
        },
        "notes": {
          "type": "array",
          "maxItems": 50,
          "items": {
            "type": "string",
            "maxLength": 2000
          }
        }
      },
      "required": [
        "state",
        "missingFields"
      ]
    }
  },
  "required": [
    "contractVersion",
    "inputs",
    "references",
    "completeness"
  ],
  "$defs": {
    "source": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "enum": [
            "user",
            "web-page",
            "connected-platform",
            "derived"
          ]
        },
        "sourceRef": {
          "type": "string",
          "format": "uri",
          "maxLength": 4096
        },
        "extractedAt": {
          "type": "string",
          "format": "date-time"
        },
        "locator": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2000
        },
        "receiptId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        }
      },
      "required": [
        "kind",
        "sourceRef",
        "extractedAt",
        "locator"
      ]
    },
    "sourcedText": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "status": {
          "enum": [
            "provided",
            "verified",
            "inferred",
            "missing",
            "ambiguous"
          ]
        },
        "value": {
          "type": [
            "string",
            "null"
          ],
          "minLength": 1,
          "maxLength": 16000
        },
        "source": {
          "anyOf": [
            {
              "$ref": "#/$defs/source"
            },
            {
              "type": "null"
            }
          ]
        },
        "note": {
          "type": "string",
          "maxLength": 2000
        }
      },
      "required": [
        "status",
        "value",
        "source"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "missing"
              }
            },
            "type": "object"
          },
          "then": {
            "properties": {
              "value": {
                "type": "null"
              },
              "source": {
                "type": "null"
              }
            },
            "type": "object"
          },
          "else": {
            "properties": {
              "source": {
                "$ref": "#/$defs/source"
              },
              "value": {
                "not": {
                  "type": "null"
                }
              }
            },
            "type": "object"
          }
        }
      ]
    },
    "sourcedDuration": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "status": {
          "enum": [
            "provided",
            "verified",
            "inferred",
            "missing",
            "ambiguous"
          ]
        },
        "value": {
          "type": [
            "number",
            "null"
          ],
          "exclusiveMinimum": 0,
          "maximum": 600
        },
        "source": {
          "anyOf": [
            {
              "$ref": "#/$defs/source"
            },
            {
              "type": "null"
            }
          ]
        },
        "note": {
          "type": "string",
          "maxLength": 2000
        }
      },
      "required": [
        "status",
        "value",
        "source"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "missing"
              }
            },
            "type": "object"
          },
          "then": {
            "properties": {
              "value": {
                "type": "null"
              },
              "source": {
                "type": "null"
              }
            },
            "type": "object"
          },
          "else": {
            "properties": {
              "source": {
                "$ref": "#/$defs/source"
              },
              "value": {
                "not": {
                  "type": "null"
                }
              }
            },
            "type": "object"
          }
        }
      ]
    },
    "sourcedAspect": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "status": {
          "enum": [
            "provided",
            "verified",
            "inferred",
            "missing",
            "ambiguous"
          ]
        },
        "value": {
          "type": [
            "string",
            "null"
          ],
          "enum": [
            "16:9",
            "9:16",
            "1:1",
            "4:5",
            null
          ]
        },
        "source": {
          "anyOf": [
            {
              "$ref": "#/$defs/source"
            },
            {
              "type": "null"
            }
          ]
        },
        "note": {
          "type": "string",
          "maxLength": 2000
        }
      },
      "required": [
        "status",
        "value",
        "source"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "missing"
              }
            },
            "type": "object"
          },
          "then": {
            "properties": {
              "value": {
                "type": "null"
              },
              "source": {
                "type": "null"
              }
            },
            "type": "object"
          },
          "else": {
            "properties": {
              "source": {
                "$ref": "#/$defs/source"
              },
              "value": {
                "not": {
                  "type": "null"
                }
              }
            },
            "type": "object"
          }
        }
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "completeness": {
            "properties": {
              "state": {
                "const": "ready-for-script"
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "inputs": {
            "type": "object",
            "properties": {
              "brand": {
                "type": "object",
                "properties": {
                  "status": {
                    "enum": [
                      "provided",
                      "verified"
                    ]
                  }
                }
              },
              "product": {
                "type": "object",
                "properties": {
                  "status": {
                    "enum": [
                      "provided",
                      "verified"
                    ]
                  }
                }
              },
              "concept": {
                "type": "object",
                "properties": {
                  "status": {
                    "enum": [
                      "provided",
                      "verified"
                    ]
                  }
                }
              },
              "scriptInputs": {
                "type": "object",
                "properties": {
                  "durationSeconds": {
                    "type": "object",
                    "properties": {
                      "status": {
                        "enum": [
                          "provided",
                          "verified"
                        ]
                      }
                    }
                  },
                  "aspectRatio": {
                    "type": "object",
                    "properties": {
                      "status": {
                        "enum": [
                          "provided",
                          "verified"
                        ]
                      }
                    }
                  }
                }
              }
            }
          },
          "completeness": {
            "type": "object",
            "properties": {
              "missingFields": {
                "maxItems": 0,
                "type": "array"
              }
            }
          }
        },
        "type": "object"
      }
    }
  ]
}
