x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  bytebot-desktop:
    <<: *defaults
    image: ${GHCR_IO_REGISTRY:-ghcr.io}/bytebot-ai/bytebot-desktop:${BYTEBOT_VERSION:-edge}
    ports:
      - '${BYTEBOT_DESKTOP_PORT_OVERRIDE:-9990}:9990'
    environment:
      - TZ=${TZ:-UTC}
    shm_size: 2gb
    deploy:
      resources:
        limits:
          cpus: ${BYTEBOT_DESKTOP_CPU_LIMIT:-2.0}
          memory: ${BYTEBOT_DESKTOP_MEMORY_LIMIT:-4G}
        reservations:
          cpus: ${BYTEBOT_DESKTOP_CPU_RESERVATION:-1.0}
          memory: ${BYTEBOT_DESKTOP_MEMORY_RESERVATION:-2G}
    healthcheck:
      test:
        - CMD
        - wget
        - --no-verbose
        - --tries=1
        - --spider
        - 'http://localhost:9990/'

      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  bytebot-agent:
    <<: *defaults
    image: ${GHCR_IO_REGISTRY:-ghcr.io}/bytebot-ai/bytebot-agent:${BYTEBOT_VERSION:-edge}
    depends_on:
      bytebot-desktop:
        condition: service_healthy
      bytebot-db:
        condition: service_healthy
    ports:
      - '${BYTEBOT_AGENT_PORT_OVERRIDE:-9991}:9991'
    environment:
      - TZ=${TZ:-UTC}
      - BYTEBOTD_URL=http://bytebot-desktop:9990
      - DATABASE_URL=postgresql://${POSTGRES_USER:-bytebot}:${POSTGRES_PASSWORD:-bytebotpass}@bytebot-db:5432/${POSTGRES_DB:-bytebot}
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
      - OPENAI_API_KEY=${OPENAI_API_KEY:-}
      - GEMINI_API_KEY=${GEMINI_API_KEY:-}
    deploy:
      resources:
        limits:
          cpus: ${BYTEBOT_AGENT_CPU_LIMIT:-1.0}
          memory: ${BYTEBOT_AGENT_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${BYTEBOT_AGENT_CPU_RESERVATION:-0.5}
          memory: ${BYTEBOT_AGENT_MEMORY_RESERVATION:-512M}
    healthcheck:
      test:
        - CMD
        - wget
        - --no-verbose
        - --tries=1
        - --spider
        - 'http://localhost:9991/health'

      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  bytebot-ui:
    <<: *defaults
    image: ${GHCR_IO_REGISTRY:-ghcr.io}/bytebot-ai/bytebot-ui:${BYTEBOT_VERSION:-edge}
    depends_on:
      bytebot-agent:
        condition: service_healthy
    ports:
      - '${BYTEBOT_UI_PORT_OVERRIDE:-9992}:9992'
    environment:
      - TZ=${TZ:-UTC}
      - BYTEBOT_AGENT_BASE_URL=http://localhost:9991
      - BYTEBOT_DESKTOP_VNC_URL=http://localhost:9990/websockify
    deploy:
      resources:
        limits:
          cpus: ${BYTEBOT_UI_CPU_LIMIT:-0.5}
          memory: ${BYTEBOT_UI_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${BYTEBOT_UI_CPU_RESERVATION:-0.25}
          memory: ${BYTEBOT_UI_MEMORY_RESERVATION:-256M}
    healthcheck:
      test:
        - CMD
        - wget
        - --no-verbose
        - --tries=1
        - --spider
        - 'http://localhost:9992/'

      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  bytebot-db:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}postgres:${POSTGRES_VERSION:-17-alpine}
    environment:
      - TZ=${TZ:-UTC}
      - POSTGRES_USER=${POSTGRES_USER:-bytebot}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-bytebotpass}
      - POSTGRES_DB=${POSTGRES_DB:-bytebot}
      - PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
      - bytebot_db_data:/var/lib/postgresql/data
    deploy:
      resources:
        limits:
          cpus: ${BYTEBOT_DB_CPU_LIMIT:-0.5}
          memory: ${BYTEBOT_DB_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${BYTEBOT_DB_CPU_RESERVATION:-0.25}
          memory: ${BYTEBOT_DB_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD-SHELL, pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

volumes:
  bytebot_db_data:
