openapi: 3.1.0
info:
  title: Askavell Bot API
  version: 1.0.0
  description: Call from a backend. CORS is intentionally disabled for browser clients.
paths:
  /api/v1/bots/{botId}/chat:
    post:
      operationId: chat
      summary: Ask a published bot
      description: >
        Reuse only server-issued sessions from the same bot and key. Sessions expire or are
        invalidated when access changes. The answer in complete replaces any provisional delta.
        A disconnect cancels upstream work where supported. An idempotency key prevents concurrent
        duplication and returns 409 after an accepted attempt within one hour. Retrying with a
        different key can start a new upstream call.
      security:
        - BotKey: []
      parameters:
        - name: botId
          in: path
          required: true
          schema: { type: string, format: uuid }
        - name: Idempotency-Key
          in: header
          schema: { type: string, minLength: 8, maxLength: 120, pattern: '^[A-Za-z0-9._:-]+$' }
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/ChatRequest' }
      responses:
        '200':
          description: Answer or configured fallback. If streaming, a failure can arrive as a terminal error event.
          headers:
            X-Request-Id: { $ref: '#/components/headers/RequestId' }
            X-RateLimit-Limit: { $ref: '#/components/headers/IntegerHeader' }
            X-RateLimit-Remaining: { $ref: '#/components/headers/IntegerHeader' }
            X-RateLimit-Reset:
              description: Unix seconds when the indicated rate bucket resets.
              schema: { type: integer }
          content:
            application/json:
              schema: { $ref: '#/components/schemas/ChatResult' }
            text/event-stream:
              schema: { type: string }
              description: >
                An event contains an event name and a JSON data line. Events are metadata
                (requestId, sessionId, optional clientReference), delta (text, provisional=true),
                citations (citations array), usage (Usage), complete (ChatResult), and error (Error).
                Only complete is authoritative. An error terminates the stream and callers should
                discard provisional text. Comment keep-alives may be interleaved.
        '400': { $ref: '#/components/responses/Error' }
        '404':
          description: Unavailable bot, inactive release, or absent, invalid, or revoked integration key.
          headers: { X-Request-Id: { $ref: '#/components/headers/RequestId' } }
          content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } }
        '409': { $ref: '#/components/responses/Error' }
        '413': { $ref: '#/components/responses/Error' }
        '422': { $ref: '#/components/responses/Error' }
        '429':
          description: An app rate or concurrency limit or the connected provider quota applies.
          headers:
            X-Request-Id: { $ref: '#/components/headers/RequestId' }
            X-RateLimit-Limit: { $ref: '#/components/headers/IntegerHeader' }
            X-RateLimit-Remaining: { $ref: '#/components/headers/IntegerHeader' }
            X-RateLimit-Reset: { $ref: '#/components/headers/IntegerHeader' }
            Retry-After: { $ref: '#/components/headers/IntegerHeader' }
          content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } }
        '499': { $ref: '#/components/responses/Error' }
        '500': { $ref: '#/components/responses/Error' }
        '502': { $ref: '#/components/responses/Error' }
        '503': { $ref: '#/components/responses/Error' }
        '504': { $ref: '#/components/responses/Error' }
components:
  securitySchemes:
    BotKey:
      type: http
      scheme: bearer
      description: Bot-scoped platform API key generated in Integrations.
  headers:
    RequestId:
      description: Correlation ID for the platform request.
      schema: { type: string, format: uuid }
    IntegerHeader:
      schema: { type: integer }
  responses:
    Error:
      description: Request validation, session conflict, provider failure, or service configuration error.
      headers: { X-Request-Id: { $ref: '#/components/headers/RequestId' } }
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
  schemas:
    ChatRequest:
      type: object
      additionalProperties: false
      required: [message]
      properties:
        message: { type: string, minLength: 1, maxLength: 4000 }
        sessionId: { type: string, format: uuid }
        stream: { type: boolean, default: false }
        clientReference: { type: string, maxLength: 100 }
    Citation:
      type: object
      required: [id, title]
      properties:
        id: { type: integer }
        title: { type: string }
        page: { type: integer }
        url: { type: string, format: uri }
    Usage:
      type: object
      required: [latencyMs]
      properties:
        latencyMs: { type: integer }
        inputTokens: { type: integer, description: Generation token count when reported by the provider. }
        outputTokens: { type: integer, description: Generation token count when reported by the provider. }
    ChatResult:
      type: object
      required: [requestId, sessionId, answer, outcome, citations, usage]
      properties:
        requestId: { type: string, format: uuid }
        sessionId: { type: string, format: uuid }
        clientReference: { type: string }
        answer: { type: string }
        outcome: { type: string, enum: [answered, fallback] }
        citations:
          type: array
          items: { $ref: '#/components/schemas/Citation' }
        usage: { $ref: '#/components/schemas/Usage' }
        supportUrl: { type: string, format: uri }
    Error:
      type: object
      required: [requestId, error]
      properties:
        requestId: { type: string, format: uuid }
        error:
          type: object
          required: [code, message]
          properties:
            code: { type: string }
            message: { type: string }
