{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Rill workflow document v2",
  "description": "Structural schema for the TOML data model. The application additionally validates IDs, nesting, speech ordering, component readiness and credential policy.",
  "type": "object",
  "properties": {
    "schema_version": {
      "const": 2
    },
    "id": {
      "type": "string",
      "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "description": {
      "type": "string"
    },
    "enabled": {
      "type": "boolean",
      "default": false
    },
    "trigger": {
      "type": "object",
      "properties": {
        "kind": {
          "enum": [
            "manual",
            "hotkey",
            "menu-bar",
            "wake-word"
          ]
        },
        "gesture": {
          "type": "string"
        }
      },
      "required": [
        "kind"
      ],
      "additionalProperties": false
    },
    "input": {
      "type": "object",
      "properties": {
        "kind": {
          "enum": [
            "audio",
            "text",
            "record"
          ]
        }
      },
      "required": [
        "kind"
      ],
      "additionalProperties": false
    },
    "ui": {
      "type": "object",
      "properties": {
        "symbol": {
          "type": "string"
        },
        "accent": {
          "type": "string"
        }
      },
      "required": [
        "symbol",
        "accent"
      ],
      "additionalProperties": false
    },
    "setup": {
      "type": "object",
      "properties": {
        "speech": {
          "type": "object",
          "properties": {
            "selection": {
              "enum": [
                "automatic",
                "fixed"
              ]
            },
            "recognizer": {
              "type": "string"
            },
            "language": {
              "type": "string"
            },
            "local_model": {
              "type": "string"
            },
            "provider_model": {
              "type": "string"
            },
            "live_preview": {
              "type": "boolean"
            },
            "live_preview_placement": {
              "enum": [
                "overlay",
                "cursor"
              ]
            },
            "streaming_profile": {
              "type": "string"
            }
          },
          "required": [
            "selection",
            "recognizer"
          ],
          "additionalProperties": false
        },
        "vocabulary": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
              },
              "collection": {
                "type": "string",
                "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
              },
              "uses": {
                "type": "array",
                "items": {
                  "enum": [
                    "recognition-hints",
                    "text-replacement"
                  ]
                },
                "minItems": 1,
                "maxItems": 2
              },
              "when": {
                "type": "object",
                "properties": {
                  "app_bundle_id": {
                    "type": "string"
                  },
                  "clipboard_group": {
                    "type": "string",
                    "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
                  },
                  "locale": {
                    "type": "string"
                  }
                },
                "required": [],
                "additionalProperties": false
              }
            },
            "required": [
              "id",
              "collection",
              "uses"
            ],
            "additionalProperties": false
          },
          "minItems": 0,
          "maxItems": 256
        },
        "wake_word": {
          "type": "object",
          "properties": {
            "phrases": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "minItems": 1,
              "maxItems": 4
            }
          },
          "required": [
            "phrases"
          ],
          "additionalProperties": false
        }
      },
      "required": [],
      "additionalProperties": false
    },
    "process": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/step"
      },
      "minItems": 0,
      "maxItems": 256
    },
    "output": {
      "type": "object",
      "properties": {
        "strategy": {
          "enum": [
            "immediate",
            "collection-first",
            "system-clipboard-only"
          ]
        },
        "actions": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[A-Za-z0-9_-]{1,128}$"
              },
              "kind": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "config": {
                "type": "object",
                "additionalProperties": {
                  "type": "string"
                }
              },
              "condition": {
                "$ref": "#/$defs/condition"
              }
            },
            "required": [
              "id",
              "kind"
            ],
            "additionalProperties": false
          },
          "minItems": 1,
          "maxItems": 256
        }
      },
      "required": [
        "actions"
      ],
      "additionalProperties": false
    },
    "options": {
      "type": "object",
      "additionalProperties": {
        "type": "string"
      }
    },
    "metadata": {
      "type": "object",
      "patternProperties": {
        "^user\\.": {
          "type": "string"
        },
        "^workflow\\.origin$": {
          "type": "string"
        }
      },
      "additionalProperties": false
    }
  },
  "required": [
    "schema_version",
    "id",
    "name",
    "trigger",
    "input",
    "output"
  ],
  "additionalProperties": false,
  "$defs": {
    "condition": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "field": {
              "enum": [
                "text",
                "context.app_bundle_id",
                "context.selected_text",
                "context.clipboard_text"
              ]
            },
            "op": {
              "enum": [
                "equals",
                "contains"
              ]
            },
            "value": {
              "type": "string"
            }
          },
          "required": [
            "field",
            "op",
            "value"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "field": {
              "enum": [
                "text",
                "context.app_bundle_id",
                "context.selected_text",
                "context.clipboard_text"
              ]
            },
            "op": {
              "const": "exists"
            }
          },
          "required": [
            "field",
            "op"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "all": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/condition"
              },
              "minItems": 1,
              "maxItems": 64
            }
          },
          "required": [
            "all"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "any": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/condition"
              },
              "minItems": 1,
              "maxItems": 64
            }
          },
          "required": [
            "any"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "not": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/condition"
              },
              "minItems": 1,
              "maxItems": 1
            }
          },
          "required": [
            "not"
          ],
          "additionalProperties": false
        }
      ]
    },
    "step": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_-]{1,128}$"
        },
        "kind": {
          "enum": [
            "if",
            "recognize-speech",
            "resolve-uncertainty",
            "apply-vocabulary",
            "snippet-replacement",
            "llm-rewrite",
            "llm-answer",
            "normalize-whitespace"
          ]
        },
        "description": {
          "type": "string"
        },
        "prompt": {
          "type": "string"
        },
        "record_duration": {
          "type": "boolean",
          "description": "Record measured time for this step. Defaults to true for speech recognition and LLM calls."
        },
        "uncertainty": {
          "type": "object",
          "properties": {
            "mode": {
              "enum": [
                "off",
                "non-blocking",
                "blocking"
              ]
            },
            "confidence_threshold": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            },
            "timeout_seconds": {
              "type": "number",
              "minimum": 0
            }
          },
          "required": [
            "mode",
            "confidence_threshold",
            "timeout_seconds"
          ],
          "additionalProperties": false
        },
        "condition": {
          "$ref": "#/$defs/condition"
        },
        "then": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/step"
          },
          "minItems": 0,
          "maxItems": 256
        },
        "else": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/step"
          },
          "minItems": 0,
          "maxItems": 256
        }
      },
      "required": [
        "id",
        "kind"
      ],
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": {
              "kind": {
                "const": "if"
              }
            }
          },
          "then": {
            "required": [
              "condition"
            ],
            "not": {
              "anyOf": [
                {
                  "required": [
                    "prompt"
                  ]
                },
                {
                  "required": [
                    "uncertainty"
                  ]
                }
              ]
            }
          },
          "else": {
            "not": {
              "anyOf": [
                {
                  "required": [
                    "condition"
                  ]
                },
                {
                  "required": [
                    "then"
                  ]
                },
                {
                  "required": [
                    "else"
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}
