FROM nousresearch/hermes-agent:latest

ARG BUN_VERSION=1.3.14
ARG JJ_VERSION=0.41.0
ARG NODE_VERSION=22.19.0
ARG TARGETARCH

USER root

RUN apt-get update \
  && apt-get install -y --no-install-recommends \
    gnupg2 \
    gh \
    jq \
    lsof \
    neovim \
    pass \
    unzip \
    xz-utils \
  && rm -rf /var/lib/apt/lists/*

ENV BUN_INSTALL=/opt/bun
RUN curl -fsSL https://bun.sh/install | bash -s "bun-v${BUN_VERSION}"
ENV PATH="/opt/bun/bin:${PATH}"

RUN set -eux; \
  case "${TARGETARCH:-amd64}" in \
    amd64) node_arch="x64" ;; \
    arm64) node_arch="arm64" ;; \
    *) echo "unsupported TARGETARCH=${TARGETARCH}" >&2; exit 1 ;; \
  esac; \
  curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-${node_arch}.tar.xz" \
    | tar -xJ -C /usr/local --strip-components=1

RUN npm install -g --ignore-scripts @earendil-works/pi-coding-agent

ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
RUN npm install -g @playwright/test \
  && npx playwright install --with-deps chromium

RUN curl -fsSL https://tailscale.com/install.sh | sh

RUN set -eux; \
  case "${TARGETARCH:-amd64}" in \
    amd64) jj_arch="x86_64-unknown-linux-musl" ;; \
    arm64) jj_arch="aarch64-unknown-linux-musl" ;; \
    *) echo "unsupported TARGETARCH=${TARGETARCH}" >&2; exit 1 ;; \
  esac; \
  curl -fsSL "https://github.com/jj-vcs/jj/releases/download/v${JJ_VERSION}/jj-v${JJ_VERSION}-${jj_arch}.tar.gz" \
    | tar -xz -C /usr/local/bin ./jj

COPY fly-hermes-entrypoint.sh /usr/local/bin/fly-hermes-entrypoint
RUN chmod +x /usr/local/bin/fly-hermes-entrypoint

ENTRYPOINT ["/usr/local/bin/fly-hermes-entrypoint"]
CMD ["gateway", "run"]
