{
  "openapi": "3.0.3",
  "info": {
    "title": "UmamiAI Platform API",
    "version": "1.0.0",
    "description": "Versioned HTTP API for UmamiAI agent runs and knowledge search. Base path: /api/v1. Authentication: Bearer API key (umami_...)."
  },
  "servers": [
    {
      "url": "http://localhost:3000",
      "description": "Local development"
    },
    {
      "url": "https://{domain}",
      "description": "Deployed web app (e.g., Vercel)",
      "variables": {
        "domain": {
          "default": "your-domain.example"
        }
      }
    }
  ],
  "tags": [
    { "name": "Agent Runs" },
    { "name": "Knowledge" }
  ],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "APIKey"
      }
    },
    "schemas": {
      "ErrorResponse": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "error": { "type": "string" }
        },
        "required": ["error"]
      },
      "CreateAgentRunRequest": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "org_id": { "type": "string", "format": "uuid" },
          "agent_version_id": { "type": "string", "format": "uuid" },
          "input": { "type": "string" }
        },
        "required": ["org_id", "agent_version_id", "input"]
      },
      "CreateAgentRunResponse": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "run_id": { "type": "string", "format": "uuid" },
          "status": { "type": "string", "enum": ["queued", "running", "succeeded", "failed"] },
          "created_at": { "type": "string", "format": "date-time" }
        },
        "required": ["run_id", "status", "created_at"]
      },
      "GetAgentRunResponse": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "run_id": { "type": "string", "format": "uuid" },
          "org_id": { "type": "string", "format": "uuid" },
          "agent_version_id": { "type": "string", "format": "uuid" },
          "input": { "type": "string" },
          "output": { "type": "string", "nullable": true },
          "status": { "type": "string", "enum": ["queued", "running", "succeeded", "failed"] },
          "error": { "type": "string", "nullable": true },
          "created_at": { "type": "string", "format": "date-time" },
          "updated_at": { "type": "string", "format": "date-time" }
        },
        "required": ["run_id", "org_id", "agent_version_id", "input", "status", "created_at", "updated_at"]
      },
      "KnowledgeSearchRequest": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "org_id": { "type": "string", "format": "uuid" },
          "query": { "type": "string" },
          "match_count": { "type": "integer", "minimum": 1, "maximum": 20, "default": 6 }
        },
        "required": ["org_id", "query"]
      },
      "KnowledgeMatch": {
        "type": "object",
        "additionalProperties": true,
        "properties": {
          "chunk_id": { "type": "string", "format": "uuid" },
          "doc_id": { "type": "string", "format": "uuid" },
          "content": { "type": "string" },
          "score": { "type": "number", "format": "float" }
        },
        "required": ["chunk_id", "doc_id", "content", "score"]
      },
      "KnowledgeSearchResponse": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "matches": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/KnowledgeMatch" }
          }
        },
        "required": ["matches"]
      }
    }
  },
  "security": [{ "bearerAuth": [] }],
  "paths": {
    "/api/v1/agent-runs": {
      "post": {
        "tags": ["Agent Runs"],
        "summary": "Create an agent run",
        "description": "Creates a run for the specified agent version inside the caller's org.",
        "operationId": "createAgentRun",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreateAgentRunRequest" },
              "examples": {
                "basic": {
                  "value": {
                    "org_id": "00000000-0000-0000-0000-000000000000",
                    "agent_version_id": "00000000-0000-0000-0000-000000000000",
                    "input": "Hello from API"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Created",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CreateAgentRunResponse" }
              }
            }
          },
          "400": { "description": "Invalid request", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "401": { "description": "Missing or invalid API key", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "403": { "description": "Forbidden (scope or org mismatch)", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "500": { "description": "Server error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/api/v1/agent-runs/{runId}": {
      "get": {
        "tags": ["Agent Runs"],
        "summary": "Get agent run",
        "description": "Fetches the run status and output (when completed).",
        "operationId": "getAgentRun",
        "parameters": [
          {
            "name": "runId",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/GetAgentRunResponse" }
              }
            }
          },
          "400": { "description": "Invalid request", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "401": { "description": "Missing or invalid API key", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "403": { "description": "Forbidden (scope)", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "404": { "description": "Not found", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "500": { "description": "Server error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/api/v1/knowledge/search": {
      "post": {
        "tags": ["Knowledge"],
        "summary": "Knowledge search",
        "description": "Embeds the query server-side and returns the top matching chunks for the org.",
        "operationId": "knowledgeSearch",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/KnowledgeSearchRequest" },
              "examples": {
                "basic": {
                  "value": {
                    "org_id": "00000000-0000-0000-0000-000000000000",
                    "query": "What changed in the last release?",
                    "match_count": 6
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/KnowledgeSearchResponse" }
              }
            }
          },
          "400": { "description": "Invalid request", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "401": { "description": "Missing or invalid API key", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "403": { "description": "Forbidden (scope or org mismatch)", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "500": { "description": "Server error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    }
  }
}
