{
  "$id": "https://skaile.ai/schemas/flow/v2.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "$schema": {
      "type": "string",
      "minLength": 1
    },
    "schemaVersion": {
      "type": "number",
      "const": 2
    },
    "id": {
      "type": "string",
      "minLength": 1
    },
    "version": {
      "type": "string",
      "minLength": 1
    },
    "name": {
      "type": "string",
      "minLength": 1
    },
    "description": {
      "type": "string",
      "minLength": 1
    },
    "input": {
      "type": "object",
      "properties": {
        "schema": {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "$ref": "#/$defs/__schema0"
          }
        },
        "example": {
          "$ref": "#/$defs/__schema1"
        },
        "labelField": {
          "type": "string",
          "minLength": 1
        }
      },
      "required": ["schema"],
      "additionalProperties": false
    },
    "output": {
      "type": "object",
      "properties": {
        "schema": {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "$ref": "#/$defs/__schema0"
          }
        }
      },
      "required": ["schema"],
      "additionalProperties": false
    },
    "defaults": {
      "type": "object",
      "properties": {
        "approval": {
          "type": "string",
          "enum": ["checkpoint", "auto", "manual"]
        },
        "researchDepth": {
          "type": "string",
          "minLength": 1
        },
        "autoReview": {
          "type": "boolean"
        },
        "verbosity": {
          "type": "string",
          "minLength": 1
        },
        "cliMode": {
          "type": "boolean"
        },
        "run_input": {
          "$ref": "#/$defs/__schema2"
        },
        "parameters": {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "$ref": "#/$defs/__schema3"
          }
        }
      },
      "additionalProperties": false
    },
    "entry": {
      "type": "string",
      "minLength": 1
    },
    "nodes": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1
          },
          "label": {
            "type": "string",
            "minLength": 1
          },
          "description": {
            "type": "string",
            "minLength": 1
          },
          "phase": {
            "type": "string",
            "minLength": 1
          },
          "run": {
            "oneOf": [
              {
                "type": "object",
                "properties": {
                  "kind": {
                    "type": "string",
                    "const": "agent"
                  },
                  "instruction": {
                    "type": "string",
                    "minLength": 1
                  },
                  "model": {
                    "type": "string",
                    "enum": ["small", "default", "deep"]
                  },
                  "assets": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    }
                  }
                },
                "required": ["kind", "instruction"],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "kind": {
                    "type": "string",
                    "const": "subprompt"
                  },
                  "instruction": {
                    "type": "string",
                    "minLength": 1
                  },
                  "model": {
                    "type": "string",
                    "enum": ["small", "default", "deep"]
                  },
                  "assets": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    }
                  }
                },
                "required": ["kind", "instruction"],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "kind": {
                    "type": "string",
                    "const": "function"
                  },
                  "runtime": {
                    "type": "string",
                    "enum": ["shell", "node", "python"]
                  },
                  "command": {
                    "type": "string",
                    "minLength": 1
                  },
                  "cwd": {
                    "type": "string",
                    "minLength": 1
                  },
                  "successExit": {
                    "type": "array",
                    "items": {
                      "type": "integer",
                      "minimum": -9007199254740991,
                      "maximum": 9007199254740991
                    }
                  },
                  "capture": {
                    "type": "object",
                    "properties": {
                      "stdout": {
                        "type": "string",
                        "minLength": 1
                      },
                      "stderr": {
                        "type": "string",
                        "minLength": 1
                      }
                    },
                    "additionalProperties": false
                  },
                  "assets": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    }
                  }
                },
                "required": ["kind", "command"],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "kind": {
                    "type": "string",
                    "const": "check"
                  },
                  "onFail": {
                    "default": "escalate",
                    "type": "string",
                    "enum": ["escalate", "fail"]
                  },
                  "runtime": {
                    "type": "string",
                    "enum": ["shell", "node", "python"]
                  },
                  "command": {
                    "type": "string",
                    "minLength": 1
                  },
                  "cwd": {
                    "type": "string",
                    "minLength": 1
                  },
                  "successExit": {
                    "type": "array",
                    "items": {
                      "type": "integer",
                      "minimum": -9007199254740991,
                      "maximum": 9007199254740991
                    }
                  },
                  "capture": {
                    "type": "object",
                    "properties": {
                      "stdout": {
                        "type": "string",
                        "minLength": 1
                      },
                      "stderr": {
                        "type": "string",
                        "minLength": 1
                      }
                    },
                    "additionalProperties": false
                  },
                  "assets": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    }
                  }
                },
                "required": ["kind", "command"],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "kind": {
                    "type": "string",
                    "const": "gate"
                  },
                  "prompt": {
                    "type": "string",
                    "minLength": 1
                  },
                  "schema": {
                    "oneOf": [
                      {
                        "type": "object",
                        "properties": {
                          "kind": {
                            "type": "string",
                            "const": "text"
                          },
                          "multiline": {
                            "type": "boolean"
                          }
                        },
                        "required": ["kind"],
                        "additionalProperties": false
                      },
                      {
                        "type": "object",
                        "properties": {
                          "kind": {
                            "type": "string",
                            "const": "choice"
                          },
                          "options": {
                            "minItems": 1,
                            "type": "array",
                            "items": {
                              "type": "object",
                              "properties": {
                                "id": {
                                  "type": "string",
                                  "minLength": 1
                                },
                                "label": {
                                  "type": "string",
                                  "minLength": 1
                                },
                                "description": {
                                  "type": "string",
                                  "minLength": 1
                                }
                              },
                              "required": ["id", "label"],
                              "additionalProperties": false
                            }
                          },
                          "multiple": {
                            "type": "boolean"
                          }
                        },
                        "required": ["kind", "options"],
                        "additionalProperties": false
                      },
                      {
                        "type": "object",
                        "properties": {
                          "kind": {
                            "type": "string",
                            "const": "form"
                          },
                          "fields": {
                            "type": "array",
                            "items": {
                              "type": "object",
                              "properties": {
                                "id": {
                                  "type": "string",
                                  "minLength": 1
                                },
                                "label": {
                                  "type": "string",
                                  "minLength": 1
                                },
                                "type": {
                                  "type": "string",
                                  "enum": [
                                    "text",
                                    "textarea",
                                    "number",
                                    "choice",
                                    "file",
                                    "date",
                                    "boolean"
                                  ]
                                },
                                "required": {
                                  "type": "boolean"
                                },
                                "options": {
                                  "type": "array",
                                  "items": {
                                    "type": "object",
                                    "properties": {
                                      "id": {
                                        "type": "string",
                                        "minLength": 1
                                      },
                                      "label": {
                                        "type": "string",
                                        "minLength": 1
                                      },
                                      "description": {
                                        "type": "string",
                                        "minLength": 1
                                      }
                                    },
                                    "required": ["id", "label"],
                                    "additionalProperties": false
                                  }
                                },
                                "accept": {
                                  "type": "array",
                                  "items": {
                                    "type": "string",
                                    "minLength": 1
                                  }
                                }
                              },
                              "required": ["id", "label", "type"],
                              "additionalProperties": false
                            }
                          }
                        },
                        "required": ["kind", "fields"],
                        "additionalProperties": false
                      },
                      {
                        "type": "object",
                        "properties": {
                          "kind": {
                            "type": "string",
                            "const": "file"
                          },
                          "accept": {
                            "type": "array",
                            "items": {
                              "type": "string",
                              "minLength": 1
                            }
                          },
                          "maxSize": {
                            "type": "number",
                            "exclusiveMinimum": 0
                          },
                          "multiple": {
                            "type": "boolean"
                          }
                        },
                        "required": ["kind"],
                        "additionalProperties": false
                      }
                    ]
                  },
                  "assets": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    }
                  }
                },
                "required": ["kind", "prompt", "schema"],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "kind": {
                    "type": "string",
                    "const": "router"
                  },
                  "routes": {
                    "minItems": 1,
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "when": {
                          "type": "string",
                          "minLength": 1
                        },
                        "target": {
                          "anyOf": [
                            {
                              "type": "string",
                              "minLength": 1
                            },
                            {
                              "type": "null"
                            }
                          ]
                        }
                      },
                      "required": ["when", "target"],
                      "additionalProperties": false
                    }
                  },
                  "assets": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    }
                  }
                },
                "required": ["kind", "routes"],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "kind": {
                    "type": "string",
                    "const": "sub-flow"
                  },
                  "flow": {
                    "type": "string",
                    "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$"
                  },
                  "passContext": {
                    "type": "boolean"
                  },
                  "assets": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    }
                  }
                },
                "required": ["kind", "flow"],
                "additionalProperties": false
              }
            ]
          },
          "contract": {
            "type": "object",
            "properties": {
              "requires": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "expr": {
                      "type": "string",
                      "minLength": 1
                    },
                    "message": {
                      "type": "string",
                      "minLength": 1
                    }
                  },
                  "required": ["expr"],
                  "additionalProperties": false
                }
              },
              "input": {
                "type": "object",
                "propertyNames": {
                  "type": "string",
                  "pattern": "^(?!__proto__$)"
                },
                "additionalProperties": {
                  "$ref": "#/$defs/__schema4"
                }
              },
              "output": {
                "type": "object",
                "properties": {
                  "fields": {
                    "type": "object",
                    "propertyNames": {
                      "type": "string"
                    },
                    "additionalProperties": {
                      "$ref": "#/$defs/__schema0"
                    }
                  },
                  "artifacts": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "kind": {
                          "type": "string",
                          "minLength": 1
                        },
                        "lifetime": {
                          "type": "string",
                          "enum": ["session", "persistent"]
                        }
                      },
                      "required": ["kind", "lifetime"],
                      "additionalProperties": false
                    }
                  }
                },
                "additionalProperties": false
              }
            },
            "additionalProperties": false
          },
          "gate": {
            "type": "object",
            "properties": {
              "approval": {
                "type": "string",
                "enum": ["none", "optional", "mandatory"]
              }
            },
            "required": ["approval"],
            "additionalProperties": false
          },
          "control": {
            "type": "object",
            "properties": {
              "optional": {
                "type": "boolean"
              },
              "retries": {
                "type": "integer",
                "minimum": 0,
                "maximum": 9007199254740991
              },
              "timeoutSec": {
                "type": "number",
                "exclusiveMinimum": 0
              },
              "parallelGroup": {
                "type": "string",
                "minLength": 1
              }
            },
            "additionalProperties": false
          }
        },
        "required": ["id", "label", "description", "run"],
        "additionalProperties": false
      }
    },
    "edges": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1
          },
          "source": {
            "type": "string",
            "minLength": 1
          },
          "target": {
            "type": "string",
            "minLength": 1
          },
          "type": {
            "type": "string",
            "enum": ["flow", "parallel", "optional"]
          }
        },
        "required": ["id", "source", "target", "type"],
        "additionalProperties": false
      }
    },
    "meta": {
      "type": "object",
      "properties": {
        "icon": {
          "type": "string",
          "minLength": 1
        },
        "category": {
          "type": "string",
          "minLength": 1
        },
        "tags": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "stage": {
          "type": "string",
          "minLength": 1
        },
        "onboarding": {
          "type": "object",
          "properties": {
            "input_style": {
              "type": "string",
              "enum": ["freeform", "structured", "repo"]
            },
            "placeholder": {
              "type": "string",
              "minLength": 1
            },
            "fields": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1
              }
            }
          },
          "required": ["input_style"],
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    }
  },
  "required": ["schemaVersion", "id", "version", "name", "nodes", "edges"],
  "additionalProperties": false,
  "$defs": {
    "__schema0": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        },
        {
          "type": "null"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/__schema0"
          }
        },
        {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "$ref": "#/$defs/__schema0"
          }
        }
      ]
    },
    "__schema1": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        },
        {
          "type": "null"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/__schema1"
          }
        },
        {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "$ref": "#/$defs/__schema1"
          }
        }
      ]
    },
    "__schema2": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        },
        {
          "type": "null"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/__schema2"
          }
        },
        {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "$ref": "#/$defs/__schema2"
          }
        }
      ]
    },
    "__schema3": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        },
        {
          "type": "null"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/__schema3"
          }
        },
        {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "$ref": "#/$defs/__schema3"
          }
        }
      ]
    },
    "__schema4": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        },
        {
          "type": "null"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/__schema4"
          }
        },
        {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "$ref": "#/$defs/__schema4"
          }
        }
      ]
    }
  }
}
