{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.aegisai.app/user-defined-tools.schema.v1.json",
  "title": "Aegis Custom Tool Import Schema v1",
  "description": "Versioned schema for importing custom tools in Aegis.",
  "x-aegis-schema-version": "1.0.0",
  "oneOf": [
    {
      "$ref": "#/$defs/aegisTool"
    },
    {
      "$ref": "#/$defs/openAiFunctionTool"
    }
  ],
  "$defs": {
    "toolId": {
      "type": "string",
      "pattern": "^[a-zA-Z0-9_.:-]{3,}$"
    },
    "toolSettings": {
      "type": "object",
      "additionalProperties": {
        "type": [
          "string",
          "number",
          "boolean",
          "null"
        ]
      }
    },
    "runtimeNetwork": {
      "type": "object",
      "properties": {
        "allowHosts": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "allowHttp": {
          "type": "boolean"
        },
        "timeoutMs": {
          "type": "number"
        },
        "maxResponseBytes": {
          "type": "number"
        },
        "maxRequestBodyBytes": {
          "type": "number"
        }
      },
      "required": [
        "allowHosts"
      ],
      "additionalProperties": false
    },
    "runtime": {
      "type": "object",
      "properties": {
        "language": {
          "type": "string",
          "enum": [
            "javascript",
            "js"
          ]
        },
        "code": {
          "type": "string",
          "minLength": 1
        },
        "timeoutMs": {
          "type": "number"
        },
        "network": {
          "$ref": "#/$defs/runtimeNetwork"
        }
      },
      "required": [
        "code"
      ],
      "additionalProperties": false
    },
    "aegisTool": {
      "type": "object",
      "properties": {
        "id": {
          "$ref": "#/$defs/toolId"
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "source": {
          "type": "string",
          "enum": [
            "custom"
          ]
        },
        "enabled": {
          "type": "boolean"
        },
        "description": {
          "type": "string"
        },
        "promptMarkdown": {
          "type": "string"
        },
        "settings": {
          "$ref": "#/$defs/toolSettings"
        },
        "parameters": {
          "type": "object"
        },
        "runtime": {
          "$ref": "#/$defs/runtime"
        }
      },
      "required": [
        "id",
        "name",
        "runtime"
      ],
      "additionalProperties": true
    },
    "openAiFunctionTool": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "const": "function"
        },
        "name": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "promptMarkdown": {
          "type": "string"
        },
        "enabled": {
          "type": "boolean"
        },
        "settings": {
          "$ref": "#/$defs/toolSettings"
        },
        "runtime": {
          "$ref": "#/$defs/runtime"
        },
        "function": {
          "type": "object",
          "properties": {
            "name": {
              "$ref": "#/$defs/toolId"
            },
            "description": {
              "type": "string"
            },
            "parameters": {
              "type": "object"
            }
          },
          "required": [
            "name",
            "parameters"
          ],
          "additionalProperties": true
        }
      },
      "required": [
        "type",
        "function",
        "runtime"
      ],
      "additionalProperties": true
    }
  }
}
