openapi: 3.1.0
info:
  title: Sparkle External RAG API
  version: "1.0.0"
  summary: Company-scoped RAG query for server-to-server and AI agents
  description: |
    Authenticate with a per-company security key issued by a Sparkle super admin
    (organization detail → 外部連携セキュリティーキー).

    Search scope is **all published documents** for that company. Access groups
    are not applied. Each successful query counts toward the company's monthly
    question quota. Portal conversation history is not created.

    Machine cheat sheet: https://sparkle.956.jp/llms-api.txt
    Human help: https://sparkle.956.jp/help_admin.html#external-api
  contact:
    name: 株式会社956
    url: https://www.956.jp/contact
  license:
    name: Proprietary
    identifier: LicenseRef-956-Sparkle
servers:
  - url: https://sparkle.956.jp
    description: Production
security:
  - BearerAuth: []
  - SecurityKeyHeader: []
tags:
  - name: rag
    description: Retrieval-augmented generation against company PDFs
  - name: documents
    description: Published PDF preview for cited document_id values
paths:
  /api/v1/external/rag/query:
    post:
      tags: [rag]
      operationId: queryRag
      summary: Ask a question against the company knowledge base
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/RagQueryRequest"
            example:
              question: ホワイトボードの予約はどう作りますか？
              include_sources: true
              history:
                - role: user
                  content: 前回の質問
                - role: assistant
                  content: 前回の回答
      responses:
        "200":
          description: Answer generated (including empty-corpus fallback copy)
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/RagQueryResponse"
        "401":
          $ref: "#/components/responses/Error"
        "403":
          $ref: "#/components/responses/Error"
        "422":
          $ref: "#/components/responses/Error"
        "429":
          $ref: "#/components/responses/Error"
        "502":
          $ref: "#/components/responses/Error"
  /api/v1/external/documents/{id}/preview:
    get:
      tags: [documents]
      operationId: previewDocument
      summary: Stream a published PDF (inline)
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
          description: document_id from a RAG source
      responses:
        "200":
          description: PDF bytes
          content:
            application/pdf:
              schema:
                type: string
                format: binary
        "401":
          $ref: "#/components/responses/Error"
        "403":
          $ref: "#/components/responses/Error"
        "404":
          $ref: "#/components/responses/Error"
components:
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      description: Company security key as Bearer token
    SecurityKeyHeader:
      type: apiKey
      in: header
      name: X-Sparkle-Security-Key
  schemas:
    RagQueryRequest:
      type: object
      additionalProperties: true
      properties:
        question:
          type: string
          description: User question. Also accepted as rag.question
        include_sources:
          type: boolean
          default: true
        history:
          type: array
          items:
            $ref: "#/components/schemas/ChatTurn"
        rag:
          type: object
          description: Alternate nested payload
          properties:
            question:
              type: string
            include_sources:
              type: boolean
            history:
              type: array
              items:
                $ref: "#/components/schemas/ChatTurn"
    ChatTurn:
      type: object
      required: [content]
      properties:
        role:
          type: string
          description: Typically user or assistant
        content:
          type: string
    RagQueryResponse:
      type: object
      required: [answer, sources, company_id, company_slug]
      properties:
        answer:
          type: string
        sources:
          type: array
          items:
            $ref: "#/components/schemas/RagSource"
        company_id:
          type: integer
        company_slug:
          type: string
    RagSource:
      type: object
      properties:
        document_id:
          type: integer
        title:
          type: string
        excerpt:
          type: string
        document_vectorized_at:
          type: string
          format: date-time
          nullable: true
    ApiError:
      type: object
      required: [error, code]
      properties:
        error:
          type: string
        code:
          type: string
          enum:
            - unauthorized
            - company_inactive
            - question_blank
            - invalid_request
            - question_limit
            - rate_limited
            - llm_unavailable
            - not_found
            - internal_error
  responses:
    Error:
      description: Error body
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ApiError"
