# yaml-language-server: $schema=https://spec.openapis.org/oas/3.1/schema/2022-10-07
openapi: 3.1.0
info:
  title: AI API
  version: main
  # TODO termsOfService: https://aish.li/terms/
  contact:
    email: support@aish.li

externalDocs:
  description: Find out more about AIsh.li!
  url: https://aish.li

servers:
  - url: http://localhost:7070
    description: Local development
  - url: https://api.aish.li
    description: Production

tags:
  - name: AI
    description: Artificial Intelligence platform
    externalDocs:
      description: Find out more
      url: https://aish.li # TODO https://aish.li/ai
  - name: Auth
    description: Operations related to user authentication and API Keys

paths:
  /v1/ai/models:
    get:
      operationId: List AI Models
      tags:
        - AI
      summary: List all supported AI Models with their ID and label
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Models'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /v1/responses:
    post:
      operationId: createResponse
      tags:
        - AI
      summary: Create an agent response
      description: |
        Creates an agent response using an OpenAI-compatible Responses API.
        Supports both non-streaming (JSON) and streaming (SSE) response modes.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateResponseBody'
            examples:
              simple:
                value:
                  model: demo/gemini
                  input: 'What is 2+2?'
              streaming:
                value:
                  model: demo/gemini
                  input: 'What is 2+2?'
                  stream: true
              withInstructions:
                value:
                  model: demo/gemini
                  instructions: 'You are a helpful assistant. Be concise.'
                  input: 'Explain quantum computing'
                  temperature: 0.7
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResponseResource'
              examples:
                simple:
                  value:
                    model: 'demo/gemini'
                    output:
                      - type: message
                        role: assistant
                        status: completed
                        content:
                          - type: text
                            text: '4'
            text/event-stream:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/ResponseCreatedStreamingEvent'
                  - $ref: '#/components/schemas/ResponseOutputTextDeltaStreamingEvent'
                  - $ref: '#/components/schemas/ResponseCompletedStreamingEvent'
                  - $ref: '#/components/schemas/ErrorStreamingEvent'
                discriminator:
                  propertyName: type
              examples:
                streaming:
                  value:
                    - type: created
                      created_at: 1741476777
                    - type: output
                      delta:
                        type: text
                        text: '4'
                    - type: completed
                      completed_at: 1741476778
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /v1/ai/sessions:
    get:
      operationId: listSessions
      tags:
        - AI
      summary: List AI sessions
      parameters:
        - name: limit
          in: query
          required: false
          schema:
            type: integer
            default: 100
        - name: pageToken
          in: query
          required: false
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SessionsPage'
              examples:
                simple:
                  value:
                    items:
                      - id: 'alice/session-123'
                        agent: 'demo/gemini'
                        title: 'Greeting'
                        lastMessageAt: '2026-04-19T11:00:00Z'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /v1/ai/sessions/{id}:
    get:
      operationId: getSession
      tags:
        - AI
      summary: Get an AI session by ID
      parameters:
        - name: id
          in: path
          required: true
          description: The session ID
          schema:
            type: string
          example: alice/session-123
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Session'
              examples:
                simple:
                  value:
                    id: 'alice/session-123'
                    agent: 'demo/gemini'
                    messages:
                      - ts: '2026-04-19T11:00:00Z'
                        role: 'model'
                        parts:
                          - text: 'Hello'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '402':
          $ref: '#/components/responses/PaymentRequired'
        '404':
          description: Session not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiProblem'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /v1/ai/agents:
    get:
      operationId: listAgents
      tags:
        - AI
      summary: List AI agents
      parameters:
        - name: limit
          in: query
          required: false
          schema:
            type: integer
            default: 100
        - name: pageToken
          in: query
          required: false
          schema:
            type: string
        - name: hl
          in: query
          required: false
          description: Host language / locale code (e.g. 'fr', 'de', 'it', 'en') to localize content, taking precedence over Accept-Language
          schema:
            type: string
        - name: Accept-Language
          in: header
          required: false
          description: Standard HTTP Accept-Language header (e.g. 'fr-CH, fr;q=0.9, en;q=0.8') for content localization fallback
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgentsPage'
              examples:
                simple:
                  value:
                    items:
                      - id: 'alice/my-agent'
                        name: 'My Agent'
                        model: 'gemini-3.8-flash'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '500':
          $ref: '#/components/responses/InternalServerError'

    post:
      operationId: storeAgent
      tags:
        - AI
      summary: Store/update an AI agent
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AgentConfig'
      responses:
        '204':
          description: Agent stored
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /v1/ai/agents/{id}:
    get:
      operationId: getAgent
      tags:
        - AI
      summary: Get an AI agent by ID
      parameters:
        - name: id
          in: path
          required: true
          description: The agent ID
          schema:
            type: string
          example: alice/my-agent
        - name: hl
          in: query
          required: false
          description: Host language / locale code (e.g. 'fr', 'de', 'it', 'en') to localize content, taking precedence over Accept-Language
          schema:
            type: string
        - name: Accept-Language
          in: header
          required: false
          description: Standard HTTP Accept-Language header (e.g. 'fr-CH, fr;q=0.9, en;q=0.8') for content localization fallback
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgentConfig'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          description: Agent not found
        '500':
          $ref: '#/components/responses/InternalServerError'

    delete:
      operationId: deleteAgent
      tags:
        - AI
      summary: Delete (archive) an AI agent
      parameters:
        - name: id
          in: path
          required: true
          description: The agent ID
          schema:
            type: string
          example: alice/my-agent
      responses:
        '204':
          description: Agent deleted
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          description: Agent not found
        '500':
          $ref: '#/components/responses/InternalServerError'

  /v1/ai/agents/{id}/edit:
    get:
      operationId: getAgentForEdit
      tags:
        - AI
      summary: Get an AI agent configuration by ID for authoring (includes raw localized maps and tests)
      parameters:
        - name: id
          in: path
          required: true
          description: The agent ID
          schema:
            type: string
          example: alice/my-agent
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgentConfig'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          description: Agent not found
        '500':
          $ref: '#/components/responses/InternalServerError'

  # /v1/form/{session}:
  #   post:
  #     operationId: startFormSession
  #     tags:
  #       - Forms
  #     summary: Starts a new AI Session for the Form
  #     parameters:
  #       - name: session
  #         in: path
  #         required: true
  #         description: The unique identifier for the form session
  #         schema:
  #           type: string
  #     responses:
  #       200:
  #         description: OK
  #         content:
  #           application/json:
  #             schema:
  #               type: object

  /v1/accounts/{accountId}/api-keys:
    post:
      operationId: createApiKey
      tags:
        - Auth
      summary: Creates a new API key for the authenticated account
      parameters:
        - name: accountId
          in: path
          required: true
          description: The account ID (must match the authenticated user ID)
          schema:
            type: string
            default: me
      requestBody:
        required: true
        description: Human-readable label for the API key
        content:
          text/plain:
            schema:
              type: string
              minLength: 1
            examples:
              default:
                value: CI Bot Key
      responses:
        '201':
          description: API key created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiKeyNew'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/InternalServerError'

    get:
      operationId: listApiKeys
      tags:
        - Auth
      summary: Lists API keys for the authenticated account
      parameters:
        - name: accountId
          in: path
          required: true
          description: The account ID (must match the authenticated user ID)
          schema:
            type: string
            default: me
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/ApiKey'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /v1/accounts/{accountId}/widget-sites:
    post:
      operationId: createWidgetSite
      tags:
        - Auth
      summary: Registers a new Widget Site for the authenticated account
      parameters:
        - name: accountId
          in: path
          required: true
          description: The account ID (must match the authenticated user ID)
          schema:
            type: string
            default: me
      requestBody:
        required: true
        description: The Widget Site Registration Request
        content:
          application/json:
            schema:
              type: object
              required:
                - origin
              properties:
                origin:
                  type: string
                  description: 'The allowed origin URL (e.g., https://example.com)'
                  example: 'https://example.com'
      responses:
        '201':
          description: Widget Site created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/WidgetSite'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/InternalServerError'

    get:
      operationId: listWidgetSites
      tags:
        - Auth
      summary: Lists registered Widget Sites for the authenticated account
      parameters:
        - name: accountId
          in: path
          required: true
          description: The account ID (must match the authenticated user ID)
          schema:
            type: string
            default: me
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/WidgetSite'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /v1/accounts/{accountId}/widget-sites/{siteId}:
    delete:
      operationId: deleteWidgetSite
      tags:
        - Auth
      summary: Unregisters a Widget Site for the authenticated account
      parameters:
        - name: accountId
          in: path
          required: true
          description: The account ID (must match the authenticated user ID)
          schema:
            type: string
            default: me
        - name: siteId
          in: path
          required: true
          description: ID of the Widget Site to delete
          schema:
            type: string
      responses:
        '204':
          description: Widget Site deleted
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /v1/accounts/{accountId}/api-keys/{apiKeyId}/revoke:
    post:
      operationId: revokeApiKey
      tags:
        - Auth
      summary: Revokes an API key for the authenticated account
      parameters:
        - name: accountId
          in: path
          required: true
          description: The account ID (must match the authenticated user ID)
          schema:
            type: string
            default: me
        - name: apiKeyId
          in: path
          required: true
          description: ID of the API key to revoke
          schema:
            type: string
      responses:
        '204':
          description: API key revoked
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/InternalServerError'

