@@ -4517,7 +4517,7 @@ class ChatUI {
45174517 return null ;
45184518 }
45194519
4520- /** Pull SerpApi YouTube tool payloads (possibly arrays) for iframe embedding. */
4520+ /** Pull SerpApi YouTube payloads with tool provenance for iframe embedding. */
45214521 _youtubeToolPayloadsForEmbeds ( toolResultsData = { } ) {
45224522 const out = [ ] ;
45234523 if ( ! toolResultsData || typeof toolResultsData !== 'object' ) return out ;
@@ -4526,10 +4526,14 @@ class ChatUI {
45264526 if ( ! tr ) continue ;
45274527 if ( Array . isArray ( tr ) ) {
45284528 for ( const item of tr ) {
4529- if ( item && typeof item === 'object' ) out . push ( item ) ;
4529+ if ( item && typeof item === 'object' ) {
4530+ const payload = item . data && typeof item . data === 'object' ? item . data : item ;
4531+ out . push ( { toolName : key , payload} ) ;
4532+ }
45304533 }
45314534 } else if ( typeof tr === 'object' ) {
4532- out . push ( tr ) ;
4535+ const payload = tr . data && typeof tr . data === 'object' ? tr . data : tr ;
4536+ out . push ( { toolName : key , payload} ) ;
45334537 }
45344538 }
45354539 return out ;
@@ -4553,6 +4557,7 @@ class ChatUI {
45534557
45544558 const embeds = [ ] ;
45554559 const seenIds = new Set ( ) ;
4560+ const youtubeSearchResultIds = new Set ( ) ;
45564561
45574562 const pushEmbed = ( videoId , titleHint = '' ) => {
45584563 if ( ! videoId || seenIds . has ( videoId ) || downloadedIds . has ( videoId ) ) return ;
@@ -4567,18 +4572,26 @@ class ChatUI {
45674572 } ) ;
45684573 } ;
45694574
4570- for ( const payload of this . _youtubeToolPayloadsForEmbeds ( toolResultsData ) ) {
4575+ for ( const { toolName , payload} of this . _youtubeToolPayloadsForEmbeds ( toolResultsData ) ) {
45714576 const primaryTitle = typeof payload . title === 'string' ? payload . title . trim ( ) : '' ;
4577+ const candidates = new Map ( ) ;
4578+ const addCandidate = ( videoId , titleHint = '' ) => {
4579+ if ( ! videoId ) return ;
4580+ const id = String ( videoId ) . trim ( ) ;
4581+ if ( ! id ) return ;
4582+ const title = typeof titleHint === 'string' ? titleHint . trim ( ) : '' ;
4583+ if ( ! candidates . has ( id ) || ( ! candidates . get ( id ) && title ) ) candidates . set ( id , title ) ;
4584+ } ;
45724585 if ( payload . top_url ) {
45734586 const vid = this . _extractYouTubeVideoId ( payload . top_url ) ;
4574- if ( vid ) pushEmbed ( vid , primaryTitle ) ;
4587+ if ( vid ) addCandidate ( vid , primaryTitle ) ;
45754588 }
45764589 if ( typeof payload . url === 'string' ) {
45774590 const vid = this . _extractYouTubeVideoId ( payload . url ) ;
4578- if ( vid ) pushEmbed ( vid , primaryTitle ) ;
4591+ if ( vid ) addCandidate ( vid , primaryTitle ) ;
45794592 }
45804593 if ( payload . video_id != null && String ( payload . video_id ) . trim ( ) ) {
4581- pushEmbed ( String ( payload . video_id ) . trim ( ) , primaryTitle ) ;
4594+ addCandidate ( String ( payload . video_id ) . trim ( ) , primaryTitle ) ;
45824595 }
45834596 for ( const listKey of [ 'results' , 'top_results' , 'candidates' ] ) {
45844597 const list = payload [ listKey ] ;
@@ -4589,12 +4602,19 @@ class ChatUI {
45894602 const hint = itemTitle || primaryTitle ;
45904603 if ( typeof item . url === 'string' ) {
45914604 const vid = this . _extractYouTubeVideoId ( item . url ) ;
4592- if ( vid ) pushEmbed ( vid , hint ) ;
4605+ if ( vid ) addCandidate ( vid , hint ) ;
45934606 } else if ( item . video_id != null && String ( item . video_id ) . trim ( ) ) {
4594- pushEmbed ( String ( item . video_id ) . trim ( ) , hint ) ;
4607+ addCandidate ( String ( item . video_id ) . trim ( ) , hint ) ;
45954608 }
45964609 }
45974610 }
4611+ if ( toolName === 'serpapi_youtube_search' ) {
4612+ for ( const videoId of candidates . keys ( ) ) youtubeSearchResultIds . add ( videoId ) ;
4613+ const first = candidates . entries ( ) . next ( ) . value ;
4614+ if ( first ) pushEmbed ( first [ 0 ] , first [ 1 ] ) ;
4615+ } else {
4616+ for ( const [ videoId , title ] of candidates . entries ( ) ) pushEmbed ( videoId , title ) ;
4617+ }
45984618 }
45994619
46004620 const sources = [ displayText , rawResponse ] ;
@@ -4607,7 +4627,12 @@ class ChatUI {
46074627 if ( embeds . length >= maxEmbeds ) break ;
46084628
46094629 const videoId = this . _extractYouTubeVideoId ( rawUrl ) ;
4610- if ( ! videoId || seenIds . has ( videoId ) || downloadedIds . has ( videoId ) ) continue ;
4630+ if (
4631+ ! videoId
4632+ || seenIds . has ( videoId )
4633+ || downloadedIds . has ( videoId )
4634+ || youtubeSearchResultIds . has ( videoId )
4635+ ) continue ;
46114636
46124637 pushEmbed ( videoId , '' ) ;
46134638 }
0 commit comments