openapi: 3.1.0
info:
  title: dss-aigateway
  version: 0.1.0
  description: |
    OpenAI-compatible LLM gateway.

    Authenticate with a virtual key issued by your gateway admin:

        Authorization: Bearer sk_...

    The gateway accepts the OpenAI request shape, maps `model` (a logical name)
    to a configured upstream provider, and forwards a normalized subset of
    parameters. Anything not listed in the request schemas below is dropped
    silently.

servers:
  - url: /
    description: Gateway

security:
  - bearerAuth: []

tags:
  - name: System
    description: Liveness + model discovery.
  - name: Chat
    description: OpenAI Chat Completions API (`/v1/chat/completions`).
  - name: Completions
    description: Legacy text completions (`/v1/completions`).
  - name: Responses
    description: OpenAI Responses API (`/v1/responses`).
  - name: Embeddings
    description: OpenAI Embeddings API (`/v1/embeddings`).

paths:
  /health:
    get:
      tags: [System]
      summary: Liveness probe
      security: []
      responses:
        '200':
          description: Gateway is running.
          content:
            application/json:
              schema:
                type: object
                properties:
                  ok:
                    type: boolean

  /v1/models:
    get:
      tags: [System]
      summary: List enabled logical models
      description: |
        Returns the distinct logical model names of every enabled mapping. Use
        these as the `model` field in your requests.
      security: []
      responses:
        '200':
          description: Model list.
          content:
            application/json:
              schema:
                type: object
                properties:
                  object:
                    type: string
                    const: list
                  data:
                    type: array
                    items:
                      type: object
                      properties:
                        id:
                          type: string
                        object:
                          type: string
                          const: model
                        owned_by:
                          type: string

  /v1/chat/completions:
    post:
      tags: [Chat]
      summary: Create a chat completion
      description: |
        Streaming and non-streaming chat completions. Internally the gateway
        always streams from the upstream so it can capture token usage; if you
        request `stream: false` the gateway aggregates and returns a single
        JSON response.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionRequest'
      responses:
        '200':
          description: "Chat completion (or SSE stream when `stream: true`)."
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatCompletion'
            text/event-stream:
              schema:
                type: string
                description: "SSE stream of `chat.completion.chunk` events terminated by `data: [DONE]`."
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '502':
          $ref: '#/components/responses/UpstreamError'

  /v1/completions:
    post:
      tags: [Completions]
      summary: Create a (legacy) text completion
      description: |
        OpenAI's legacy text-completions endpoint, served by routing through a
        chat-completions upstream under the hood. Tool calling is **not**
        supported on this endpoint.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/LegacyCompletionRequest'
      responses:
        '200':
          description: "Text completion (or SSE stream when `stream: true`)."
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LegacyCompletion'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '502':
          $ref: '#/components/responses/UpstreamError'

  /v1/responses:
    post:
      tags: [Responses]
      summary: Create a response (OpenAI Responses API)
      description: |
        OpenAI's Responses API, with a normalized subset of parameters. The
        gateway translates Responses-shape fields (`max_output_tokens`,
        `text.format`, flat-tools, `reasoning.effort`, …) to the upstream
        chat-completions wire shape.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ResponsesRequest'
      responses:
        '200':
          description: "Response object (or SSE stream of typed events when `stream: true`)."
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResponsesResult'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '502':
          $ref: '#/components/responses/UpstreamError'

  /v1/embeddings:
    post:
      tags: [Embeddings]
      summary: Create embeddings
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EmbeddingsRequest'
      responses:
        '200':
          description: Embedding vectors.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EmbeddingsResult'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '502':
          $ref: '#/components/responses/UpstreamError'

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: virtual-key (sk_…)

  responses:
    BadRequest:
      description: Malformed request body or missing required field.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    Unauthorized:
      description: Missing or invalid virtual key.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    Forbidden:
      description: The virtual key is not allowed to use this model.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    NotFound:
      description: The requested model does not exist or is disabled.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    UpstreamError:
      description: The upstream provider returned an error.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'

  schemas:
    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
            type:
              type: string
            code:
              type: string

    # ─── chat completions ────────────────────────────────────────────────────
    ChatMessage:
      type: object
      required: [role]
      properties:
        role:
          type: string
          enum: [system, user, assistant, tool]
        content:
          oneOf:
            - type: string
            - type: 'null'
        name:
          type: string
        tool_call_id:
          type: string
          description: "Required on `role: tool` messages."
        tool_calls:
          type: array
          description: Set on assistant messages that invoked tools.
          items:
            $ref: '#/components/schemas/ChatToolCall'

    ChatToolCall:
      type: object
      required: [id, type, function]
      properties:
        id:
          type: string
        type:
          type: string
          const: function
        function:
          type: object
          required: [name, arguments]
          properties:
            name:
              type: string
            arguments:
              type: string
              description: JSON-encoded arguments.

    ChatToolDef:
      type: object
      required: [type, function]
      properties:
        type:
          type: string
          const: function
        function:
          type: object
          required: [name]
          properties:
            name:
              type: string
            description:
              type: string
            parameters:
              type: object
              description: JSON Schema for the function arguments.
            strict:
              type: boolean

    ChatToolChoice:
      oneOf:
        - type: string
          enum: [auto, none, required]
        - type: object
          required: [type, function]
          properties:
            type:
              type: string
              const: function
            function:
              type: object
              required: [name]
              properties:
                name:
                  type: string

    ChatResponseFormat:
      oneOf:
        - type: object
          required: [type]
          properties:
            type:
              type: string
              const: text
        - type: object
          required: [type]
          properties:
            type:
              type: string
              const: json_object
        - type: object
          required: [type, json_schema]
          properties:
            type:
              type: string
              const: json_schema
            json_schema:
              type: object
              required: [name, schema]
              properties:
                name:
                  type: string
                description:
                  type: string
                schema:
                  type: object
                strict:
                  type: boolean

    ChatCompletionRequest:
      type: object
      required: [model, messages]
      properties:
        model:
          type: string
          description: Logical model name (see `GET /v1/models`).
        messages:
          type: array
          minItems: 1
          items:
            $ref: '#/components/schemas/ChatMessage'
        stream:
          type: boolean
          default: false
        temperature:
          type: number
        top_p:
          type: number
        max_tokens:
          type: integer
        max_completion_tokens:
          type: integer
          description: Alias for `max_tokens` (used by reasoning models).
        stop:
          oneOf:
            - type: string
            - type: array
              items:
                type: string
        seed:
          type: integer
        presence_penalty:
          type: number
        frequency_penalty:
          type: number
        user:
          type: string
        reasoning_effort:
          type: string
          enum: [minimal, low, medium, high]
        tools:
          type: array
          items:
            $ref: '#/components/schemas/ChatToolDef'
        tool_choice:
          $ref: '#/components/schemas/ChatToolChoice'
        parallel_tool_calls:
          type: boolean
        response_format:
          $ref: '#/components/schemas/ChatResponseFormat'

    ChatCompletion:
      type: object
      properties:
        id:
          type: string
        object:
          type: string
          const: chat.completion
        created:
          type: integer
        model:
          type: string
        choices:
          type: array
          items:
            type: object
            properties:
              index:
                type: integer
              message:
                $ref: '#/components/schemas/ChatMessage'
              finish_reason:
                type: string
                enum: [stop, length, content_filter, tool_calls]
        usage:
          $ref: '#/components/schemas/ChatUsage'

    ChatUsage:
      type: object
      properties:
        prompt_tokens:
          type: integer
        completion_tokens:
          type: integer
        total_tokens:
          type: integer

    # ─── legacy completions ─────────────────────────────────────────────────
    LegacyCompletionRequest:
      type: object
      required: [model, prompt]
      properties:
        model:
          type: string
        prompt:
          oneOf:
            - type: string
            - type: array
              items:
                type: string
        stream:
          type: boolean
          default: false
        echo:
          type: boolean
          description: Echo the prompt back in the response.
        temperature:
          type: number
        top_p:
          type: number
        max_tokens:
          type: integer
        stop:
          oneOf:
            - type: string
            - type: array
              items:
                type: string
        seed:
          type: integer
        presence_penalty:
          type: number
        frequency_penalty:
          type: number
        user:
          type: string

    LegacyCompletion:
      type: object
      properties:
        id:
          type: string
        object:
          type: string
          const: text_completion
        created:
          type: integer
        model:
          type: string
        choices:
          type: array
          items:
            type: object
            properties:
              text:
                type: string
              index:
                type: integer
              finish_reason:
                type: string
        usage:
          $ref: '#/components/schemas/ChatUsage'

    # ─── responses ──────────────────────────────────────────────────────────
    ResponsesInputItem:
      oneOf:
        - type: object
          required: [role, content]
          properties:
            type:
              type: string
              const: message
            role:
              type: string
              enum: [system, developer, user, assistant]
            content:
              oneOf:
                - type: string
                - type: array
                  items:
                    type: object
                    properties:
                      type:
                        type: string
                      text:
                        type: string
        - type: object
          required: [type, call_id, name, arguments]
          properties:
            type:
              type: string
              const: function_call
            call_id:
              type: string
            name:
              type: string
            arguments:
              type: string
        - type: object
          required: [type, call_id, output]
          properties:
            type:
              type: string
              const: function_call_output
            call_id:
              type: string
            output:
              type: string

    ResponsesToolDef:
      type: object
      required: [type, name]
      description: Flat tool shape (no nested `function` field).
      properties:
        type:
          type: string
          const: function
        name:
          type: string
        description:
          type: string
        parameters:
          type: object
        strict:
          type: boolean

    ResponsesToolChoice:
      oneOf:
        - type: string
          enum: [auto, none, required]
        - type: object
          required: [type, name]
          properties:
            type:
              type: string
              const: function
            name:
              type: string

    ResponsesTextFormat:
      type: object
      properties:
        format:
          oneOf:
            - type: object
              required: [type]
              properties:
                type:
                  type: string
                  enum: [text, json_object]
            - type: object
              required: [type, name, schema]
              properties:
                type:
                  type: string
                  const: json_schema
                name:
                  type: string
                description:
                  type: string
                schema:
                  type: object
                strict:
                  type: boolean

    ResponsesRequest:
      type: object
      required: [model, input]
      properties:
        model:
          type: string
        input:
          oneOf:
            - type: string
            - type: array
              items:
                $ref: '#/components/schemas/ResponsesInputItem'
        instructions:
          type: string
          description: System-style instructions prepended to the conversation.
        stream:
          type: boolean
          default: false
        temperature:
          type: number
        top_p:
          type: number
        max_output_tokens:
          type: integer
          description: Mapped to `max_tokens` upstream.
        seed:
          type: integer
        user:
          type: string
        parallel_tool_calls:
          type: boolean
        tools:
          type: array
          items:
            $ref: '#/components/schemas/ResponsesToolDef'
        tool_choice:
          $ref: '#/components/schemas/ResponsesToolChoice'
        text:
          $ref: '#/components/schemas/ResponsesTextFormat'
        reasoning:
          type: object
          properties:
            effort:
              type: string
              enum: [minimal, low, medium, high]

    ResponsesResult:
      type: object
      properties:
        id:
          type: string
        object:
          type: string
          const: response
        created_at:
          type: integer
        status:
          type: string
          enum: [in_progress, completed, failed]
        model:
          type: string
        output:
          type: array
          items:
            type: object
        output_text:
          type: string
        usage:
          type: object
          properties:
            input_tokens:
              type: integer
            output_tokens:
              type: integer
            total_tokens:
              type: integer

    # ─── embeddings ─────────────────────────────────────────────────────────
    EmbeddingsRequest:
      type: object
      required: [model, input]
      properties:
        model:
          type: string
        input:
          oneOf:
            - type: string
            - type: array
              items:
                type: string
        encoding_format:
          type: string
          enum: [float, base64]
        dimensions:
          type: integer
        user:
          type: string

    EmbeddingsResult:
      type: object
      properties:
        object:
          type: string
          const: list
        data:
          type: array
          items:
            type: object
            properties:
              object:
                type: string
                const: embedding
              index:
                type: integer
              embedding:
                type: array
                items:
                  type: number
        model:
          type: string
        usage:
          type: object
          properties:
            prompt_tokens:
              type: integer
            total_tokens:
              type: integer
