-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathemulation.ts
More file actions
89 lines (79 loc) · 2.55 KB
/
emulation.ts
File metadata and controls
89 lines (79 loc) · 2.55 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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {PredefinedNetworkConditions} from 'puppeteer-core';
import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';
import {ToolCategories} from './categories.js';
import {defineTool} from './ToolDefinition.js';
const throttlingOptions: [string, ...string[]] = [
'No emulation',
'Offline',
...Object.keys(PredefinedNetworkConditions),
];
export const emulateNetwork = defineTool({
name: 'emulate_network',
description: `Emulates network conditions such as throttling or offline mode on the selected page.`,
annotations: {
category: ToolCategories.EMULATION,
readOnlyHint: false,
},
schema: {
throttlingOption: zod
.enum(throttlingOptions)
.describe(
`The network throttling option to emulate. Available throttling options are: ${throttlingOptions.join(', ')}. Set to "No emulation" to disable. Set to "Offline" to simulate offline network conditions.`,
),
},
handler: async (request, _response, context) => {
const page = context.getSelectedPage();
const conditions = request.params.throttlingOption;
if (conditions === 'No emulation') {
await page.emulateNetworkConditions(null);
context.setNetworkConditions(null);
return;
}
if (conditions === 'Offline') {
await page.emulateNetworkConditions({
offline: true,
download: 0,
upload: 0,
latency: 0,
});
context.setNetworkConditions('Offline');
return;
}
if (conditions in PredefinedNetworkConditions) {
const networkCondition =
PredefinedNetworkConditions[
conditions as keyof typeof PredefinedNetworkConditions
];
await page.emulateNetworkConditions(networkCondition);
context.setNetworkConditions(conditions);
}
},
});
export const emulateCpu = defineTool({
name: 'emulate_cpu',
description: `Emulates CPU throttling by slowing down the selected page's execution.`,
annotations: {
category: ToolCategories.EMULATION,
readOnlyHint: false,
},
schema: {
throttlingRate: zod
.number()
.min(1)
.max(20)
.describe(
'The CPU throttling rate representing the slowdown factor 1-20x. Set the rate to 1 to disable throttling',
),
},
handler: async (request, _response, context) => {
const page = context.getSelectedPage();
const {throttlingRate} = request.params;
await page.emulateCPUThrottling(throttlingRate);
context.setCpuThrottlingRate(throttlingRate);
},
});