# syntax=docker/dockerfile:1.7
# ==============================================================================
# @vexa/bot — the v0.12 carved meetings WORKER (P7 ephemeral container).
#
# Boots from VEXA_BOT_CONFIG (invocation.v1), joins a meeting via @vexa/join over
# a @vexa/remote-browser page, captures → @vexa/{gmeet,mixed}-pipeline →
# @vexa/transcribe-whisper, records via @vexa/recording, then dies.
#
# Two stages, BOTH on `vexa/meet-join-env:dev` (the published Playwright +
# humanized-X11 env, node 22 + corepack, chromium prebaked in /ms-playwright):
#   • builder  — corepack pnpm, install the whole v0.12 workspace, build @vexa/bot
#                and its workspace deps (pnpm --filter @vexa/bot... build).
#   • runtime  — same base, carry the built workspace tree + node_modules, add the
#                bot's own audio deps (pulseaudio/fluxbox), run node dist/index.js
#                under Xvfb/PulseAudio via the entrypoint.
#
# Building ON the env image keeps node ABI + the pnpm symlink farm valid across
# stages (same base layout), and reuses the browsers already in /ms-playwright
# (PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1) instead of re-fetching them.
#
# Build context = the v0.12 repo root (pnpm-workspace.yaml is there). Build with:
#   docker build -t vexaai/vexa-bot:v012 -f meetings/services/bot/Dockerfile .
# ==============================================================================

# ------------------------------------------------------------------------------
# Stage 1: builder — install + build the workspace slice @vexa/bot depends on.
# ------------------------------------------------------------------------------
FROM vexa/meet-join-env:dev AS builder

ENV PNPM_HOME=/pnpm \
    PATH=/pnpm:$PATH \
    # Browsers are already in the env image (/ms-playwright) — never re-download
    # them during `pnpm install` (playwright/remote-browser have a postinstall).
    PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 \
    PLAYWRIGHT_BROWSERS_PATH=/ms-playwright \
    # onnxruntime-node (via @vexa/mixed-pipeline) — skip the optional CUDA fetch;
    # the segmenter runs CPU and the smoke/gmeet path never loads it anyway.
    CI=1

# pnpm pinned to the workspace's packageManager (pnpm@11.7.0) via corepack.
RUN corepack enable && corepack prepare pnpm@11.7.0 --activate && pnpm --version

WORKDIR /build

# Workspace manifests first (best layer cache for the install step). Copying the
# whole tree is simplest+robust for a carved monorepo; the dep layer still caches
# on the lockfile via the cache mount below.
COPY pnpm-workspace.yaml pnpm-lock.yaml package.json tsconfig.base.json turbo.json ./
# The source the bot build needs: the five backend domains under core/ (the bot + its @vexa/* workspace
# deps all live under core/meetings/modules/*). The other pnpm-workspace globs
# (integrations/* · clients/* · sdks/* · tools/*) are green-on-empty: a fresh git clone omits empty
# dirs and the bot depends on none of them, so they are intentionally NOT copied (COPYing an absent
# dir fails the build).
COPY core ./core
# NB: `scripts/` and `schemas/` are pnpm-workspace globs that are green-on-empty in this carved tree
# (no packages) — they aren't present in the open-core repo, so they're intentionally not COPYed here.

# Install the whole workspace from pnpm-lock.yaml as it is (--frozen-lockfile): the bot carries native
# modules, and the image runs exactly the versions the lockfile pins. A stale lockfile fails the build;
# refresh it with `pnpm install` at the repo root and commit it (scripts/image-lockfile.test.mjs).
# allowBuilds in pnpm-workspace.yaml lets esbuild/onnxruntime-node/sharp/protobufjs
# run their native postinstall; the playwright postinstall no-ops (SKIP flag above).
RUN --mount=type=cache,id=pnpm-store,target=/pnpm/store \
    pnpm install --frozen-lockfile

# Build the page-side capture bricks FIRST. These are NOT bot dependencies
# (gate:isolation — they are a BROWSER bundle, not a Node import), so the
# `@vexa/bot...` selector below does NOT pull them. The bot's own `build` script
# runs build-browser-utils.mjs, which esbuild-bundles these bricks' dist into
# window.VexaBrowserUtils (the global capture-bridge.ts injects via addInitScript) —
# so their dist/ must already exist when the bot build runs.
RUN pnpm --filter "@vexa/capture-codec" --filter "@vexa/gmeet-capture" --filter "@vexa/mixed-capture-core" --filter "@vexa/record-chunker" --filter "@vexa/jitsi-capture" --filter "@vexa/teams-capture" --filter "@vexa/zoom-capture" run build

