# Playwright MCP addon image (contract G.1): Playwright MCP installed in the toolbox (its Node runs
# it), shipped as the installed tree only, mounted read-only at /opt/addons/playwright. The browser
# is the toolbox's Chromium: no Playwright browser download.
#
# Not pinned: every rebuild installs the latest release; the check (playwright-mcp-check) makes
# sure it still drives the toolbox's Chromium.
ARG TOOLBOX_IMAGE=localhost/handler-ai-toolbox:latest
FROM ${TOOLBOX_IMAGE} AS build
ARG PREFIX=/opt/addons/playwright

COPY handler/ /tmp/handler/
RUN PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm install --prefix "$PREFIX" --no-fund --no-audit @playwright/mcp \
    && npm cache clean --force \
    && pkg="$PREFIX/node_modules/@playwright/mcp" \
    && cli="$(jq -er '.bin | if type == "string" then . else (to_entries[0].value) end' "$pkg/package.json")" \
    && mkdir -p "$PREFIX/bin" "$PREFIX/lib" \
    && ln -s "../node_modules/@playwright/mcp/${cli#./}" "$PREFIX/lib/mcp-cli.js" \
    && install -m 0755 /tmp/handler/playwright-mcp.mjs /tmp/handler/playwright-mcp-check.mjs "$PREFIX/lib/" \
    && ln -s ../lib/playwright-mcp.mjs "$PREFIX/bin/playwright-mcp" \
    && ln -s ../lib/playwright-mcp-check.mjs "$PREFIX/bin/playwright-mcp-check" \
    && mcp="$(jq -er .version "$pkg/package.json")" \
    && core="$(jq -er .version "$PREFIX/node_modules/playwright-core/package.json")" \
    && echo "Playwright MCP ${mcp} (Playwright ${core})" > "$PREFIX/version" \
    && rm -rf /home/agent /tmp/handler  # build-time npm logs (npm runs as root with HOME=/home/agent)

FROM scratch
COPY --from=build /opt/addons/playwright /
LABEL org.opencontainers.image.title="handler-ai-addon-playwright"
