# opencode (1.18.23) + a small control gateway, driving Qwen3.8-Flash-Next on Basalt in the
# same container. The first two RUN steps are qwen38-flash-next-basalt-5090's, line for line,
# so Docker reuses that build.
#
# Basalt (github.com/jesdga95/basalt, MIT): a Strata fork tuned for Qwen3.8-Flash-Next on
# Blackwell sm_120 (the RTX 5090). Base: nvidia/cuda:13.0.0-devel-ubuntu24.04, pinned by digest.
FROM node@sha256:48e4b67d85f87bd551df43704e24d252f56cc5f8e9718841aace50f19948f0f9 AS node

FROM nvidia/cuda@sha256:1e8ac7a54c184a1af8ef2167f28fa98281892a835c981ebcddb1fad04bdd452d

RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential cmake git curl ca-certificates \
        python3 python3-pip \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /opt/basalt
RUN git init -q . \
    && git fetch -q --depth 1 https://github.com/jesdga95/basalt 5b7eff1677ce90a3e2be61c913aa7f880d46ac1e \
    && git checkout -q FETCH_HEAD \
    && test "$(git rev-parse HEAD)" = 5b7eff1677ce90a3e2be61c913aa7f880d46ac1e

# The commit's requirements.txt, spelled out so the pins are visible here (Python 3.12).
RUN cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \
    && cmake --build build -j"$(nproc)" \
    && python3 -m pip install --no-cache-dir --break-system-packages \
        numpy==2.5.3 jinja2==3.1.6 markupsafe==3.0.3 regex==2026.9.10 pyyaml==6.0.3 \
        tqdm==4.70.1 pillow==12.3.0 psutil==7.2.2

# --- opencode side (the opencode-dsflash capsule's tools, on Ubuntu 24.04) -----------------

RUN apt-get update -qq && apt-get install -y --no-install-recommends \
      ripgrep jq python3-venv procps less file unzip ttyd \
    && rm -rf /var/lib/apt/lists/*

# Node 22.23.2 from node:22-bookworm-slim (glibc 2.36; this Ubuntu has 2.39).
COPY --from=node /usr/local/bin/node /usr/local/bin/node
COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -s ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \
    && ln -s ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx \
    && npm install -g opencode-ai@1.18.23 && npm cache clean --force

# Gateway deps, the same pins as opencode-dsflash's gateway, resolved for Python 3.12.
RUN python3 -m venv /opt/gateway && /opt/gateway/bin/pip install --no-cache-dir \
      fastapi==0.115.14 uvicorn==0.32.1 httpx==0.28.1 \
      starlette==0.46.2 pydantic==2.14.1 pydantic_core==2.50.1 annotated-types==0.8.0 \
      typing_extensions==4.16.0 typing-inspection==0.4.4 anyio==4.15.1 idna==3.20 \
      certifi==2026.10.23 httpcore==1.0.9 h11==0.16.0 click==8.5.0

# Everything opencode would otherwise fetch at run time is disabled; the npm packages it
# installs on a project's first use are warmed here, where the build has network.
ENV OPENCODE_DISABLE_AUTOUPDATE=1 \
    OPENCODE_DISABLE_LSP_DOWNLOAD=1 \
    OPENCODE_DISABLE_MODELS_FETCH=1 \
    OPENCODE_DISABLE_SHARE=1
COPY opencode.json /etc/opencode/opencode.json
COPY warm_opencode.sh /tmp/warm_opencode.sh
RUN bash /tmp/warm_opencode.sh && rm /tmp/warm_opencode.sh

WORKDIR /scratch
