openapi: 3.0.3
info:
  title: Fluent Agent API
  description: |
    Semantic search API for Fluent Commerce documentation.

    This API enables AI coding agents to search through documentation for configurable
    assets (rules, components, utilities, etc.) using vector embeddings and semantic search.

    ## Features
    - Semantic search using Amazon Titan Text Embeddings V2
    - Module/version filtering for targeted results
    - Optional reranking with Cohere Rerank 3.5
    - Sub-second query latency

    ## Pipeline
    The search uses a 5-stage pipeline:
    1. **Vectorize**: Generate embedding from query
    2. **Retrieve**: Find top 100 matches in S3 Vectors
    3. **Hydrate**: Fetch full documents from S3
    4. **Rerank**: Improve result order with cross-encoder (optional)
    5. **Return**: Send top N results to client
  version: 1.0.0
  contact:
    name: Fluent Commerce
    url: https://fluentcommerce.com
  license:
    name: Proprietary

servers:
  - url: http://localhost:3000
    description: Local development server
  - url: https://agent-api.fluentcommerce.com
    description: Production server

tags:
  - name: search
    description: Semantic search operations
  - name: health
    description: Health check and monitoring

paths:
  /api/v1/search:
    get:
      summary: Health check
      description: Simple health check endpoint that returns OK status
      operationId: healthCheck
      tags:
        - search
      responses:
        '200':
          description: Service is healthy
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: ok
              example:
                status: ok

    post:
      summary: Semantic search
      description: |
        Search documentation using semantic vector search.

        Supports filtering by module and version, and returns ranked results
        with relevance scores.
      operationId: search
      tags:
        - search
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SearchRequest'
            examples:
              basic:
                summary: Basic search
                value:
                  query: How do I implement a custom fulfillment rule?
              withFilters:
                summary: Search with module filter
                value:
                  query: split shipment configuration
                  modules:
                    - name: module-order
                      version: 2.x
                  first: 5
      responses:
        '200':
          description: Search results
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SearchResponse'
        '400':
          description: Validation error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ValidationError'
        '404':
          description: No documents found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '503':
          description: Service unavailable
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'

  /api/v1/health:
    get:
      summary: Comprehensive health check
      description: |
        Detailed health check that verifies all service dependencies:
        - AWS credentials
        - S3 Vectors bucket access
        - S3 document bucket access
        - Bedrock model access

        Returns health status with latency for each check.
      operationId: comprehensiveHealthCheck
      tags:
        - health
      responses:
        '200':
          description: Service health status (healthy or degraded)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HealthStatus'
        '503':
          description: Service is unhealthy
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HealthStatus'

components:
  schemas:
    SearchRequest:
      type: object
      required:
        - query
      properties:
        query:
          type: string
          minLength: 1
          maxLength: 20000
          description: Search query text (1-20,000 characters)
          example: How do I implement a custom fulfillment rule?
        modules:
          type: array
          description: Optional filter to search within specific modules
          items:
            $ref: '#/components/schemas/ModuleFilter'
        first:
          type: integer
          minimum: 1
          maximum: 10
          default: 10
          description: Number of results to return (1-10)
          example: 5

    ModuleFilter:
      type: object
      required:
        - name
        - version
      properties:
        name:
          type: string
          minLength: 1
          description: Module name
          example: module-order
        version:
          type: string
          minLength: 1
          description: Module version
          example: 2.x

    SearchResponse:
      type: object
      required:
        - results
      properties:
        results:
          type: array
          items:
            $ref: '#/components/schemas/SearchResult'

    SearchResult:
      type: object
      required:
        - id
        - title
        - module
        - version
        - content
        - relevance_score
      properties:
        id:
          type: string
          description: Unique document identifier
          example: doc_12345
        title:
          type: string
          description: Document title
          example: Split Shipment Rule
        module:
          type: string
          description: Module name
          example: module-order
        version:
          type: string
          description: Module version
          example: 2.x
        content:
          type: string
          description: Full document content in Markdown format
          example: "# Split Shipment Rule\n\nThis rule handles split shipments..."
        relevance_score:
          type: number
          format: float
          minimum: 0
          maximum: 1
          description: Relevance score (0-1, higher is more relevant)
          example: 0.98

    ValidationError:
      type: object
      required:
        - error
        - details
      properties:
        error:
          type: string
          example: Validation failed
        details:
          type: array
          items:
            type: object
            properties:
              path:
                type: string
                description: JSON path to the invalid field
                example: query
              message:
                type: string
                description: Validation error message
                example: Query cannot be empty

    Error:
      type: object
      required:
        - error
        - message
      properties:
        error:
          type: string
          description: Error type
          example: Service unavailable
        message:
          type: string
          description: Detailed error message
          example: Unable to connect to AWS services

    HealthStatus:
      type: object
      required:
        - status
        - timestamp
        - checks
      properties:
        status:
          type: string
          enum: [healthy, degraded, unhealthy]
          description: |
            Overall health status:
            - healthy: All checks passed
            - degraded: Some checks failed
            - unhealthy: All checks failed
          example: healthy
        timestamp:
          type: string
          format: date-time
          description: Timestamp of health check
          example: '2026-03-12T10:30:00Z'
        checks:
          type: object
          required:
            - aws_credentials
            - s3_vectors
            - s3_documents
            - bedrock
          properties:
            aws_credentials:
              $ref: '#/components/schemas/HealthCheck'
            s3_vectors:
              $ref: '#/components/schemas/HealthCheck'
            s3_documents:
              $ref: '#/components/schemas/HealthCheck'
            bedrock:
              $ref: '#/components/schemas/HealthCheck'

    HealthCheck:
      type: object
      required:
        - status
      properties:
        status:
          type: string
          enum: [ok, error]
          description: Check status
          example: ok
        message:
          type: string
          description: Error message (only present if status is error)
          example: Failed to access S3 Vectors
        latency:
          type: integer
          description: Check latency in milliseconds
          example: 123
