services:
  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: repopilot
      POSTGRES_USER: repopilot
      POSTGRES_PASSWORD: repopilot
    volumes:
      - pgdata:/var/lib/postgresql/data
    # Port not exposed to host — only accessible within Docker network.
    # Uncomment if you need direct host access and port 5432 is free.
    # ports:
    #   - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U repopilot"]
      interval: 5s
      timeout: 5s
      retries: 5

  repopilot:
    build:
      context: .
      args:
        NEXT_PUBLIC_AGENT_COMPACTION_THRESHOLD_TOKENS: ${NEXT_PUBLIC_AGENT_COMPACTION_THRESHOLD_TOKENS:-100000}
        NEXT_PUBLIC_AGENT_MIN_HISTORY_MESSAGES_FOR_COMPACTION: ${NEXT_PUBLIC_AGENT_MIN_HISTORY_MESSAGES_FOR_COMPACTION:-12}
    ports:
      - "${REPOPILOT_HOST_PORT:-3000}:3000"
    depends_on:
      postgres:
        condition: service_healthy
    # Explicit env_file to avoid inheriting local .env which contains
    # host-specific paths (REPOS_PATH, SOURCE_PATH) that don't exist in the container.
    env_file: []
    environment:
      # Core settings — hardcoded for container, not inherited from .env
      REPOS_PATH: /repos
      SOURCE_PATH: /workspace
      DATABASE_URL: postgresql://repopilot:repopilot@postgres:5432/repopilot
      RUST_AGENT_BINARY_PATH: /usr/local/bin/agent
      NODE_ENV: production
      # LLM settings — can be overridden via `docker compose --env-file .env.docker up`
      # In this WSL/Docker setup the host-side proxy is reachable on the WSL IP,
      # while host.docker.internal resolves to an unreachable Docker Desktop gateway.
      # Override via env if your host IP changes.
      LITELLM_PROXY_URL: ${LITELLM_PROXY_URL:-http://172.30.162.95:4141}
      LLM_MODEL: ${LLM_MODEL:-gpt-5-mini}
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY:-dummy}
      # Agent tuning
      REPOSITORY_ZIP_EXTRACT_TIMEOUT_MS: ${REPOSITORY_ZIP_EXTRACT_TIMEOUT_MS:-120000}
      REPOSITORY_GIT_CLONE_TIMEOUT_MS: ${REPOSITORY_GIT_CLONE_TIMEOUT_MS:-300000}
      LLM_HEALTHCHECK_TIMEOUT_MS: ${LLM_HEALTHCHECK_TIMEOUT_MS:-5000}
      AGENT_ALLOWED_TOOLS: ${AGENT_ALLOWED_TOOLS:-read_file,search_files,list_directory}
      AGENT_TIMEOUT_MS: ${AGENT_TIMEOUT_MS:-300000}
      AGENT_MAX_THINKING_TOKENS: ${AGENT_MAX_THINKING_TOKENS:-4000}
      AGENT_MAX_TURNS: ${AGENT_MAX_TURNS:-16}
      AGENT_CONVERSATION_SUMMARY_MAX_CHARS: ${AGENT_CONVERSATION_SUMMARY_MAX_CHARS:-5000}
      AGENT_CONVERSATION_ASSISTANT_MAX_CHARS: ${AGENT_CONVERSATION_ASSISTANT_MAX_CHARS:-2800}
      AGENT_CONVERSATION_USER_MAX_CHARS: ${AGENT_CONVERSATION_USER_MAX_CHARS:-1800}
      AGENT_CONVERSATION_HISTORY_MESSAGES: ${AGENT_CONVERSATION_HISTORY_MESSAGES:-8}
      CHAT_COMPACTION_MODEL: ${CHAT_COMPACTION_MODEL:-gpt-5-mini}
      CHAT_COMPACTION_MAX_TURNS: ${CHAT_COMPACTION_MAX_TURNS:-3}
      WIKI_AGENT_MODEL: ${WIKI_AGENT_MODEL:-gpt-5-mini}
      WIKI_AGENT_ALLOWED_TOOLS: ${WIKI_AGENT_ALLOWED_TOOLS:-read_file,search_files,list_directory,write_file,verify_mermaid}
      WIKI_AGENT_MAX_TURNS: ${WIKI_AGENT_MAX_TURNS:-24}
      # Rust agent LLM routing via LiteLLM proxy
      LLM_PROVIDER: ${LLM_PROVIDER:-openai}
      OPENAI_API_KEY: ${OPENAI_API_KEY:-${ANTHROPIC_API_KEY:-dummy}}
      OPENAI_API_BASE_URL: ${OPENAI_API_BASE_URL:-${LITELLM_PROXY_URL:-http://host.docker.internal:4141}}
      ANTHROPIC_BASE_URL: ${ANTHROPIC_BASE_URL:-${LITELLM_PROXY_URL:-http://host.docker.internal:4141}}
    user: "${UID:-1000}:${GID:-1000}"
    volumes:
      - repos:/repos
      - ${LOCAL_SOURCE_PATH:-/tmp/empty}:/workspace
    extra_hosts:
      - "host.docker.internal:host-gateway"

volumes:
  pgdata:
  repos:
