Add --profile flag for persistent browser profiles (#68)
* Add --profile flag for persistent browser profiles Adds support for persistent browser profiles that preserve cookies, localStorage, and login sessions across browser restarts. Changes: - Add --profile <path> CLI flag (flags.rs) - Add AGENT_BROWSER_PROFILE environment variable support - Add profile field to LaunchCommand type (types.ts) - Use launchPersistentContext when profile is specified (browser.ts) - Update help text and README with documentation Usage: agent-browser --profile ~/.myapp-profile open myapp.com This enables AI agents to maintain authenticated sessions across browser restarts without re-authenticating each time. * Expand tilde in profile path to home directory * fix: add missing profile field to test Flags struct --------- Co-authored-by: Chris Tate <chris@ctate.dev>
This commit is contained in:
co-authored by
Chris Tate
parent
c6a92a1472
commit
36cca10c10
@@ -818,11 +818,16 @@ export class BrowserManager {
|
||||
// Determine CDP endpoint: prefer cdpUrl over cdpPort for flexibility
|
||||
const cdpEndpoint = options.cdpUrl ?? (options.cdpPort ? String(options.cdpPort) : undefined);
|
||||
const hasExtensions = !!options.extensions?.length;
|
||||
const hasProfile = !!options.profile;
|
||||
|
||||
if (hasExtensions && cdpEndpoint) {
|
||||
throw new Error('Extensions cannot be used with CDP connection');
|
||||
}
|
||||
|
||||
if (hasProfile && cdpEndpoint) {
|
||||
throw new Error('Profile cannot be used with CDP connection');
|
||||
}
|
||||
|
||||
if (this.isLaunched()) {
|
||||
const needsRelaunch =
|
||||
(!cdpEndpoint && this.cdpEndpoint !== null) ||
|
||||
@@ -863,6 +868,7 @@ export class BrowserManager {
|
||||
|
||||
let context: BrowserContext;
|
||||
if (hasExtensions) {
|
||||
// Extensions require persistent context in a temp directory
|
||||
const extPaths = options.extensions!.join(',');
|
||||
const session = process.env.AGENT_BROWSER_SESSION || 'default';
|
||||
// Combine extension args with custom args
|
||||
@@ -881,7 +887,19 @@ export class BrowserManager {
|
||||
}
|
||||
);
|
||||
this.isPersistentContext = true;
|
||||
} else if (hasProfile) {
|
||||
// Profile uses persistent context for durable cookies/storage
|
||||
// Expand ~ to home directory since it won't be shell-expanded
|
||||
const profilePath = options.profile!.replace(/^~\//, os.homedir() + '/');
|
||||
context = await launcher.launchPersistentContext(profilePath, {
|
||||
headless: options.headless ?? true,
|
||||
executablePath: options.executablePath,
|
||||
viewport,
|
||||
extraHTTPHeaders: options.headers,
|
||||
});
|
||||
this.isPersistentContext = true;
|
||||
} else {
|
||||
// Regular ephemeral browser
|
||||
this.browser = await launcher.launch({
|
||||
headless: options.headless ?? true,
|
||||
executablePath: options.executablePath,
|
||||
|
||||
Reference in New Issue
Block a user