openapi: 3.0.0
info:
  title: Tenant Session API
  version: 1.0.0
  description: API for managing tenant sessions in the DryMerge application.
servers:
  - url: https://api.drymerge.com

paths:
  /tenant/session:
    post:
      summary: Start a new tenant session
      security:
        - apiKeyAuth: [ ]
      description: Creates a new tenant session and returns a session key.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/StartTenantSessionEndpointApiArgs'
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/StartTenantSessionEndpointApiResponse'
        '400':
          description: Bad request
        '500':
          description: Internal server error

  /generate/templates:
    post:
      summary: Generate from a template
      security:
        - apiKeyAuth: [ ]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateFromTemplateEndpointArgs'
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GenerateFromTemplateEndpointResponse'

  /generate/run:
    post:
      summary: Generate and run a workflow
      security:
        - apiKeyAuth: [ ]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateFromTemplateEndpointArgs'
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: object

  /generate/delete:
    post:
      summary: Generate and delete a workflow
      security:
        - apiKeyAuth: [ ]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateFromTemplateEndpointArgs'
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: object

  /generate/search:
    post:
      summary: Generate and search workflows
      security:
        - apiKeyAuth: [ ]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateFromTemplateEndpointArgs'
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: object
  /agent:
    post:
      summary: Run the Agent
      security:
        - apiKeyAuth: [ ]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateFromTemplateEndpointArgs'
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GenerateFromTemplateEndpointResponse'

components:
  securitySchemes:
    apiKeyAuth:
      type: http
      scheme: bearer

  schemas:
    Metadata:
      type: object
      additionalProperties: true
      description: Arbitrary metadata about the tenant as a key-value store, where each value can be of any type.

    StartTenantSessionEndpointApiArgs:
      type: object
      required:
        - tenant_id
      properties:
        public_key:
          type: string
          description: Your organization's public key.
        tenant_id:
          type: string
          description: The unique identifier of the tenant.
        tenant_metadata:
          $ref: '#/components/schemas/Metadata'

    StartTenantSessionEndpointApiResponse:
      type: object
      properties:
        session_key:
          type: string
          description: The session key for the new tenant session.

    GenerateFromTemplateEndpointArgs:
      type: object
      properties:
        prompt:
          type: string
          description: The user's most recent message.
        agent:
          $ref: '#/components/schemas/Identity'
        history:
          type: array
          items:
            $ref: '#/components/schemas/ChatMsg'
          description: The history of the chat up to this point.

    GenerateFromTemplateEndpointResponse:
      type: object
      properties:
        decision:
          $ref: '#/components/schemas/CrudDecision'

    Identity:
      type: object
      description: An identity is a unique identifier for an entity.
      properties:
        type:
          type: string
          description: Type of the identity.
        organization:
          type: string
          description: Name of the organization that owns the entity.
        namespace:
          type: string
          description: Namespace delineating logical grouping of entities.
        name:
          type: string
          description: The name of the entity; must be unique within a namespace.
        isolation:
          type: string
          description: The isolation level of the entity.

    ChatMsg:
      type: object
      properties:
        text:
          type: string
          description: The text of the chat message.
        sender:
          type: string
          enum:
            - user
            - system
            - assistant
          description: The sender of the message.

    CrudDecision:
      type: object
      properties:
        Operate:
          $ref: '#/components/schemas/OperateDecision'
        Yield:
          $ref: '#/components/schemas/YieldDecision'
        Skip:
          type: string
          description: Reason for skipping the operation.

    OperateDecision:
      type: object
      properties:
        data:
          type: object
          additionalProperties: true
          description: Data on which the agent has decided to operate.
        ui:
          type: array
          items:
            $ref: '#/components/schemas/UserInterfaceElement'
          description: UI elements to be rendered for the user.

    YieldDecision:
      type: object
      properties:
        ui:
          type: array
          items:
            $ref: '#/components/schemas/UserInterfaceElement'
          description: UI elements to be rendered while awaiting further user input.

    UserInterfaceElement:
      type: object
      properties:
        Form:
          $ref: '#/components/schemas/FormElement'
        Auth:
          type: array
          items:
            $ref: '#/components/schemas/AuthElement'
        Confirmation:
          $ref: '#/components/schemas/ConfirmedAction'
        Display:
          type: array
          items:
            $ref: '#/components/schemas/WorkflowGroupItem'
        Update:
          $ref: '#/components/schemas/UpdateElement'

    FormElement:
      type: object
      properties:
        id:
          $ref: '#/components/schemas/Identity'
        variant:
          $ref: '#/components/schemas/FormType'
        schema:
          type: object
          additionalProperties: true

    FormType:
      type: string
      enum:
        - Template
        - Workflow

    AuthElement:
      type: object
      oneOf:
        - $ref: '#/components/schemas/SecretData'
        - $ref: '#/components/schemas/Identity'

    SecretData:
      type: object
      properties:
        name:
          type: string
        declared:
          type: boolean
        used:
          type: boolean

    ConfirmedAction:
      type: object
      oneOf:
        - $ref: '#/components/schemas/FormElement'
        - type: array
          items:
            $ref: '#/components/schemas/Identity'

    WorkflowGroupItem:
      type: object
      properties:
        id:
          $ref: '#/components/schemas/Identity'
        description:
          type: string
        tags:
          type: array
          items:
            type: string

    UpdateElement:
      type: object
      properties:
        message:
          type: string
        theme:
          $ref: '#/components/schemas/UpdateTheme'

    UpdateTheme:
      type: string
      enum:
        - Success
        - Failure
        - Neutral
