@@ -858,9 +858,9 @@ export class Page extends SdkObject {
858858 await Promise . all ( this . frames ( ) . map ( frame => frame . hideHighlight ( ) . catch ( ( ) => { } ) ) ) ;
859859 }
860860
861- async snapshotForAI ( progress : Progress , options : { track ?: string , mode ?: ' full' | ' incremental' } ) : Promise < string > {
861+ async snapshotForAI ( progress : Progress , options : { track ?: string } ) : Promise < { full : string , incremental ?: string } > {
862862 const snapshot = await snapshotFrameForAI ( progress , this . mainFrame ( ) , options ) ;
863- return snapshot . lines . join ( '\n' ) ;
863+ return { full : snapshot . full . join ( '\n' ) , incremental : snapshot . incremental ?. join ( '\n' ) } ;
864864 }
865865}
866866
@@ -1035,9 +1035,9 @@ class FrameThrottler {
10351035 }
10361036}
10371037
1038- async function snapshotFrameForAI ( progress : Progress , frame : frames . Frame , options : { track ?: string , mode ?: 'full' | 'incremental' } ) : Promise < { lines : string [ ] , isIncremental : boolean } > {
1038+ async function snapshotFrameForAI ( progress : Progress , frame : frames . Frame , options : { track ?: string } ) : Promise < { full : string [ ] , incremental ?: string [ ] } > {
10391039 // Only await the topmost navigations, inner frames will be empty when racing.
1040- const { snapshot, iframeRefs , isIncremental } = await frame . retryWithProgressAndTimeouts ( progress , [ 1000 , 2000 , 4000 , 8000 ] , async continuePolling => {
1040+ const snapshot = await frame . retryWithProgressAndTimeouts ( progress , [ 1000 , 2000 , 4000 , 8000 ] , async continuePolling => {
10411041 try {
10421042 const context = await progress . race ( frame . _utilityContext ( ) ) ;
10431043 const injectedScript = await progress . race ( context . injectedScript ( ) ) ;
@@ -1046,7 +1046,7 @@ async function snapshotFrameForAI(progress: Progress, frame: frames.Frame, optio
10461046 if ( ! node )
10471047 return true ;
10481048 return injected . incrementalAriaSnapshot ( node , { mode : 'ai' , ...options } ) ;
1049- } , { refPrefix : frame . seq ? 'f' + frame . seq : '' , incremental : options . mode === 'incremental' , track : options . track } ) ) ;
1049+ } , { refPrefix : frame . seq ? 'f' + frame . seq : '' , track : options . track } ) ) ;
10501050 if ( snapshotOrRetry === true )
10511051 return continuePolling ;
10521052 return snapshotOrRetry ;
@@ -1057,50 +1057,50 @@ async function snapshotFrameForAI(progress: Progress, frame: frames.Frame, optio
10571057 }
10581058 } ) ;
10591059
1060- const lines = snapshot . split ( '\n' ) ;
1061- const result = [ ] ;
1062-
1063- if ( isIncremental ) {
1064- result . push ( ...lines ) ;
1065- for ( const ref of iframeRefs ) {
1066- const childSnapshot = await snapshotFrameRefForAI ( progress , frame , ref , options ) ;
1067- if ( ! childSnapshot . lines . length )
1068- continue ;
1069- if ( childSnapshot . isIncremental )
1070- result . push ( ...childSnapshot . lines ) ;
1071- else
1072- result . push ( '- <changed> iframe [ref=' + ref + ']:' , ...childSnapshot . lines . map ( l => ' ' + l ) ) ;
1060+ const childSnapshotPromises = snapshot . iframeRefs . map ( ref => snapshotFrameRefForAI ( progress , frame , ref , options ) ) ;
1061+ const childSnapshots = await Promise . all ( childSnapshotPromises ) ;
1062+
1063+ const full = [ ] ;
1064+ let incremental : string [ ] | undefined ;
1065+
1066+ if ( snapshot . incremental !== undefined ) {
1067+ incremental = snapshot . incremental . split ( '\n' ) ;
1068+ for ( let i = 0 ; i < snapshot . iframeRefs . length ; i ++ ) {
1069+ const childSnapshot = childSnapshots [ i ] ;
1070+ if ( childSnapshot . incremental )
1071+ incremental . push ( ...childSnapshot . incremental ) ;
1072+ else if ( childSnapshot . full . length )
1073+ incremental . push ( '- <changed> iframe [ref=' + snapshot . iframeRefs [ i ] + ']:' , ...childSnapshot . full . map ( l => ' ' + l ) ) ;
10731074 }
1074- return { lines : result , isIncremental } ;
10751075 }
10761076
1077- for ( const line of lines ) {
1077+ for ( const line of snapshot . full . split ( '\n' ) ) {
10781078 const match = line . match ( / ^ ( \s * ) - i f r a m e (?: \[ a c t i v e \] ) ? \[ r e f = ( [ ^ \] ] * ) \] / ) ;
10791079 if ( ! match ) {
1080- result . push ( line ) ;
1080+ full . push ( line ) ;
10811081 continue ;
10821082 }
10831083
10841084 const leadingSpace = match [ 1 ] ;
10851085 const ref = match [ 2 ] ;
1086- const childSnapshot = await snapshotFrameRefForAI ( progress , frame , ref , options ) ;
1087- result . push ( childSnapshot . lines . length ? line + ':' : line ) ;
1088- result . push ( ...childSnapshot . lines . map ( l => leadingSpace + ' ' + l ) ) ;
1086+ const childSnapshot = childSnapshots [ snapshot . iframeRefs . indexOf ( ref ) ] ?? { full : [ ] } ;
1087+ full . push ( childSnapshot . full . length ? line + ':' : line ) ;
1088+ full . push ( ...childSnapshot . full . map ( l => leadingSpace + ' ' + l ) ) ;
10891089 }
10901090
1091- return { lines : result , isIncremental } ;
1091+ return { full , incremental } ;
10921092}
10931093
1094- async function snapshotFrameRefForAI ( progress : Progress , parentFrame : frames . Frame , frameRef : string , options : { track ?: string , mode ?: 'full' | 'incremental' } ) : Promise < { lines : string [ ] , isIncremental : boolean } > {
1094+ async function snapshotFrameRefForAI ( progress : Progress , parentFrame : frames . Frame , frameRef : string , options : { track ?: string , mode ?: 'full' | 'incremental' } ) : Promise < { full : string [ ] , incremental ?: string [ ] } > {
10951095 const frameSelector = `aria-ref=${ frameRef } >> internal:control=enter-frame` ;
10961096 const frameBodySelector = `${ frameSelector } >> body` ;
10971097 const child = await progress . race ( parentFrame . selectors . resolveFrameForSelector ( frameBodySelector , { strict : true } ) ) ;
10981098 if ( ! child )
1099- return { lines : [ ] , isIncremental : false } ;
1099+ return { full : [ ] } ;
11001100 try {
11011101 return await snapshotFrameForAI ( progress , child . frame , options ) ;
11021102 } catch {
1103- return { lines : [ ] , isIncremental : false } ;
1103+ return { full : [ ] } ;
11041104 }
11051105}
11061106
0 commit comments