Chrome DevTools for coding agents
fix: record screencast at the page's device pixel size (#3155)
Alex Rudenko62e2aa404466
diff --git a/src/tools/screencast.ts b/src/tools/screencast.tsindex 5db386ca..3703c722 100644--- a/src/tools/screencast.ts+++ b/src/tools/screencast.ts@@ -11,6 +11,7 @@ import path from 'node:path';import type {ParsedArguments} from '../config/ConfigParser.js';import {zod} from '../third_party/index.js';import type {+ Page,ScreenRecorder,ScreenRecording,VideoFormat,@@ -52,6 +53,23 @@ function getLegacyExtension(filePath: string): SupportedVideoExtension {return matchedExtension ?? '.mp4';}+/**+ * Returns the size of the page viewport in device pixels.+ */+async function getPagePixelSize(+ pptrPage: Page,+): Promise<{maxWidth: number; maxHeight: number}> {+ const {width, height, dpr} = await pptrPage.evaluate(() => ({+ width: window.innerWidth,+ height: window.innerHeight,+ dpr: window.devicePixelRatio,+ }));+ return {+ maxWidth: Math.max(1, Math.round(width * dpr)),+ maxHeight: Math.max(1, Math.round(height * dpr)),+ };+}+export const startScreencast = definePageTool((args: ParsedArguments) => ({name: 'screencast_start',description: `Starts recording a screencast (video) of the target page.`,@@ -102,9 +120,14 @@ export const startScreencast = definePageTool((args: ParsedArguments) => ({fps: args?.screencastFps,});} else {+ // Without explicit limits Chrome downscales frames to a small default+ // size. Record at the actual device pixel size of the page instead.+ const {maxWidth, maxHeight} = await getPagePixelSize(page.pptrPage);recorder = await page.pptrPage.record({path: resolvedPath,fps: args?.screencastFps,+ maxWidth,+ maxHeight,});}} catch (err) {diff --git a/tests/tools/screencast.test.ts b/tests/tools/screencast.test.tsindex 9578ff50..47977b65 100644--- a/tests/tools/screencast.test.ts+++ b/tests/tools/screencast.test.ts@@ -30,6 +30,7 @@ function createScreencastMocks() {const mockRecording = sinon.createStubInstance(CdpScreenRecording);const mockRecorder = sinon.createStubInstance(ScreenRecorder);page.pptrPage.record.resolves(mockRecording);+ page.pptrPage.evaluate.resolves({width: 800, height: 600, dpr: 2});page.pptrPage.screencast.resolves(mockRecorder);page.getScreenRecorder.returns(null);return {page, context, response, mockRecording, mockRecorder, args};@@ -65,6 +66,8 @@ describe('screencast', () => {sinon.assert.calledOnceWithExactly(page.pptrPage.record, {path: filePath,fps: undefined,+ maxWidth: 1600,+ maxHeight: 1200,});sinon.assert.notCalled(page.pptrPage.screencast);sinon.assert.calledOnceWithExactly(page.setScreenRecorder, {@@ -98,6 +101,8 @@ describe('screencast', () => {sinon.assert.calledOnceWithExactly(page.pptrPage.record, {path: expectedPath,fps: undefined,+ maxWidth: 1600,+ maxHeight: 1200,});sinon.assert.calledOnceWithExactly(page.setScreenRecorder, {recorder: mockRecording,@@ -142,6 +147,8 @@ describe('screencast', () => {sinon.assert.calledOnceWithExactly(page.pptrPage.record, {path: filePath,fps: 10,+ maxWidth: 1600,+ maxHeight: 1200,});});@@ -157,6 +164,8 @@ describe('screencast', () => {sinon.assert.calledOnceWithExactly(page.pptrPage.record, {path: filePath,fps: 10,+ maxWidth: 1600,+ maxHeight: 1200,});});