@@ -111,6 +111,7 @@ export interface PreviewTabState {
111111 audible : boolean ;
112112 controller : "human" | "agent" | "none" ;
113113 favicon ?: DesktopPreviewFavicon ;
114+ captureRequestIds ?: ReadonlyArray < string > ;
114115 serverTab ?: { readonly threadId : string ; readonly tabId : string } ;
115116 updatedAt : string ;
116117}
@@ -562,6 +563,14 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
562563 const recordingFrameListenersRef = yield * Ref . make < ReadonlySet < RecordingFrameListener > > (
563564 new Set ( ) ,
564565 ) ;
566+ const captureRequests = new Map <
567+ string ,
568+ {
569+ readonly tabId : string ;
570+ readonly webContentsId : number ;
571+ readonly ready : Deferred . Deferred < void > ;
572+ }
573+ > ( ) ;
565574 const pickSessionsRef = yield * Ref . make < ReadonlyMap < string , PickSession > > ( new Map ( ) ) ;
566575 const controlSessionsRef = yield * SynchronizedRef . make <
567576 ReadonlyMap < number , BrowserControlSession >
@@ -722,7 +731,11 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
722731 ) {
723732 yield * SynchronizedRef . modifyEffect ( frameCaptureSessionsRef , ( sessions ) => {
724733 const current = sessions . get ( tabId ) ;
725- if ( ! current || current . unthrottledWebContentsIds . has ( wc . id ) ) {
734+ if (
735+ ! current ||
736+ ! [ ...current . consumers ] . some ( ( consumer ) => typeof consumer === "string" ) ||
737+ current . unthrottledWebContentsIds . has ( wc . id )
738+ ) {
726739 return Effect . succeed ( [ undefined , sessions ] as const ) ;
727740 }
728741 return setFrameCaptureWebContentsBackgroundThrottling ( wc , false ) . pipe (
@@ -2349,21 +2362,67 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
23492362 ) ;
23502363 } ) ;
23512364
2365+ const publishCaptureRequests = Effect . fnUntraced ( function * ( tabId : string ) {
2366+ const next = yield * SynchronizedRef . modify ( tabsRef , ( tabs ) => {
2367+ const current = tabs . get ( tabId ) ;
2368+ if ( ! current ) return [ undefined , tabs ] as const ;
2369+ const state = {
2370+ ...current ,
2371+ captureRequestIds : [ ...captureRequests ] . flatMap ( ( [ id , request ] ) =>
2372+ request . tabId === tabId ? [ id ] : [ ] ,
2373+ ) ,
2374+ } ;
2375+ return [ state , replaceMap ( tabs , ( copy ) => copy . set ( tabId , state ) ) ] as const ;
2376+ } ) ;
2377+ if ( next ) yield * emitIfCurrent ( tabId , next ) ;
2378+ } ) ;
2379+
2380+ const captureReady = Effect . fn ( "PreviewManager.captureReady" ) ( function * (
2381+ tabId : string ,
2382+ webContentsId : number ,
2383+ requestId : string ,
2384+ ) {
2385+ const request = captureRequests . get ( requestId ) ;
2386+ if ( request ?. tabId !== tabId || request . webContentsId !== webContentsId ) return ;
2387+ const tab = ( yield * SynchronizedRef . get ( tabsRef ) ) . get ( tabId ) ;
2388+ if ( tab ?. webContentsId === webContentsId )
2389+ yield * Deferred . succeed ( request . ready , undefined ) . pipe ( Effect . asVoid ) ;
2390+ } ) ;
2391+
23522392 const captureAgentScreenshot = Effect . fn ( "PreviewManager.captureAgentScreenshot" ) ( function * (
23532393 tabId : string ,
23542394 wc : Electron . WebContents ,
23552395 debuggee : Electron . Debugger ,
23562396 params : Record < string , unknown > ,
23572397 ) {
23582398 const consumer = Symbol ( "agent-screenshot" ) ;
2399+ const requestId = yield * crypto . randomUUIDv4 ( ) . pipe ( Effect . orDie ) ;
2400+ const ready = yield * Deferred . make < void > ( ) ;
23592401 return yield * Effect . acquireUseRelease (
23602402 startFrameCapture ( tabId , consumer ) ,
23612403 ( ) =>
2362- Effect . tryPromise ( ( signal ) =>
2363- sendPageCommand ( wc , debuggee , "Page.captureScreenshot" , params , signal ) ,
2364- ) . pipe (
2365- // ServerBrowserPage gives the whole snapshot ten seconds.
2366- Effect . timeout ( "8 seconds" ) ,
2404+ Effect . acquireUseRelease (
2405+ Effect . sync ( ( ) => {
2406+ captureRequests . set ( requestId , { tabId, webContentsId : wc . id , ready } ) ;
2407+ } ) ,
2408+ ( ) =>
2409+ publishCaptureRequests ( tabId ) . pipe (
2410+ Effect . andThen ( Deferred . await ( ready ) ) ,
2411+ // The renderer must commit the guest's in-viewport position before the first
2412+ // native copy: an offscreen copy can stall until the capture deadline.
2413+ Effect . andThen (
2414+ Effect . tryPromise ( ( signal ) =>
2415+ sendPageCommand ( wc , debuggee , "Page.captureScreenshot" , params , signal ) ,
2416+ ) ,
2417+ ) ,
2418+ // ServerBrowserPage gives the whole snapshot ten seconds, including preparation.
2419+ Effect . timeout ( "8 seconds" ) ,
2420+ ) ,
2421+ ( ) =>
2422+ Effect . gen ( function * ( ) {
2423+ captureRequests . delete ( requestId ) ;
2424+ yield * publishCaptureRequests ( tabId ) ;
2425+ } ) ,
23672426 ) ,
23682427 ( ) => stopFrameCapture ( tabId , consumer ) ,
23692428 ) ;
@@ -2709,7 +2768,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
27092768 if ( current . consumers . has ( consumer ) ) {
27102769 return [ false , sessions ] as const ;
27112770 }
2712- if ( ! current . unthrottledWebContentsIds . has ( wc . id ) ) {
2771+ if ( typeof consumer === "string" && ! current . unthrottledWebContentsIds . has ( wc . id ) ) {
27132772 yield * setFrameCaptureWebContentsBackgroundThrottling ( wc , false ) ;
27142773 }
27152774 let scope = current . scope ;
@@ -2724,21 +2783,27 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
27242783 ...current ,
27252784 scope,
27262785 consumers : new Set ( [ ...current . consumers , consumer ] ) ,
2727- unthrottledWebContentsIds : new Set ( [ ...current . unthrottledWebContentsIds , wc . id ] ) ,
2786+ unthrottledWebContentsIds : new Set ( [
2787+ ...current . unthrottledWebContentsIds ,
2788+ ...( typeof consumer === "string" ? [ wc . id ] : [ ] ) ,
2789+ ] ) ,
27282790 } ) ;
27292791 } ) ,
27302792 ] as const ;
27312793 }
27322794 if ( sessions . size === 0 ) {
27332795 yield * setFrameCaptureBackgroundThrottling ( false ) ;
27342796 }
2735- yield * setFrameCaptureWebContentsBackgroundThrottling ( wc , false ) . pipe (
2736- Effect . onError ( ( ) =>
2737- sessions . size === 0
2738- ? setFrameCaptureBackgroundThrottling ( true ) . pipe ( Effect . ignore )
2739- : Effect . void ,
2740- ) ,
2741- ) ;
2797+ // Agent screenshots need presentation, not a page visibility override.
2798+ if ( typeof consumer === "string" ) {
2799+ yield * setFrameCaptureWebContentsBackgroundThrottling ( wc , false ) . pipe (
2800+ Effect . onError ( ( ) =>
2801+ sessions . size === 0
2802+ ? setFrameCaptureBackgroundThrottling ( true ) . pipe ( Effect . ignore )
2803+ : Effect . void ,
2804+ ) ,
2805+ ) ;
2806+ }
27422807 const scope =
27432808 consumer === "picture-in-picture" ? yield * Scope . fork ( parentScope , "sequential" ) : null ;
27442809 if ( scope !== null ) {
@@ -2750,7 +2815,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
27502815 copy . set ( tabId , {
27512816 scope,
27522817 consumers : new Set ( [ consumer ] ) ,
2753- unthrottledWebContentsIds : new Set ( [ wc . id ] ) ,
2818+ unthrottledWebContentsIds : new Set ( typeof consumer === "string" ? [ wc . id ] : [ ] ) ,
27542819 lastPictureInPictureFingerprint : null ,
27552820 } ) ;
27562821 } ) ,
@@ -3392,6 +3457,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
33923457
33933458 return {
33943459 cancelPickElement,
3460+ captureReady,
33953461 captureScreenshot,
33963462 closeTab,
33973463 copyArtifactToClipboard,
@@ -3600,6 +3666,11 @@ export class PreviewManager extends Context.Service<
36003666 tabId : string ,
36013667 webContentsId : number ,
36023668 ) => Effect . Effect < void , PreviewManagerError > ;
3669+ readonly captureReady : (
3670+ tabId : string ,
3671+ webContentsId : number ,
3672+ requestId : string ,
3673+ ) => Effect . Effect < void > ;
36033674 readonly prepareWebview : ( webContents : Electron . WebContents ) => Effect . Effect < void > ;
36043675 readonly navigate : ( tabId : string , url : string ) => Effect . Effect < void , PreviewManagerError > ;
36053676 readonly goBack : ( tabId : string ) => Effect . Effect < void , PreviewManagerError > ;
@@ -3733,6 +3804,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
37333804
37343805 return PreviewManager . of ( {
37353806 setMainWindow : operations . setMainWindow ,
3807+ captureReady : operations . captureReady ,
37363808 setForwardedShortcuts : operations . setForwardedShortcuts ,
37373809 getBrowserSession : Effect . fn ( "PreviewManager.getBrowserSession" ) (
37383810 function * ( scope , persistent , namespace ) {
0 commit comments