{
  "openapi": "3.1.0",
  "info": {
    "title": "BEST Agent Service",
    "description": "HTTP API for the io.best.agents service — command ingestion, command discovery, event log, and queries.",
    "version": "0.9.23",
    "license": {
      "name": "Apache-2.0",
      "url": "https://www.apache.org/licenses/LICENSE-2.0"
    }
  },
  "servers": [
    {
      "url": "{endpoint}",
      "description": "BEST-compliant endpoint base URL (from the http.endpoint field in the /.well-known/best manifest). All API paths below are appended to this base URL. For example, if http.endpoint is https://app.example.com/BEST/, then GET /commands resolves to https://app.example.com/BEST/commands.",
      "variables": {
        "endpoint": {
          "default": "https://your.compliant.BEST.endpoint"
        }
      }
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/.well-known/best": {
      "get": {
        "operationId": "getDiscoveryManifest",
        "summary": "Discovery manifest",
        "description": "Returns the BEST discovery manifest describing available services, capabilities, and transport bindings.",
        "tags": [
          "Discovery"
        ],
        "responses": {
          "200": {
            "description": "Discovery manifest",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/discovery.json"
                }
              }
            }
          }
        }
      }
    },
    "/commands": {
      "get": {
        "operationId": "listCommandCatalogue",
        "summary": "List available command types",
        "description": "Returns the catalogue of command types this service accepts, each with its dataschema URI. This is the discovery surface: callers (Process Managers, AI agents, UIs) call this to learn what commands they can POST and what schema each command's data must conform to.",
        "tags": [
          "Commands"
        ],
        "responses": {
          "200": {
            "description": "Command catalogue",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/agents/commands.json#/$defs/commandCatalogue"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      },
      "post": {
        "operationId": "sendCommand",
        "summary": "Send a command to the service",
        "description": "Single CloudEvent ingestion entry point. The runtime validates the data property against the JSON Schema at the dataschema URI (hosted by this API), then queues the command for processing. Returns 201 on success.",
        "tags": [
          "Commands"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/cloudevents+json": {
              "schema": {
                "$ref": "../../schemas/agents/commands.json#/$defs/command"
              }
            },
            "application/json": {
              "schema": {
                "$ref": "../../schemas/agents/commands.json#/$defs/command"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Command accepted and durably queued",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["id"],
                  "properties": {
                    "id": {
                      "type": "string",
                      "description": "The command's envelope id (idempotency key), echoed back."
                    },
                    "correlationId": {
                      "type": "string",
                      "description": "The effective correlation identifier — the command's correlationid extension attribute, or its id when none was supplied. Matches the correlationid attribute on resulting events."
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid command — schema validation failed or missing required CloudEvent attributes",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/error.json"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/events": {
      "get": {
        "operationId": "listEvents",
        "summary": "Query published events",
        "description": "Returns the queryable log of events published by this service. Supports filtering by event type, correlation ID, source, and time range, plus cursor-based pagination. All parameters are optional and combinable.",
        "tags": [
          "Events"
        ],
        "parameters": [
          {
            "name": "type",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Filter by CloudEvent type (PascalCase, e.g. ChatKitMessageRememberedV1). Returns all events of this type across all interactions."
          },
          {
            "name": "correlationId",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Filter by correlation identifier — matches the correlationid envelope attribute on events (the correlationId echoed by POST /commands)."
          },
          {
            "name": "source",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Filter by event source — the string identifying the service that published the event (matches the CloudEvent source field)."
          },
          {
            "name": "from",
            "in": "query",
            "schema": { "type": "string", "format": "date-time" },
            "description": "Return only events published at or after this RFC 3339 timestamp (e.g. 2025-07-01T00:00:00Z); any other form is a 400 naming the parameter."
          },
          {
            "name": "to",
            "in": "query",
            "schema": { "type": "string", "format": "date-time" },
            "description": "Return only events published at or before this RFC 3339 timestamp; any other form is a 400 naming the parameter."
          },
          {
            "name": "limit",
            "in": "query",
            "schema": { "type": "integer", "minimum": 1 },
            "description": "Maximum number of events to return. Server may apply a lower ceiling. Defaults to a server-defined value."
          },
          {
            "name": "after",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Pagination cursor. Pass the nextCursor value from a previous response to retrieve the next page. Opaque — do not construct manually."
          }
        ],
        "responses": {
          "200": {
            "description": "List of published events, with optional pagination cursor",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/agents/events.json#/$defs/eventList"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/events/stream": {
      "get": {
        "operationId": "streamEvents",
        "summary": "Subscribe to live events (SSE)",
        "description": "Opens a persistent Server-Sent Events connection. The server pushes events as they are produced — no polling required. Delivers only events produced after the connection is opened; use GET /events for history. Each event is delivered as an SSE data field containing the CloudEvent JSON payload, with the CloudEvent id echoed as the SSE event id for reconnection via Last-Event-ID.",
        "tags": [
          "Events"
        ],
        "parameters": [
          {
            "name": "correlationId",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Receive only events whose correlationid envelope attribute matches — pass the correlationId echoed by POST /commands."
          },
          {
            "name": "type",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Filter by CloudEvent type (PascalCase). Receive only events of this type across all interactions."
          },
          {
            "name": "source",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Filter by publishing service — matches the CloudEvent source field."
          }
        ],
        "responses": {
          "200": {
            "description": "Persistent SSE stream. Each event is an SSE data field containing a CloudEvent JSON payload.",
            "content": {
              "text/event-stream": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/events/{schema}/{version}": {
      "parameters": [
        {
          "name": "schema",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Event schema name in kebab-case, as a typed event's dataschema URI names it (e.g. counter-proposed)"
        },
        {
          "name": "version",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Schema version string (e.g. 1.0, 2.1)"
        }
      ],
      "get": {
        "operationId": "getEventSchema",
        "summary": "Get a versioned event schema",
        "description": "Returns the JSON Schema document for a specific event type and version. Mirrors GET /commands/{schema}/{version}. The full URL of this endpoint is the canonical value of a typed event's dataschema; there is no event catalogue.",
        "tags": [
          "Events"
        ],
        "responses": {
          "200": {
            "description": "JSON Schema document",
            "content": {
              "application/schema+json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Schema or version not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/error.json"
                }
              }
            }
          }
        }
      }
    },
    "/queries": {
      "get": {
        "operationId": "listQueryCatalogue",
        "summary": "List available query types",
        "description": "Returns the catalogue of query types this service supports, each with its dataschema URI. Same discovery pattern as GET /commands: callers use this to learn what queries they can execute and how to construct the parameters.",
        "tags": [
          "Queries"
        ],
        "responses": {
          "200": {
            "description": "Query catalogue",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/agents/queries.json#/$defs/queryCatalogue"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/queries/{schema}/{version}": {
      "parameters": [
        {
          "name": "schema",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Query schema name in kebab-case, matching the 'schema' field of the catalogue entry (e.g. list-brokers)"
        },
        {
          "name": "version",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Schema version string (e.g. 1.0, 2.1)"
        }
      ],
      "get": {
        "operationId": "getQuerySchema",
        "summary": "Get a versioned query schema",
        "description": "Returns the JSON Schema document for a specific query type and version, describing the accepted query string parameters and the response body shape.",
        "tags": [
          "Queries"
        ],
        "responses": {
          "200": {
            "description": "Query schema document",
            "content": {
              "application/schema+json": {
                "schema": {
                  "$ref": "../../schemas/agents/queries.json#/$defs/querySchemaDocument"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Schema or version not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/error.json"
                }
              }
            }
          }
        }
      }
    },
    "/queries/{schema}": {
      "parameters": [
        {
          "name": "schema",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Query schema name in kebab-case, matching the 'schema' field of the catalogue entry (e.g. list-brokers)"
        }
      ],
      "get": {
        "operationId": "executeQuery",
        "summary": "Execute a query",
        "description": "Executes the query synchronously and returns current domain state. Parameters (if any) are passed as query string key-value pairs matching the 'parameters' schema in the query schema document. The response body matches the 'response' schema in that document.",
        "tags": [
          "Queries"
        ],
        "responses": {
          "200": {
            "description": "Query result — shape defined by the 'response' schema in the query schema document",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "400": {
            "description": "Required parameters missing or invalid",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/error.json"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Query schema name not supported",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/error.json"
                }
              }
            }
          }
        }
      }
    },
    "/workflows": {
      "get": {
        "operationId": "listWorkflows",
        "summary": "List published workflows",
        "description": "Returns the workflow index — id, name, and description for every published recipe, never the steps. The index is deliberately shallow so a consumer can hold the whole list in one read; fetch full recipes one at a time via GET /workflows/{id}.",
        "tags": [
          "Workflows"
        ],
        "responses": {
          "200": {
            "description": "Workflow index",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/agents/workflows.json#/$defs/workflowIndex"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/workflows/{id}": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Workflow id from the index (e.g. io.example.workflows.onboard-a-worker)"
        }
      ],
      "get": {
        "operationId": "getWorkflow",
        "summary": "Get one published workflow",
        "description": "Returns one full recipe: identity, summary, and the ordered steps. Each step references a live catalogue operation by its dataschema URI; the caller drives every step itself — the service never executes them.",
        "tags": [
          "Workflows"
        ],
        "responses": {
          "200": {
            "description": "One workflow recipe with its ordered steps",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/agents/workflows.json#/$defs/workflow"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Workflow id not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/error.json"
                }
              }
            }
          }
        }
      }
    },
    "/commands/{schema}/{version}": {
      "parameters": [
        {
          "name": "schema",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Schema name in kebab-case, matching the 'schema' field of the catalogue entry (e.g. propose-counter, submit-order)"
        },
        {
          "name": "version",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Schema version string (e.g. 1.0, 2.1)"
        }
      ],
      "get": {
        "operationId": "getSchema",
        "summary": "Get a versioned command schema",
        "description": "Returns the JSON Schema document for a specific command type and version. The full URL of this endpoint is the canonical value for the dataschema URI in a command catalogue entry.",
        "tags": [
          "Commands"
        ],
        "responses": {
          "200": {
            "description": "JSON Schema document",
            "content": {
              "application/schema+json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Schema or version not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "../../schemas/error.json"
                }
              }
            }
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "Discovery",
      "description": "Protocol discovery"
    },
    {
      "name": "Commands",
      "description": "io.best.agents.commands — Discover command types (GET /commands), fetch versioned schemas (GET /commands/{schema}/{version}), and send commands (POST /commands)"
    },
    {
      "name": "Events",
      "description": "io.best.agents.events — Query published events (GET /events), subscribe to live events (GET /events/stream), and fetch versioned event schemas (GET /events/{schema}/{version})"
    },
    {
      "name": "Queries",
      "description": "io.best.agents.queries — Discover query types (GET /queries), fetch versioned query schemas (GET /queries/{schema}/{version}), and execute queries (GET /queries/{schema})"
    },
    {
      "name": "Workflows",
      "description": "io.best.agents.workflows — List published workflow recipes (GET /workflows) and fetch one recipe with its ordered steps (GET /workflows/{id}). Strictly descriptive: the caller drives every step."
    }
  ],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "description": "Bearer token authentication. Include the token in the Authorization header: Authorization: Bearer <token>. Required when the discovery manifest's authentication.type is 'bearer'. Omit if authentication.type is 'none'."
      },
      "apiKeyHeader": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "API key passed as a request header. Used when authentication.type is 'apiKey' and authentication.in is 'header'."
      },
      "apiKeyQuery": {
        "type": "apiKey",
        "in": "query",
        "name": "api_key",
        "description": "API key passed as a query parameter. Used when authentication.type is 'apiKey' and authentication.in is 'query'."
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Authentication required or credentials invalid. The endpoint requires authentication as declared in the discovery manifest's authentication block.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "../../schemas/error.json"
            },
            "example": {
              "error": {
                "code": "UNAUTHORIZED",
                "message": "Bearer token is missing or invalid",
                "details": {}
              }
            }
          }
        }
      }
    }
  }
}
