{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.gooshi.co/docs/trigger-draft-proposal-v1.schema.json",
  "title": "Gooshi trigger draft proposal v1",
  "description": "An intent-level planning format for preparing a trigger draft for human review in Gooshi. This schema is not a publication, activation, import, or payment API.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "proposalVersion",
    "draft",
    "explanation",
    "assumptions",
    "questions"
  ],
  "properties": {
    "proposalVersion": {
      "const": 1
    },
    "draft": {
      "$ref": "#/$defs/draft"
    },
    "explanation": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2000
    },
    "assumptions": {
      "type": "array",
      "maxItems": 20,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 500
      }
    },
    "questions": {
      "type": "array",
      "maxItems": 20,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 500
      }
    }
  },
  "$defs": {
    "identifier": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "reason": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200
    },
    "scalar": {
      "oneOf": [
        {
          "type": "string",
          "maxLength": 500
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        }
      ]
    },
    "condition": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type"
          ],
          "properties": {
            "type": {
              "const": "always"
            },
            "reason": {
              "$ref": "#/$defs/reason"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "meterRef",
            "freeUnits"
          ],
          "properties": {
            "type": {
              "const": "quota_exceeded"
            },
            "meterRef": {
              "$ref": "#/$defs/identifier"
            },
            "freeUnits": {
              "type": "number",
              "minimum": 0
            },
            "reason": {
              "$ref": "#/$defs/reason"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "left",
            "operator",
            "right"
          ],
          "properties": {
            "type": {
              "const": "comparison"
            },
            "left": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "ref"
              ],
              "properties": {
                "ref": {
                  "$ref": "#/$defs/identifier"
                }
              }
            },
            "operator": {
              "enum": [
                ">",
                ">=",
                "<",
                "<=",
                "==",
                "!="
              ]
            },
            "right": {
              "$ref": "#/$defs/scalar"
            },
            "reason": {
              "$ref": "#/$defs/reason"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "conditions"
          ],
          "properties": {
            "type": {
              "enum": [
                "all",
                "any"
              ]
            },
            "conditions": {
              "type": "array",
              "minItems": 1,
              "maxItems": 20,
              "items": {
                "$ref": "#/$defs/condition"
              }
            },
            "reason": {
              "$ref": "#/$defs/reason"
            }
          }
        }
      ]
    },
    "payment": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "amountMinor",
            "currency"
          ],
          "properties": {
            "type": {
              "const": "one_time"
            },
            "amountMinor": {
              "type": "integer",
              "minimum": 1,
              "maximum": 999999999
            },
            "currency": {
              "type": "string",
              "pattern": "^[A-Z]{3}$"
            },
            "label": {
              "type": "string",
              "minLength": 1,
              "maxLength": 120
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "amountMinor",
            "currency",
            "interval"
          ],
          "properties": {
            "type": {
              "const": "subscription"
            },
            "amountMinor": {
              "type": "integer",
              "minimum": 1,
              "maximum": 999999999
            },
            "currency": {
              "type": "string",
              "pattern": "^[A-Z]{3}$"
            },
            "interval": {
              "enum": [
                "month",
                "year"
              ]
            },
            "label": {
              "type": "string",
              "minLength": 1,
              "maxLength": 120
            }
          }
        }
      ]
    },
    "quotaConditionPresence": {
      "anyOf": [
        {
          "type": "object",
          "required": [
            "type"
          ],
          "properties": {
            "type": {
              "const": "quota_exceeded"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "type",
            "conditions"
          ],
          "properties": {
            "type": {
              "enum": [
                "all",
                "any"
              ]
            },
            "conditions": {
              "type": "array",
              "contains": {
                "$ref": "#/$defs/quotaConditionPresence"
              }
            }
          }
        }
      ]
    },
    "draft": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "contractVersion",
        "toolRefs",
        "condition",
        "payment",
        "resumable"
      ],
      "properties": {
        "contractVersion": {
          "const": 1
        },
        "toolRefs": {
          "type": "array",
          "minItems": 1,
          "maxItems": 100,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/identifier"
          }
        },
        "condition": {
          "$ref": "#/$defs/condition"
        },
        "payment": {
          "$ref": "#/$defs/payment"
        },
        "usage": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "increment"
          ],
          "properties": {
            "meterRef": {
              "$ref": "#/$defs/identifier"
            },
            "increment": {
              "enum": [
                "on_success",
                "on_attempt"
              ]
            }
          }
        },
        "resumable": {
          "type": "boolean"
        },
        "blockedCopy": {
          "type": "string",
          "minLength": 1,
          "maxLength": 1000
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "condition": {
                "$ref": "#/$defs/quotaConditionPresence"
              }
            },
            "required": [
              "condition"
            ]
          },
          "then": {
            "properties": {
              "usage": {}
            },
            "required": [
              "usage"
            ]
          }
        }
      ]
    }
  },
  "examples": [
    {
      "proposalVersion": 1,
      "draft": {
        "contractVersion": 1,
        "toolRefs": [
          "tool_ref_from_gooshi"
        ],
        "condition": {
          "type": "quota_exceeded",
          "meterRef": "meter_ref_from_gooshi",
          "freeUnits": 10,
          "reason": "Require payment after the included calls are used."
        },
        "payment": {
          "type": "one_time",
          "amountMinor": 250,
          "currency": "USD",
          "label": "Additional tool access"
        },
        "usage": {
          "meterRef": "meter_ref_from_gooshi",
          "increment": "on_success"
        },
        "resumable": true,
        "blockedCopy": "Your included calls are used. Complete payment to continue this request."
      },
      "explanation": "Allow ten completed calls, then present a one-time offer.",
      "assumptions": [
        "The displayed tool and meter references were copied from the current Gooshi workspace."
      ],
      "questions": [
        "Should failed attempts count toward the included allowance?"
      ]
    }
  ]
}
