feat(connect): make auto-connect to user's Chrome the default behavior
- Auto-connect is now ON by default (was opt-in via --auto-connect) - Added --launch/--new flags to explicitly start a fresh browser - CI environments (CI env var) automatically use --launch mode - Friendly error message with platform-specific Chrome relaunch guide - Mentions Chrome 144+ runtime CDP toggle (chrome://inspect) - --cdp and --provider flags implicitly disable auto-connect - AGENT_BROWSER_NO_AUTO_CONNECT=1 to disable, AGENT_BROWSER_FORCE_LAUNCH=1 to force Track 3 of native-stealth migration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
77616a209c
commit
7ee3d5fb94
+76
-21
@@ -1555,18 +1555,40 @@ async fn auto_launch(state: &mut DaemonState) -> Result<(), String> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if env::var("AGENT_BROWSER_AUTO_CONNECT").is_ok() {
|
||||
state.reset_input_state();
|
||||
state.browser = Some(connect_auto_with_fresh_tab().await?);
|
||||
state.subscribe_to_browser_events();
|
||||
state.start_fetch_handler();
|
||||
state.start_dialog_handler();
|
||||
state.update_stream_client().await;
|
||||
apply_launch_init_scripts(state).await;
|
||||
try_auto_restore_state(state).await;
|
||||
try_load_storage_state(state, &storage_state_path).await;
|
||||
apply_stealth_to_browser(state).await;
|
||||
return Ok(());
|
||||
let force_launch = env::var("AGENT_BROWSER_FORCE_LAUNCH").is_ok();
|
||||
|
||||
// Default behavior: try to connect to the user's existing Chrome first.
|
||||
// This shares cookies/sessions so the agent can reuse logged-in state.
|
||||
// Skip if --launch/--new was passed or running in CI.
|
||||
if env::var("AGENT_BROWSER_AUTO_CONNECT").is_ok() && !force_launch {
|
||||
match connect_auto_with_fresh_tab().await {
|
||||
Ok(mgr) => {
|
||||
state.reset_input_state();
|
||||
state.browser = Some(mgr);
|
||||
state.subscribe_to_browser_events();
|
||||
state.start_fetch_handler();
|
||||
state.start_dialog_handler();
|
||||
state.update_stream_client().await;
|
||||
apply_launch_init_scripts(state).await;
|
||||
try_auto_restore_state(state).await;
|
||||
try_load_storage_state(state, &storage_state_path).await;
|
||||
apply_stealth_to_browser(state).await;
|
||||
return Ok(());
|
||||
}
|
||||
Err(_e) => {
|
||||
// Could not find a running Chrome with CDP enabled.
|
||||
// Return a helpful error guiding the user to enable it.
|
||||
return Err(format!(
|
||||
"Could not connect to your Chrome browser.\n\n\
|
||||
To let agent-browser work with your existing Chrome (recommended):\n\
|
||||
{}\n\n\
|
||||
Or start a standalone browser with: agent-browser --launch open <url>\n\n\
|
||||
Tip: On Chrome 144+, you can enable CDP without restarting:\n\
|
||||
Open chrome://inspect/#remote-debugging and toggle it on.",
|
||||
chrome_relaunch_hint(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cloud provider: when AGENT_BROWSER_PROVIDER is set, connect via the
|
||||
@@ -1689,6 +1711,23 @@ async fn apply_launch_init_scripts(state: &DaemonState) {
|
||||
}
|
||||
|
||||
/// Inject stealth scripts into the active browser session.
|
||||
/// Platform-specific hint for relaunching Chrome with CDP enabled.
|
||||
fn chrome_relaunch_hint() -> &'static str {
|
||||
if cfg!(target_os = "macos") {
|
||||
" 1. Quit Chrome completely\n\
|
||||
2. Run: open -a \"Google Chrome\" --args --remote-debugging-port=9222\n\
|
||||
3. Then retry your agent-browser command"
|
||||
} else if cfg!(target_os = "windows") {
|
||||
" 1. Close Chrome completely\n\
|
||||
2. Run: start chrome --remote-debugging-port=9222\n\
|
||||
3. Then retry your agent-browser command"
|
||||
} else {
|
||||
" 1. Close Chrome completely\n\
|
||||
2. Run: google-chrome --remote-debugging-port=9222\n\
|
||||
3. Then retry your agent-browser command"
|
||||
}
|
||||
}
|
||||
|
||||
/// Called after every successful launch / CDP connect / auto-connect.
|
||||
async fn apply_stealth_to_browser(state: &DaemonState) {
|
||||
if env::var("AGENT_BROWSER_STEALTH").map(|v| v == "0").unwrap_or(false) {
|
||||
@@ -1998,15 +2037,31 @@ async fn handle_launch(cmd: &Value, state: &mut DaemonState) -> Result<Value, St
|
||||
}
|
||||
|
||||
if auto_connect {
|
||||
state.reset_input_state();
|
||||
state.browser = Some(connect_auto_with_fresh_tab().await?);
|
||||
state.subscribe_to_browser_events();
|
||||
state.start_fetch_handler();
|
||||
state.start_dialog_handler();
|
||||
state.update_stream_client().await;
|
||||
load_storage_state_or_rollback(state, &storage_state_owned).await?;
|
||||
apply_launch_init_scripts(state).await;
|
||||
return Ok(json!({ "launched": true }));
|
||||
match connect_auto_with_fresh_tab().await {
|
||||
Ok(mgr) => {
|
||||
state.reset_input_state();
|
||||
state.browser = Some(mgr);
|
||||
state.subscribe_to_browser_events();
|
||||
state.start_fetch_handler();
|
||||
state.start_dialog_handler();
|
||||
state.update_stream_client().await;
|
||||
load_storage_state_or_rollback(state, &storage_state_owned).await?;
|
||||
apply_launch_init_scripts(state).await;
|
||||
apply_stealth_to_browser(state).await;
|
||||
return Ok(json!({ "launched": true }));
|
||||
}
|
||||
Err(_e) => {
|
||||
return Err(format!(
|
||||
"Could not connect to your Chrome browser.\n\n\
|
||||
To let agent-browser work with your existing Chrome (recommended):\n\
|
||||
{}\n\n\
|
||||
Or start a standalone browser with: agent-browser --launch open <url>\n\n\
|
||||
Tip: On Chrome 144+, you can enable CDP without restarting:\n\
|
||||
Open chrome://inspect/#remote-debugging and toggle it on.",
|
||||
chrome_relaunch_hint(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(provider) = cmd.get("provider").and_then(|v| v.as_str()) {
|
||||
|
||||
Reference in New Issue
Block a user