@@ -353,63 +353,77 @@ export class BrowserSession {
353353 // Remove trailing slash for comparison
354354 const normalizedNewUrl = url . replace ( / \/ $ / , "" )
355355
356- // Extract the root domain from the URL
357- const rootDomain = this . getRootDomain ( normalizedNewUrl )
358-
356+ // Check if browser assistant mode is enabled
357+ const browserAssistantModeEnabled = this . context . globalState . get ( "browserAssistantModeEnabled" ) as
358+ | boolean
359+ | undefined
360+ console . log ( `browserAssistantModeEnabled: ${ browserAssistantModeEnabled } ` )
359361 // Get all current pages
360362 const pages = await this . browser . pages ( )
361363
362- // Try to find a page with the same root domain
364+ // Try to find a page with the same root domain or use the latest tab in assistant mode
363365 let existingPage : Page | undefined
364366
365- for ( const page of pages ) {
366- try {
367- const pageUrl = page . url ( )
368- if ( pageUrl && this . getRootDomain ( pageUrl ) === rootDomain ) {
369- existingPage = page
370- break
367+ if ( browserAssistantModeEnabled ) {
368+ // In assistant mode, always use the latest tab (last in the array)
369+ // Skip the first page which is usually the blank page
370+ existingPage = pages . length >= 1 ? pages [ pages . length - 1 ] : undefined
371+ console . log ( `Browser assistant mode enabled, using latest tab` )
372+ } else {
373+ // Extract the root domain from the URL
374+ const rootDomain = this . getRootDomain ( normalizedNewUrl )
375+
376+ // Try to find a page with the same root domain
377+ for ( const page of pages ) {
378+ try {
379+ const pageUrl = page . url ( )
380+ if ( pageUrl && this . getRootDomain ( pageUrl ) === rootDomain ) {
381+ existingPage = page
382+ break
383+ }
384+ } catch ( error ) {
385+ // Skip pages that might have been closed or have errors
386+ console . log ( `Error checking page URL: ${ error } ` )
387+ continue
371388 }
372- } catch ( error ) {
373- // Skip pages that might have been closed or have errors
374- console . log ( `Error checking page URL: ${ error } ` )
375- continue
376389 }
377390 }
378391
379392 if ( existingPage ) {
380393 // Tab with the same root domain exists, switch to it
381- console . log ( `Tab with domain ${ rootDomain } already exists, switching to it` )
394+ console . log ( `Tab exists, switching to it` )
382395
383396 // Update the active page
384397 this . page = existingPage
385398 existingPage . bringToFront ( )
386399
387- // Navigate to the new URL if it's different]
400+ // Navigate to the new URL if it's different
388401 const currentUrl = existingPage . url ( ) . replace ( / \/ $ / , "" ) // Remove trailing / if present
389- if ( this . getRootDomain ( currentUrl ) === rootDomain && currentUrl !== normalizedNewUrl ) {
402+
403+ if (
404+ browserAssistantModeEnabled ||
405+ ( this . getRootDomain ( currentUrl ) === this . getRootDomain ( normalizedNewUrl ) &&
406+ currentUrl !== normalizedNewUrl )
407+ ) {
390408 console . log ( `Navigating to new URL: ${ normalizedNewUrl } ` )
391409 console . log ( `Current URL: ${ currentUrl } ` )
392- console . log ( `Root domain: ${ this . getRootDomain ( currentUrl ) } ` )
393- console . log ( `New URL: ${ normalizedNewUrl } ` )
394410 // Navigate to the new URL
395411 return this . doAction ( async ( page ) => {
396412 await this . navigatePageToUrl ( page , normalizedNewUrl )
397413 } )
398414 } else {
399- console . log ( `Tab with domain ${ rootDomain } already exists, and URL is the same: ${ normalizedNewUrl } ` )
415+ console . log ( `URL is the same: ${ normalizedNewUrl } ` )
400416 // URL is the same, just reload the page to ensure it's up to date
401417 console . log ( `Reloading page: ${ normalizedNewUrl } ` )
402418 console . log ( `Current URL: ${ currentUrl } ` )
403- console . log ( `Root domain: ${ this . getRootDomain ( currentUrl ) } ` )
404- console . log ( `New URL: ${ normalizedNewUrl } ` )
405419 return this . doAction ( async ( page ) => {
406420 await page . reload ( { timeout : 7_000 , waitUntil : [ "domcontentloaded" , "networkidle2" ] } )
407421 await this . waitTillHTMLStable ( page )
408422 } )
409423 }
410424 } else {
411425 // No tab with this root domain exists, create a new one
412- console . log ( `No tab with domain ${ rootDomain } exists, creating a new one` )
426+ console . log ( `No suitable tab exists, creating a new one` )
413427 return this . createNewTab ( normalizedNewUrl )
414428 }
415429 }
0 commit comments