components:
  responses:
    BadRequest:
      description: 🚫 Bad Request; change bad request body, parameters, or (!) header(s).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiProblem'
    Unauthorized:
      description: 🔐 Not authenticated; just Login or send an 🔑 API Key, in the correct HTTP header format.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiProblem'
    PaymentRequired:
      description: 💳 Payment Required; account credit balance is insufficient or exhausted.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiProblem'
    Forbidden:
      description: 🚫 Not authorized to access this resource.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiProblem'
    TooManyRequests:
      description: 🤯 Too many 🌋 requests; use an 🔑 API Key, 💸 upgrade your plan, 🕰️ wait a bit, or 🤙 contact support.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiProblem'
    NotFound:
      description: 🔍 Resource not found.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiProblem'
    InternalServerError:
      description: 🛠️ Internal Server Error; something went wrong on our side.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiProblem'

  schemas:
    ApiProblem:
      type: object
      additionalProperties: true
      title: API Problem
      description: |
        🚨 **API Problem**

        A structured error response body following the [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457) standard.
      required:
        - type
        - title
        - status
        - instance
        - ts
        - level
      properties:
        type:
          type: string
          format: uri
          description: URI identifying the problem type.
          example: about:blank
        title:
          type: string
          description: Short, stable description of the problem class.
          example: Argument Exception
        status:
          type: integer
          description: The HTTP status code.
          example: 400
        detail:
          type: string
          nullable: true
          description: Human-readable explanation specific to this occurrence.
          example: Invalid API key label
        instance:
          type: string
          description: Opaque correlation token that uniquely identifies this error occurrence.
          example: urn:error:12345
        stackTrace:
          type: string
          nullable: true
          description: Full stack trace, included in development mode or for users with the DEBUGGER role.
        ts:
          type: string
          format: date-time
          description: Timestamp when the problem was recorded.
          example: 2026-11-01T20:08:33.816319365Z
        level:
          type: string
          enum: [ERROR, WARN, INFO, DEBUG, TRACE]
          description: Severity level of the problem.
          example: ERROR
        canContinue:
          type: boolean
          description: When true, this problem represents a recoverable guardrail checkpoint where the user can continue on the next turn.
          example: true

    UserID:
      type: string
      description: |
        🆔 **User ID (AKA 'UID')**

        The User ID is guaranteed to be eternally stable and unique, within this "system" (app, website, domain, organisation, company). This is what must be used for any references to this User. This is generally considered "internal" and not directly exposed to the end-user on any UI, even though there is nothing technically "secret" about this.

        This is typically backed by an ID from the underlying Identity Provider (IDP). The fallback, if no external ID is available, may be just a random string, such as a UUID.

        Starts with `user_` followed by 1-59 Base64Url characters.
      pattern: ^user_[a-zA-Z0-9-_]{1,59}$
      example: user_18zirgy7ZIqCvwa2Vj3ZQGBsXid

    Username:
      type: string
      description: |
        🔗 **Name is the "username"**

        This is typically "fairly stable", but technically "mutable" and may be changed by Users over time. Like id, it must also be unique within this "system" (app, website, domain, organisation, company).

        Once a username is taken, it is "burned" - even if the name is changed or the entire account is deleted, the name is never available again.

        This is often also known as a "handle" in social media contexts, or "slug" when used in URLs.

        Format: Upper case letters are allowed, but the name is case-insensitive for lookup and duplicate comparison purposes. Additionally, it must be at least 3 characters long.

        Pattern: 3-64 characters, mixed case (but case-insensitive), starting with a letter or digit, allowing hyphens and underscores (not consecutive).
      pattern: ^[a-zA-Z0-9](?:[a-zA-Z0-9]|[-_](?=[a-zA-Z0-9])){2,63}$
      example: tony.stark

    UserRef:
      type: string
      description: |
        👤 **User Reference**
        Can be either a User ID or a Username.
      pattern: ^(?:user_[a-zA-Z0-9-_]{1,59}|[a-zA-Z0-9](?:[a-zA-Z0-9]|[-_](?=[a-zA-Z0-9])){0,63})$
      example: tony.stark

    Base64UrlID:
      type: string
      description: |
        🆔 **Base64Url ID**
        1-64 Base64Url characters (letters, digits, hyphen, underscore).
      pattern: ^[a-zA-Z0-9-_]{1,64}$
      example: uEiAoRjlVuPv0KtbMnU4SqFejci9QNtr4jDEzPI_bCdlXyA

    Slug:
      type: string
      description: |
        🐌 **Slug**
        1-39 lowercase letters, digits, or hyphens (not at start/end or consecutive).
      pattern: ^(?![0-9]+$)[a-z0-9](?:[a-z0-9]|[-](?=[a-z0-9])){0,38}$
      example: my-agent

    SessionID:
      type: string
      description: |
        🆔 **Session ID**
        Format: `{UserRef}/{Base64UrlID}`
      pattern: ^(?:user_[a-zA-Z0-9-_]{1,59}|[a-zA-Z0-9](?:[a-zA-Z0-9]|[-_](?=[a-zA-Z0-9])){0,63})/[a-zA-Z0-9-_]{1,64}$
      example: alice/uEiAoRjlVuPv0KtbMnU4SqFejci9QNtr4jDEzPI_bCdlXyA

    AgentConfigID:
      type: string
      description: |
        🆔 **Agent Configuration ID**
        Format: `{UserRef}/{Slug}`
      pattern: ^(?:user_[a-zA-Z0-9-_]{1,59}|[a-zA-Z0-9](?:[a-zA-Z0-9]|[-_](?=[a-zA-Z0-9])){0,63})/(?![0-9]+$)[a-z0-9](?:[a-z0-9]|[-](?=[a-z0-9])){0,38}$
      example: google/gemini

    User:
      type: object
      additionalProperties: false
      title: User
      description: |
        👤 **User is a human Subject.**

        See [W3's “Personal names around the world”](https://www.w3.org/International/questions/qa-personal-names?hl=en-US-u-fw-mon-mu-celsius) for background re. the label and shortName fields.
      properties:
        id:
          $ref: '#/components/schemas/UserID'
        name:
          $ref: '#/components/schemas/Username'
        label:
          type: string
          description: |
            🏷️ **Label is the "full name"**

            Beware, this is NOT neccessarily unique!

            * A western style “first name followed by a an optional middle name or initial and then a last name”, like “Tony Stark”
            * Given name(s) plus double surnames, one from each parent, commonly used in Spanish and Portuguese-speaking cultures
            * The “first family surname, then given name” style used in Japan and other Asian cultures
            * A single Mononym name or a patronymic (father's name) initial, followed by a given name, as used in southern India

            The fallback, if no name label is available, may be even just the id.
          title: FullName
          pattern: ^[^\v\t]{1,255}$
          example: Tony Stark
        shortName:
          type: string
          description: |
            📛 **Short name is a “given name” or “preferred display name”**

            It's recommended to be used in UI elements like "Hello, …!" type greetings, incl. at the start of email texts. It's almost certainly not very unique at all.

            It’s typically just a shorter form of the label, or a user set preferred variant therefore, like "Mike" for a "Michael". It may also be a completely unrelated nickname, such as those very commonly used in Thailand.

            In a Form, this should be labeled (something like): “What should we call you? (For example, when we send you mail?)”.

            The fallback, if no short name is available, may be even just the label.
          title: ShortName
          pattern: ^[^\v\t]{1,255}$
          example: Tony
        image:
          type: string
          nullable: true
          description: |
            🖼️ **Image**

            Image is the URL of the user's profile picture.
          title: Image
          format: uri
          pattern: ^https?://.+$
          example: https://example.com/profile.jpg
        email:
          type: string
          nullable: true
          description: |
            📧 **Email (primary)**

            Email (primary) must be verified if present, but may be empty.
          title: Email
          example: tony@example.com
        roles:
          type: array
          items:
            type: string
          description: |
            🎭 **Roles**
          title: Roles
        # publicKeys:
        #   type: array
        #   items:
        #     $ref: "#/components/schemas/PublicKey"
        #   description: |
        #     🔐 **Public keys**

        #     Public keys of this User, if any.
        #   title: PublicKeys
      required:
        - id
        - name
        - label
        - shortName
      example:
        id: 'user_123'
        name: 'tony.stark'
        label: 'Tony Stark'
        shortName: 'Tony'
        email: 'tony@example.com'

    ApiKey:
      type: object
      additionalProperties: false
      title: API Key
      description: |
        🔑 **API Key**

        An API key. (Its secret is not available.)
      properties:
        id:
          $ref: '#/components/schemas/Base64UrlID'
          description: API key ID
        label:
          type: string
          description: Human-readable label for the key
          example: CI Bot Key
        secretSuffix:
          type: string
          description: Last few characters of secret, just to help a user match it in the UI
          pattern: ^[0-9a-zA-Z]{4}$
          example: ThFM
        owner:
          $ref: '#/components/schemas/UserID'
          description: User ID of the owner of this API key
        createdAt:
          type: string
          format: date-time
          example: 2026-03-07T15:58:45.676869954Z
        revokedAt:
          type: string
          format: date-time
          nullable: true
          example: 2026-03-07T16:03:38.196929834Z
      required:
        - id
        - label
        - secretSuffix
        - owner
        - createdAt
      example:
        id: 'p8u1'
        label: 'Prod Key'
        secretSuffix: 'abcd'
        owner: 'user_1'
        createdAt: '2026-04-06T21:15:00Z'

    ApiKeyNew:
      type: object
      additionalProperties: false
      title: API Key (new)
      description: |
        🗝️ **API Key (new)**

        Newly created API key ID and its secret - available only once here, never later anymore.
      properties:
        id:
          $ref: '#/components/schemas/Base64UrlID'
          description: API key ID
        secret:
          type: string
          description: API key secret to store securely
          pattern: ^sk_[0-9a-zA-Z]+
          example: sk_zmH1Hes7eyGvzJnbfPYeyXjxbr2PaJCsyHc3e5LEu6h94Z9KzH
      required:
        - id
        - secret

    WidgetSite:
      type: object
      additionalProperties: false
      title: Widget Site
      description: |
        🌐 **Widget Site**

        A registered website origin that is allowed to embed the chat widget.
      properties:
        id:
          type: string
          description: Widget Site composite ID (user_id/site_id)
        origin:
          type: string
          description: The allowed origin URL
          example: https://example.com
        createdAt:
          type: string
          format: date-time
          example: 2026-03-07T15:58:45.676869954Z
        deletedAt:
          type: string
          format: date-time
          nullable: true
          example: 2026-03-07T16:03:38.196929834Z
      required:
        - id
        - origin
        - createdAt

    Session:
      type: object
      properties:
        id:
          $ref: '#/components/schemas/SessionID'
        agent:
          $ref: '#/components/schemas/AgentConfigID'
        title:
          type: string
          nullable: true
        messages:
          type: array
          items:
            $ref: '#/components/schemas/Message'
        problems:
          type: array
          items:
            $ref: '#/components/schemas/ApiProblem'
      required:
        - id
        - agent
        - messages

    SessionOverview:
      type: object
      properties:
        id:
          $ref: '#/components/schemas/SessionID'
        agent:
          $ref: '#/components/schemas/AgentConfigID'
        title:
          type: string
          nullable: true
        lastMessageAt:
          type: string
          format: date-time
      required:
        - id
        - agent
        - lastMessageAt

    SessionsPage:
      type: object
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/SessionOverview'
        nextPageToken:
          type: string
          nullable: true
      required:
        - items

    AgentConfig:
      type: object
      properties:
        id:
          $ref: '#/components/schemas/AgentConfigID'
        kind:
          type: string
          enum: [agent, form, AGENT, FORM]
          default: agent
        name:
          type: string
          nullable: true
        label:
          oneOf:
            - type: string
            - type: object
              minProperties: 1
              additionalProperties:
                type: string
          nullable: true
        description:
          oneOf:
            - type: string
            - type: object
              minProperties: 1
              additionalProperties:
                type: string
          nullable: true
        model:
          $ref: '#/components/schemas/ModelID'
        instruction:
          type: string
          nullable: true
        intro:
          oneOf:
            - type: string
            - type: object
              minProperties: 1
              additionalProperties:
                type: string
          nullable: true
        prompt:
          type: string
          nullable: true
        thinking:
          $ref: '#/components/schemas/AgentThinking'
          nullable: true
        starters:
          type: array
          items:
            $ref: '#/components/schemas/AgentStarter'
          nullable: true
        knowledge:
          type: array
          items:
            $ref: '#/components/schemas/AgentKnowledge'
          nullable: true
        tools:
          type: array
          items:
            $ref: '#/components/schemas/ToolID'
          nullable: true
        limits:
          $ref: '#/components/schemas/AgentLimits'
          nullable: true
        input:
          $ref: '#/components/schemas/AgentFormat'
          nullable: true
        output:
          $ref: '#/components/schemas/AgentFormat'
          nullable: true
        tests:
          type: object
          additionalProperties:
            type: array
            items:
              $ref: '#/components/schemas/AgentTestStep'
          nullable: true
        actions:
          $ref: '#/components/schemas/AgentActions'
          nullable: true
        deletedAt:
          type: string
          format: date-time
          nullable: true
          example: 2026-03-07T16:03:38.196929834Z
      required:
        - id
        - model

    AgentLimits:
      type: object
      description: Execution guardrails and timeout profile overrides for an agent.
      properties:
        toolIterations:
          type: integer
          minimum: 1
          maximum: 128
          nullable: true
          description: Maximum number of tool call iterations per turn (defaults to 7 when unset).
        turnCredits:
          type: integer
          minimum: 1
          maximum: 1000
          nullable: true
          description: Maximum number of credits allowed in a single turn (defaults to 50 when unset).
        outputTokens:
          type: integer
          minimum: 1
          nullable: true
          description: Maximum number of output tokens per response. Subject to billing-based clamping.
        timeouts:
          type: string
          enum: [interactive, batch]
          nullable: true
          description: AI timeout and retry profile (interactive or batch).

    AgentsPage:
      type: object
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/AgentConfig'
        nextPageToken:
          type: string
          nullable: true
      required:
        - items

    AgentStarter:
      type: object
      properties:
        label:
          oneOf:
            - type: string
            - type: object
              minProperties: 1
              additionalProperties:
                type: string
          nullable: true
        prompt:
          oneOf:
            - type: string
            - type: object
              minProperties: 1
              additionalProperties:
                type: string
      required:
        - prompt

    AgentKnowledge:
      type: object
      properties:
        url:
          type: string
      required:
        - url

    AgentFormat:
      type: object
      properties:
        mediaType:
          type: string
        schema:
          type: object
          nullable: true
      required:
        - mediaType

    AgentTestStep:
      type: object
      properties:
        prompt:
          type: string
          default: ''
        textContains:
          type: array
          items:
            type: string
          nullable: true
        data:
          type: object
          nullable: true
        dataContains:
          type: object
          nullable: true

    AgentActions:
      type: object
      properties:
        email:
          $ref: '#/components/schemas/AgentEmailAction'
          nullable: true

    AgentEmailAction:
      type: object
      properties:
        enabled:
          type: boolean
          default: true
        to:
          type: string
          description: Recipient email address.
          example: reservations@myrestaurant.com
        on:
          $ref: '#/components/schemas/AgentEmailEventTriggers'
          nullable: true
      required:
        - to

    AgentEmailEventTriggers:
      type: object
      properties:
        formCompleted:
          type: boolean
          default: true
        includeTranscript:
          type: boolean
          default: false
        sessionEnded:
          type: boolean
          default: false

    AgentThinking:
      description: Thinking configuration, for models that support it.
      type: object
      properties:
        level:
          type: string
          description: Thinking level (effort).
          enum: [OFF, LOW, MEDIUM, HIGH]
          default: MEDIUM
        tokenBudget:
          type: integer
          description: Maximum number of tokens for thinking (if supported).
          nullable: true
        include:
          type: boolean
          description: Whether to include the thinking process in the output (if supported).
          default: false

    Message:
      type: object
      properties:
        ts:
          type: string
          format: date-time
        role:
          type: string
        parts:
          type: array
          items:
            $ref: '#/components/schemas/Part'
      required:
        - ts
        - role
        - parts

    Part:
      type: object
      properties:
        type:
          type: string
          enum: [TEXT, THINKING, DATA, TOOL_CALL, TOOL_RESPONSE]
      oneOf:
        - type: object
          properties:
            text:
              type: string
          required: [text]
        - type: object
          properties:
            id:
              type: string
            name:
              type: string
            object:
              type: object
          required: [object]
        - type: object
          properties:
            name:
              type: string
            mediaType:
              type: string
            data:
              type: string
          required: [name, mediaType, data]

    ModelID:
      type: string
      title: AI Model ID
      description: The `modelVersion` property indicates which model was used for generating the content, which can be important for cost calculation and analysis.
      example: gemini-3.8-flash
      enum:
        # NB: Keep in sync with the Model.java definition!
        - gemini-3.8-flash
        - gemini-3.5-flash-lite
        - gemini-3.1-pro-preview

    ToolID:
      title: AI Tool ID
      description: |
        🧰 **Tool**

        The ID of a tool available to an AI model (`http` is a legacy alias for `http-text`).
      anyOf:
        - type: string
          enum:
            - http-text
            - http-get
            - http-post
            - http
            - search-google
            - geo-google-maps
            - code-execution
        - type: string
      examples:
        - http-text
        - http-get
        - http-post
        - search-google
        - geo-google-maps
        - code-execution

    Models:
      type: array
      items:
        $ref: '#/components/schemas/Model'

    Model:
      type: object
      additionalProperties: false
      title: AI Model Details
      properties:
        id:
          $ref: '#/components/schemas/ModelID'
          example: gemini-3.8-flash
        label:
          type: string
          example: Gemini 3.8 Flash
      required:
        - id
        - label
      example:
        id: gemini-3.8-flash
        label: Gemini 3.8 Flash

    # PublicKey:
    #   type: object
    #   additionalProperties: false
    #   properties:
    #     algorithm:
    #       type: string
    #     format:
    #       type: string
    #     encoded:
    #       type: string
    #       format: binary
    #     params:
    #       $ref: "#/components/schemas/AlgorithmParameterSpec"
    # AlgorithmParameterSpec:
    #   type: object
    #   additionalProperties: false

    CreateResponseBody:
      type: object
      additionalProperties: true
      title: Create Response Request
      description: Request body for POST /v1/responses.
      properties:
        model:
          oneOf:
            - $ref: '#/components/schemas/ModelID'
            - $ref: '#/components/schemas/AgentConfigID'
          nullable: true
          description: 'The model ID or Agent configuration ID to use for this request.'
          example: gemini-3.5-flash-lite
        input:
          nullable: true
          description: 'Context for the model. Either a plain string (user message) or an array of input items.'
          oneOf:
            - type: string
            - type: array
              items:
                $ref: '#/components/schemas/ResponseInputItem'
        previous_response_id:
          $ref: '#/components/schemas/SessionID'
          nullable: true
          description: 'The ID of the response to use as the prior turn for this request.'
        instructions:
          type: string
          nullable: true
          description: Additional instructions to guide the model for this request.
        temperature:
          type: number
          nullable: true
          description: Sampling temperature, between 0 and 2. Higher values produce more random output.
        top_p:
          type: number
          nullable: true
          description: Nucleus sampling parameter, between 0 and 1.
        stream:
          type: boolean
          nullable: true
          description: "When true, response events are returned as server-sent events (SSE). Requires the 'Accept: text/event-stream' header."

    ResponseInputItem:
      type: object
      title: Response Input Item
      description: |
        A single input item. Discriminated by the `type` field.
        Supported types: `message`, `reasoning`, `function_call`, `function_call_output`, `item_reference`.
      discriminator:
        propertyName: type
      oneOf:
        - $ref: '#/components/schemas/ResponseMessageInputItem'
        - $ref: '#/components/schemas/ResponseReasoningInputItem'
        - $ref: '#/components/schemas/ResponseFunctionCallInputItem'
        - $ref: '#/components/schemas/ResponseFunctionCallOutputInputItem'
        - $ref: '#/components/schemas/ResponseItemReferenceInputItem'
      required:
        - type

    ResponseMessageInputItem:
      type: object
      additionalProperties: false
      title: Message Input Item
      description: A message input item (user, assistant, system, or developer message).
      properties:
        type:
          type: string
          enum: [message]
          description: The item type. Always `message`.
        id:
          $ref: '#/components/schemas/Base64UrlID'
          nullable: true
          description: Optional unique ID for the message item.
        role:
          type: string
          enum: [user, assistant, system, developer]
          description: The message role.
        content:
          description: Message content as a string or array of text content parts.
          oneOf:
            - type: string
            - type: array
              items:
                oneOf:
                  - $ref: '#/components/schemas/ResponseTextContentPart'
                  - $ref: '#/components/schemas/ResponseImageContentPart'
                  - $ref: '#/components/schemas/ResponseFileContentPart'
                  - $ref: '#/components/schemas/ResponseVideoContentPart'
        status:
          type: string
          nullable: true
      required:
        - type

    ResponseTextContentPart:
      type: object
      additionalProperties: false
      title: Text Content Part
      description: A text content part in a message.
      properties:
        type:
          type: string
          enum: [input_text, output_text, text]
          description: The content type.
        text:
          type: string
          description: The text content.
      required:
        - type
        - text

    ResponseImageContentPart:
      type: object
      additionalProperties: false
      title: Image Content Part
      description: An image content part in a message.
      properties:
        type:
          type: string
          enum: [input_image]
          description: The content type.
        image_url:
          type: string
          description: Data URL or absolute URL of the image.
        detail:
          type: string
          enum: [auto, low, high]
          default: auto
          description: The detail level of the image.
      required:
        - type
        - image_url

    ResponseFileContentPart:
      type: object
      additionalProperties: false
      title: File Content Part
      description: A file content part in a message.
      properties:
        type:
          type: string
          enum: [input_file]
          description: The content type.
        file_url:
          type: string
          description: Data URL or absolute URL of the file.
        filename:
          type: string
          nullable: true
          description: The name of the file.
      required:
        - type
        - file_url

    ResponseVideoContentPart:
      type: object
      additionalProperties: false
      title: Video Content Part
      description: A video content part in a message.
      properties:
        type:
          type: string
          enum: [input_video]
          description: The content type.
        video_url:
          type: string
          description: Data URL or absolute URL of the video.
      required:
        - type
        - video_url

    ResponseReasoningInputItem:
      type: object
      additionalProperties: false
      title: Reasoning Input Item
      description: A reasoning input item containing reasoning summary text.
      properties:
        type:
          type: string
          enum: [reasoning]
          description: The item type. Always `reasoning`.
        summary:
          type: array
          description: Reasoning summary content parts.
          items:
            type: object
            additionalProperties: false
            properties:
              type:
                type: string
                enum: [summary_text]
              text:
                type: string
            required: [type, text]
        encrypted_content:
          type: string
          nullable: true
          description: An encrypted representation of the reasoning content.
      required:
        - type
        - summary

    ResponseFunctionCallInputItem:
      type: object
      additionalProperties: false
      title: Function Call Input Item
      description: A function call input item representing a model-requested tool invocation.
      properties:
        type:
          type: string
          enum: [function_call]
          description: The item type. Always `function_call`.
        call_id:
          type: string
          description: The call ID of the function call.
        name:
          type: string
          description: The name of the function to call.
        arguments:
          type: string
          description: The arguments to pass to the function, as a JSON string.
      required:
        - type

    ResponseFunctionCallOutputInputItem:
      type: object
      additionalProperties: false
      title: Function Call Output Input Item
      description: A function call output (tool result) input item.
      properties:
        type:
          type: string
          enum: [function_call_output]
          description: The item type. Always `function_call_output`.
        call_id:
          type: string
          description: The call ID of the function call this output is for.
        output:
          description: The output of the function call, as a string or array of text content parts.
          oneOf:
            - type: string
            - type: array
              items:
                $ref: '#/components/schemas/ResponseTextContentPart'
      required:
        - type

    ResponseItemReferenceInputItem:
      type: object
      additionalProperties: false
      title: Item Reference Input Item
      description: A reference to a previously-created response item by its ID.
      properties:
        type:
          type: string
          enum: [item_reference]
          description: The item type. Always `item_reference`.
        id:
          $ref: '#/components/schemas/Base64UrlID'
          description: The ID of the item to reference.
      required:
        - type
        - id

    ResponseResource:
      type: object
      additionalProperties: true
      title: Response Resource
      description: The response object returned by POST /v1/responses.
      properties:
        id:
          $ref: '#/components/schemas/SessionID'
          nullable: true
          description: The unique ID of the response, which can be passed as previous_response_id in subsequent requests to continue the session.
          example: alice/uEiAoRjlVuPv0KtbMnU4SqFejci9QNtr4jDEzPI_bCdlXyA
        object:
          type: string
          enum: [response]
          nullable: true
          description: The object type. Always `response`.
        created_at:
          type: integer
          nullable: true
          description: Unix timestamp (seconds) for when the response was created.
          example: 1741476777
        completed_at:
          type: integer
          nullable: true
          description: Unix timestamp (seconds) for when the response was completed.
          example: 1741476778
        status:
          type: string
          enum: [in_progress, completed, incomplete]
          nullable: true
          description: The status of the response.
        model:
          oneOf:
            - $ref: '#/components/schemas/ModelID'
            - $ref: '#/components/schemas/AgentConfigID'
          nullable: true
          description: The model that generated this response.
          example: gemini-3.5-flash-lite
        output:
          type: array
          nullable: true
          description: The output items generated by the model.
          items:
            $ref: '#/components/schemas/ResponseOutputMessage'
        error:
          $ref: '#/components/schemas/ApiProblem'
          nullable: true
          description: The error that occurred, if the response failed.
      example:
        model: gemini-3.5-flash-lite
        created_at: 1741476777
        completed_at: 1741476778
        output:
          - type: message
            role: assistant
            status: completed
            content:
              - type: text
                text: 'Hello! How can I help you?'

    ResponseOutputMessage:
      type: object
      additionalProperties: true
      title: Response Output Message
      description: A message output item in the response.
      properties:
        type:
          type: string
          enum: [message]
          description: The item type. Always `message`.
        id:
          $ref: '#/components/schemas/Base64UrlID'
          nullable: true
          description: The unique ID of the message.
        role:
          type: string
          enum: [assistant]
          description: The role of the message. Always `assistant` for model output.
        status:
          type: string
          enum: [in_progress, completed, incomplete]
          nullable: true
          description: The status of the message.
        content:
          type: array
          nullable: true
          description: The text content parts of the message.
          items:
            $ref: '#/components/schemas/ResponseOutputTextContent'
      required:
        - type

    ResponseOutputTextContent:
      type: object
      additionalProperties: false
      title: Response Output Text Content
      description: A text content part in a response output message.
      properties:
        type:
          type: string
          enum: [text]
          description: The content type. Always `text`.
        text:
          type: string
          description: The text content.
      required:
        - type
        - text

    ResponseCreatedStreamingEvent:
      type: object
      additionalProperties: false
      title: Response Created SSE Event
      description: Server-sent event emitted when a response object is first created.
      properties:
        type:
          type: string
          enum: [response.created]
          description: The event type. Always `response.created`.
        sequence_number:
          type: integer
          description: Monotonically increasing sequence number for ordering events.
        response:
          $ref: '#/components/schemas/ResponseResource'
      required:
        - type
        - sequence_number
        - response

    ResponseOutputTextDeltaStreamingEvent:
      type: object
      additionalProperties: false
      title: Response Output Text Delta SSE Event
      description: Server-sent event emitted when output text is incrementally appended.
      properties:
        type:
          type: string
          enum: [response.output_text.delta]
          description: The event type. Always `response.output_text.delta`.
        sequence_number:
          type: integer
          description: Monotonically increasing sequence number for ordering events.
        output_index:
          type: integer
          description: The index of the output item being updated.
        content_index:
          type: integer
          description: The index of the content part being updated.
        delta:
          type: string
          description: The text fragment appended in this event.
      required:
        - type
        - sequence_number
        - output_index
        - content_index
        - delta

    ResponseCompletedStreamingEvent:
      type: object
      additionalProperties: false
      title: Response Completed SSE Event
      description: Server-sent event emitted when a response has been fully generated.
      properties:
        type:
          type: string
          enum: [response.completed]
          description: The event type. Always `response.completed`.
        sequence_number:
          type: integer
          description: Monotonically increasing sequence number for ordering events.
        response:
          $ref: '#/components/schemas/ResponseResource'
      required:
        - type
        - sequence_number
        - response

    ErrorStreamingEvent:
      type: object
      additionalProperties: false
      title: Error SSE Event
      description: Server-sent event emitted when an error occurs during response generation.
      properties:
        type:
          type: string
          enum: [error]
          description: The event type. Always `error`.
        sequence_number:
          type: integer
          description: Monotonically increasing sequence number for ordering events.
        error:
          $ref: '#/components/schemas/ApiProblem'
      required:
        - type
        - sequence_number
        - error

  securitySchemes:
    ApiKeyAuth:
      in: header
      name: X-API-Key
      type: apiKey
    BearerAuth:
      scheme: bearer
      type: http
security:
  - ApiKeyAuth: []
  - BearerAuth: []
