{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://yarramate.org/schema/visual-response/v1",
  "title": "YarraMate visual session response",
  "description": "One agent response correlated to the browser event that triggered it.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "format",
    "sessionId",
    "responseId",
    "eventId",
    "type",
    "timestamp",
    "payload"
  ],
  "properties": {
    "format": {
      "const": "yarramate/visual-response/v1"
    },
    "sessionId": {
      "$ref": "https://yarramate.org/schema/visual-event/v1#/$defs/identifier"
    },
    "responseId": {
      "$ref": "https://yarramate.org/schema/visual-event/v1#/$defs/identifier"
    },
    "eventId": {
      "$ref": "https://yarramate.org/schema/visual-event/v1#/$defs/identifier"
    },
    "type": {
      "enum": [
        "chat.response",
        "agent.status",
        "choice.present",
        "operations.propose",
        "handoff.complete",
        "diagnostic",
        "filter.result",
        "view.save.result",
        "apply.result",
        "layout.save.result"
      ]
    },
    "timestamp": {
      "$ref": "https://yarramate.org/schema/visual-event/v1#/$defs/timestamp"
    },
    "payload": {
      "type": "object"
    }
  },
  "oneOf": [
    {
      "type": "object",
      "required": [
        "type",
        "payload"
      ],
      "properties": {
        "type": {
          "const": "chat.response"
        },
        "payload": {
          "$ref": "#/$defs/chatResponsePayload"
        }
      }
    },
    {
      "type": "object",
      "required": [
        "type",
        "payload"
      ],
      "properties": {
        "type": {
          "const": "agent.status"
        },
        "payload": {
          "$ref": "#/$defs/agentStatusPayload"
        }
      }
    },
    {
      "type": "object",
      "required": [
        "type",
        "payload"
      ],
      "properties": {
        "type": {
          "const": "choice.present"
        },
        "payload": {
          "$ref": "#/$defs/choicePresentPayload"
        }
      }
    },
    {
      "type": "object",
      "required": [
        "type",
        "payload"
      ],
      "properties": {
        "type": {
          "const": "operations.propose"
        },
        "payload": {
          "$ref": "#/$defs/operationsProposePayload"
        }
      }
    },
    {
      "type": "object",
      "required": [
        "type",
        "payload"
      ],
      "properties": {
        "type": {
          "const": "handoff.complete"
        },
        "payload": {
          "$ref": "#/$defs/handoffSummary"
        }
      }
    },
    {
      "type": "object",
      "required": [
        "type",
        "payload"
      ],
      "properties": {
        "type": {
          "const": "diagnostic"
        },
        "payload": {
          "$ref": "#/$defs/diagnosticPayload"
        }
      }
    },
    {
      "type": "object",
      "required": [
        "type",
        "payload"
      ],
      "properties": {
        "type": {
          "const": "filter.result"
        },
        "payload": {
          "$ref": "#/$defs/filterResultPayload"
        }
      }
    },
    {
      "type": "object",
      "required": [
        "type",
        "payload"
      ],
      "properties": {
        "type": {
          "const": "view.save.result"
        },
        "payload": {
          "$ref": "#/$defs/viewSaveResultPayload"
        }
      }
    },
    {
      "type": "object",
      "required": [
        "type",
        "payload"
      ],
      "properties": {
        "type": {
          "const": "apply.result"
        },
        "payload": {
          "$ref": "#/$defs/applyResultPayload"
        }
      }
    },
    {
      "type": "object",
      "required": [
        "type",
        "payload"
      ],
      "properties": {
        "type": {
          "const": "layout.save.result"
        },
        "payload": {
          "$ref": "#/$defs/layoutSaveResultPayload"
        }
      }
    }
  ],
  "$defs": {
    "shortId": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$"
    },
    "summaryText": {
      "type": "string",
      "minLength": 1,
      "maxLength": 20000
    },
    "statementList": {
      "type": "array",
      "maxItems": 256,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 4000
      }
    },
    "viewList": {
      "type": "array",
      "maxItems": 256,
      "items": {
        "$ref": "https://yarramate.org/schema/visual-model/v1#/$defs/viewId"
      }
    },
    "filterResultPayload": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "query",
        "matchedIds"
      ],
      "properties": {
        "query": {
          "$ref": "https://yarramate.org/schema/projection/v1#/$defs/query"
        },
        "matchedIds": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string"
          }
        }
      }
    },
    "chatAppliedQuery": {
      "description": "The filter a chat turn resolved to. The agent states the query; the runtime evaluates it against the same graph a filter.query event is evaluated against, and fills matchedIds in before the response is journaled or streamed. matchedIds is optional here so a resolved response round-trips through the transcript, not so an agent may assert one.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "query"
      ],
      "properties": {
        "query": {
          "$ref": "https://yarramate.org/schema/projection/v1#/$defs/query"
        },
        "matchedIds": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string"
          }
        }
      }
    },
    "chatResponsePayload": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "text"
      ],
      "properties": {
        "text": {
          "type": "string",
          "minLength": 1,
          "maxLength": 65536
        },
        "appliedQuery": {
          "$ref": "#/$defs/chatAppliedQuery"
        }
      }
    },
    "agentStatusPayload": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "thinking",
            "compiling",
            "waiting",
            "idle"
          ]
        },
        "detail": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4000
        }
      }
    },
    "choicePresentPayload": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "choiceId",
        "question",
        "options"
      ],
      "properties": {
        "choiceId": {
          "$ref": "#/$defs/shortId"
        },
        "question": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4000
        },
        "options": {
          "type": "array",
          "minItems": 2,
          "maxItems": 32,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "id",
              "label"
            ],
            "properties": {
              "id": {
                "$ref": "#/$defs/shortId"
              },
              "label": {
                "type": "string",
                "minLength": 1,
                "maxLength": 400
              },
              "description": {
                "type": "string",
                "minLength": 1,
                "maxLength": 4000
              }
            }
          }
        }
      }
    },
    "handoffSummary": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "summary",
        "confirmedDecisions",
        "requestedChanges",
        "unresolvedQuestions",
        "finalViews"
      ],
      "properties": {
        "summary": {
          "$ref": "#/$defs/summaryText"
        },
        "confirmedDecisions": {
          "$ref": "#/$defs/statementList"
        },
        "requestedChanges": {
          "$ref": "#/$defs/statementList"
        },
        "unresolvedQuestions": {
          "$ref": "#/$defs/statementList"
        },
        "finalViews": {
          "$ref": "#/$defs/viewList"
        }
      }
    },
    "diagnosticPayload": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "diagnostics"
      ],
      "properties": {
        "diagnostics": {
          "type": "array",
          "minItems": 1,
          "maxItems": 1000,
          "items": {
            "$ref": "https://yarramate.org/schema/visual-diagnostic-result/v1#/$defs/diagnostic"
          }
        }
      }
    },
    "viewSaveResultPayload": {
      "type": "object",
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "ok",
            "id",
            "path"
          ],
          "properties": {
            "ok": {
              "const": true
            },
            "id": {
              "$ref": "https://yarramate.org/schema/projection/v1#/$defs/id"
            },
            "path": {
              "type": "string",
              "minLength": 1,
              "maxLength": 4096
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "ok",
            "diagnostics"
          ],
          "properties": {
            "ok": {
              "const": false
            },
            "diagnostics": {
              "type": "array",
              "minItems": 1,
              "maxItems": 1000,
              "items": {
                "$ref": "https://yarramate.org/schema/visual-diagnostic-result/v1#/$defs/diagnostic"
              }
            }
          }
        }
      ]
    },
    "applyResultPayload": {
      "type": "object",
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "ok",
            "result"
          ],
          "properties": {
            "ok": {
              "const": true
            },
            "result": {
              "$ref": "https://yarrasys.dev/schema/yarramate-apply-result.schema.json"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "ok",
            "diagnostics"
          ],
          "properties": {
            "ok": {
              "const": false
            },
            "diagnostics": {
              "type": "array",
              "minItems": 1,
              "maxItems": 1000,
              "items": {
                "$ref": "https://yarramate.org/schema/visual-diagnostic-result/v1#/$defs/diagnostic"
              }
            }
          }
        }
      ]
    },
    "layoutSaveResultPayload": {
      "type": "object",
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "ok",
            "path"
          ],
          "properties": {
            "ok": {
              "const": true
            },
            "path": {
              "type": "string",
              "minLength": 1,
              "maxLength": 4096
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "ok",
            "message"
          ],
          "properties": {
            "ok": {
              "const": false
            },
            "message": {
              "type": "string",
              "minLength": 1,
              "maxLength": 4096
            }
          }
        }
      ]
    },
    "operationsProposePayload": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "note",
        "operations"
      ],
      "properties": {
        "note": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4000
        },
        "operations": {
          "type": "array",
          "minItems": 1,
          "maxItems": 256,
          "items": {
            "$ref": "https://yarrasys.dev/schema/yarramate-operations.schema.json#/$defs/operation"
          }
        }
      }
    }
  }
}
