//#region src/browser/backend-from-config.ts
/**
* Whether the gateway should keep the Chrome extension WebSocket bridge listening.
* True when browser tools are enabled and the resolved backend is extension (including default).
*/
function shouldRunExtensionBridgeServer(cfg) {
	if ((cfg?.agents?.defaults?.browser)?.enabled === false) return false;
	return resolveBrowserBackendFromConfig(cfg).mode === "extension";
}
/**
* Resolve browser backend from agent defaults.
* Precedence: `backend: 'extension'` → `cloakbrowser` → `cdpUrl` (CDP) → `cloudProvider` (remote) → `local` (explicit) → extension (default).
*/
function resolveBrowserBackendFromConfig(cfg) {
	const b = cfg?.agents?.defaults?.browser;
	const headless = b?.headless === true;
	if (b?.backend === "extension") {
		const ex = b.extension;
		const cmdSec = b.commandTimeout;
		return {
			mode: "extension",
			config: {
				port: ex?.port,
				host: ex?.host,
				connectionTimeout: ex?.connectionTimeout,
				commandTimeout: typeof cmdSec === "number" && Number.isFinite(cmdSec) && cmdSec > 0 ? Math.floor(cmdSec * 1e3) : void 0
			}
		};
	}
	if (b?.backend === "cloakbrowser") {
		const cb = b.cloakbrowser;
		return {
			mode: "cloakbrowser",
			config: {
				headless,
				keepOpen: cb?.keepOpen,
				temporaryProfile: cb?.temporaryProfile,
				cacheDir: cb?.cacheDir,
				binaryPath: cb?.binaryPath,
				timezone: cb?.timezone,
				locale: cb?.locale,
				webrtcIp: cb?.webrtcIp,
				fingerprintPlatform: cb?.fingerprintPlatform,
				extraArgs: cb?.extraArgs,
				humanize: b.humanize ?? true,
				humanPreset: b.humanPreset ?? "careful"
			}
		};
	}
	const cdpUrl = typeof b?.cdpUrl === "string" ? b.cdpUrl.trim() : "";
	if (cdpUrl.length > 0) return {
		mode: "cdp",
		config: { wsEndpoint: cdpUrl }
	};
	const cp = b?.cloudProvider;
	if (cp === "browserbase" || cp === "browser-use") {
		const cloud = b.cloud;
		const apiKey = typeof cloud?.apiKey === "string" ? cloud.apiKey.trim() : void 0;
		const projectId = typeof cloud?.projectId === "string" ? cloud.projectId.trim() : void 0;
		const region = typeof cloud?.region === "string" ? cloud.region.trim() : void 0;
		return {
			mode: "cloud",
			config: {
				type: cp,
				...apiKey ? { apiKey } : {},
				...projectId ? { projectId } : {},
				...region ? { region } : {}
			}
		};
	}
	if (b?.backend === "local") return {
		mode: "local",
		headless
	};
	const ex = b?.extension;
	const cmdSec = b?.commandTimeout;
	return {
		mode: "extension",
		config: {
			port: ex?.port,
			host: ex?.host,
			connectionTimeout: ex?.connectionTimeout,
			commandTimeout: typeof cmdSec === "number" && Number.isFinite(cmdSec) && cmdSec > 0 ? Math.floor(cmdSec * 1e3) : void 0
		}
	};
}
/** CloakBrowser launch options aligned with saved agent defaults (headed for manual use). */
function cloakBrowserConfigFromAgentDefaults(cfg) {
	const cb = (cfg?.agents?.defaults?.browser)?.cloakbrowser;
	return {
		headless: false,
		keepOpen: cb?.keepOpen,
		temporaryProfile: cb?.temporaryProfile,
		cacheDir: cb?.cacheDir,
		binaryPath: cb?.binaryPath,
		timezone: cb?.timezone,
		locale: cb?.locale,
		webrtcIp: cb?.webrtcIp,
		fingerprintPlatform: cb?.fingerprintPlatform,
		extraArgs: cb?.extraArgs,
		reuseExisting: true
	};
}
//#endregion
export { cloakBrowserConfigFromAgentDefaults, resolveBrowserBackendFromConfig, shouldRunExtensionBridgeServer };

//# sourceMappingURL=backend-from-config.js.map