@@ -26,7 +26,9 @@ const AGENT_INPUT_COMMAND =
2626 / " m e t h o d " : " (?: I n p u t \. | P a g e \. n a v i g a t e " | R u n t i m e \. (?: e v a l u a t e | c a l l F u n c t i o n O n ) " ) / ;
2727/** The same commands as above, by method, for the relay's sends. */
2828const AGENT_INPUT_METHOD = / ^ (?: I n p u t \. | P a g e \. n a v i g a t e $ | R u n t i m e \. (?: e v a l u a t e | c a l l F u n c t i o n O n ) $ ) / ;
29- const AGENT_DOWNLOAD_WINDOW_MS = 5_000 ;
29+ const AGENT_INPUT_WINDOW_MS = 5_000 ;
30+ /** A viewer's pointer travelling over the stream activates nothing on the page. */
31+ const POINTER_TRAVEL = new Set < unknown > ( [ "mouseMoved" , "mouseWheel" ] ) ;
3032const FOCUS_IDLE = "5 seconds" ;
3133/** Well inside the server's 10 s DESKTOP_ATTACH_TIMEOUT, for a first page that never reports it is up. */
3234const FIRST_PAGE_TIMEOUT = "5 seconds" ;
@@ -60,6 +62,8 @@ interface AttachedTab {
6062 downloadDirectory : string | null ;
6163 pendingDownloadGuid : string | null ;
6264 agentInputAt : number ;
65+ /** When the agent last drove the page with no person's input through a viewer since. */
66+ agentLedAt : number ;
6367 focusEmulated : boolean ;
6468 focusChange : Promise < void > | null ;
6569 focusUses : number ;
@@ -98,6 +102,8 @@ export class DesktopBrowserHost extends Context.Service<
98102 readonly loadFailed : ( key : DesktopBrowserTabKey , failure : PreviewAutomationLoadError ) => void ;
99103 readonly setShown : ( key : DesktopBrowserTabKey , shown : boolean | undefined ) => void ;
100104 readonly humanStartedDownload : ( source : Electron . WebContents ) => boolean ;
105+ /** Whether what the page does now follows the agent's input, not a viewer's. */
106+ readonly agentLedPage : ( source : Electron . WebContents ) => boolean ;
101107 readonly placeDownload : ( source : Electron . WebContents , item : Electron . DownloadItem ) => boolean ;
102108 readonly pointers : Stream . Stream < {
103109 readonly key : DesktopBrowserTabKey ;
@@ -351,11 +357,13 @@ export const make = Effect.gen(function* () {
351357 if ( tab . screencasts . size > 0 ) return Promise . resolve ( { } ) ;
352358 }
353359 // A person's input through a viewer, or Playwright's own housekeeping, is not the agent's.
354- if (
355- AGENT_INPUT_METHOD . test ( method ) &&
356- ( owner === undefined || ! streamingOwners . has ( owner ) )
357- ) {
358- tab . agentUsed = true ;
360+ if ( AGENT_INPUT_METHOD . test ( method ) ) {
361+ if ( owner === undefined || ! streamingOwners . has ( owner ) ) {
362+ tab . agentUsed = true ;
363+ tab . agentLedAt = performance . now ( ) ;
364+ } else if ( method . startsWith ( "Input." ) && ! POINTER_TRAVEL . has ( params [ "type" ] ) ) {
365+ tab . agentLedAt = Number . NEGATIVE_INFINITY ;
366+ }
359367 }
360368 // Waits for focus emulation; insertText and other non-CDP input would not queue behind it.
361369 const forward = ( current ?: ( ) => boolean ) => {
@@ -490,6 +498,7 @@ export const make = Effect.gen(function* () {
490498 downloadDirectory : null ,
491499 pendingDownloadGuid : null ,
492500 agentInputAt : Number . NEGATIVE_INFINITY ,
501+ agentLedAt : Number . NEGATIVE_INFINITY ,
493502 focusEmulated : false ,
494503 focusChange : null ,
495504 focusUses : 0 ,
@@ -604,7 +613,12 @@ export const make = Effect.gen(function* () {
604613
605614 const humanStartedDownload = ( source : Electron . WebContents ) => {
606615 const tab = [ ...tabs . values ( ) ] . find ( ( candidate ) => candidate . debuggee . webContents === source ) ;
607- return tab !== undefined && performance . now ( ) - tab . agentInputAt > AGENT_DOWNLOAD_WINDOW_MS ;
616+ return tab !== undefined && performance . now ( ) - tab . agentInputAt > AGENT_INPUT_WINDOW_MS ;
617+ } ;
618+
619+ const agentLedPage = ( source : Electron . WebContents ) => {
620+ const tab = [ ...tabs . values ( ) ] . find ( ( candidate ) => candidate . debuggee . webContents === source ) ;
621+ return tab !== undefined && performance . now ( ) - tab . agentLedAt <= AGENT_INPUT_WINDOW_MS ;
608622 } ;
609623
610624 const announceAll = Effect . suspend ( ( ) => {
@@ -647,6 +661,7 @@ export const make = Effect.gen(function* () {
647661 setShown,
648662 placeDownload,
649663 humanStartedDownload,
664+ agentLedPage,
650665 } ) ;
651666} ) ;
652667
0 commit comments