# syntax=docker/dockerfile:1.7
#
# Hivemind Content Studio, hosted mode: the control API and the built studio,
# for a public server, with the desktop's media gateway and agent MCP beside it
# so members run studio jobs on GPUs they rent exactly as the desktop does
# (deploy/hosted/entrypoint.sh). No ComfyUI and no MLX engines: anything a
# member makes runs on the hosted rail or on a machine they rented, on their
# own credits (docs/HOSTING.md).

# ── the studio frontend ─────────────────────────────────────────────────────
FROM node:22-bookworm-slim AS frontend
RUN corepack enable
WORKDIR /src/packages/open-generative-ai
COPY packages/open-generative-ai/package.json packages/open-generative-ai/pnpm-lock.yaml packages/open-generative-ai/pnpm-workspace.yaml ./
RUN pnpm install --frozen-lockfile
COPY packages/open-generative-ai/ ./
# The build stamps the product version from the one file that owns it.
COPY pyproject.toml /src/pyproject.toml
RUN pnpm run vite:build

# ── the photo editor ────────────────────────────────────────────────────────
# PhotoCraft (MIT OR Apache-2.0) runs in the visitor's browser as WebAssembly;
# this stage builds it from its pinned release with the studio's host bridge
# (scripts/install_photocraft.sh). Thin LTO and one job keep the compiler
# inside a small builder's memory, as upstream's own Dockerfile does.
FROM rust:1.98-bookworm AS photocraft
RUN apt-get update \
 && apt-get install -y --no-install-recommends python3 git curl zip ca-certificates \
 && rm -rf /var/lib/apt/lists/*
WORKDIR /src
COPY scripts/install_photocraft.sh scripts/install_photocraft.sh
COPY patches/photocraft-host-bridge.patch patches/photocraft-host-bridge.patch
ENV CARGO_PROFILE_WASM_RELEASE_LTO=thin CARGO_BUILD_JOBS=1
RUN ./scripts/install_photocraft.sh --web

# ── the agent MCP's two packages ────────────────────────────────────────────
# bin/media-studio-mcp.mjs imports these and nothing else from npm (its other
# import is a sibling file). Pinned to the versions the desktop runs.
FROM node:22-bookworm-slim AS mcpdeps
WORKDIR /mcp
RUN npm init -y >/dev/null \
 && npm install --omit=dev --no-audit --no-fund @modelcontextprotocol/sdk@1.29.0 zod@4.4.3

# ── the control API ─────────────────────────────────────────────────────────
FROM python:3.12-slim-bookworm AS app
RUN apt-get update \
 && apt-get install -y --no-install-recommends ffmpeg git ca-certificates openssh-client libstdc++6 \
 && rm -rf /var/lib/apt/lists/*
COPY --from=ghcr.io/astral-sh/uv:0.8 /uv /usr/local/bin/uv
# Node for the agent MCP, from the same bookworm base the frontend built on.
COPY --from=mcpdeps /usr/local/bin/node /usr/local/bin/node
WORKDIR /app
COPY . .
COPY --from=frontend /src/packages/open-generative-ai/dist /app/packages/open-generative-ai/dist
COPY --from=photocraft /src/vendor/photocraft-web /app/vendor/photocraft-web
COPY --from=mcpdeps /mcp/node_modules /app/packages/media-gateway/node_modules
# The AGPL-3.0 §13 offer: every network user can download the exact source
# this image runs, from /source (api/legal.py). Built from what was copied in,
# so it is the deployed tree by construction.
RUN tar --exclude=./packages/open-generative-ai/dist --exclude=./.venv --exclude=./vendor/photocraft-web \
        --exclude=./packages/media-gateway/node_modules \
        -czf /tmp/source.tar.gz -C /app . \
 && mv /tmp/source.tar.gz /app/source.tar.gz
# The image's own Python (3.12, inside pyproject's >=3.11,<3.13), never one uv
# downloads: a downloaded interpreter lands in root's home, which the runtime
# user cannot read, and the venv would point at nothing.
ENV UV_LINK_MODE=copy UV_PROJECT_ENVIRONMENT=/app/.venv \
    UV_PYTHON=/usr/local/bin/python3.12 UV_PYTHON_DOWNLOADS=never
# The mcp extra: a member's agents reach the hosted studio at /mcp (hosted_mcp.py).
RUN uv sync --frozen --no-dev --extra mcp
RUN useradd --create-home --uid 10001 studio && mkdir -p /data && chown studio:studio /data
USER studio

ENV CONTENT_STUDIO_HOSTED=1 \
    CONTENT_STUDIO_CONTROL_HOST=0.0.0.0 \
    CONTENT_STUDIO_CONTROL_PORT=8765 \
    CONTENT_STUDIO_DATA_DIR=/data \
    HIVEMIND_MEDIA_STATE_DIR=/data/media-state \
    CONTENT_STUDIO_FRONTEND_DIST=/app/packages/open-generative-ai/dist \
    CONTENT_STUDIO_PHOTOCRAFT_DIST=/app/vendor/photocraft-web \
    CONTENT_STUDIO_SOURCE_ARCHIVE=/app/source.tar.gz \
    HOME=/data/home \
    PATH=/app/.venv/bin:$PATH
VOLUME ["/data"]
EXPOSE 8765
HEALTHCHECK --interval=30s --timeout=5s --start-period=120s --retries=3 \
  CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8765/healthz', timeout=4).status == 200 else 1)"
# Run through bash, not by the file's execute bit: a commit that wrote entrypoint.sh as 100644
# took studio.hivemindos.app down twice on 2026-11-15.
CMD ["bash", "/app/deploy/hosted/entrypoint.sh"]
