@@ -11,6 +11,7 @@ import {
1111 HTTPRequest ,
1212 Page ,
1313 SerializedAXNode ,
14+ PredefinedNetworkConditions ,
1415} from 'puppeteer-core' ;
1516import { Context } from './tools/ToolDefinition.js' ;
1617import { Debugger } from 'debug' ;
@@ -36,6 +37,23 @@ export interface TextSnapshot {
3637const DEFAULT_TIMEOUT = 5_000 ;
3738const NAVIGATION_TIMEOUT = 10_000 ;
3839
40+ function getNetworkMultiplierFromString ( condition : string | null ) : number {
41+ const puppeteerCondition =
42+ condition as keyof typeof PredefinedNetworkConditions ;
43+
44+ switch ( puppeteerCondition ) {
45+ case 'Fast 4G' :
46+ return 1 ;
47+ case 'Slow 4G' :
48+ return 2.5 ;
49+ case 'Fast 3G' :
50+ return 5 ;
51+ case 'Slow 3G' :
52+ return 10 ;
53+ }
54+ return 1 ;
55+ }
56+
3957export class McpContext implements Context {
4058 browser : Browser ;
4159 logger : Debugger ;
@@ -136,6 +154,7 @@ export class McpContext implements Context {
136154 } else {
137155 this . #networkConditionsMap. set ( page , conditions ) ;
138156 }
157+ this . #updateSelectedPageTimeouts( ) ;
139158 }
140159
141160 getNetworkConditions ( ) : string | null {
@@ -146,6 +165,7 @@ export class McpContext implements Context {
146165 setCpuThrottlingRate ( rate : number ) : void {
147166 const page = this . getSelectedPage ( ) ;
148167 this . #cpuThrottlingRateMap. set ( page , rate ) ;
168+ this . #updateSelectedPageTimeouts( ) ;
149169 }
150170
151171 getCpuThrottlingRate ( ) : number {
@@ -205,12 +225,22 @@ export class McpContext implements Context {
205225 this . #selectedPageIdx = idx ;
206226 const newPage = this . getSelectedPage ( ) ;
207227 newPage . on ( 'dialog' , this . #dialogHandler) ;
228+ this . #updateSelectedPageTimeouts( ) ;
229+ }
208230
231+ #updateSelectedPageTimeouts( ) {
232+ const page = this . getSelectedPage ( ) ;
209233 // For waiters 5sec timeout should be sufficient.
210- newPage . setDefaultTimeout ( DEFAULT_TIMEOUT ) ;
234+ // Increased in case we throttle the CPU
235+ const cpuMultiplier = this . getCpuThrottlingRate ( ) ;
236+ page . setDefaultTimeout ( DEFAULT_TIMEOUT * cpuMultiplier ) ;
211237 // 10sec should be enough for the load event to be emitted during
212238 // navigations.
213- newPage . setDefaultNavigationTimeout ( NAVIGATION_TIMEOUT ) ;
239+ // Increased in case we throttle the network requests
240+ const networkMultiplier = getNetworkMultiplierFromString (
241+ this . getNetworkConditions ( ) ,
242+ ) ;
243+ page . setDefaultNavigationTimeout ( NAVIGATION_TIMEOUT * networkMultiplier ) ;
214244 }
215245
216246 async getElementByUid ( uid : string ) : Promise < ElementHandle < Element > > {
@@ -315,9 +345,26 @@ export class McpContext implements Context {
315345 return this . #traceResults;
316346 }
317347
348+ getWaitForHelper (
349+ page : Page ,
350+ cpuMultiplier : number ,
351+ networkMultiplier : number ,
352+ ) {
353+ return new WaitForHelper ( page , cpuMultiplier , networkMultiplier ) ;
354+ }
355+
318356 waitForEventsAfterAction ( action : ( ) => Promise < unknown > ) : Promise < void > {
319357 const page = this . getSelectedPage ( ) ;
320- const waitForHelper = new WaitForHelper ( page ) ;
358+ const cpuMultiplier = this . getCpuThrottlingRate ( ) ;
359+ const networkMultiplier = getNetworkMultiplierFromString (
360+ this . getNetworkConditions ( ) ,
361+ ) ;
362+
363+ const waitForHelper = this . getWaitForHelper (
364+ page ,
365+ cpuMultiplier ,
366+ networkMultiplier ,
367+ ) ;
321368 return waitForHelper . waitForEventsAfterAction ( action ) ;
322369 }
323370}
0 commit comments