feat(web-integration): use WebP for browser screenshot producers - #2858
feat(web-integration): use WebP for browser screenshot producers#2858quanruzhuoxiu wants to merge 5 commits into
Conversation
88e097e to
675a785
Compare
d84b795 to
3fde745
Compare
675a785 to
92530ad
Compare
3fde745 to
9e844d2
Compare
92530ad to
ebfa101
Compare
cf0b274 to
c31d6c4
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c31d6c41a2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| void canonicalizeRecorderScreenshot(dataUrl).then( | ||
| (webpDataUrl) => sendResponse(webpDataUrl), |
There was a problem hiding this comment.
Keep WebP encoding within the recorder's one-second budget
On high-resolution displays or slower machines, decoding the full PNG and completing OffscreenCanvas.convertToBlob() can take more than the fixed 1,000 ms timeout in event-recorder-bridge.ts:78-97. Because the response is now delayed until this conversion completes, the bridge times out and records the event without its screenshot even when captureVisibleTab succeeded; extend the capture budget or move conversion off the response-critical path.
Useful? React with 👍 / 👎.
| (error) => { | ||
| console.error( | ||
| '[ServiceWorker] Failed to encode recorder screenshot as WebP:', | ||
| error, | ||
| ); | ||
| sendResponse(null); |
There was a problem hiding this comment.
Preserve the PNG when WebP encoding fails
If createImageBitmap, the canvas context, or WebP encoding fails, this converts a successfully captured PNG into a null response; the recorder interprets that as no screenshot and silently omits the before/after image. Return the original PNG as a fallback or propagate an explicit failure instead of discarding valid capture data.
AGENTS.md reference: AGENTS.md:L6-L8
Useful? React with 👍 / 👎.
Summary
chat.completions.create()and verify that Markdown exports the same WebP bytes, without depending on the independent model-input recorder PRThe reusable PNG/JPEG-to-WebP implementation lives in
packages/shared/src/img/transform.ts:canonicalizeScreenshotBase64()callsconvertImgBufferToWebp(), which uses Sharp in Node and the browser encoder for RGBA pixels. This PR wires browser producers to native WebP capture where possible and uses that shared encoder only for fallback paths.WebP generation paths
packages/web-integration/src/chrome-extension/screenshot.tsPage.captureScreenshot({ format: 'webp', quality: 90 })packages/web-integration/src/puppeteer/base-page.tspage.screenshot({ type: 'webp', quality: 90 })packages/web-integration/src/puppeteer/base-page.tspackages/web-integration/src/puppeteer/base-page.tsandpackages/shared/src/img/transform.tscanonicalizeScreenshotBase64()encodes WebP with Sharpapps/chrome-extension/src/utils/screenshot.tscaptureVisibleTab()produces PNG, thenOffscreenCanvas.convertToBlob({ type: 'image/webp', quality: 0.9 })encodes WebPapps/chrome-extension/src/extension/recorder/screenshot-export.tsStack
Validation
pnpm --filter chrome-extension test(10 files, 40 tests passed)pnpm --filter @midscene/playground test(19 files, 274 tests passed)pnpm --filter @midscene/web test(50 files, 412 passed, 1 skipped)pnpm exec vitest --run tests/unit-test/base-page-screenshot.test.tsfrompackages/web-integration(5 tests passed)pnpm exec playwright test tests/ai/web/playwright/screenshot-cdp-webp.spec.ts --config=tests/playwright.config.tsfrompackages/web-integration(1 test passed)pnpm exec nx test:ai @midscene/web --skip-nx-cache -- tests/ai/web/puppeteer/webp-report.test.ts(4 consecutive runs passed)pnpm exec nx build @midscene/webpnpm exec nx run-many -t build -p chrome-extension @midscene/playground @midscene/webpnpm run type-check:testspnpm run lint