openapi: 3.1.0
info:
  title: Cube Cloud Chat API
  version: 1.0.0
  description: |-
    Real-time streaming conversations with Cube Cloud AI agents for analytics and
    data exploration. Copy the exact Chat API URL from your agent settings
    (Admin → Agents → Chat API URL), or — to talk to a deployment's Auto agent —
    from the deployment's Settings → API credentials → Chat API tab.

    The deployment-scoped base URL accepts only Cube token authentication; an API
    key carries no agent, so it cannot resolve the Auto agent.
servers:
  - url: https://ai.{cloudRegion}.cubecloud.dev/api/v1/public/{accountName}/agents/{agentId}
    description: >-
      Agent-scoped base URL — talks to that specific agent. Copy the exact URL from your agent
      settings (Admin → Agents → Chat API URL). Accepts an API key or a Cube token.
    variables:
      cloudRegion:
        default: gcp-us-central1
        description: Cloud region identifier
      accountName:
        default: your-account
        description: Your Cube Cloud account/tenant name
      agentId:
        default: '1'
        description: AI agent identifier (Admin → Agents)
  - url: https://ai.{cloudRegion}.cubecloud.dev/api/v1/public/{accountName}/deployments/{deploymentId}
    description: >-
      Deployment-scoped base URL — talks to the deployment's Auto agent. Copy the exact URL from
      Settings → API credentials → Chat API. Accepts only Cube token authentication, which is
      disabled by default (Settings → Configuration → Use Cube authentication for Chat and
      embedding APIs).
    variables:
      cloudRegion:
        default: gcp-us-central1
        description: Cloud region identifier
      accountName:
        default: your-account
        description: Your Cube Cloud account/tenant name
      deploymentId:
        default: '9'
        description: Deployment identifier (from the deployment URL, e.g. /d/9)
security:
  - apiKey: []
  - cubeToken: []
tags:
  - name: Chat
paths:
  /chat/stream-chat-state:
    post:
      description: >-
        Real-time streaming conversation with Cube's AI agents for analytics and data exploration.


        The response is streamed as newline-delimited JSON (each line is a standalone JSON object

        terminated by `\n`) and served with `Content-Type: application/json`. Each line represents a

        chat message, a state update, or an error object. Assistant content arrives incrementally —

        updates to a previously emitted message carry the same `id` with `isDelta: true`.


        Provide `input` to send a new message. Omit `input` (with a `chatId`) to retrieve the
        current

        thread state. If `chatId` is omitted a new thread is created and its id is returned on the

        `__cutoff__` message.


        Special sentinel messages:

        - `__cutoff__` — initial marker emitted before new content; also carries the thread `chatId`
        and `isStreaming` under `state`.

        - `__state__` — final thread snapshot emitted at the end, carrying the full `{ messages:
        [...] }` state.


        Errors mid-stream are emitted as a standalone line `{ "error": "<message>" }` rather than a
        non-200 status.


        Finding the final answer: filter assistant messages where `graphPath[0] === "final"` and

        `graphPath.length <= 2`; the last such message is the final answer.


        Idempotency: provide a client-generated `messageId` in the format `<timestamp>-message` (a
        13+ digit

        Unix-millisecond timestamp). If a request with the same `messageId` arrives while a stream
        for that

        message is still active on the thread, the API resubscribes the new request to the existing
        stream

        instead of starting a new one.
      operationId: streamChatState
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatRequest'
        description: >-
          Chat request. Provide `input` to send a new message, or omit it together with a `chatId`
          to retrieve the current thread state.
        required: false
      responses:
        '200':
          content:
            application/json:
              examples:
                assistantDelta:
                  summary: Assistant response start (delta)
                  value:
                    content: ''
                    graphPath:
                      - cube_data_analyst_agent
                    id: cdfe1a84-08d7-40b9-8b1c-e7e3a698647e
                    isDelta: true
                    isInProcess: true
                    role: assistant
                    sort: 2
                error:
                  summary: Error line
                  value:
                    error: 'BadRequestError: Bad branch'
                finalState:
                  summary: Final state (__state__)
                  value:
                    id: __state__
                    isDelta: false
                    role: assistant
                    state:
                      messages: []
                initialState:
                  summary: Initial state (__cutoff__)
                  value:
                    id: __cutoff__
                    role: assistant
                    sort: 0
                    state:
                      chatId: a1b2c3d4-08d7-40b9-8b1c-e7e3a698647e
                      isStreaming: false
                toolCall:
                  summary: Tool call (cubeMeta)
                  value:
                    graphPath:
                      - cube_data_analyst_agent
                      - tools
                    id: 4849adb2-b55d-4afe-946b-fc117bcadaf5
                    isInProcess: true
                    role: assistant
                    toolCall:
                      input: '{"searchQuery":"revenue trends"}'
                      name: cubeMeta
                userEcho:
                  summary: User message echo
                  value:
                    content: Show me revenue trends for the last 6 months
                    id: 1732512345678-message
                    isDelta: false
                    role: user
                    sort: 1
              schema:
                properties:
                  content:
                    description: >-
                      Message content. Streamed incrementally for assistant messages when `isDelta`
                      is true.
                    type: string
                  error:
                    description: >-
                      Present only on error lines (`{ "error": "<message>" }`). When set, the line
                      carries no message fields.
                    type: string
                  graphPath:
                    description: Agent graph node path, e.g. `["cube_data_analyst_agent"]` or `["final"]`.
                    items:
                      type: string
                    type: array
                  id:
                    description: >-
                      Unique message identifier. Sentinel values: `__cutoff__` (initial marker,
                      carries the chatId under `state`) and `__state__` (final thread state
                      snapshot). Absent on error lines.
                    type: string
                  isDelta:
                    description: >-
                      Whether this is an incremental update to a previously sent message with the
                      same id.
                    type: boolean
                  isInProcess:
                    description: Whether the message is still being generated.
                    type: boolean
                  role:
                    enum:
                      - user
                      - assistant
                    type: string
                  sort:
                    description: Message ordering sequence.
                    type: number
                  state:
                    description: >-
                      Streaming state. Carries `{ isStreaming, chatId }` on the `__cutoff__` message
                      and `{ messages: [...] }` on the `__state__` message.
                    type: object
                  thinking:
                    description: Agent's internal reasoning. Only present in development mode.
                    type: string
                  toolCall:
                    description: Present when the message represents a tool invocation or its result.
                    properties:
                      input:
                        description: JSON-encoded tool input.
                        type: string
                      name:
                        description: Tool name, e.g. `cubeMeta` or `cubeSqlApi`.
                        type: string
                      result:
                        description: >-
                          JSON-encoded tool result. Absent until the tool finishes; on failure
                          contains a JSON object with an `error` field.
                        type: string
                    type: object
                type: object
            text/html; charset=utf-8: {}
          description: >-
            Newline-delimited stream of chat message / state / error objects (served as
            `Content-Type: application/json`). Each line is a JSON object of the schema below.
      security:
        - apiKey: []
        - cubeToken: []
      summary: Stream chat state
      tags:
        - Chat
  /chat/abort:
    post:
      description: >-
        Stops an in-progress chat stream — for example, when the user cancels a request or
        navigates away while the agent is still generating a response.


        The abort URL is derived from the Chat API URL by replacing `/stream-chat-state` with
        `/abort`.


        A successful cancellation is indicated solely by the `204 No Content` status — the
        response has no body. Note that the aborted `stream-chat-state` request itself does not
        fail with an HTTP error: the streaming response already returned `200 OK` when the stream
        opened, so treat this endpoint's `204` as the confirmation that the stream was aborted.
      operationId: abortChat
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AbortChatRequest'
        description: Abort request identifying the chat thread to stop.
        required: true
      responses:
        '204':
          description: Chat streaming was aborted successfully. The response has no body.
        '403':
          description: The authenticated user does not own the specified chat thread.
        '404':
          description: The specified `chatId` does not exist.
      security:
        - apiKey: []
        - cubeToken: []
      summary: Abort chat stream
      tags:
        - Chat
