{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.runagents.io/cli/plan-schema.json",
  "title": "RunAgents Action Plan",
  "type": "object",
  "required": ["actions"],
  "properties": {
    "plan_id": {
      "type": "string",
      "description": "Optional correlation id for the plan"
    },
    "continue_on_error": {
      "type": "boolean",
      "description": "Continue applying remaining actions after a failure"
    },
    "actions": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/action"
      }
    }
  },
  "additionalProperties": false,
  "$defs": {
    "action": {
      "type": "object",
      "required": ["type", "idempotency_key"],
      "properties": {
        "id": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "enum": [
            "tool.upsert",
            "model_provider.upsert",
            "policy.upsert",
            "deploy_draft.create_or_patch",
            "deploy.execute",
            "starter_kit.seed"
          ]
        },
        "idempotency_key": {
          "type": "string",
          "minLength": 1
        },
        "params": {
          "type": "object"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "type": {
                "const": "tool.upsert"
              }
            }
          },
          "then": {
            "properties": {
              "params": {
                "type": "object",
                "required": ["name", "spec"],
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 1
                  },
                  "spec": {
                    "type": "object",
                    "required": ["connection"],
                    "properties": {
                      "connection": {
                        "type": "object",
                        "required": ["baseUrl"],
                        "properties": {
                          "baseUrl": {
                            "type": "string",
                            "minLength": 1
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "model_provider.upsert"
              }
            }
          },
          "then": {
            "properties": {
              "params": {
                "type": "object",
                "required": ["name", "spec"],
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 1
                  },
                  "spec": {
                    "type": "object",
                    "required": ["provider", "endpoint", "models"],
                    "properties": {
                      "provider": {
                        "type": "string",
                        "minLength": 1
                      },
                      "endpoint": {
                        "type": "string",
                        "minLength": 1
                      },
                      "models": {
                        "type": "array",
                        "minItems": 1,
                        "items": {
                          "type": "string"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "policy.upsert"
              }
            }
          },
          "then": {
            "properties": {
              "params": {
                "type": "object",
                "required": ["name", "spec"],
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 1
                  },
                  "spec": {
                    "type": "object"
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "deploy_draft.create_or_patch"
              }
            }
          },
          "then": {
            "properties": {
              "params": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "expected_version": {
                    "type": "integer",
                    "minimum": 1
                  },
                  "artifact_id": {
                    "type": "string"
                  },
                  "source_type": {
                    "type": "string"
                  },
                  "step": {
                    "type": "string"
                  },
                  "status": {
                    "type": "string",
                    "enum": ["IN_PROGRESS", "COMPLETED", "ABANDONED"]
                  },
                  "policies": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  },
                  "tool_mappings": {
                    "type": "object",
                    "additionalProperties": {
                      "type": "string"
                    }
                  },
                  "model_mappings": {
                    "type": "object",
                    "additionalProperties": {
                      "type": "object",
                      "properties": {
                        "provider": {
                          "type": "string"
                        },
                        "model": {
                          "type": "string"
                        }
                      }
                    }
                  },
                  "agent_config": {
                    "type": "object"
                  },
                  "user_id": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "deploy.execute"
              }
            }
          },
          "then": {
            "properties": {
              "params": {
                "type": "object",
                "required": ["agent_name"],
                "properties": {
                  "agent_name": {
                    "type": "string",
                    "minLength": 1
                  },
                  "draft_id": {
                    "type": "string"
                  },
                  "artifact_id": {
                    "type": "string"
                  },
                  "image": {
                    "type": "string"
                  },
                  "source_files": {
                    "type": "object",
                    "additionalProperties": {
                      "type": "string"
                    }
                  },
                  "user_id": {
                    "type": "string"
                  }
                },
                "anyOf": [
                  {
                    "required": ["draft_id"]
                  },
                  {
                    "required": ["artifact_id"]
                  },
                  {
                    "required": ["image"]
                  },
                  {
                    "required": ["source_files"]
                  }
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "starter_kit.seed"
              }
            }
          },
          "then": {
            "properties": {
              "params": {
                "type": "object"
              }
            }
          }
        }
      ]
    }
  }
}
