# The OpenBot management agent: one small process, run unprivileged. It talks to the Kubernetes API
# with its own in-process client and its ServiceAccount; the image carries no kubectl.
#
# Built from the repository root: docker build --file management-agent/Dockerfile .
#
# The agent is bundled into one file in the first stage, so the image carries no node_modules.

# oven/bun:1.4.3-slim, pinned.
FROM oven/bun@sha256:14a223fa35747ec728bce300ef85888bc5f7958164d15bbba6a54b22b52a3288 AS build
WORKDIR /src
COPY package.json bun.lock ./
COPY app/package.json app/package.json
COPY shared/package.json shared/package.json
COPY extension/package.json extension/package.json
COPY management-agent/package.json management-agent/package.json
COPY tokens/package.json tokens/package.json
RUN bun install --frozen-lockfile --ignore-scripts --production \
      --filter @openbot/shared --filter @openbot/management-agent
COPY shared/management-contract.ts shared/kubernetes-promotion.ts shared/release-manifest.ts shared/
COPY management-agent/src management-agent/src
ARG AGENT_VERSION=0.1.0
RUN bun build management-agent/src/main.ts --target bun \
      --define "process.env.OPENBOT_AGENT_VERSION=\"${AGENT_VERSION}\"" \
      --outfile /out/agent.js

FROM oven/bun@sha256:14a223fa35747ec728bce300ef85888bc5f7958164d15bbba6a54b22b52a3288
# The repository this image is built from, as every published OpenBot image names it.
LABEL org.opencontainers.image.source="https://github.com/CopilotKit/OpenBot"
COPY --from=build /out/agent.js /app/agent.js
# Not root, and no shell needed at run time. /tmp is the writable volume; with no kubeconfig under
# $HOME the client uses the pod's ServiceAccount.
ENV HOME=/tmp
USER 65532:65532
WORKDIR /tmp
ENTRYPOINT ["bun", "/app/agent.js"]