components:
  securitySchemes:
    apiKey:
      type: apiKey
      in: header
      name: Authorization
      description: 'API key authentication. Send `Authorization: Api-Key <YOUR_API_KEY>`.'
    cubeToken:
      type: apiKey
      in: header
      name: X-Cube-Auth-Token
      description: >-
        Cube token authentication. Send your deployment's own Cube token (JWT) in the
        `X-Cube-Auth-Token` header. The token must be valid for the agent's deployment, include
        an `externalId` (or `sub`) claim identifying the external user, and carry the `chat`
        scope in its `scope` claim (an array). Disabled by default — enable it in your
        deployment settings.
  schemas:
    AbortChatRequest:
      properties:
        chatId:
          description: >-
            Chat thread ID to abort. Must match the `chatId` of an active or recently completed
            chat session.
          type: string
        sessionSettings:
          $ref: '#/components/schemas/SessionSettingsDto'
      required:
        - chatId
      type: object
    ChatRequest:
      properties:
        activeBranchName:
          description: Development branch name to run queries against. Uses the main branch if omitted.
          type: string
        chatId:
          description: >-
            Chat thread ID. If omitted, a new thread is created automatically. Should match the
            previously returned `chatId` carried on the `__cutoff__` message.
          type: string
        images:
          items:
            $ref: '#/components/schemas/ImageAttachmentDto'
          type: array
        input:
          description: >-
            User's message/question. If omitted, returns the current thread state for the provided
            `chatId`.
          type: string
        isDefaultBranch:
          description: Whether `activeBranchName` is the default branch. Defaults to `true` when not set.
          type: boolean
        messageId:
          description: >-
            Client-provided identifier in the format `<timestamp>-message` (13+ digit Unix
            milliseconds). Makes requests idempotent.
          pattern: ^\d{13,}-message$
          type: string
        sessionSettings:
          $ref: '#/components/schemas/SessionSettingsDto'
      type: object
    ImageAttachmentDto:
      properties:
        data:
          type: string
        mimeType:
          type: string
        name:
          type: string
      required:
        - mimeType
        - data
      type: object
    SessionSettingsDto:
      properties:
        email:
          description: User's email address.
          type: string
        externalId:
          description: >-
            Unique identifier for an external user. Must be lowercase and trimmed (no
            leading/trailing whitespace). Either `externalId` or `internalId` is required.
          type: string
        groups:
          description: Array of group names the user belongs to. Not allowed together with `internalId`.
          items:
            type: string
          type: array
        internalId:
          description: >-
            Username of an internal Cube Cloud user, matched lowercased and trimmed; the user must
            already exist and must not be external. An email is a common username form but is not
            required. Either `externalId` or `internalId` is required.
          type: string
        publicDashboardId:
          type: string
        roles:
          items:
            type: string
          type: array
        securityContext:
          description: Custom security context object for Cube queries. Not allowed together with `internalId`.
          type: object
        userAttributes:
          description: >-
            Array of `{ name, value }` pairs for row-level security. Not allowed together with
            `internalId`.
          items: {}
          type: array
      type: object
