FROM node:22-bookworm-slim

RUN corepack enable

WORKDIR /app

ENV NODE_ENV=production
ENV PORT=8791

COPY package.json pnpm-lock.yaml pnpm-workspace.yaml /app/
COPY patches /app/patches
COPY packages/types/package.json /app/packages/types/package.json
COPY ee/packages/utils/package.json /app/ee/packages/utils/package.json
COPY ee/packages/den-db/package.json /app/ee/packages/den-db/package.json
COPY ee/apps/inference/package.json /app/ee/apps/inference/package.json

RUN pnpm install --frozen-lockfile --trust-lockfile

COPY packages/types /app/packages/types
COPY ee/packages/utils /app/ee/packages/utils
COPY ee/packages/den-db /app/ee/packages/den-db
COPY ee/apps/inference /app/ee/apps/inference

RUN pnpm --dir /app/packages/types run build
RUN pnpm --dir /app/ee/packages/utils run build
RUN pnpm --dir /app/ee/packages/den-db run build
RUN pnpm --dir /app/ee/apps/inference run build

EXPOSE 8791

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 CMD node -e "fetch('http://127.0.0.1:' + (process.env.PORT || '8791') + '/health').then((res) => process.exit(res.ok ? 0 : 1)).catch(() => process.exit(1))"

CMD ["node", "--import", "/app/ee/apps/inference/dist/instrumentation.js", "/app/ee/apps/inference/dist/server.js"]