# Build @vexa/bot and everything it depends on (the `...` selector pulls the deps;
# pnpm runs them in topological order, each package's own `build` = tsc → dist/).
# The bot's build = `tsc && node build-browser-utils.mjs`, so this also emits the
# capture bundle dist/browser-utils.global.js (the bricks above are now built).
RUN pnpm --filter "@vexa/bot..." run build

# Sanity: the entry the runtime will exec + the capture bundle must exist after the build.
RUN test -f /build/core/meetings/services/bot/dist/index.js \
    && echo "[builder] @vexa/bot dist/index.js present" \
    && test -s /build/core/meetings/services/bot/dist/browser-utils.global.js \
    && echo "[builder] capture bundle browser-utils.global.js present ($(wc -c < /build/core/meetings/services/bot/dist/browser-utils.global.js) bytes)"

# Bake the mixed-lane diarization model (onnx-community/pyannote-segmentation-3.0)
# into an image-resident HF cache so the Zoom/Teams lane loads it OFFLINE at
# runtime — no HuggingFace download stall mid-meeting (prep for O6). The builder
# has network, so this downloads + caches into /opt/hf-cache; the runtime stage
# copies that dir and points VEXA_HF_CACHE at it (chunked-transcriber.ts reads it).
ENV VEXA_HF_CACHE=/opt/hf-cache
# The script self-resolves @huggingface/transformers via the mixed-pipeline
# package (the bot doesn't list it as a direct dep — it's transitive), so CWD
# doesn't matter; run from /build for a stable path to the script.
RUN cd /build && node core/meetings/services/bot/warm-hf-cache.mjs

# ------------------------------------------------------------------------------
# Stage 2: runtime — the ephemeral worker. Same env base; add the bot's own audio
# deps; carry the built workspace tree (incl. node_modules symlink farm) intact.
# ------------------------------------------------------------------------------
FROM vexa/meet-join-env:dev AS runtime

