{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://gemtavern.app/schemas/game-link-protocol-v1.schema.json",
  "title": "GemTavern Game Link Protocol v1",
  "type": "object",
  "additionalProperties": true,
  "required": ["protocol", "protocolVersion", "type", "integration", "character", "promptDirective"],
  "properties": {
    "protocol": { "const": "gemtavern.game_link" },
    "protocolVersion": { "const": 1 },
    "type": {
      "type": "string",
      "enum": ["character_card_upsert", "live_event"]
    },
    "integration": {
      "type": "object",
      "additionalProperties": true,
      "required": ["id", "gameId", "gameName"],
      "properties": {
        "id": { "type": "string", "minLength": 1, "maxLength": 80 },
        "gameId": { "type": "string", "minLength": 1, "maxLength": 80 },
        "gameName": { "type": "string", "minLength": 1, "maxLength": 120 },
        "modName": { "type": "string", "maxLength": 120 },
        "modVersion": { "type": "string", "maxLength": 80 }
      }
    },
    "character": {
      "type": "object",
      "additionalProperties": true,
      "required": ["id", "name"],
      "properties": {
        "id": { "type": "string", "minLength": 1, "maxLength": 160 },
        "name": { "type": "string", "minLength": 1, "maxLength": 120 },
        "cardJSON": { "type": "string", "maxLength": 500000 },
        "avatarPNGBase64": { "type": "string", "maxLength": 3000000 }
      }
    },
    "scene": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "kind": { "type": "string", "maxLength": 80 },
        "family": { "type": "string", "maxLength": 80 },
        "priority": { "type": "number" },
        "line": { "type": "string", "maxLength": 1000 }
      }
    },
    "promptDirective": {
      "type": "object",
      "additionalProperties": true,
      "required": ["protocolVersion", "sceneContext", "autoEventGuide", "directUserGuide"],
      "properties": {
        "protocolVersion": { "type": "integer", "minimum": 1 },
        "sceneContext": { "type": "string", "minLength": 1, "maxLength": 12000 },
        "autoEventGuide": { "type": "string", "minLength": 1, "maxLength": 16000 },
        "directUserGuide": { "type": "string", "minLength": 1, "maxLength": 16000 },
        "promptVersion": { "type": "string", "maxLength": 120 }
      }
    },
    "event": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "id": { "type": "string", "maxLength": 160 },
        "visibleText": { "type": "string", "maxLength": 2000 },
        "replyPolicy": { "type": "string", "enum": ["auto", "silent"] },
        "createdAtUnix": { "type": "number" }
      }
    },
    "state": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": { "properties": { "type": { "const": "character_card_upsert" } } },
      "then": { "properties": { "character": { "required": ["id", "name", "cardJSON"] } } }
    },
    {
      "if": { "properties": { "type": { "const": "live_event" } } },
      "then": { "required": ["event"], "properties": { "event": { "required": ["visibleText", "replyPolicy"] } } }
    }
  ]
}
