-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathscreenshot.ts
More file actions
240 lines (224 loc) · 7.12 KB
/
screenshot.ts
File metadata and controls
240 lines (224 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {zod} from '../third_party/index.js';
import type {
BoundingBox,
ElementHandle,
Page,
ScreenshotClip,
} from '../third_party/index.js';
import {ToolCategory} from './categories.js';
import {definePageTool} from './ToolDefinition.js';
type ScreenshotFormat = 'png' | 'jpeg' | 'webp';
function isScreenshotFormat(value: unknown): value is ScreenshotFormat {
return value === 'png' || value === 'jpeg' || value === 'webp';
}
function isPositiveFiniteNumber(value: number | undefined): value is number {
return value !== undefined && Number.isFinite(value) && value > 0;
}
async function getSourceBox(
page: Page,
element: ElementHandle | undefined,
fullPage: boolean,
): Promise<BoundingBox | undefined> {
if (element) {
const box = await element.boundingBox();
return box ?? undefined;
}
if (fullPage) {
const dims = await page.evaluate(() => ({
width: Math.max(
document.documentElement.scrollWidth,
document.body?.scrollWidth ?? 0,
),
height: Math.max(
document.documentElement.scrollHeight,
document.body?.scrollHeight ?? 0,
),
}));
if (dims.width <= 0 || dims.height <= 0) {
return undefined;
}
return {x: 0, y: 0, width: dims.width, height: dims.height};
}
const viewport = page.viewport();
if (!viewport) {
return undefined;
}
return {x: 0, y: 0, width: viewport.width, height: viewport.height};
}
function computeDownscaleClip(
box: BoundingBox,
maxWidth: number | undefined,
maxHeight: number | undefined,
): ScreenshotClip | undefined {
const widthScale = isPositiveFiniteNumber(maxWidth)
? Math.min(1, maxWidth / box.width)
: 1;
const heightScale = isPositiveFiniteNumber(maxHeight)
? Math.min(1, maxHeight / box.height)
: 1;
const scale = Math.min(widthScale, heightScale);
if (scale >= 1) {
return undefined;
}
// Skip degenerate sub-pixel results.
if (Math.round(box.width * scale) < 1 || Math.round(box.height * scale) < 1) {
return undefined;
}
return {
x: box.x,
y: box.y,
width: box.width,
height: box.height,
scale,
};
}
export const screenshot = definePageTool(args => {
const {
screenshotFormat,
screenshotQuality,
screenshotMaxWidth,
screenshotMaxHeight,
} = args ?? {};
const defaultFormat: ScreenshotFormat = isScreenshotFormat(screenshotFormat)
? screenshotFormat
: 'png';
const defaultQuality = isPositiveFiniteNumber(screenshotQuality)
? screenshotQuality
: undefined;
const maxWidth = isPositiveFiniteNumber(screenshotMaxWidth)
? screenshotMaxWidth
: undefined;
const maxHeight = isPositiveFiniteNumber(screenshotMaxHeight)
? screenshotMaxHeight
: undefined;
return {
name: 'take_screenshot',
description: `Take a screenshot of the page or element.`,
annotations: {
category: ToolCategory.DEBUGGING,
// Not read-only due to filePath param.
readOnlyHint: false,
},
schema: {
format: zod
.enum(['png', 'jpeg', 'webp'])
.default(defaultFormat)
.describe(
`Type of format to save the screenshot as. Default is "${defaultFormat}"`,
),
quality: zod
.number()
.min(0)
.max(100)
.optional()
.describe(
'Compression quality for JPEG and WebP formats (0-100). Higher values mean better quality but larger file sizes. Ignored for PNG format.',
),
uid: zod
.string()
.optional()
.describe(
'The uid of an element on the page from the page content snapshot. If omitted, takes a page screenshot.',
),
fullPage: zod
.boolean()
.optional()
.describe(
'If set to true takes a screenshot of the full page instead of the currently visible viewport. Incompatible with uid.',
),
filePath: zod
.string()
.optional()
.describe(
'The absolute path, or a path relative to the current working directory, to save the screenshot to instead of attaching it to the response.',
),
},
handler: async (request, response, context) => {
if (request.params.uid && request.params.fullPage) {
throw new Error('Providing both "uid" and "fullPage" is not allowed.');
}
const page = request.page.pptrPage;
const element = request.params.uid
? await request.page.getElementByUid(request.params.uid)
: undefined;
const format = request.params.format;
const quality =
format === 'png'
? undefined
: (request.params.quality ?? defaultQuality);
const fullPage = request.params.fullPage ?? false;
// Compute downscale clip when maxWidth/maxHeight is set and the source
// exceeds either bound. The smaller scale factor wins so both bounds
// are respected while preserving aspect ratio.
let clip: ScreenshotClip | undefined;
if (maxWidth !== undefined || maxHeight !== undefined) {
const box = await getSourceBox(page, element, fullPage);
if (box) {
clip = computeDownscaleClip(box, maxWidth, maxHeight);
}
}
let screenshot: Uint8Array;
if (clip) {
// page.screenshot with clip lets the CDP scale param downscale the
// capture for viewport, full-page and element shots alike. We rely on
// Puppeteer's default of captureBeyondViewport=true when a clip is
// present so element/full-page captures below the fold still work.
screenshot = await page.screenshot({
type: format,
quality,
optimizeForSpeed: true,
clip,
});
} else if (element) {
screenshot = await element.screenshot({
type: format,
quality,
optimizeForSpeed: true,
});
} else {
screenshot = await page.screenshot({
type: format,
fullPage,
quality,
optimizeForSpeed: true,
});
}
if (request.params.uid) {
response.appendResponseLine(
`Took a screenshot of node with uid "${request.params.uid}".`,
);
} else if (fullPage) {
response.appendResponseLine(
'Took a screenshot of the full current page.',
);
} else {
response.appendResponseLine(
"Took a screenshot of the current page's viewport.",
);
}
if (request.params.filePath) {
const file = await context.saveFile(
screenshot,
request.params.filePath,
);
response.appendResponseLine(`Saved screenshot to ${file.filename}.`);
} else if (screenshot.length >= 2_000_000) {
const {filepath} = await context.saveTemporaryFile(
screenshot,
`screenshot.${request.params.format}`,
);
response.appendResponseLine(`Saved screenshot to ${filepath}.`);
} else {
response.attachImage({
mimeType: `image/${request.params.format}`,
data: Buffer.from(screenshot).toString('base64'),
});
}
},
};
});