# The bot's OWN runtime deps NOT in the meet-join env: PulseAudio (the speak/TTS
# + capture audio chain), fluxbox (a WM for the X11 session), and the aws CLI —
# the authenticated-bot session store (@vexa/remote-browser session-store.ts)
# shells out to `aws s3` to restore the signed-in profile before launch and write
# it back on clean teardown; without the binary every authenticated spawn fails
# at session-restore. The Playwright + humanized-X11 stack (xvfb·xdotool·xclip·
# x11vnc·websockify) comes from the base.
# The Playwright base pins a browser and therefore an Ubuntu snapshot; `apt-get upgrade` takes the
# archive's security updates since (OpenSSL, GnuPG) without moving the browser. The bot runs `node` and
# nothing else, so the base's package managers (npm, corepack, yarn) and the dependencies npm bundles go.
# noble (#1011) dropped `awscli` from the archive — `apt-get install awscli` has no
# installation candidate there, which broke this image the moment the base moved. AWS's
# own v2 installer is the supported path on 24.04; it is the same binary the session
# store shells out to, so nothing above this line changes.
RUN export DEBIAN_FRONTEND=noninteractive \
    && apt-get update \
    && apt-get upgrade -y --no-install-recommends \
    && apt-get install -y --no-install-recommends \
       pulseaudio \
       fluxbox \
       socat \
       ca-certificates \
       curl \
       unzip \
    && curl -fsSL "https://awscli.amazonaws.com/awscli-exe-linux-$(uname -m).zip" -o /tmp/awscliv2.zip \
    && unzip -q /tmp/awscliv2.zip -d /tmp \
    && /tmp/aws/install \
    && rm -rf /tmp/aws /tmp/awscliv2.zip \
    && rm -rf /var/lib/apt/lists/* \
    && aws --version \
    && rm -rf /usr/lib/node_modules/npm /usr/lib/node_modules/corepack /usr/lib/node_modules/yarn \
         /usr/bin/npm /usr/bin/npx /usr/bin/corepack /usr/bin/yarn /usr/bin/yarnpkg

# certutil, for corporate TLS inspection: entrypoint.sh imports VEXA_EXTRA_CA_FILE into Chromium's
# NSS store (Chromium on Linux reads no CA bundle file). Its own layer, so the one above is unchanged.
RUN export DEBIAN_FRONTEND=noninteractive \
    && apt-get update \
    && apt-get install -y --no-install-recommends libnss3-tools \
    && rm -rf /var/lib/apt/lists/* \
    && certutil -H >/dev/null 2>&1; command -v certutil

ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright \
    DISPLAY=:99 \
    NODE_ENV=production

# Which build this is. It is stamped into every captured-signal tape header and into each sidecar's
# mini-header, because a prod fixture outlives the deployment that produced it: the same observation
# ("the bot dropped the tail") means opposite things on either side of a fix, and a tape that cannot
# name its build proves a behaviour it cannot attribute. The build passes the git sha
# (`--build-arg VEXA_IMAGE_VERSION=$(git rev-parse --short HEAD)`); an unstamped image records
# 'unknown', which is the honest answer rather than a guess.
ARG VEXA_IMAGE_VERSION=unknown
ENV VEXA_IMAGE_VERSION=${VEXA_IMAGE_VERSION}

WORKDIR /app

# Carry the entire built workspace (sources, dist/, and the pnpm node_modules
# symlink farm). Same base ⇒ the workspace symlinks (node_modules/@vexa/* →
# ../../meetings/modules/*) and the .pnpm hardlink store stay valid; no install
# in runtime. The bot is run from its package dir so its relative schema-path
# (src→../../../contracts/invocation.v1) and pnpm-linked deps resolve.
COPY --from=builder /build /app

# The page-side capture bundle, at the EXACT path capture-bridge.ts injects via
# addInitScript: BROWSER_UTILS_PATH = '/app/browser-utils.global.js'. It is already
# in the tree above (meetings/services/bot/dist/), but the bot resolves it from
# /app, so place it there explicitly so window.VexaBrowserUtils is present on every
# page navigation (the gmeet per-channel + mixed-lane capture wiring depends on it).
COPY --from=builder /build/core/meetings/services/bot/dist/browser-utils.global.js /app/browser-utils.global.js

# The baked HF model cache (mixed-lane pyannote segmentation), carried from the
# builder. VEXA_HF_CACHE points the @huggingface/transformers env.cacheDir here
# so the Zoom/Teams diarizer loads the model from disk with NO network at runtime.
COPY --from=builder /opt/hf-cache /opt/hf-cache
ENV VEXA_HF_CACHE=/opt/hf-cache
# License hygiene: the baked weights are onnx-community/pyannote-segmentation-3.0
# (MIT). MIT requires the copyright + permission notice travel with the software,
# so ship it next to the cache. Recorded in repo THIRD_PARTY_LICENSES.md + the
# per-release SPDX SBOM (scripts/sbom.mjs); model weights sit outside gate:licenses.
COPY licenses/onnx-community-pyannote-segmentation-3.0.LICENSE.txt /opt/hf-cache/LICENSE.pyannote-segmentation-3.0

# Entrypoint: bring up Xvfb + PulseAudio (the env the browser/capture/speak path
# expects), then run the worker. Boot → validate config → launch browser → join.
COPY core/meetings/services/bot/entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

# A WRITABLE HOME, because the UID is not ours to choose. Under OpenShift's `restricted-v2`
# SCC (and any PSA-restricted namespace) admission assigns a random per-namespace UID that
# exists in no /etc/passwd — measured on a real admitted bot pod: runAsUser 1000920000. With
# no HOME in the image the runtime falls back to `/`, which that UID cannot write, and the
# entrypoint's whole bringup dies on it: pulseaudio wants $HOME/.config/pulse, fluxbox wants
# $HOME/.fluxbox, Chromium wants $HOME/.config + $HOME/.cache, and the aws CLI wants
# $HOME/.aws. /tmp is mode 1777, so it is writable by every UID without knowing which one.
# See vexa-delivery docs/engineering/openshift-parity.mdx — "HOME=/tmp is the one-variable fix".
ENV HOME=/tmp

# A NON-ROOT uid, because Chromium will not sandbox a browser running as root, and nothing the bot
# does needs root: its display, audio daemon, browser profile and recordings all live in /tmp. The
# two places it writes outside /tmp are made writable by any uid (so OpenShift's assigned UID works
# as well as this default): the join module's checkpoint screenshots, and the X socket directory a
# non-root Xvfb cannot create under a root-owned /tmp/.X11-unix. The runtime runs the bot's own
# container under a seccomp profile that lets the browser build its sandbox (user namespaces).
RUN install -d -m 1777 /app/storage/screenshots /tmp/.X11-unix
USER 10002:10002

# noVNC (browser view) — present in the env base; harmless if unused.
EXPOSE 6080

ENTRYPOINT ["/app/entrypoint.sh"]
