Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Enhance authentication flow and user feedback in CLI
Magnus Müller304716e30fa6
diff --git a/browser_use/agent/cloud_events.py b/browser_use/agent/cloud_events.pyindex 127277fc8..d29d73384 100644--- a/browser_use/agent/cloud_events.py+++ b/browser_use/agent/cloud_events.py@@ -268,3 +268,14 @@ class CreateAgentSessionEvent(BaseEvent):'allowed_domains': agent.browser_profile.allowed_domains if agent.browser_profile else [],},)+++class UpdateAgentSessionEvent(BaseEvent):+ """Event to update an existing agent session"""+ # Model fields+ id: str # Session ID to update+ user_id: str = Field(max_length=255)+ device_id: str | None = Field(None, max_length=255)+ browser_session_stopped: bool | None = None+ browser_session_stopped_at: datetime | None = None+ end_reason: str | None = Field(None, max_length=100) # Why the session endeddiff --git a/browser_use/cli.py b/browser_use/cli.pyindex a560f1f26..fcc4fe42c 100644--- a/browser_use/cli.py+++ b/browser_use/cli.py@@ -1575,15 +1575,28 @@ async def textual_interface(config: dict[str, Any]):async def run_auth_command():- """Run the authentication command."""+ """Run the authentication command with dummy task in UI."""+ import asyncio+ import osfrom browser_use.sync.auth import DeviceAuthClientprint('🔐 Browser Use Cloud Authentication')print('=' * 40)+ # Ensure cloud sync is enabled (should be default, but make sure)+ os.environ['BROWSER_USE_CLOUD_SYNC'] = 'true'+auth_client = DeviceAuthClient()+ print(f'🔍 Debug: Checking authentication status...')+ print(f' API Token: {"✅ Present" if auth_client.api_token else "❌ Missing"}')+ print(f' User ID: {auth_client.user_id}')+ print(f' Is Authenticated: {auth_client.is_authenticated}')+ if auth_client.auth_config.authorized_at:+ print(f' Authorized at: {auth_client.auth_config.authorized_at}')+ print()+# Check if already authenticatedif auth_client.is_authenticated:print('✅ Already authenticated!')@@ -1599,103 +1612,218 @@ async def run_auth_command():print(' This will open a browser window for you to sign in.')print()+ # Initialize variables for exception handling+ task_id = None+ sync_service = None+try:- # Create a minimal sync service to establish session context like main branch does+ # Create authentication flow with dummy taskfrom uuid_extensions import uuid7str- from browser_use.agent.cloud_events import CreateAgentSessionEvent+ from browser_use.agent.cloud_events import (+ CreateAgentSessionEvent,+ CreateAgentStepEvent,+ CreateAgentTaskEvent,+ UpdateAgentTaskEvent,+ )from browser_use.sync.service import CloudSync- # Create session ID and sync service with auth context flag+ # IDs for our session and tasksession_id = uuid7str()+ task_id = uuid7str()++ # Create special sync service that allows auth eventssync_service = CloudSync(allow_session_events_for_auth=True)+ sync_service.set_auth_flow_active() # Explicitly enable auth flow+ sync_service.session_id = session_id # Set session ID for auth context+ sync_service.auth_client = auth_client # Use the same auth client instance!- # Create a minimal session event to establish context (like main branch)+ # 1. Create session (like main branch does at start)session_event = CreateAgentSessionEvent(+ id=session_id,user_id=auth_client.temp_user_id,browser_session_id=uuid7str(),browser_session_live_url='',browser_session_cdp_url='',device_id=auth_client.device_id,+ browser_state={+ 'viewport': {'width': 1280, 'height': 720},+ 'user_agent': None,+ 'headless': True,+ 'initial_url': None,+ 'final_url': None,+ 'total_pages_visited': 0,+ 'session_duration_seconds': 0,+ },+ browser_session_data={+ 'cookies': [],+ 'secrets': {},+ 'allowed_domains': [],+ },)- session_event.id = session_id # Set the session ID-- # Handle the session event to set up the sync contextawait sync_service.handle_event(session_event)- # Check if already authenticated- if auth_client.is_authenticated:- # Already authenticated - send completion immediately- from browser_use.agent.cloud_events import CreateAgentTaskEvent, UpdateAgentTaskEvent-- task_event = CreateAgentTaskEvent(- agent_session_id=session_id,- llm_model='auth-flow',- task='🔐 Authentication status check - your future tasks will appear here',- user_id=auth_client.user_id, # Use real user_id since already authenticated- done_output=None,- user_feedback_type=None,- user_comment=None,- gif_url=None,- device_id=auth_client.device_id,+ # 2. Create task (like main branch does at start)+ task_event = CreateAgentTaskEvent(+ id=task_id,+ agent_session_id=session_id,+ llm_model='auth-flow',+ task='🔐 Complete authentication and join the browser-use community',+ user_id=auth_client.temp_user_id,+ device_id=auth_client.device_id,+ done_output=None,+ user_feedback_type=None,+ user_comment=None,+ gif_url=None,+ )+ await sync_service.handle_event(task_event)++ # Brief delay to let events process+ await asyncio.sleep(0.1)++ # 3. Run authentication with timeout+ print('⏳ Waiting for authentication... (this may take up to 2 minutes for testing)')+ print(' Complete the authentication in your browser, then this will continue automatically.')+ print()++ try:+ print('🔧 Debug: Starting authentication process...')+ print(f' Original auth client authenticated: {auth_client.is_authenticated}')+ print(f' Sync service auth client authenticated: {sync_service.auth_client.is_authenticated}')+ print(f' Same auth client? {auth_client is sync_service.auth_client}')+ print(f' Session ID: {sync_service.session_id}')++ # Create a task to show periodic status updates+ async def show_auth_progress():+ for i in range(1, 25): # Show updates every 5 seconds for 2 minutes+ await asyncio.sleep(5)+ fresh_check = DeviceAuthClient()+ print(f'⏱️ Waiting for authentication... ({i * 5}s elapsed)')+ print(f' Status: {"✅ Authenticated" if fresh_check.is_authenticated else "⏳ Still waiting"}')+ if fresh_check.is_authenticated:+ print('🎉 Authentication detected! Completing...')+ break++ # Run authentication and progress updates concurrently+ auth_start_time = asyncio.get_event_loop().time()+ auth_task = asyncio.create_task(sync_service.authenticate(show_instructions=True))+ progress_task = asyncio.create_task(show_auth_progress())++ # Wait for authentication to complete, with timeout+ success = await asyncio.wait_for(auth_task, timeout=120.0) # 2 minutes for initial testing+ progress_task.cancel() # Stop the progress updates++ auth_duration = asyncio.get_event_loop().time() - auth_start_time+ print(f'🔧 Debug: Authentication returned: {success} (took {auth_duration:.1f}s)')++ except asyncio.TimeoutError:+ print('⏱️ Authentication timed out after 2 minutes.')+ print(' Checking if authentication completed in background...')++ # Create a fresh auth client to check current status+ fresh_auth_client = DeviceAuthClient()+ print(f'🔧 Debug: Fresh auth client check:')+ print(f' API Token: {"✅ Present" if fresh_auth_client.api_token else "❌ Missing"}')+ print(f' Is Authenticated: {fresh_auth_client.is_authenticated}')++ if fresh_auth_client.is_authenticated:+ print('✅ Authentication was successful!')+ success = True+ # Update the sync service's auth client+ sync_service.auth_client = fresh_auth_client+ else:+ print('❌ Authentication not completed. Please try again.')+ success = False+ except Exception as e:+ print(f'❌ Authentication error: {type(e).__name__}: {e}')+ import traceback++ print(f'📄 Full traceback: {traceback.format_exc()}')+ success = False++ if success:+ # 4. Send step event to show progress (like main branch during execution)+ # Use the sync service's auth client which has the updated user_id+ step_event = CreateAgentStepEvent(+ id=f'{task_id}-step-1',+ user_id=sync_service.auth_client.user_id, # Use updated auth client+ device_id=sync_service.auth_client.device_id, # Use consistent device_id+ agent_task_id=task_id,+ step=1,+ actions=[+ {+ 'click': {+ 'coordinate': [800, 400],+ 'description': 'Click on Star button',+ 'success': True,+ },+ 'done': {+ 'success': True,+ 'text': '⭐ Starred browser-use/browser-use repository! Welcome to the community!',+ },+ }+ ],+ next_goal='⭐ Star browser-use GitHub repository to join the community',+ evaluation_previous_goal='Authentication completed successfully',+ memory='User authenticated with Browser Use Cloud and is now part of the community',+ screenshot_url=None,+ url='https://github.com/browser-use/browser-use',)- await sync_service.handle_event(task_event)+ print('📤 Sending dummy step event...')+ await sync_service.handle_event(step_event)++ # Small delay to ensure step is processed before completion+ await asyncio.sleep(0.5)- # Immediately send completion+ # 5. Complete task (like main branch does at end)completion_event = UpdateAgentTaskEvent(- id=session_id,- agent_session_id=session_id,- user_id=auth_client.user_id,- done_output='✅ Already authenticated! Cloud sync is enabled for your browser-use runs.',+ id=task_id,+ user_id=sync_service.auth_client.user_id,+ device_id=sync_service.auth_client.device_id, # Use consistent device_id+ done_output="🎉 Welcome to Browser Use! You're now authenticated and part of our community. ⭐ Your future tasks will sync to the cloud automatically.",user_feedback_type=None,user_comment=None,gif_url=None,- device_id=auth_client.device_id,)await sync_service.handle_event(completion_event)++ print('🎉 Authentication successful!')+ print(' Future browser-use runs will now sync to the cloud.')else:- # Not authenticated - run auth flow- from browser_use.agent.cloud_events import CreateAgentTaskEvent-- task_event = CreateAgentTaskEvent(- agent_session_id=session_id,- llm_model='auth-flow',- task='🔐 Authentication in progress - your future tasks will appear here',- user_id=auth_client.temp_user_id,- done_output=None,+ # Failed - still complete the task with failure message+ completion_event = UpdateAgentTaskEvent(+ id=task_id,+ user_id=auth_client.temp_user_id, # Still temp user since auth failed+ device_id=auth_client.device_id,+ done_output='❌ Authentication failed. Please try again.',user_feedback_type=None,user_comment=None,gif_url=None,- device_id=auth_client.device_id,)- await sync_service.handle_event(task_event)+ await sync_service.handle_event(completion_event)- # Now authenticate using the established session context- success = await sync_service.authenticate(show_instructions=True)+ print('❌ Authentication failed.')+ print(' Please try again or check your internet connection.')- if success:- # Send completion event to finish the auth task in UI+ except Exception as e:+ print(f'❌ Authentication error: {e}')+ # Still try to complete the task in UI with error message+ if task_id and sync_service:+ try:from browser_use.agent.cloud_events import UpdateAgentTaskEventcompletion_event = UpdateAgentTaskEvent(- id=session_id, # Required field- agent_session_id=session_id,- user_id=auth_client.user_id, # Required field - now we have the real user_id after auth- done_output='✅ Authentication successful! Future browser-use runs will now sync to the cloud.',+ id=task_id,+ user_id=auth_client.temp_user_id,+ device_id=auth_client.device_id,+ done_output=f'❌ Authentication error: {e}',user_feedback_type=None,user_comment=None,gif_url=None,- device_id=auth_client.device_id,)await sync_service.handle_event(completion_event)-- print('🎉 Authentication successful!')- print(' Future browser-use runs will now sync to the cloud.')- else:- print('❌ Authentication failed.')- print(' Please try again or check your internet connection.')- except Exception as e:- print(f'❌ Authentication error: {e}')+ except Exception:+ pass # Don't fail if we can't send the error eventsys.exit(1)diff --git a/browser_use/sync/service.py b/browser_use/sync/service.pyindex 475b8a328..8b650097d 100644--- a/browser_use/sync/service.py+++ b/browser_use/sync/service.py@@ -25,6 +25,7 @@ class CloudSync:self.auth_task = Noneself.session_id: str | None = Noneself.allow_session_events_for_auth = allow_session_events_for_auth+ self.auth_flow_active = False # Flag to indicate auth flow is runningasync def handle_event(self, event: BaseEvent) -> None:"""Handle an event by sending it to the cloud"""@@ -45,9 +46,12 @@ class CloudSync:if self.auth_client.is_authenticated:# User is authenticated - send all eventsawait self._send_event(event)- elif event.event_type == 'CreateAgentSessionEvent' and self.allow_session_events_for_auth:- # Special case: allow session events for auth flow only+ elif self.allow_session_events_for_auth:+ # Special case: allow ALL events during auth flowawait self._send_event(event)+ # Mark auth flow as active when we see a session event+ if event.event_type == 'CreateAgentSessionEvent':+ self.auth_flow_active = Trueelse:# User is not authenticated - don't send anythinglogger.debug(f'Skipping event {event.event_type} - user not authenticated')@@ -104,24 +108,24 @@ class CloudSync:assert self.auth_client, 'auth_client must exist before calling CloudSync._background_auth()'assert self.session_id, 'session_id must be set before calling CloudSync._background_auth() can fire'try:- # If already authenticated, just show the cloud URL+ # Always show the cloud URL (auth happens immediately when session starts now)+ frontend_url = CONFIG.BROWSER_USE_CLOUD_UI_URL or self.base_url.replace('//api.', '//cloud.')+ session_url = f'{frontend_url.rstrip("/")}/agent/{agent_session_id}'+ terminal_width, _terminal_height = shutil.get_terminal_size((80, 20))+if self.auth_client.is_authenticated:- # Use frontend URL for user-facing links- frontend_url = CONFIG.BROWSER_USE_CLOUD_UI_URL or self.base_url.replace('//api.', '//cloud.')- session_url = f'{frontend_url.rstrip("/")}/agent/{agent_session_id}'- terminal_width, _terminal_height = shutil.get_terminal_size((80, 20))- logger.info('─' * max(terminal_width - 40, 20) + '\n')+ # User is authenticated - show direct link+ logger.info('─' * max(terminal_width - 40, 20))logger.info('🌐 View the details of this run in Browser Use Cloud:')logger.info(f' 👉 {session_url}')- logger.info('─' * max(terminal_width - 40, 20) + '\n\n')- return-- # Otherwise show simple auth prompt- terminal_width, _terminal_height = shutil.get_terminal_size((80, 20))- logger.info('─' * max(terminal_width - 40, 20))- logger.info('🔐 To view this run in Browser Use Cloud, authenticate with:')- logger.info(' 👉 browser-use auth')- logger.info('─' * max(terminal_width - 40, 20) + '\n')+ logger.info('─' * max(terminal_width - 40, 20) + '\n')+ else:+ # User not authenticated - show auth prompt+ logger.info('─' * max(terminal_width - 40, 20))+ logger.info('🔐 To view this run in Browser Use Cloud, authenticate with:')+ logger.info(' 👉 browser-use auth')+ logger.info(' or: python -m browser_use.cli auth')+ logger.info('─' * max(terminal_width - 40, 20) + '\n')except Exception as e:logger.debug(f'Cloud sync authentication failed: {e}')@@ -165,6 +169,20 @@ class CloudSync:if self.auth_task and not self.auth_task.done():await self.auth_task+ def set_auth_flow_active(self) -> None:+ """Mark auth flow as active to allow all events"""+ self.auth_flow_active = True+async def authenticate(self, show_instructions: bool = True) -> bool:"""Authenticate with the cloud service"""+ # Check if already authenticated first+ if self.auth_client.is_authenticated:+ import logging++ logger = logging.getLogger(__name__)+ if show_instructions:+ logger.info('✅ Already authenticated! Skipping OAuth flow.')+ return True++ # Not authenticated - run OAuth flowreturn await self.auth_client.authenticate(agent_session_id=self.session_id, show_instructions=show_instructions)diff --git a/docs/development/monitoring/observability.mdx b/docs/development/monitoring/observability.mdxindex a5cc6b5cc..40024566f 100644--- a/docs/development/monitoring/observability.mdx+++ b/docs/development/monitoring/observability.mdx@@ -70,6 +70,8 @@ browser-use authpython -m browser_use.cli auth```+**Note**: Cloud sync is enabled by default. If you've disabled it, you can re-enable with `export BROWSER_USE_CLOUD_SYNC=true`.+### Manual Authentication```python