@agimon-ai/browse-tool 0.20.0 → 0.20.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +5 -5
- package/dist/cli.mjs +23 -23
- package/dist/extension/background.js +16 -0
- package/dist/extension/background.js.map +1 -1
- package/dist/extension/content.js +1 -0
- package/dist/extension/content.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{playwright-test-DuyeBPh3.cjs → playwright-test-Cv_VY8VH.cjs} +1 -1
- package/dist/streamable-http-AtkbCdt-.mjs +53 -0
- package/dist/streamable-http-BLUtoU2x.cjs +53 -0
- package/dist/stubs/playwright-test.cjs +1 -1
- package/package.json +5 -5
- package/dist/streamable-http-BPEGdTS4.mjs +0 -53
- package/dist/streamable-http-u32a2By0.cjs +0 -53
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=require(`./playwright-test-DuyeBPh3.cjs`);require(`reflect-metadata/lite`);let l=require(`@agimon-ai/foundation-port-registry`),u=require(`@agimon-ai/foundation-process-registry`),d=require(`inversify`),f=require(`node:path`);f=s(f,1);let p=require(`node:fs`);p=s(p,1);let m=require(`node:fs/promises`),h=require(`node:os`);h=s(h,1);let g=require(`node:child_process`),_=require(`node:util`),v=require(`node:async_hooks`),y=require(`node:crypto`),b=require(`node:url`),x=require(`playwright`);x=s(x,1);let S=require(`node:stream`),C=require(`node:stream/promises`),w=require(`node:module`),T=require(`zod`),ee=require(`@agimon-ai/log-sink-mcp`),E=require(`@opentelemetry/api`),te=require(`@opentelemetry/exporter-trace-otlp-http`),D=require(`@opentelemetry/resources`),ne=require(`@opentelemetry/sdk-trace-node`),re=require(`yaml`),ie=require(`esbuild`),ae=require(`@modelcontextprotocol/sdk/types.js`),oe=require(`@modelcontextprotocol/sdk/server/stdio.js`),se=require(`node:http`),ce=require(`@modelcontextprotocol/sdk/server/streamableHttp.js`);const O={ProfileService:Symbol.for(`ProfileService`),ProxyConfigService:Symbol.for(`ProxyConfigService`),PageRegistry:Symbol.for(`PageRegistry`),BrowserService:Symbol.for(`BrowserService`),BrowserProcessRegistry:Symbol.for(`BrowserProcessRegistry`),ElementLocatorService:Symbol.for(`ElementLocatorService`),PageMonitorService:Symbol.for(`PageMonitorService`),PauseController:Symbol.for(`PauseController`),AutomationRunner:Symbol.for(`AutomationRunner`),SpecRunner:Symbol.for(`SpecRunner`),SpecBundlerService:Symbol.for(`SpecBundlerService`),SpecDiscoveryService:Symbol.for(`SpecDiscoveryService`),SpecMetadataService:Symbol.for(`SpecMetadataService`),SetupRunner:Symbol.for(`SetupRunner`),WebServerManager:Symbol.for(`WebServerManager`),Logger:Symbol.for(`Logger`),TelemetryService:Symbol.for(`TelemetryService`),CodeSnippetService:Symbol.for(`CodeSnippetService`),HttpServerHealthCheck:Symbol.for(`HttpServerHealthCheck`),HttpServerManager:Symbol.for(`HttpServerManager`),HttpBrowserClient:Symbol.for(`HttpBrowserClient`),RemoteToolExecutor:Symbol.for(`RemoteToolExecutor`),ExtensionTaskQueue:Symbol.for(`ExtensionTaskQueue`),ExtensionToolDelegator:Symbol.for(`ExtensionToolDelegator`),ToolExecutor:Symbol.for(`ToolExecutor`),StealthLauncher:Symbol.for(`StealthLauncher`),BrowserLockManager:Symbol.for(`BrowserLockManager`),ExtensionSessionRegistry:Symbol.for(`ExtensionSessionRegistry`),ExtensionPageProxy:Symbol.for(`ExtensionPageProxy`),ExtensionSpecRunner:Symbol.for(`ExtensionSpecRunner`),McpSessionTracker:Symbol.for(`McpSessionTracker`),ChromeForTestingService:Symbol.for(`ChromeForTestingService`),WebSocketHub:Symbol.for(`WebSocketHub`),IdleCleanupService:Symbol.for(`IdleCleanupService`),PortRegistryService:Symbol.for(`PortRegistryService`),McpPortAllocationService:Symbol.for(`McpPortAllocationService`),ProcessRegistryService:Symbol.for(`ProcessRegistryService`),Tool:Symbol.for(`Tool`)};function k(e,t){if(typeof Reflect==`object`&&typeof Reflect.metadata==`function`)return Reflect.metadata(e,t)}function A(e,t){return function(n,r){t(n,r,e)}}function j(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let le=class{sessions=new Map;sessionIdCounter=0;constructor(e,t,n,r,i,a){this.browserService=e,this.specRunner=t,this.specMetadataService=n,this.setupRunner=r,this.webServerManager=i,this.pageRegistry=a}async runSpec(e){let{specPath:t,automationId:n,keepBrowserOpen:r=!1,browserOptions:i={},browserId:a,pageId:o,hooksPath:s}=e,c=n??`spec-${++this.sessionIdCounter}`,l={id:c,scriptPath:t,status:`pending`,pageIds:[],browserId:null,error:null,createdAt:new Date,updatedAt:new Date};this.sessions.set(c,l);let u=!!(a&&o);try{u&&(l.browserId=a,l.pageIds.push(o)),l.status=`running`,l.updatedAt=new Date;let e=await this.specRunner.executeSpec({specPath:t,sessionId:c,browserType:i.browserType,headless:i.headless,hooksPath:s,baseURL:i.baseURL,keepBrowserOpen:r});if(r&&e.browser){let t=this.browserService.registerExternalBrowser(e.browser);if(l.browserId=t,e.keptPages?.length)for(let n of e.keptPages){let e=await this.pageRegistry.register({page:n.page,context:n.context,browserId:t,onClose:e=>{let t=l.pageIds.indexOf(e);t>=0&&l.pageIds.splice(t,1)}});l.pageIds.push(e)}}return l.status=`completed`,l.updatedAt=new Date,{automationId:c,status:`completed`,pageIds:r||u?[...l.pageIds]:[],browserId:r||u?l.browserId:null,specResult:e}}catch(e){return l.status=`failed`,l.error=e instanceof Error?e.message:String(e),l.updatedAt=new Date,{automationId:c,status:`failed`,pageIds:u?[...l.pageIds]:[],browserId:u?l.browserId:null}}}async runSpecEnhanced(e){let{specPath:t,automationId:n,keepBrowserOpen:r=!1,browserOptions:i={},testFilter:a,specArgs:o,configPath:s,runSetup:c=!1,startServer:l=!1,stopServerAfter:u=!0,browserId:d,pageId:p,hooksPath:m}=e,h=n??`spec-${++this.sessionIdCounter}`,g={id:h,scriptPath:t,status:`pending`,pageIds:[],browserId:null,error:null,createdAt:new Date,updatedAt:new Date};this.sessions.set(h,g);let _,v;try{let e=s?await this.specMetadataService.loadPlaywrightConfig(s):null,n=s?(0,f.dirname)(s):process.cwd();l&&e?.webServer&&(_=await this.webServerManager.startServer(e.webServer,n));let y=!!(d&&p);if(y&&(g.browserId=d,g.pageIds.push(p)),g.status=`running`,g.updatedAt=new Date,c&&e?.setupFile){let t=e.setupFile.startsWith(`/`)?e.setupFile:`${n}/${e.setupFile}`,r=e.setupExport??`default`,a=await this.browserService.launch(i);try{if(v=await this.setupRunner.runSetup(t,r,{browser:a.browserInstance.browser,context:a.browserInstance.context,page:a.page}),!v.success)throw Error(v.error??`Setup failed`)}finally{await this.browserService.closeBrowser(a.browserInstance.id)}}let b=await this.specRunner.executeSpecEnhanced({specPath:t,sessionId:h,browserType:i.browserType,headless:i.headless,testFilter:a,specArgs:{...o,...v?.state},hooksPath:m,baseURL:i.baseURL,keepBrowserOpen:r});if(r&&b.browser){let e=this.browserService.registerExternalBrowser(b.browser);if(g.browserId=e,b.keptPages?.length)for(let t of b.keptPages){let n=await this.pageRegistry.register({page:t.page,context:t.context,browserId:e,onClose:e=>{let t=g.pageIds.indexOf(e);t>=0&&g.pageIds.splice(t,1)}});g.pageIds.push(n)}}return g.status=`completed`,g.updatedAt=new Date,u&&_?.started&&await this.webServerManager.stopServer(),{automationId:h,status:`completed`,pageIds:r||y?[...g.pageIds]:[],browserId:r||y?g.browserId:null,specResult:b,serverResult:_,setupResult:v}}catch(e){g.status=`failed`,g.error=e instanceof Error?e.message:String(e),g.updatedAt=new Date,u&&_?.started&&await this.webServerManager.stopServer();let t=!!(d&&p);return{automationId:h,status:`failed`,pageIds:t?[...g.pageIds]:[],browserId:t?g.browserId:null,serverResult:_,setupResult:v}}}getSession(e){return this.sessions.get(e)}listSessions(){return Array.from(this.sessions.values())}async stop(e){let t=this.sessions.get(e);return t?(t.browserId&&await this.browserService.closeBrowser(t.browserId),t.status=`completed`,t.updatedAt=new Date,!0):!1}};le=j([(0,d.injectable)(),A(0,(0,d.inject)(O.BrowserService)),A(1,(0,d.inject)(O.SpecRunner)),A(2,(0,d.inject)(O.SpecMetadataService)),A(3,(0,d.inject)(O.SetupRunner)),A(4,(0,d.inject)(O.WebServerManager)),A(5,(0,d.inject)(O.PageRegistry)),k(`design:paramtypes`,[Object,Object,Object,Object,Object,Object])],le);var ue=class extends Error{constructor(e,t,n){super(e),this.pid=t,this.userDataDir=n,this.name=`BrowserSessionConflictError`}};let de=class{platform=h.default.platform();async checkLock(e){let t={isLocked:!1};return this.platform===`win32`?this.checkWindowsLock(e,t):this.checkUnixLock(e,t)}async cleanupOrphanedLocks(e){let t=await this.checkLock(e);if(!t.isLocked)return!0;if(t.isProcessAlive)return!1;let n=[f.default.join(e,`SingletonLock`),f.default.join(e,`SingletonCookie`),f.default.join(e,`SingletonSocket`),f.default.join(e,`lockfile`)];for(let e of n)try{p.default.existsSync(e)&&await(0,m.unlink)(e)}catch{}let r=f.default.join(e,`Default`,`LOCK`);try{p.default.existsSync(r)&&await(0,m.unlink)(r)}catch{}return!0}async ensureAvailable(e){if(!p.default.existsSync(e))return;let t=await this.checkLock(e);if(t.isLocked){if(t.isProcessAlive)throw new ue(`Browser session conflict: Another Chrome process (PID: ${t.pid}) is using this profile.\nUser data directory: ${e}\n\nTo resolve:\n1. Close the existing browser session manually\n2. Or use a different profile name\n3. Or kill the process: kill ${t.pid}`,t.pid,e);if(!await this.cleanupOrphanedLocks(e))throw Error(`Failed to clean up orphaned browser lock in ${e}`)}}async checkUnixLock(e,t){let n=f.default.join(e,`SingletonLock`),r=f.default.join(e,`SingletonSocket`);if(t.lockFilePath=n,t.socketPath=r,this.symlinkExists(n))try{let e=p.default.readlinkSync(n).match(/-(\d+)$/);e&&(t.isLocked=!0,t.pid=Number.parseInt(e[1],10),t.isProcessAlive=this.isProcessRunning(t.pid))}catch{t.isLocked=!0,t.isProcessAlive=p.default.existsSync(r)}return!t.isLocked&&p.default.existsSync(r)&&(t.isLocked=!0,t.isProcessAlive=!0),t}symlinkExists(e){try{return p.default.lstatSync(e).isSymbolicLink()}catch{return!1}}async checkWindowsLock(e,t){let n=f.default.join(e,`lockfile`);if(t.lockFilePath=n,p.default.existsSync(n))try{let e=await(0,m.readFile)(n,`utf-8`),r=Number.parseInt(e.trim(),10);Number.isNaN(r)||(t.isLocked=!0,t.pid=r,t.isProcessAlive=this.isProcessRunning(r))}catch{t.isLocked=!0,t.isProcessAlive=!0}return t}isProcessRunning(e){try{return process.kill(e,0),!0}catch(e){return e.code===`EPERM`}}};de=j([(0,d.injectable)()],de);const fe=`BROWSE_TOOL_WORKSPACE_ROOT`,pe=[`pnpm-workspace.yaml`,`nx.json`,`.git`];function me(e=[]){for(let t=0;t<e.length;t+=1){let n=e[t];if(n===`--workspace-root`)return e[t+1];if(n.startsWith(`--workspace-root=`))return n.slice(17)}}function he(e){return f.default.resolve(e)}function M(e={}){let t=e.env??process.env,n=me(e.argv)??t.BROWSE_TOOL_WORKSPACE_ROOT;if(n&&n.trim().length>0)return he(n);let r=f.default.resolve(e.startPath??process.cwd());for(;;){for(let e of pe)if((0,p.existsSync)(f.default.join(r,e)))return r;let t=f.default.dirname(r);if(t===r)return f.default.resolve(e.startPath??process.cwd());r=t}}function ge(e){let t=he(e);return process.env[fe]=t,t}function _e(e=process.env){return e.BROWSE_TOOL_ALLOW_GLOBAL_HTTP_REUSE===`1`}const ve=new v.AsyncLocalStorage;function ye(e,t){return e?ve.run(e,t):t()}function N(){return ve.getStore()}function be(e={}){if(e.ownerId&&e.ownerId.trim().length>0)return e.ownerId.trim();let t=e.env??process.env,n=t.BROWSE_TOOL_OWNER;if(n&&n.trim().length>0)return n.trim();let r=t.BROWSE_TOOL_WORKFLOW_OWNER??t.WORKFLOW_RUN_ID??t.AGIFLOW_WORKFLOW_RUN_ID??(t.WORKFLOW_RUN_DIR?(0,f.basename)(t.WORKFLOW_RUN_DIR):void 0);if(r&&r.trim().length>0)return r.trim();let i=e.cwd??process.cwd(),a=M({startPath:i,env:t});return`cli-${(0,y.createHash)(`sha256`).update(`${a}:${i}`).digest(`hex`).slice(0,12)}`}function xe(){let e=new Map,t=(0,g.execSync)(`ps -axo pid=,ppid=`,{encoding:`utf-8`,stdio:[`ignore`,`pipe`,`ignore`],timeout:3e3});for(let n of t.split(`
|
|
2
|
-
`)){let[t,r]=n.trim().split(/\s+/,2);if(!t||!r)continue;let i=Number(t),a=Number(r);if(Number.isNaN(i)||Number.isNaN(a))continue;let o=e.get(a);o?o.push(i):e.set(a,[i])}return e}function Se(e){let t=[];try{let n;try{n=(0,g.execSync)(`pgrep -P ${e}`,{encoding:`utf-8`,stdio:[`ignore`,`pipe`,`ignore`],timeout:3e3}).trim().split(`
|
|
3
|
-
`).filter(Boolean).map(Number).filter(e=>!Number.isNaN(e))}catch{n=xe().get(e)??[]}for(let e of n)t.push(...Se(e)),t.push(e)}catch{}return t}function Ce(e){try{return process.kill(e,0),!0}catch{return!1}}async function we(e,t=3e3){if(!Ce(e))return;let n=[...Se(e),e];for(let e of n)try{process.kill(e,`SIGTERM`)}catch{}let r=Date.now()+t;for(;Date.now()<r;){if(!n.some(Ce))return;await new Promise(e=>setTimeout(e,100))}for(let e of n)try{Ce(e)&&process.kill(e,`SIGKILL`)}catch{}}const Te=(0,_.promisify)(g.execFile),Ee=`browse-tool-browser`;let De=class{repositoryPath;environment;isProcessRunningFn;getProcessCommandLineFn;killProcessTreeFn;cleanupProfileLocksFn;constructor(e,t={}){this.processRegistry=e,this.repositoryPath=t.repositoryPath??M(),this.environment=t.environment??process.env.NODE_ENV??`development`,this.isProcessRunningFn=t.isProcessRunning??this.isProcessRunning,this.getProcessCommandLineFn=t.getProcessCommandLine??this.getProcessCommandLine,this.killProcessTreeFn=t.killProcessTree??we,this.cleanupProfileLocksFn=t.cleanupProfileLocks}async registerBrowserProcess(e){let t=await this.processRegistry.registerProcess({repositoryPath:this.repositoryPath,serviceName:this.serviceName(e.browserId),serviceType:`tool`,environment:this.environment,pid:e.pid,command:e.command,args:e.args,tags:[Ee],metadata:{browserId:e.browserId,profileName:e.profileName,userDataDir:e.userDataDir,serverPid:e.serverPid??process.pid,serverPort:e.serverPort,ownerId:e.ownerId??N(),mode:e.mode},force:!0});if(!t.success)throw Error(t.error??`Failed to register browser process "${e.browserId}"`)}async releaseBrowserProcess(e,t={}){let n=await this.processRegistry.releaseProcess({repositoryPath:this.repositoryPath,serviceName:this.serviceName(e),serviceType:`tool`,environment:this.environment,pid:t.pid,kill:!1,releasePort:!1});if(!n.success&&n.error&&!n.error.includes(`No matching process entry`))throw Error(n.error)}async findBrowserProcess(e){return(await this.processRegistry.listProcesses({repositoryPath:this.repositoryPath,serviceType:`tool`,serviceName:this.serviceName(e),environment:this.environment,tags:[Ee]}))[0]}async reconcileOrphanedBrowsers(e={}){e.reconnectGraceMs&&e.reconnectGraceMs>0&&await new Promise(t=>setTimeout(t,e.reconnectGraceMs));let t=await this.processRegistry.listProcesses({repositoryPath:this.repositoryPath,serviceType:`tool`,environment:this.environment,tags:[Ee]});for(let n of t)await this.reconcileEntry(n,e)}parseMetadata(e){let t=e.metadata??{},n=typeof t.browserId==`string`?t.browserId:void 0,r=typeof t.userDataDir==`string`?t.userDataDir:void 0;if(!(!n||!r))return{browserId:n,userDataDir:r,profileName:typeof t.profileName==`string`?t.profileName:void 0,serverPid:typeof t.serverPid==`number`?t.serverPid:void 0,serverPort:typeof t.serverPort==`number`?t.serverPort:void 0,ownerId:typeof t.ownerId==`string`?t.ownerId:void 0,mode:this.parseBrowserMode(t.mode)}}async reconcileEntry(e,t){let n=this.parseMetadata(e);if(!n){await this.releaseEntry(e);return}if(!this.isProcessRunningFn(e.pid)){await this.releaseEntry(e),await this.cleanupProfileLocksFn?.(n.userDataDir);return}let r=n.serverPid,i=r===(t.currentServerPid??process.pid),a=typeof r==`number`&&this.isProcessRunningFn(r);if(i||a)return;let o=await this.getProcessCommandLineFn(e.pid);if(!this.isVerifiedBrowserProcess(e,n,o)){await this.releaseEntry(e);return}await this.killProcessTreeFn(e.pid),await this.cleanupProfileLocksFn?.(n.userDataDir),await this.releaseEntry(e)}async releaseEntry(e){let t=this.parseMetadata(e)?.browserId??e.serviceName.slice(20);await this.releaseBrowserProcess(t,{pid:e.pid})}isVerifiedBrowserProcess(e,t,n){return n.includes(t.userDataDir)||n.includes(t.browserId)}serviceName(e){return`browse-tool-browser-${e.replace(/[^a-zA-Z0-9_.-]/g,`-`)}`}parseBrowserMode(e){if(e===`playwright`||e===`extension`||e===`vm`)return e}isProcessRunning(e){try{return process.kill(e,0),!0}catch(e){return e.code===`EPERM`}}async getProcessCommandLine(e){try{let{stdout:t}=await Te(`ps`,[`-p`,String(e),`-o`,`command=`]);return t.trim()}catch{return``}}};De=j([(0,d.injectable)(),A(0,(0,d.inject)(O.ProcessRegistryService)),A(1,(0,d.unmanaged)()),k(`design:paramtypes`,[Object,Object])],De);var Oe=`147.0.7727.50`,ke=`browse-tool/chrome-for-testing:147.0.7727.50`,Ae=`/usr/local/bin/google-chrome-for-testing`,je=`linux/amd64`,Me=`browse-tool/cloakbrowser:latest`,Ne=`/usr/local/bin/cloakbrowser`,Pe=`linux/amd64`;const P=18e4;var Fe;const Ie=Oe,Le=`BROWSE_TOOL_CHROME_FOR_TESTING_PATH`;function Re(e,t){let n=e.split(`.`).map(e=>Number.parseInt(e,10)||0),r=t.split(`.`).map(e=>Number.parseInt(e,10)||0),i=Math.max(n.length,r.length);for(let e=0;e<i;e+=1){let t=n[e]??0,i=r[e]??0;if(t!==i)return t>i?1:-1}return 0}function ze(){let e=process.env.BROWSE_TOOL_CHROME_FOR_TESTING_VERSION?.trim();return e&&e.length>0?e:Ie}function Be(){let e=process.env[Le]?.trim();return e&&e.length>0?e:void 0}function Ve(e){return Re(e,`147.0.7727.3`)>=0}function He(){return Ve(ze())}let Ue=class{static{Fe=this}static API_URL=`https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json`;cacheDir;constructor(){this.cacheDir=(0,f.join)((0,h.homedir)(),`.cache`,`chrome-for-testing`)}async getExecutablePath(){let e=Be();if(e)return this.validateConfiguredExecutablePath(e);let t=this.getExpectedExecutablePath(),n=await this.getInstalledVersion();try{let e=this.getRequestedVersion(),r=await this.fetchVersionInfo(e);return(0,p.existsSync)(t)&&n===e?t:this.download(r)}catch(e){if((0,p.existsSync)(t))return t;throw e}}async isInstalled(){return(0,p.existsSync)(this.getExpectedExecutablePath())}async getInstalledVersion(){let e=(0,f.join)(this.cacheDir,`version.txt`);if(!(0,p.existsSync)(e))return null;try{return(await(0,m.readFile)(e,`utf-8`)).trim()}catch{return null}}async update(){let e=(0,f.join)(this.cacheDir,`chrome`);return(0,p.existsSync)(e)&&await(0,m.rm)(e,{recursive:!0,force:!0}),this.download(await this.fetchVersionInfo(this.getRequestedVersion()))}async download(e){(0,p.mkdirSync)(this.cacheDir,{recursive:!0});let t=this.getDownloadUrl(e);if(!t)throw Error(`No Chrome for Testing download available for ${this.getPlatformKey()}`);let n=(0,f.join)(this.cacheDir,`chrome.zip`),r=(0,f.join)(this.cacheDir,`chrome`);await this.downloadFile(t,n),await this.extractZip(n,r),await(0,m.rm)(n,{force:!0}),await(0,m.writeFile)((0,f.join)(this.cacheDir,`version.txt`),e.version,`utf-8`);let i=this.getExpectedExecutablePath();if(!(0,p.existsSync)(i))throw Error(`Chrome for Testing executable not found at expected path: ${i}`);return(0,h.platform)()!==`win32`&&await(0,m.chmod)(i,493),i}async fetchVersionInfo(e){let t=await fetch(Fe.API_URL);if(!t.ok)throw Error(`Failed to fetch Chrome for Testing info: ${t.statusText}`);let n=(await t.json()).versions.find(t=>t.version===e);if(!n)throw Error(`Chrome for Testing version ${e} not found in feed`);return n}getRequestedVersion(){return ze()}async validateConfiguredExecutablePath(e){try{if(!(await(0,m.stat)(e)).isFile())throw Error(`configured path is not a file`);return(0,h.platform)()!==`win32`&&await(0,m.access)(e,p.constants.X_OK),e}catch(t){throw Error(`Chrome for Testing executable configured by ${Le} was not found or is not executable: ${e}`,{cause:t})}}getDownloadUrl(e){let t=this.getPlatformKey();return e.downloads.chrome.find(e=>e.platform===t)?.url}getPlatformKey(){let e=(0,h.platform)(),t=(0,h.arch)();if(e===`darwin`)return t===`arm64`?`mac-arm64`:`mac-x64`;if(e===`linux`)return`linux64`;if(e===`win32`)return t===`x64`?`win64`:`win32`;throw Error(`Unsupported platform: ${e}`)}getExpectedExecutablePath(){let e=(0,h.platform)(),t=(0,f.join)(this.cacheDir,`chrome`);if(e===`darwin`)return(0,f.join)(t,`chrome-mac-${(0,h.arch)()===`arm64`?`arm64`:`x64`}`,`Google Chrome for Testing.app`,`Contents`,`MacOS`,`Google Chrome for Testing`);if(e===`linux`)return(0,f.join)(t,`chrome-linux64`,`chrome`);if(e===`win32`)return(0,f.join)(t,`chrome-win${(0,h.arch)()===`x64`?`64`:`32`}`,`chrome.exe`);throw Error(`Unsupported platform: ${e}`)}async downloadFile(e,t){let n=await fetch(e);if(!n.ok||!n.body)throw Error(`Failed to download: ${n.statusText}`);let r=`${t}.tmp`,i=(0,p.createWriteStream)(r),a=n.body.getReader();await(0,C.pipeline)(new S.Readable({async read(){let{done:e,value:t}=await a.read();e?this.push(null):this.push(Buffer.from(t))}}),i),await(0,m.rename)(r,t)}async extractZip(e,t){let n=(0,h.platform)();return new Promise((r,i)=>{let a;n===`win32`?a=(0,g.spawn)(`powershell`,[`-NoProfile`,`-Command`,`Expand-Archive -Path "${e}" -DestinationPath "${t}" -Force`]):((0,p.mkdirSync)(t,{recursive:!0}),a=(0,g.spawn)(`unzip`,[`-o`,`-q`,e,`-d`,t])),a.on(`close`,e=>{e===0?r():i(Error(`Failed to extract zip (exit code: ${e})`))}),a.on(`error`,e=>{i(Error(`Failed to spawn extraction process: ${e.message}`))})})}};Ue=Fe=j([(0,d.injectable)(),k(`design:paramtypes`,[])],Ue);const We=[`pnpm-workspace.yaml`,`nx.json`,`.git`],Ge=ke,Ke=je,qe=Ge.split(`:`)[0]??Ge,Je=Me,Ye=Je.split(`:`)[0]??Je;function Xe(e=process.cwd()){let t=f.default.resolve(e);for(;;){for(let e of We)if(Ze(f.default.join(t,e)))return t;let n=f.default.dirname(t);if(n===t)return e;t=n}}function Ze(e){return p.default.existsSync(e)}function Qe(){return qe}function $e(e){return`${Qe()}:${e}`}function et(e){let t=f.default.resolve(e);for(let e=0;e<8;e+=1){let e=f.default.join(t,`package.json`);if(Ze(e))try{if(JSON.parse(p.default.readFileSync(e,`utf8`)).name===`@agimon-ai/browse-tool`)return t}catch{}let n=f.default.dirname(t);if(n===t)break;t=n}return f.default.resolve(e,`../..`)}function tt(){let e=f.default.dirname((0,b.fileURLToPath)(require(`url`).pathToFileURL(__filename).href));return f.default.join(et(e),`docker/chrome-for-testing.Dockerfile`)}function nt(){return Xe(et(f.default.dirname((0,b.fileURLToPath)(require(`url`).pathToFileURL(__filename).href))))}function rt(e){if(!e?.includes(`:`))return;let[,t]=e.split(`:`,2),n=t?.trim();if(!(!n||!/^\d+\.\d+\.\d+\.\d+$/.test(n)))return n}function it(e){return(e?.trim()||Ke).toLowerCase()}function at(e){let t=it(e);return t.includes(`arm64`)||t.includes(`aarch64`)?`linux-arm64`:`linux64`}function ot(e){return e.startsWith(`${Qe()}:`)}async function st(e,t){return await new Promise((n,r)=>{let i=``,a=(0,g.spawn)(`docker`,e,{stdio:t===`pipe`?[`ignore`,`pipe`,`pipe`]:[`ignore`,t,t]});t===`pipe`&&a.stderr?.on(`data`,e=>{i+=e.toString(`utf8`)}),a.once(`error`,r),a.once(`close`,e=>{n({exitCode:e??1,stderr:i})})})}async function ct(e){return(await st([`image`,`inspect`,e],`ignore`)).exitCode===0}async function lt(e={}){let t=e.version?.trim()||rt(e.image)||ze(),n=e.image?.trim()||$e(t),r=it(e.platform),i=at(r),a=e.stdio??`inherit`,o=await st([`buildx`,`build`,`--platform`,r,`--load`,`-t`,n,`--build-arg`,`CFT_VERSION=${t}`,`--build-arg`,`CFT_ARCHIVE_PLATFORM=${i}`,`-f`,tt(),nt()],a);if(o.exitCode!==0){let e=o.stderr.trim();throw Error(e?`Failed to build Docker image ${n}: ${e}`:`Failed to build Docker image ${n}`)}return{image:n,version:t,platform:r}}async function ut(e){return!ot(e.image)||await ct(e.image)?!1:(await lt({image:e.image,platform:e.platform,stdio:`inherit`}),!0)}function dt(e){return e.startsWith(`${Ye}:`)}function ft(){let e=f.default.dirname((0,b.fileURLToPath)(require(`url`).pathToFileURL(__filename).href));return f.default.join(et(e),`docker/cloakbrowser.Dockerfile`)}async function pt(e={}){let t=e.image?.trim()||Je,n=it(e.platform),r=e.stdio??`inherit`,i=await st([`buildx`,`build`,`--platform`,n,`--load`,`-t`,t,`-f`,ft(),nt()],r);if(i.exitCode!==0){let e=i.stderr.trim();throw Error(e?`Failed to build CloakBrowser Docker image ${t}: ${e}`:`Failed to build CloakBrowser Docker image ${t}`)}return{image:t,platform:n}}async function mt(e){return!dt(e.image)||await ct(e.image)?!1:(await pt({image:e.image,platform:e.platform,stdio:`inherit`}),!0)}function ht(e,t){let n=f.default.join(h.default.tmpdir(),`proxy-auth-ext-${Date.now()}-${Math.random().toString(36).slice(2,8)}`);p.default.mkdirSync(n,{recursive:!0});let r={manifest_version:3,name:`Proxy Auth Helper`,version:`1.0.0`,permissions:[`webRequest`,`webRequestAuthProvider`],host_permissions:[`<all_urls>`],background:{service_worker:`background.js`}},i=[`chrome.webRequest.onAuthRequired.addListener(`,` (details) => ({ authCredentials: { username: ${JSON.stringify(e)}, password: ${JSON.stringify(t)} } }),`,` { urls: ["<all_urls>"] },`,` ["blocking"]`,`);`,``].join(`
|
|
4
|
-
`);return p.default.writeFileSync(f.default.join(n,`manifest.json`),JSON.stringify(r,null,2)),p.default.writeFileSync(f.default.join(n,`background.js`),i),n}function gt(e){let t=[`--proxy-server=${e.server}`];return e.bypass&&t.push(`--proxy-bypass-list=${e.bypass}`),t}var F;const _t=new Set([`docker`,`docker-vm`,`docker-chromium`,`docker-chrome-testing`,`docker-cloakbrowser`]),vt=ke,yt=Ae,bt=Me,xt=Ne,St=4e3,I=process.env.BROWSE_TOOL_DEBUG_EXTENSION_RECORDING===`1`,Ct=[`Current Session`,`Current Tabs`,`Last Session`,`Last Tabs`,`Sessions`],wt=new Set([`Default`,`Guest Profile`,`System Profile`]);var Tt=class extends Error{constructor(e,t){super(`Browser launch for profile "${e}" is busy: another launch held the profile for more than ${t}ms. Retry shortly, or use a different profile to run in parallel.`),this.profileName=e,this.timeoutMs=t,this.name=`BrowserLaunchBusyError`}};let Et=class{static{F=this}static MAX_ACTIVE_BROWSERS=15;static MAX_PAGES_PER_BROWSER=15;static chromeForTestingLaunchQueue=Promise.resolve();browsers=new Map;closingExtensionRecordings=new Map;profileToBrowserId=new Map;browserIdCounter=0;profileLaunchTails=new Map;static DEFAULT_PROFILE_LAUNCH_TIMEOUT_MS=12e4;constructor(e,t,n,r,i,a,o,s,c){this.profileService=e,this.pageRegistry=t,this.lockManager=n,this.chromeForTesting=r,this.pageMonitor=i,this.webSocketHub=a,this.extensionSessionRegistry=o,this.extensionTaskQueue=s,this.browserProcessRegistry=c}getBrowserByProfile(e){let t=this.profileToBrowserId.get(e);if(t){let n=this.browsers.get(t);if(n){if(this.isBrowserAlive(n))return n;this.cleanupStaleBrowser(t,e)}else this.profileToBrowserId.delete(e)}}async launch(e={}){return e.profileName?this.withProfileLaunchLock(e.profileName,()=>this.launchInternal(e)):this.launchInternal(e)}async launchInternal(e={}){let{profileName:t,browserType:n=`chromium`,headless:r=!0,onPageClose:i,baseURL:a,recordVideo:o,proxy:s,browserId:c}=e;if(t){let e=this.getBrowserByProfile(t);if(e){let{pageId:t,page:n}=await this.newPage(e.id,i);return{browserInstance:e,pageId:t,page:n}}}await this.enforceMaxBrowserLimit();let l=c||(t?`browser-${t}`:`browser-${++this.browserIdCounter}`);if(c&&this.browsers.has(c))throw Error(`Browser ID "${c}" is already in use`);let u=this.getBrowserType(n),d=t?await this.profileService.get(t):null;t&&!d&&(d=await this.profileService.create({name:t,browserType:n}));let f=s?{proxy:{server:s.server,username:s.username,password:s.password,bypass:s.bypass}}:{},p=process.platform===`linux`&&typeof process.getuid==`function`&&process.getuid()===0,m=n===`chromium`?process.env.CHROME_BIN||process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH:void 0,h=process.env.BROWSE_TOOL_DISABLE_GPU===`true`,g=n===`chromium`?[...h?[`--disable-gpu`,`--disable-software-rasterizer`,`--disable-dev-shm-usage`,`--disable-features=VizDisplayCompositor`]:[],...p?[`--no-sandbox`,`--disable-setuid-sandbox`]:[]]:[],_=n===`chromium`?{...g.length>0?{args:g}:{},...m?{executablePath:m}:{}}:{},v;if(!r&&n===`chromium`)try{v=await u.launch({headless:r,...m?{}:{channel:`chrome`},..._,...f})}catch{v=await u.launch({headless:r,..._,...f})}else v=await u.launch({headless:r,..._,...f});let y=this.buildContextOptions(d,a,o);if(d&&t){let e=await this.profileService.loadStorageState(t);e&&(y.storageState=e)}let b=await v.newContext(y),x=await b.newPage(),S=new Date,C={id:l,mode:`playwright`,browser:v,context:b,profileName:t,pageIds:new Set,currentPageId:null,createdAt:S,lastAccessedAt:S};this.browsers.set(l,C),v.on(`disconnected`,()=>{this.cleanupStaleBrowser(l,t)}),t&&this.profileToBrowserId.set(t,l);let w=await this.pageRegistry.register({page:x,browser:v,context:b,browserId:l,profileName:t,onClose:e=>{if(C.pageIds.delete(e),C.currentPageId===e){let e=Array.from(C.pageIds);C.currentPageId=e.length>0?e[0]:null}i?.(e),C.pageIds.size===0&&this.closeBrowser(l).catch(()=>{})}});return this.pageMonitor?.startMonitoring(w,x),C.pageIds.add(w),C.currentPageId=w,{browserInstance:C,pageId:w,page:x}}async launchWithExtension(e={}){return e.profileName?this.withProfileLaunchLock(e.profileName,()=>this.launchWithExtensionInternal(e)):this.launchWithExtensionInternal(e)}async launchWithExtensionInternal(e={}){let{profileName:t,command:n,commandArgs:r=[],browserId:i,mode:a=`extension`}=e;if(t){let e=this.getBrowserByProfile(t);if(e)if(!this.canReuseExtensionBrowser(e))await this.closeBrowser(e.id).catch(()=>{this.cleanupStaleBrowser(e.id,t)});else{let t=this.getTrackedExtensionPageId(e);return this.recordBrowserActivity(e.id,t),{browserInstance:e,pageId:t,process:{pid:e.pid}}}}await this.enforceMaxBrowserLimit();let o=this.resolveExtensionPath(e.extensionPath),s=f.default.join(o,`manifest.json`);if(!p.default.existsSync(s))throw Error(`Extension manifest not found at ${s}. Please build the extension first: pnpm build:extension`);let c=i||(t?`browser-${t}`:`browser-${++this.browserIdCounter}`);if(i&&this.browsers.has(i))throw Error(`Browser ID "${i}" is already in use`);t&&(await this.profileService.get(t)||await this.profileService.create({name:t,browserType:`chromium`}));let l=this.createUserDataDir(t);l&&await this.lockManager.ensureAvailable(l);let u=await this.resolveVmRuntime({command:n,launchMode:a,vmDockerBrowser:e.vmDockerBrowser,extensionHostPath:o,requestedRuntimeExtensionPath:e.extensionPath,userDataDir:l});if(u.kind===`docker`)try{this.ensureDockerWritableUserDataDir(l)}catch(e){let t=e instanceof Error?e.message:String(e);throw Error(`Failed to prepare Docker user-data directory "${l}": ${t}`,{cause:e})}let d=[u.extensionPath],m=[];if(u.kind===`docker`&&m.push(...this.createDockerVolumeArgs(o,u.extensionPath,!0)),e.proxy?.username&&e.proxy?.password){let t=ht(e.proxy.username,e.proxy.password);if(u.kind===`docker`){let e=`${u.docker.extensionPathInContainer}-proxy-auth-${Date.now()}`;d.push(e),m.push(...this.createDockerVolumeArgs(t,e,!0))}else d.push(t)}let h=d.join(`,`),g=process.env.BROWSE_TOOL_AUTO_SELECT_TAB_CAPTURE_SOURCE_TITLE?.trim(),_=process.env.BROWSE_TOOL_AUTO_SELECT_DESKTOP_CAPTURE_SOURCE?.trim(),v=process.env.BROWSE_TOOL_USE_FAKE_UI_FOR_MEDIA_STREAM===`1`,y=u.kind===`docker`||process.platform===`linux`&&typeof process.getuid==`function`&&process.getuid()===0,b=[`--user-data-dir=${u.userDataDir}`,`--load-extension=${h}`,`--disable-extensions-except=${h}`,`--enable-extensions`,`--auto-accept-this-tab-capture`,...v?[`--use-fake-ui-for-media-stream`]:[],...g?[`--auto-select-tab-capture-source-by-title=${g}`]:[],..._?[`--auto-select-desktop-capture-source=${_}`]:[],`--disable-blink-features=AutomationControlled`,`--disable-infobars`,`--no-first-run`,`--no-default-browser-check`,`--disable-background-timer-throttling`,`--disable-backgrounding-occluded-windows`,`--disable-renderer-backgrounding`,`--disable-component-update`,`--disable-features=TranslateUI`,`--disable-ipc-flooding-protection`,...u.kind===`docker`?[`--disable-gpu`]:[],`--silent-debugger-extension-api`,`--password-store=basic`,`--use-mock-keychain`,`--disable-dev-shm-usage`,`--webrtc-ip-handling-policy=disable_non_proxied_udp`,`--enforce-webrtc-ip-permission-check`,`--lang=${process.env.LANG?.split(`.`)[0]?.replace(`_`,`-`)||`en-US`}`,...y?[`--no-sandbox`,`--disable-setuid-sandbox`]:[],`--window-size=1920,1080`,...e.proxy?gt(e.proxy):[],e.url??`about:blank`],x=e=>{try{let t=new URL(e);return t.protocol===`http:`||t.protocol===`https:`||t.protocol===`file:`}catch{return!1}},S=e=>{if(!x(e))return e;try{let t=new URL(e),n=t.pathname.endsWith(`/`)&&t.pathname!==`/`?t.pathname.slice(0,-1):t.pathname;return`${t.protocol}//${t.host}${n}${t.search}`}catch{return e}},C=S(e.url??`about:blank`),w=a===`vm`&&r.length>0?[...b,...r.filter(e=>x(e)?S(e)!==C:!0)]:r.length>0?r:b,T=u.kind===`docker`?{TZ:Intl.DateTimeFormat().resolvedOptions().timeZone,...e.dockerEnv}:e.dockerEnv,ee=u.kind===`docker`?[...this.buildDockerSpawnArgs(u.docker,l,m,e.dockerRunArgs,T,e.dockerEnableVnc),...w]:w,E,te=0,D;for(let n=1;n<=3;n+=1)try{let n=()=>this.startExtensionBrowser({browserId:c,launchMode:a,profileName:t,vmRuntime:u,spawnArgs:ee,recordVideo:e.recordVideo,startupDelayMs:e.startupDelayMs,launchUrl:e.url});return this.shouldSerializeChromeForTestingLaunch(u)?await this.runSerializedChromeForTestingLaunch(n):await n()}catch(e){if(E=e instanceof Error?e:Error(String(e)),!this.isRetryableExtensionLaunchError(E)||n===3){if(u.kind===`docker`&&(te>0||D)){let e=[te>0?`Docker retry cleanup attempts: ${te}.`:``,D?`Last Docker retry cleanup: ${D}.`:``].filter(Boolean).join(` `);E.message=`${E.message} ${e}`.trim()}throw E}u.kind===`docker`&&(te+=1,D=await this.prepareDockerRetryUserDataDir(l)),await this.delay(250*n)}throw E??Error(`Extension browser launch failed`)}async resolveVmRuntime(e){let{command:t,launchMode:n,vmDockerBrowser:r,extensionHostPath:i,requestedRuntimeExtensionPath:a,userDataDir:o}=e;if(n===`vm`&&this.isDockerVmAlias(t)){let e=this.resolveDockerRuntimeExtensionPath(a),t=process.env.PLAYWRIGHT_VM_DOCKER_USER_DATA_DIR||`/vm/user-data`,n=this.resolveDockerBrowserType(r),{image:i,browserBinary:o,platform:s}=this.resolveDockerImageConfig(n);return n===`chrome-for-testing`?await ut({image:i,platform:s}):await mt({image:i,platform:s}),{kind:`docker`,command:`docker`,extensionPath:e,userDataDir:t,docker:{image:i,browserBinary:o,platform:s,extensionPathInContainer:e,userDataDirInContainer:t}}}return{kind:`host`,command:await this.resolveLaunchCommand({command:t,launchMode:n,vmDockerBrowser:r}),extensionPath:i,userDataDir:o}}resolveDockerRuntimeExtensionPath(e){return e&&!p.default.existsSync(f.default.join(e,`manifest.json`))?e:process.env.PLAYWRIGHT_VM_DOCKER_EXTENSION_PATH||`/vm/extensions/playwright`}resolveDockerBrowserType(e){let t=process.env.PLAYWRIGHT_VM_DOCKER_BROWSER?.trim().toLowerCase();return t===`chrome-for-testing`||t===`cft`?`chrome-for-testing`:t===`cloakbrowser`||t===`cloak`?`cloakbrowser`:e||(process.env.PLAYWRIGHT_VM_DOCKER_IMAGE?`chrome-for-testing`:`cloakbrowser`)}resolveDockerImageConfig(e){return e===`chrome-for-testing`?{image:process.env.PLAYWRIGHT_VM_DOCKER_IMAGE||vt,browserBinary:process.env.PLAYWRIGHT_VM_DOCKER_BROWSER_BINARY||yt,platform:process.env.PLAYWRIGHT_VM_DOCKER_PLATFORM||je}:{image:process.env.PLAYWRIGHT_VM_DOCKER_IMAGE||bt,browserBinary:process.env.PLAYWRIGHT_VM_DOCKER_BROWSER_BINARY||xt,platform:process.env.PLAYWRIGHT_VM_DOCKER_PLATFORM||Pe}}createDockerVolumeArgs(e,t,n){return[`-v`,`${f.default.resolve(e)}:${t}${n?`:ro`:``}`]}ensureDockerWritableUserDataDir(e){p.default.mkdirSync(e,{recursive:!0,mode:511});let t=[e];for(;t.length>0;){let e=t.pop();if(!e)continue;p.default.chmodSync(e,511);let n=p.default.readdirSync(e,{withFileTypes:!0});for(let r of n){let n=f.default.join(e,r.name);p.default.chmodSync(n,r.isDirectory()?511:438),r.isDirectory()&&t.push(n)}}}buildDockerSpawnArgs(e,t,n,r,i,a){let o=[`run`,`--rm`,`--init`,`--shm-size`,process.env.PLAYWRIGHT_VM_DOCKER_SHM_SIZE||`2g`];return e.platform&&o.push(`--platform`,e.platform),process.env.PLAYWRIGHT_VM_DOCKER_NETWORK&&o.push(`--network`,process.env.PLAYWRIGHT_VM_DOCKER_NETWORK),process.platform===`linux`&&o.push(`--add-host`,`host.docker.internal:host-gateway`),o.push(...n),o.push(...this.createDockerVolumeArgs(t,e.userDataDirInContainer,!1)),o.push(...this.createDockerEnvArgs(i)),o.push(...this.parseDockerRunArgs(process.env.PLAYWRIGHT_VM_DOCKER_RUN_ARGS)),o.push(...r??[]),a?(o.push(`--entrypoint`,`/bin/bash`,e.image,`-lc`,this.buildDockerBrowserLaunchScript(e.browserBinary,{enableVnc:!0}),`_`),o):(o.push(`--entrypoint`,`/bin/bash`,e.image,`-lc`,this.buildDockerBrowserLaunchScript(e.browserBinary,{enableVnc:!1}),`_`),o)}buildDockerBrowserLaunchScript(e,t){let n=[`set -euo pipefail`,`export XDG_RUNTIME_DIR="/tmp/runtime-browse"`,`mkdir -p "$XDG_RUNTIME_DIR" "$XDG_RUNTIME_DIR/pulse"`,`chmod 700 "$XDG_RUNTIME_DIR" || true`,`command -v dbus-daemon >/dev/null 2>&1 && export DBUS_SESSION_BUS_ADDRESS="$(dbus-daemon --session --fork --print-address --nopidfile)" || true`,`if command -v pulseaudio >/dev/null 2>&1; then pulseaudio --check >/dev/null 2>&1 || pulseaudio --daemonize=yes --exit-idle-time=-1 --disable-shm=true --log-target=stderr >/tmp/pulseaudio.log 2>&1; export PULSE_SERVER="unix:$XDG_RUNTIME_DIR/pulse/native"; fi`,`Xvfb :99 -screen 0 1920x1080x24 -nolisten tcp >/tmp/xvfb.log 2>&1 &`,`if command -v xdpyinfo >/dev/null 2>&1; then for i in $(seq 1 30); do xdpyinfo -display :99 >/dev/null 2>&1 && break; sleep 0.2; done; else sleep 1; fi`,`export DISPLAY=:99`,`BROWSER_BIN=${JSON.stringify(e)}`,`exec "$BROWSER_BIN" "$@"`];return t.enableVnc&&n.splice(n.length-2,0,'if [ -n "${SE_VNC_PASSWORD:-}" ]; then',' x11vnc -storepasswd "${SE_VNC_PASSWORD}" /tmp/vnc-passwd >/dev/null 2>&1',` X11VNC_AUTH="-rfbauth /tmp/vnc-passwd"`,`else`,` X11VNC_AUTH=""`,`fi`,'x11vnc -forever -shared -rfbport "${SE_VNC_PORT:-5900}" -display :99 $X11VNC_AUTH >/tmp/x11vnc.log 2>&1 &',`if command -v websockify >/dev/null 2>&1; then`,' websockify --web /opt/bin/noVNC "${SE_NO_VNC_PORT:-7900}" "localhost:${SE_VNC_PORT:-5900}" >/tmp/novnc.log 2>&1 &',`fi`),n.join(`
|
|
5
|
-
`)}createDockerEnvArgs(e){if(!e)return[];let t=[];for(let[n,r]of Object.entries(e))n&&t.push(`-e`,`${n}=${r}`);return t}parseDockerRunArgs(e){return e?e.split(/\s+/).map(e=>e.trim()).filter(e=>e.length>0):[]}isDockerVmAlias(e){if(!e)return!1;let t=e.trim().toLowerCase();return _t.has(t)}async resolveLaunchCommand(e){let{command:t,launchMode:n,vmDockerBrowser:r}=e;if(n===`vm`&&r===`chrome-for-testing`){if(!t)return this.findChromeExecutable();if(this.isChromeTestingAlias(t)||this.isGenericChromeAlias(t))return this.chromeForTesting.getExecutablePath()}return t||this.findChromeExecutable()}isChromeTestingAlias(e){let t=e.trim().toLowerCase();return t===`chrome-testing`||t===`chrome-for-testing`||t===`google-chrome-for-testing`||t.endsWith(`google chrome for testing`)||t.endsWith(`google-chrome-for-testing`)||t.endsWith(`chrome-for-testing.exe`)}isGenericChromeAlias(e){let t=e.trim().toLowerCase(),n=f.default.basename(t);return n===`chrome`||n===`google-chrome`||n===`googlechrome`}resolveExtensionPath(e){if(e&&p.default.existsSync(f.default.join(e,`manifest.json`)))return e;let t=f.default.dirname((0,b.fileURLToPath)(require(`url`).pathToFileURL(__filename).href)),n=this.findPackageRoot(t),r=[n?f.default.resolve(n,`dist/extension`):``,f.default.resolve(t,`../dist/extension`),f.default.resolve(t,`../../dist/extension`)].filter(Boolean);for(let e of r)if(p.default.existsSync(f.default.join(e,`manifest.json`)))return e;let i=f.default.resolve(process.cwd(),`dist/extension`);if(p.default.existsSync(f.default.join(i,`manifest.json`)))return i;throw Error(`Chrome extension not found. Build the extension first: pnpm build:extension`)}findPackageRoot(e,t=`@agimon-ai/browse-tool`){let n=e;for(let e=0;e<8;e++){let e=f.default.join(n,`package.json`);if(p.default.existsSync(e))try{if(JSON.parse(p.default.readFileSync(e,`utf8`))?.name===t)return n}catch{}let r=f.default.dirname(n);if(r===n)break;n=r}return null}getExtensionUserDataDir(e){return f.default.join(this.profileService.getProfilesDir(),e,`extension-user-data`)}createUserDataDir(e){if(e){let t=this.getExtensionUserDataDir(e);return p.default.mkdirSync(t,{recursive:!0}),this.cleanupExtensionSessionRestoreArtifacts(t),t}return p.default.mkdtempSync(f.default.join(h.default.tmpdir(),`extension-chrome-`))}cleanupExtensionSessionRestoreArtifacts(e){for(let t of this.getSessionRestoreProfileDirs(e))for(let e of Ct)try{p.default.rmSync(f.default.join(t,e),{recursive:!0,force:!0})}catch{}}getSessionRestoreProfileDirs(e){let t=[e],n=(()=>{try{return p.default.readdirSync(e,{withFileTypes:!0})}catch{return null}})();if(!n)return t;for(let r of n)r.isDirectory()&&(wt.has(r.name)||r.name.startsWith(`Profile `))&&t.push(f.default.join(e,r.name));return t}async findChromeExecutable(){try{return await this.chromeForTesting.getExecutablePath()}catch(e){if(Be())throw e}let e=process.platform,t={darwin:[`/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`,`/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary`,`${h.default.homedir()}/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`],linux:[`/usr/bin/google-chrome`,`/usr/bin/google-chrome-stable`,`/usr/bin/chromium`,`/usr/bin/chromium-browser`,`/snap/bin/chromium`],win32:[`${process.env.LOCALAPPDATA}\\Google\\Chrome\\Application\\chrome.exe`,`${process.env.PROGRAMFILES}\\Google\\Chrome\\Application\\chrome.exe`,`${process.env[`PROGRAMFILES(X86)`]}\\Google\\Chrome\\Application\\chrome.exe`]};for(let n of t[e]??[])if(p.default.existsSync(n))return n;throw Error(`Chrome not found. Install Google Chrome or run with Chrome for Testing.`)}async newPage(e,t){let n=this.browsers.get(e);if(!n)throw Error(`Browser "${e}" not found`);if(n.mode===`extension`||n.mode===`vm`)throw Error(`Cannot create new page in extension mode browser. Use extension APIs directly.`);if(!n.context||!n.browser)throw Error(`Browser "${e}" has no Playwright context`);await this.enforceMaxPageLimit(n);let r=await n.context.newPage(),i=await this.pageRegistry.register({page:r,browser:n.browser,context:n.context,browserId:e,profileName:n.profileName,onClose:r=>{if(n.pageIds.delete(r),n.currentPageId===r){let e=Array.from(n.pageIds);n.currentPageId=e.length>0?e[0]:null}t?.(r),n.pageIds.size===0&&this.closeBrowser(e).catch(()=>{})}});return this.pageMonitor?.startMonitoring(i,r),n.pageIds.add(i),{pageId:i,page:r}}getBrowser(e){return this.browsers.get(e)}getDefaultBrowser(){let e=this.listBrowsers();for(let t of e){if(this.isBrowserAlive(t))return t;this.cleanupStaleBrowser(t.id,t.profileName)}}async getOrCreateDefaultBrowser(e){return this.getDefaultBrowser()||(await this.launch(e)).browserInstance}setCurrentPage(e,t){let n=this.browsers.get(e);if(!n)throw Error(`Browser "${e}" not found`);if(!n.pageIds.has(t))throw Error(`Page "${t}" does not belong to browser "${e}"`);n.extensionRecording?.active&&n.extensionRecording.pageId!==t&&console.warn(`[BrowserService] Extension recording for browser "${e}" remains pinned to initial page "${n.extensionRecording.pageId}"`),n.currentPageId=t}async releaseOwnerPages(e,t){let n=this.pageRegistry.findByOwner(t).filter(t=>t.browserId===e);for(let e of n)try{e.page?await e.page.close():this.pageRegistry.remove(e.id)}catch{}}async closeBrowser(e){let t=N();if(t&&this.browsers.get(e)&&this.pageRegistry.findByBrowser(e).some(e=>e.ownerId!==t)){await this.releaseOwnerPages(e,t);return}let n=this.browsers.get(e);if(n){let t,r=n.extensionRecording,i=r?.outputPath,a=r?.active===!0,o=!1;if(r&&this.closingExtensionRecordings.set(e,r),a)try{if(t=(await this.attemptExtensionRecordingFinalization(e)).error,t&&r&&this.shouldCloseRecordedExtensionPageFirst(n)&&(await this.closeRecordedExtensionPage(n,r),i)){if(!await this.waitForExtensionRecordingArtifact(i))throw Error(`Extension recording for browser "${e}" was not persisted after the recorded tab closed`);r.active=!1,t=void 0}}catch(e){t=e instanceof Error?e:Error(String(e))}else try{await this.finalizeExtensionRecording(e)}catch(e){t=e instanceof Error?e:Error(String(e))}if(n.browser)await n.browser.close();else if(n.context)if(n.pid)try{await we(n.pid)}catch{try{let e=n.context.pages();await Promise.all(e.map(e=>e.close().catch(e=>{}))),await n.context.close()}catch{}}else{let e=n.context.pages();await Promise.all(e.map(e=>e.close().catch(e=>{}))),await n.context.close()}else if(n.pid)try{await we(n.pid)}catch{}if(i&&r&&!p.default.existsSync(i)){let n=await this.waitForExtensionRecordingArtifactOrChunks(r),a=n.artifactReady;!a&&n.chunkReady&&(a=await this.persistExtensionRecordingArtifact(e)||p.default.existsSync(i)),a?t=void 0:r&&(o=!0)}if(n.profileName&&this.profileToBrowserId.delete(n.profileName),this.browsers.delete(e),await this.browserProcessRegistry?.releaseBrowserProcess(e).catch(()=>{}),o?this.finalizeClosingExtensionRecording(e):this.closingExtensionRecordings.delete(e),t)throw t}}async closeAll(){let e=Array.from(this.browsers.keys());await Promise.all(e.map(e=>this.closeBrowser(e).catch(()=>{}))),this.browsers.clear(),this.profileToBrowserId.clear(),this.pageRegistry.clear()}listBrowsers(){return Array.from(this.browsers.values())}touchBrowser(e){let t=this.browsers.get(e);t&&(t.lastAccessedAt=new Date)}recordBrowserActivity(e,t){let n=this.browsers.get(e);return n?(n.lastAccessedAt=new Date,t&&this.pageRegistry.touchPage(t),!0):!1}registerExternalBrowser(e){let t=`browser-${++this.browserIdCounter}`,n=new Date,r={id:t,mode:`playwright`,browser:e,context:void 0,pageIds:new Set,currentPageId:null,createdAt:n,lastAccessedAt:n,specOrigin:!0};return this.browsers.set(t,r),e.on(`disconnected`,()=>{this.cleanupStaleBrowser(t)}),t}registerExtensionBrowser(){let e=`browser-${++this.browserIdCounter}`,t=new Date,n={id:e,mode:`extension`,browser:void 0,context:void 0,pageIds:new Set,currentPageId:null,createdAt:t,lastAccessedAt:t};return this.browsers.set(e,n),e}registerExtensionBrowserWithId(e){if(this.browsers.has(e))return;let t=new Date,n={id:e,mode:`extension`,browser:void 0,context:void 0,pageIds:new Set,currentPageId:null,createdAt:t,lastAccessedAt:t};this.browsers.set(e,n),this.hydrateExtensionBrowserFromRegistry(e)}async hydrateExtensionBrowserFromRegistry(e){if(!this.browserProcessRegistry)return;let t=await this.browserProcessRegistry.findBrowserProcess(e).catch(()=>void 0);if(!t)return;let n=this.browserProcessRegistry.parseMetadata(t),r=this.browsers.get(e);!n||!r||(r.pid=t.pid,r.profileName=n.profileName,r.userDataDir=n.userDataDir,n.mode&&(r.mode=n.mode),n.profileName&&this.profileToBrowserId.set(n.profileName,e))}async ensureExtensionRecordingActive(e,t){let n=this.browsers.get(e),r=n?.extensionRecording;!n||!r||r.pageId!==t||r.active||(r.startPromise||=this.startExtensionRecording(n,r).finally(()=>{n.extensionRecording===r&&(n.extensionRecording.startPromise=void 0)}),await r.startPromise)}async startPageRecording(e,t,n){let r=this.browsers.get(e);if(!r)throw Error(`Browser "${e}" not found`);if(r.mode!==`extension`&&r.mode!==`vm`)throw Error(`Browser "${e}" does not support extension recording in "${r.mode}" mode`);let i=r.extensionRecording;if(i&&i.pageId!==t){if(i.active)throw Error(`Browser "${e}" is already recording page "${i.pageId}". Stop the current recording before starting another one.`);r.extensionRecording=void 0,i=void 0}let a=n?f.default.resolve(n):i?.outputPath??f.default.join(h.default.tmpdir(),`browse-tool-recordings`,`recording-${this.toSafeRecordingId(e)}-${Date.now()}.webm`);if(!i)this.initializeExtensionRecordingAtPath(r,t,a),i=r.extensionRecording;else if(i.outputPath!==a){if(i.active)throw Error(`Browser "${e}" is already recording to "${i.outputPath}". Stop the current recording before changing output path.`);i.outputPath=a,i.chunkPath=`${a}.part`,this.clearExtensionRecordingChunkState(i)}if(!i)throw Error(`Failed to initialize recording for browser "${e}"`);return await this.ensureExtensionRecordingActive(e,t),{outputPath:i.outputPath}}async finalizeExtensionRecording(e,t){let n=this.browsers.get(e),r=n?.extensionRecording;if(!n||!r||t&&r.pageId!==t||!r.active)return;if(!this.extensionTaskQueue)throw Error(`Extension recording task queue is unavailable for browser "${e}"`);let i=He();I&&console.log(`[ExtensionRecordingDebug] queue stop browser=${e} page=${r.pageId} active=${r.active} cleanupUi=${i}`);let a;try{a=await this.extensionTaskQueue.queueTask(`browser_stop_recording`,{pageId:r.pageId,cleanupUi:i},P,e)}finally{r.active=!1}I&&console.log(`[ExtensionRecordingDebug] stop result browser=${e} success=${a?.success===!0} isError=${a?.result?.isError===!0}`);let o;try{o=this.parseExtensionRecordingPayload(a)}catch(t){if(await this.persistExtensionRecordingArtifact(e))return;throw t}if(o.videoBase64){p.default.mkdirSync(f.default.dirname(r.outputPath),{recursive:!0}),p.default.writeFileSync(r.outputPath,Buffer.from(o.videoBase64,`base64`)),this.clearExtensionRecordingChunkState(r),r.active=!1;return}if(!await this.persistExtensionRecordingArtifact(e))throw Error(`Extension recording for browser "${e}" stopped without video data`)}async stopPageRecording(e,t,n={}){let r=this.browsers.get(e)?.extensionRecording??this.closingExtensionRecordings.get(e);if(!r)throw Error(`Browser "${e}" has no recording session`);if(t&&r.pageId!==t)throw Error(`Page "${t}" does not match the active recording for browser "${e}"`);this.closingExtensionRecordings.set(e,r);let i;try{i=(await this.attemptExtensionRecordingFinalization(e)).error}catch(e){i=e instanceof Error?e:Error(String(e))}if(!p.default.existsSync(r.outputPath)){let t=await this.waitForExtensionRecordingArtifactOrChunks(r),n=t.artifactReady;if(!n&&t.chunkReady&&(n=await this.persistExtensionRecordingArtifact(e)||p.default.existsSync(r.outputPath)),!n&&i)throw i}if(!p.default.existsSync(r.outputPath))throw Error(`Recording artifact for browser "${e}" was not written to "${r.outputPath}"`);let a=p.default.readFileSync(r.outputPath);return this.closingExtensionRecordings.delete(e),{outputPath:r.outputPath,fileSizeBytes:a.byteLength,...n.includeBase64?{videoBase64:a.toString(`base64`)}:{}}}async persistExtensionRecordingArtifact(e,t){let n=this.getExtensionRecordingSession(e);if(!n)return!1;if(!t&&this.hasPersistedExtensionRecordingArtifact(n))return n.active=!1,this.closingExtensionRecordings.delete(e),!0;if(p.default.mkdirSync(f.default.dirname(n.outputPath),{recursive:!0}),t)p.default.writeFileSync(n.outputPath,Buffer.from(t,`base64`)),this.clearExtensionRecordingChunkState(n);else if(!this.persistExtensionRecordingChunksToArtifact(n))return!1;return n.active=!1,this.closingExtensionRecordings.delete(e),!0}async persistExtensionRecordingChunk(e,t){let n=this.getExtensionRecordingSession(e);if(!n||!t)return null;let r=Buffer.from(t,`base64`);return r.length===0?null:(p.default.mkdirSync(f.default.dirname(n.chunkPath),{recursive:!0}),p.default.appendFileSync(n.chunkPath,r),n.chunkCount+=1,n.chunkBytes+=r.length,{chunkBytes:r.length,totalBytes:n.chunkBytes,chunkCount:n.chunkCount})}async enforceMaxPageLimit(e){if(e.pageIds.size<F.MAX_PAGES_PER_BROWSER)return;let t=this.pageRegistry.findByBrowser(e.id);t.sort((e,t)=>e.createdAt.getTime()-t.createdAt.getTime());let n=t[0];n&&(n.page?await n.page.close():(this.pageRegistry.remove(n.id),e.pageIds.delete(n.id)))}async enforceMaxBrowserLimit(){if(this.browsers.size>=F.MAX_ACTIVE_BROWSERS){let e=Array.from(this.browsers.values());e.sort((e,t)=>e.createdAt.getTime()-t.createdAt.getTime());let t=e[0];t&&await this.closeBrowser(t.id)}}isBrowserAlive(e){try{if(e.mode===`playwright`)return e.browser?.isConnected()??!1;if(e.mode===`extension`||e.mode===`vm`){if(!e.pid||!this.isProcessRunning(e.pid))return!1;if(!e.context)return!0;try{return e.context.pages(),!0}catch{return!1}}return!1}catch{return!1}}isProcessRunning(e){try{return process.kill(e,0),!0}catch(e){return e.code===`EPERM`}}captureProcessOutput(){let e=[],t=0,n=(n,r)=>{if(typeof r!=`string`&&!Buffer.isBuffer(r))return;let i=typeof r==`string`?r:r.toString(`utf8`);if(!i)return;let a=32768-t;if(a<=0)return;let o=Buffer.from(i,`utf8`).subarray(0,a).toString(`utf8`);t+=Buffer.byteLength(o,`utf8`),e.push(`[${n}] ${o}`)};return{attach:e=>{e.stdout?.on(`data`,e=>n(`stdout`,e)),e.stderr?.on(`data`,e=>n(`stderr`,e))},snapshot:()=>e.join(``)}}async startExtensionBrowser(e){let{browserId:t,launchMode:n,profileName:r,vmRuntime:i,spawnArgs:a,recordVideo:o,startupDelayMs:s,launchUrl:c}=e,l=i.userDataDir,u=this.captureProcessOutput(),d=(0,g.spawn)(i.command,a,{detached:!1,stdio:[`ignore`,`pipe`,`pipe`],env:i.kind===`docker`?process.env:{...process.env,DISPLAY:process.env.DISPLAY}});if(!d.pid)throw Error(`Failed to start Chrome process`);u.attach(d);let f=new Date,p={id:t,mode:n,browser:void 0,context:void 0,profileName:r,pageIds:new Set,currentPageId:null,pid:d.pid,userDataDir:l,createdAt:f,lastAccessedAt:f};d.on(`exit`,()=>this.cleanupStaleBrowser(t,r)),d.on(`error`,()=>this.cleanupStaleBrowser(t,r)),this.browsers.set(t,p),r&&this.profileToBrowserId.set(r,t);let m=this.pageRegistry.registerExtensionPage(t);p.pageIds.add(m),p.currentPageId=m;try{if(await this.browserProcessRegistry?.registerBrowserProcess({browserId:t,profileName:r,userDataDir:l,pid:d.pid,command:i.command,args:a,serverPid:process.pid,serverPort:Number.parseInt(process.env.PLAYWRIGHT_PORT??``,10)||void 0,ownerId:N(),mode:n}).catch(e=>{console.warn(`[BrowserService] Failed to register browser process "${t}":`,e)}),await this.waitForExtensionReady(t,d,u,{vmRuntimeKind:i.kind,launchUrl:c}),o){I&&console.log(`[ExtensionRecordingDebug] initialize recording browser=${p.id} page=${m} dir=${o.dir}`),this.initializeExtensionRecording(p,m,o);let e=p.extensionRecording;e&&this.startExtensionRecording(p,e).catch(e=>{console.warn(`[BrowserService] Failed to eagerly start extension recording for browser "${p.id}" on page "${m}":`,e)})}}catch(e){throw await this.closeBrowser(t).catch(()=>{}),e}return s&&s>0&&await this.delay(s),{browserInstance:p,pageId:m,process:d}}async waitForExtensionReady(e,t,n,r){if(!this.webSocketHub&&!this.extensionSessionRegistry)return;let i=null,a=null,o;t.once(`exit`,(e,t)=>{i=e,a=t}),t.once(`error`,e=>{o=e});let s=null,c=!1,l=0,u=0,d=null,f=!1,p=Date.now(),m=this.getExtensionStartupTimeoutMs(r.vmRuntimeKind),h=this.getExtensionStartupGraceTimeoutMs(r.vmRuntimeKind),g=this.getExtensionReadyStabilityMs(),_=this.getExtensionStartupPollIntervalMs(),v=p+m;for(;;){let t=Date.now();if(t>=v){if(!f&&o===void 0&&i===null&&a===null){f=!0,v=t+h;continue}break}let n=this.getExtensionReadyState(e);if(n.connected){if(c=!0,n.explicit||(s??=t,d??=t,u=Math.max(u,t-s),t-s>=g))return}else s!==null&&(l+=1,u=Math.max(u,t-s)),s=null;if(o||i!==null||a!==null)break;await new Promise(e=>setTimeout(e,_))}let y=this.getExtensionReadyState(e);if(y.connected&&(y.explicit||s!==null&&Date.now()-s>=g))return;let b=n.snapshot(),x=this.writeLaunchFailureLog(e,b),S=[`Chrome extension browser "${e}" failed to become ready within ${m+(f?h:0)}ms.`,`Ready state requires a stable session for ${g}ms.`,`Observed connection: ${c?`yes`:`no`}.`,c&&d!==null?`First connection observed after ${Math.max(0,d-p)}ms.`:void 0,`Longest stable connection: ${u}ms.`,l>0?`Connection flaps: ${l}.`:void 0,f?`Startup grace applied: ${h}ms.`:void 0,r.launchUrl?`Launch URL: ${r.launchUrl}.`:void 0,o?`Error: ${o.message}`:void 0,i===null?void 0:`Exit code: ${i}`,a?`Signal: ${a}`:void 0,x?`Launch log: ${x}`:void 0].filter(Boolean).join(` `);throw Error(S)}getExtensionStartupTimeoutMs(e){return e===`docker`?12e3:5e3}getExtensionStartupGraceTimeoutMs(e){return e===`docker`?8e3:5e3}getExtensionReadyStabilityMs(){return 1500}getExtensionStartupPollIntervalMs(){return 100}isRetryableExtensionLaunchError(e){return/failed to become ready/i.test(e.message)||/Failed to start Chrome process/i.test(e.message)}shouldSerializeChromeForTestingLaunch(e){return process.platform===`darwin`&&e.kind===`host`&&this.isChromeTestingAlias(e.command)}async runSerializedChromeForTestingLaunch(e){let t,n=F.chromeForTestingLaunchQueue;F.chromeForTestingLaunchQueue=new Promise(e=>{t=e}),await n;try{return await e()}finally{t?.()}}getProfileLaunchTimeoutMs(){let e=process.env.BROWSE_TOOL_PROFILE_LAUNCH_TIMEOUT_MS,t=e?Number.parseInt(e,10):NaN;return Number.isFinite(t)&&t>0?t:F.DEFAULT_PROFILE_LAUNCH_TIMEOUT_MS}async withProfileLaunchLock(e,t){let n=this.profileLaunchTails.get(e)??Promise.resolve(),r,i=new Promise(e=>{r=e}),a=n.then(()=>i);this.profileLaunchTails.set(e,a);let o=this.getProfileLaunchTimeoutMs();if(await this.didTimeout(n,o))throw r?.(),new Tt(e,o);try{return await t()}finally{r?.(),this.profileLaunchTails.get(e)===a&&this.profileLaunchTails.delete(e)}}async didTimeout(e,t){let n,r=new Promise(e=>{n=setTimeout(()=>e(!0),t)});try{return await Promise.race([e.then(()=>!1).catch(()=>!1),r])}finally{n&&clearTimeout(n)}}async delay(e){await new Promise(t=>setTimeout(t,e))}getExtensionReadyState(e){return this.extensionSessionRegistry?{connected:!!this.extensionSessionRegistry.getSessionByBrowserId(e),explicit:!0}:{connected:!!this.webSocketHub?.hasConnection(e),explicit:!1}}hasExtensionConnection(e){return this.getExtensionReadyState(e).connected}canReuseExtensionBrowser(e){return e.mode!==`extension`&&e.mode!==`vm`||!e.pid?!1:this.isProcessRunning(e.pid)&&this.hasExtensionConnection(e.id)}getTrackedExtensionPageId(e){let t=e.currentPageId&&this.pageRegistry.get(e.currentPageId)?e.currentPageId:Array.from(e.pageIds).find(e=>this.pageRegistry.get(e));if(t)return e.currentPageId=t,t;let n=this.pageRegistry.registerExtensionPage(e.id);return e.pageIds.add(n),e.currentPageId=n,n}writeLaunchFailureLog(e,t){if(t.trim())try{let n=f.default.join(h.default.tmpdir(),`${e}-launch.log`);return p.default.writeFileSync(n,t,`utf8`),n}catch{return}}async prepareDockerRetryUserDataDir(e){await this.delay(250);try{let t=await this.lockManager.cleanupOrphanedLocks(e);return this.cleanupExtensionSingletonArtifacts(e),`${t?`cleaned`:`checked`} ${e}`}catch(t){let n=t instanceof Error?t.message:String(t);return this.cleanupExtensionSingletonArtifacts(e),`lock-manager cleanup failed (${n}); singleton artifacts removed directly from ${e}`}}cleanupExtensionSingletonArtifacts(e){let t=[f.default.join(e,`SingletonLock`),f.default.join(e,`SingletonCookie`),f.default.join(e,`SingletonSocket`),f.default.join(e,`lockfile`),f.default.join(e,`Default`,`LOCK`)];for(let e of t)try{p.default.rmSync(e,{force:!0})}catch{}}cleanupStaleBrowser(e,t){if(t){let e=this.getExtensionUserDataDir(t);this.cleanupExtensionSingletonArtifacts(e),this.lockManager.cleanupOrphanedLocks(e).catch(()=>{})}this.browsers.delete(e),this.browserProcessRegistry?.releaseBrowserProcess(e).catch(()=>{}),t&&this.profileToBrowserId.delete(t)}initializeExtensionRecording(e,t,n){let r=f.default.join(n.dir,`recording-${this.toSafeRecordingId(e.id)}-${e.createdAt.getTime()}.webm`);this.initializeExtensionRecordingAtPath(e,t,r)}initializeExtensionRecordingAtPath(e,t,n){e.extensionRecording={pageId:t,outputPath:n,chunkPath:`${n}.part`,active:!1,chunkCount:0,chunkBytes:0}}async startExtensionRecording(e,t){if(!this.extensionTaskQueue)throw Error(`Extension recording task queue is unavailable for browser "${e.id}"`);I&&console.log(`[ExtensionRecordingDebug] queue start browser=${e.id} page=${t.pageId} active=${t.active}`);let n=await this.extensionTaskQueue.queueTask(`browser_start_recording`,{pageId:t.pageId},P,e.id);if(I&&console.log(`[ExtensionRecordingDebug] start result browser=${e.id} success=${n?.success===!0} isError=${n?.result?.isError===!0}`),!n.success||n.result?.isError){let r=this.getExtensionTaskError(n);if(this.isExtensionRecordingAlreadyActiveError(r)){t.active=!0;return}throw Error(r??`Failed to start extension recording for "${e.id}"`)}t.active=!0}isExtensionRecordingAlreadyActiveError(e){return e?/active stream/i.test(e)||/recording already in progress/i.test(e):!1}async attemptExtensionRecordingFinalization(e){let t=this.finalizeExtensionRecording(e).then(()=>({completed:!0})).catch(e=>({completed:!0,error:e instanceof Error?e:Error(String(e))})),n=this.delay(1500).then(()=>({completed:!1}));return Promise.race([t,n])}async waitForExtensionRecordingArtifact(e){let t=Date.now();for(;Date.now()-t<St;){try{let t=p.default.statSync(e);if(t.isFile()&&t.size>0)return!0}catch{}await this.delay(100)}return!1}async waitForExtensionRecordingArtifactOrChunks(e){let t=Date.now();for(;Date.now()-t<St;){try{let t=p.default.statSync(e.outputPath);if(t.isFile()&&t.size>0)return{artifactReady:!0,chunkReady:!1}}catch{}try{let t=p.default.statSync(e.chunkPath);if(t.isFile()&&t.size>0)return{artifactReady:!1,chunkReady:!0}}catch{}await this.delay(100)}return{artifactReady:!1,chunkReady:!1}}getExtensionRecordingSession(e){return this.browsers.get(e)?.extensionRecording??this.closingExtensionRecordings.get(e)}async finalizeClosingExtensionRecording(e){let t=this.closingExtensionRecordings.get(e);if(t)try{let n=await this.waitForExtensionRecordingArtifactOrChunks(t);!n.artifactReady&&n.chunkReady&&await this.persistExtensionRecordingArtifact(e)}finally{this.closingExtensionRecordings.delete(e)}}shouldCloseRecordedExtensionPageFirst(e){return(e.mode===`extension`||e.mode===`vm`)&&!!e.extensionRecording&&!!this.extensionTaskQueue}async closeRecordedExtensionPage(e,t){if(!this.extensionTaskQueue)throw Error(`Extension recording task queue is unavailable for browser "${e.id}"`);if(e.pageIds.size<=1){let t=await this.extensionTaskQueue.queueTask(`browser_new_page`,{url:`about:blank`,setAsCurrent:!1},P,e.id);if(!t.success||t.result?.isError)throw Error(this.getExtensionTaskError(t)??`Failed to create a keepalive tab for browser "${e.id}" before closing the recorded page`)}let n=await this.extensionTaskQueue.queueTask(`browser_close_page`,{pageId:t.pageId},P,e.id);if(!n.success||n.result?.isError)throw Error(this.getExtensionTaskError(n)??`Failed to close the recorded page "${t.pageId}" for browser "${e.id}"`)}parseExtensionRecordingPayload(e){if(!e.success||e.result?.isError)throw Error(this.getExtensionTaskError(e)??`Extension recording task failed`);let t=e.result?.content[0],n=t?.type===`text`?t.text:void 0;if(!n)return{};try{return JSON.parse(n)}catch{throw Error(`Extension recording task returned invalid JSON payload`)}}getExtensionTaskError(e){if(e.error)return e.error;let t=e.result?.content[0];if(t?.type===`text`)return t.text}toSafeRecordingId(e){return e.replace(/[^a-zA-Z0-9_-]+/g,`-`)}persistExtensionRecordingChunksToArtifact(e){try{let t=p.default.statSync(e.chunkPath);if(!t.isFile()||t.size<=0)return!1}catch{return!1}if(p.default.existsSync(e.outputPath)){let t=p.default.statSync(e.outputPath);if(t.isFile()&&t.size>0)return this.clearExtensionRecordingChunkState(e),!0}return p.default.renameSync(e.chunkPath,e.outputPath),e.chunkCount=0,e.chunkBytes=0,!0}hasPersistedExtensionRecordingArtifact(e){try{let t=p.default.statSync(e.outputPath);return t.isFile()&&t.size>0}catch{return!1}}clearExtensionRecordingChunkState(e){e.chunkCount=0,e.chunkBytes=0;try{p.default.existsSync(e.chunkPath)&&p.default.rmSync(e.chunkPath,{force:!0})}catch{}}getBrowserType(e){switch(e){case`chromium`:return x.chromium;case`firefox`:return x.firefox;case`webkit`:return x.webkit}}buildContextOptions(e,t,n){let r={};return t&&(r.baseURL=t),n&&(r.recordVideo=n),e?(e.viewport&&(r.viewport=e.viewport),e.userAgent&&(r.userAgent=e.userAgent),e.locale&&(r.locale=e.locale),e.timezone&&(r.timezoneId=e.timezone),e.geolocation&&(r.geolocation=e.geolocation),e.permissions&&(r.permissions=e.permissions),e.colorScheme&&(r.colorScheme=e.colorScheme),r):r}};Et=F=j([(0,d.injectable)(),A(0,(0,d.inject)(O.ProfileService)),A(1,(0,d.inject)(O.PageRegistry)),A(2,(0,d.inject)(O.BrowserLockManager)),A(3,(0,d.inject)(O.ChromeForTestingService)),A(4,(0,d.inject)(O.PageMonitorService)),A(4,(0,d.optional)()),A(5,(0,d.inject)(O.WebSocketHub)),A(5,(0,d.optional)()),A(6,(0,d.inject)(O.ExtensionSessionRegistry)),A(6,(0,d.optional)()),A(7,(0,d.inject)(O.ExtensionTaskQueue)),A(7,(0,d.optional)()),A(8,(0,d.inject)(O.BrowserProcessRegistry)),A(8,(0,d.optional)()),k(`design:paramtypes`,[Object,Object,Object,Object,Object,Object,Object,Object,Object])],Et);function Dt(e){return e.trim().toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``)||`snippet`}function Ot(e){return e.replace(/\r\n/g,`
|
|
6
|
-
`).split(`
|
|
7
|
-
`).map(e=>e.length>0?` ${e}`:e).join(`
|
|
8
|
-
`)}function kt(e){return[`export const name = ${JSON.stringify(e.name)};`,`export const description = ${JSON.stringify(e.description)};`,`export const run = async ({ page, context, browser }) => {`,Ot(e.code),`};`,``].join(`
|
|
9
|
-
`)}async function At(e){let t=(0,w.stripTypeScriptTypes)(await(0,m.readFile)(e,`utf8`),{mode:`strip`});return await import(`data:text/javascript;base64,${Buffer.from(t,`utf8`).toString(`base64`)}`)}var jt=class{constructor(e=process.env.BROWSE_TOOL_SNIPPETS_DIR){this.snippetsDir=e}getDirectory(){return this.snippetsDir?f.default.resolve(this.snippetsDir):void 0}async listSnippets(){let e=this.requireDirectory();await(0,m.mkdir)(e,{recursive:!0});let t=await(0,m.readdir)(e,{withFileTypes:!0}),n=[];for(let r of t){if(!r.isFile()||!r.name.endsWith(`.ts`))continue;let t=r.name,i=await At(f.default.join(e,r.name)),a=typeof i.name==`string`&&i.name.trim().length>0?i.name:r.name.replace(/\.ts$/,``),o=typeof i.description==`string`?i.description:``;n.push({name:a,description:o,snippetPath:t})}return n.sort((e,t)=>e.name.localeCompare(t.name)),n}async loadSnippet(e){let t=await this.resolveSnippetPath(e),n=await At(t.filePath);if(typeof n.run!=`function`)throw Error(`Snippet "${t.snippetPath}" must export a "run" function`);return{name:typeof n.name==`string`&&n.name.trim().length>0?n.name:t.snippetPath.replace(/\.ts$/,``),description:typeof n.description==`string`?n.description:``,snippetPath:t.snippetPath,run:n.run}}async saveSnippet(e){let t=this.requireDirectory();await(0,m.mkdir)(t,{recursive:!0});let n=`${Dt(e.name)}.ts`;return await(0,m.writeFile)(f.default.join(t,n),kt(e),`utf8`),{name:e.name,description:e.description,snippetPath:n}}requireDirectory(){let e=this.getDirectory();if(!e)throw Error(`Snippet storage is not configured. Start browse-tool with --snippets-dir.`);return e}async resolveSnippetPath(e){let t=this.requireDirectory(),n=e.endsWith(`.ts`)?e:`${e}.ts`,r=f.default.resolve(t,n);if(!r.startsWith(`${t}${f.default.sep}`)&&r!==f.default.join(t,n))throw Error(`Snippet path must stay within the configured snippets directory`);if(!(await(0,m.stat)(r).catch(()=>null))?.isFile())throw Error(`Snippet "${e}" not found`);return{filePath:r,snippetPath:f.default.relative(t,r)}}};function Mt(e){let t=e.trim(),n=t[0];return(n===`"`||n===`'`)&&t.endsWith(n)?{text:t.slice(1,-1),exact:!0}:{text:t,exact:!1}}function Nt(e){let t=e.trim();if(t.startsWith(`text=`)){let e=Mt(t.slice(5));return{kind:`text`,text:e.text,exact:e.exact}}let n=t.match(/^([a-zA-Z][\w-]*)?:has-text\((.*)\)$/);if(n){let e=Mt(n[2]??``);return{kind:`hasText`,tagName:n[1],text:e.text,exact:!1}}return null}const L=T.z.object({uid:T.z.string().optional().describe(`Accessibility snapshot UID reference`),role:T.z.string().optional().describe(`ARIA role (e.g., "button", "textbox")`),name:T.z.string().optional().describe(`Accessible name used with the role selector`),label:T.z.string().optional().describe(`Associated label text`),placeholder:T.z.string().optional().describe(`Placeholder text`),testId:T.z.string().optional().describe(`data-testid value`),text:T.z.string().optional().describe(`Visible text content`),exact:T.z.boolean().optional().default(!1).describe(`Use exact matching for semantic selectors`),selector:T.z.string().optional().describe(`CSS selector`),xpath:T.z.string().optional().describe(`XPath expression`),frame:T.z.string().optional().describe(`Frame selector`)});let Pt=class{async locate(e,t){let n=await this.getFrameContext(e,t.frame),r=this.buildLocator(n,t);if(await r.count()===0)throw Error(`Element not found: ${this.describeSelector(t)}`);return r.first()}async locateAll(e,t){let n=await this.getFrameContext(e,t.frame);return this.buildLocator(n,t)}async waitForElement(e,t,n={}){let r=await this.getFrameContext(e,t.frame),i=this.buildLocator(r,t);return await i.waitFor({timeout:n.timeout??18e4,state:n.state??`visible`}),i.first()}async getFrameContext(e,t){return t?e.frameLocator(t):e}buildLocator(e,t){let n=t.exact??!1;if(t.uid)return e.locator(`[data-pw-proxy="${t.uid}"], [data-browse-tool-uid="${t.uid}"], [data-uid="${t.uid}"]`);if(t.role){let r=t.role;return t.name?e.getByRole(r,{name:t.name,exact:n}):e.getByRole(r)}if(t.label)return e.getByLabel(t.label,{exact:n});if(t.placeholder)return e.getByPlaceholder(t.placeholder,{exact:n});if(t.testId)return e.getByTestId(t.testId);if(t.text)return e.getByText(t.text,{exact:n});if(t.selector){let n=Nt(t.selector);return n?.kind===`text`?e.getByText(n.text,{exact:n.exact}):n?.kind===`hasText`?e.locator(n.tagName??`*`).filter({hasText:n.text}):e.locator(t.selector)}if(t.xpath)return e.locator(`xpath=${t.xpath}`);throw Error(`No selector provided. Specify one of: uid, role, label, placeholder, text, selector, or xpath.`)}describeSelector(e){let t=[];return e.uid&&t.push(`uid="${e.uid}"`),e.role&&t.push(`role="${e.role}"`),e.name&&t.push(`name="${e.name}"`),e.label&&t.push(`label="${e.label}"`),e.placeholder&&t.push(`placeholder="${e.placeholder}"`),e.testId&&t.push(`testId="${e.testId}"`),e.text&&t.push(`text="${e.text}"`),e.exact&&t.push(`exact=true`),e.selector&&t.push(`selector="${e.selector}"`),e.xpath&&t.push(`xpath="${e.xpath}"`),e.frame&&t.push(`frame="${e.frame}"`),t.length>0?t.join(`, `):`empty selector`}};Pt=j([(0,d.injectable)()],Pt);const Ft=process.env.BROWSE_TOOL_DEBUG_CUSTOM_TOOLS===`1`,It=`data-browse-tool-handle-id`;function Lt(e){return JSON.stringify(e,(e,t)=>typeof t==`string`&&t.length>240?`${t.slice(0,240)}...<trimmed>`:t)}function R(e,t){if(Ft){if(t){console.error(`[ExtensionPageProxy] ${e}`,t);return}console.error(`[ExtensionPageProxy] ${e}`)}}function Rt(e){return typeof e==`object`&&e&&`value`in e?e.value:e}let zt=class{[c.v]=!0;currentUrl=`about:blank`;defaultTimeoutMs=P;_pageId;_browserId;constructor(e){this.taskQueue=e}setTarget(e,t){this._pageId=e,this._browserId=t,this.currentUrl=`about:blank`,this.currentUrlAsync().catch(()=>{})}get pageId(){return this._pageId}get browserId(){return this._browserId}async goto(e,t){let n=await this.executeTask(`browser_navigate`,{url:e,waitUntil:t?.waitUntil,timeout:t?.timeout});if(!n.success)throw Error(n.error??`Navigation failed`);this.currentUrl=e}async click(e,t){let n=await this.executeTask(`browser_click`,{selector:e,clickCount:t?.clickCount,delay:t?.delay,button:t?.button,modifiers:t?.modifiers});if(!n.success)throw Error(n.error??`Click failed on ${e}`)}async fill(e,t,n){let r=await this.executeTask(`browser_fill`,{selector:e,text:t,force:n?.force});if(!r.success)throw Error(r.error??`Fill failed on ${e}`)}async type(e,t,n){let r=await this.executeTask(`browser_type`,{selector:e,text:t,delay:n?.delay});if(!r.success)throw Error(r.error??`Type failed on ${e}`)}async press(e){let t=await this.executeTask(`browser_press_key`,{key:e});if(!t.success)throw Error(t.error??`Press key failed: ${e}`)}async hover(e){let t=await this.executeTask(`browser_hover`,{selector:e});if(!t.success)throw Error(t.error??`Hover failed on ${e}`)}async selectOption(e,t){let n=await this.executeTask(`browser_select`,{selector:e,values:Array.isArray(t)?t:[t]});if(!n.success)throw Error(n.error??`Select option failed on ${e}`)}async waitForSelector(e,t){let n=await this.executeTask(`browser_wait_for`,{selector:e,state:t?.state??`visible`,timeout:t?.timeout});if(!n.success)throw Error(n.error??`Wait for selector failed: ${e}`)}async waitForTimeout(e){let t=await this.executeTask(`browser_wait_for`,{timeout:e});if(!t.success)throw Error(t.error??`Wait for timeout failed`)}async screenshot(e){let t=await this.executeTask(`browser_screenshot`,{path:e?.path,fullPage:e?.fullPage,type:e?.type,format:e?.type});if(!t.success)throw Error(t.error??`Screenshot failed`);let n=t.result?.content?.[0];return e?.path&&n?.type===`image`&&typeof n.data==`string`?((0,p.mkdirSync)((0,f.dirname)(e.path),{recursive:!0}),(0,p.writeFileSync)(e.path,Buffer.from(n.data,`base64`)),e.path):t.result?.path??``}async content(){let e=await this.executeTask(`browser_evaluate_script`,{script:`document.documentElement.outerHTML`});if(!e.success)throw Error(e.error??`Get content failed`);return Rt(e.result)??``}async title(){let e=await this.executeTask(`browser_evaluate_script`,{script:`document.title`});if(!e.success)throw Error(e.error??`Get title failed`);return Rt(e.result)??``}async textContent(e){return this.locator(e).textContent()}async innerText(e){return this.locator(e).innerText()}async inputValue(e){return this.locator(e).inputValue()}url(){return this.currentUrl}async evaluate(e,...t){let n=typeof e==`function`?`(${e.toString()})(${t.map(e=>JSON.stringify(e)).join(`,`)})`:e,r=await this.executeTask(`browser_evaluate_script`,{script:n});if(!r.success)throw Error(r.error??`Evaluate failed`);return Rt(r.result)}async evaluateHandle(e,...t){let n=`handle-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,r=typeof e==`function`?e.toString():e,i=JSON.stringify(t),a=JSON.stringify(It),o=JSON.stringify(n),s=await this.evaluate(`async () => {
|
|
10
|
-
const pageFunctionOrValue = (${r});
|
|
11
|
-
const args = ${i};
|
|
12
|
-
const value = typeof pageFunctionOrValue === 'function'
|
|
13
|
-
? await pageFunctionOrValue(...args)
|
|
14
|
-
: pageFunctionOrValue;
|
|
15
|
-
if (!(value instanceof Element)) {
|
|
16
|
-
return { element: false };
|
|
17
|
-
}
|
|
18
|
-
value.setAttribute(${a}, ${o});
|
|
19
|
-
return { element: true };
|
|
20
|
-
}`);return new Bt(this,It,n,!!s?.element)}async getSnapshot(){let e=await this.executeTask(`browser_snapshot`,{});if(!e.success)throw Error(e.error??`Get snapshot failed`);let t=Rt(e.result);return typeof t==`string`?t:t==null?``:JSON.stringify(t,null,2)}getByRole(e,t){return new c.y(this,[{type:`role`,role:e,options:t}])}getByText(e,t){return new c.y(this,[{type:`text`,text:e,options:t}])}getByLabel(e,t){return new c.y(this,[{type:`label`,text:e,options:t}])}getByPlaceholder(e,t){return new c.y(this,[{type:`placeholder`,text:e,options:t}])}getByTestId(e){return new c.y(this,[{type:`testId`,testId:e}])}locator(e){return new c.y(this,[{type:`css`,selector:e}])}context(){return{clearCookies:async()=>{await this.clearStorageState()}}}async reload(e){let t=await this.executeTask(`browser_reload`,{waitUntil:e?.waitUntil,timeout:e?.timeout});if(!t.success)throw Error(t.error??`Reload failed`)}async currentUrlAsync(){let e=await this.evaluate(`window.location.href`);return e&&(this.currentUrl=e),this.currentUrl}async waitForResponse(e,t){let n=typeof e==`string`?{url:e}:e instanceof RegExp?{urlPattern:{source:e.source,flags:e.flags}}:null;if(!n)throw Error(`Extension mode only supports waitForResponse with a string or RegExp target`);let r=await this.executeTask(`browser_wait_for_response`,{...n,timeout:t?.timeout??this.defaultTimeoutMs});if(!r.success)throw Error(r.error??`Wait for response failed`)}async getStorageState(){let e=await this.executeTask(`browser_get_storage_state`,{});if(!e.success)throw Error(e.error??`Get storage state failed`);return e.result??{}}async clearStorageState(){let e=await this.executeTask(`browser_clear_storage_state`,{});if(!e.success)throw Error(e.error??`Clear storage state failed`)}async startRecording(){let e=await this.executeTask(`browser_start_recording`,{});if(!e.success)throw Error(e.error??`Start recording failed`)}async stopRecording(){let e=await this.executeTask(`browser_stop_recording`,{});if(!e.success)throw Error(e.error??`Stop recording failed`);return e.result?.videoBase64??``}async setInputFilesBySelector(e,t){let n=await this.executeTask(`browser_upload_file`,{selector:e,files:t});if(!n.success)throw Error(n.error??`Set input files failed on ${e}`)}async executeTask(e,t){if(!this._pageId||!this._browserId)return{success:!1,error:`Extension page proxy target is not initialized`};let n={...t,pageId:this._pageId};R(`Queueing extension task`,{tool:e,browserId:this._browserId,pageId:this._pageId,args:Lt(n)});let r;try{r=await this.taskQueue.queueTask(e,n,this.defaultTimeoutMs,this._browserId)}catch(t){let n=t instanceof Error?t.message:String(t);return R(`Extension task threw before result`,{tool:e,browserId:this._browserId,pageId:this._pageId,error:n}),{success:!1,error:`Extension task "${e}" threw for page "${this._pageId}" in browser "${this._browserId}": ${n}`}}if(!r.success)return R(`Extension task returned unsuccessful result`,{tool:e,browserId:this._browserId,pageId:this._pageId,taskError:r.error??`Task failed`,taskResult:r}),{success:!1,error:r.error??`Extension task "${e}" failed for page "${this._pageId}" in browser "${this._browserId}"`};let i=r.result?.content?.[0];if(i?.type===`text`&&i.text)try{let t=JSON.parse(i.text);return R(`Extension task completed with JSON text result`,{tool:e,browserId:this._browserId,pageId:this._pageId}),{success:!0,result:t}}catch{return R(`Extension task completed with plain text result`,{tool:e,browserId:this._browserId,pageId:this._pageId,textPreview:i.text.slice(0,240)}),{success:!0,result:{value:i.text}}}return R(`Extension task completed with non-text result payload`,{tool:e,browserId:this._browserId,pageId:this._pageId,taskResult:r}),{success:!0,result:r.result}}};zt=j([(0,d.injectable)(),A(0,(0,d.inject)(O.ExtensionTaskQueue)),k(`design:paramtypes`,[Object])],zt);var Bt=class{constructor(e,t,n,r){this.page=e,this.markerAttr=t,this.markerValue=n,this.isElement=r}asElement(){return this.isElement?this:null}async evaluate(e,...t){this.assertElement();let n=typeof e==`function`?e.toString():e,r=JSON.stringify(t);return this.page.evaluate(`async () => {
|
|
21
|
-
${this.buildElementLookupScript()}
|
|
22
|
-
const element = findMarkedElement();
|
|
23
|
-
if (!element) {
|
|
24
|
-
throw new Error('Extension element handle is no longer attached');
|
|
25
|
-
}
|
|
26
|
-
const pageFunction = (${n});
|
|
27
|
-
const args = ${r};
|
|
28
|
-
return await pageFunction(element, ...args);
|
|
29
|
-
}`)}async setInputFiles(e){this.assertElement(),await this.page.setInputFilesBySelector(`[${this.markerAttr}="${this.markerValue}"]`,e)}async dispose(){this.isElement&&await this.page.evaluate(`() => {
|
|
30
|
-
${this.buildElementLookupScript()}
|
|
31
|
-
findMarkedElement()?.removeAttribute(${JSON.stringify(this.markerAttr)});
|
|
32
|
-
}`)}assertElement(){if(!this.isElement)throw Error(`Extension handle does not reference a DOM element`)}buildElementLookupScript(){return`
|
|
33
|
-
const collectRoots = (root, roots = []) => {
|
|
34
|
-
roots.push(root);
|
|
35
|
-
root.querySelectorAll('*').forEach((element) => {
|
|
36
|
-
if (element.shadowRoot) collectRoots(element.shadowRoot, roots);
|
|
37
|
-
});
|
|
38
|
-
return roots;
|
|
39
|
-
};
|
|
40
|
-
const findMarkedElement = () => {
|
|
41
|
-
const selector = ${JSON.stringify(`[${this.markerAttr}="${this.markerValue}"]`)};
|
|
42
|
-
for (const root of collectRoots(document)) {
|
|
43
|
-
const element = root.querySelector(selector);
|
|
44
|
-
if (element) return element;
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
};
|
|
48
|
-
`}};let Vt=class{sessions=new Map;browserIdToSessionId=new Map;sessionIdCounter=0;defaultStaleThresholdMs=6e4;register(e){let t=this.browserIdToSessionId.get(e.browserId);t&&(this.sessions.delete(t),this.browserIdToSessionId.delete(e.browserId));let n=`session-${++this.sessionIdCounter}-${Date.now()}`,r=new Date,i={id:n,browserId:e.browserId,tabId:e.tabId,currentUrl:e.url,createdAt:r,lastHeartbeatAt:r,controlMode:`manual`,handoffRequested:!1,metadata:e.metadata};return this.sessions.set(n,i),this.browserIdToSessionId.set(e.browserId,n),i}heartbeat(e){let t=this.sessions.get(e.sessionId);if(t)return t.lastHeartbeatAt=new Date,e.tabId!==void 0&&(t.tabId=e.tabId),e.url!==void 0&&(t.currentUrl=e.url),e.state&&(t.metadata={...t.metadata,...e.state}),t}requestHandoff(e){let t=this.sessions.get(e.sessionId);if(t)return t.handoffRequested=!0,e.pageState&&(t.currentUrl=e.pageState.url,t.metadata={...t.metadata,handoffReason:e.reason,handoffPageState:e.pageState}),t}acknowledgeHandoff(e){let t=this.sessions.get(e);if(!(!t||!t.handoffRequested))return t.handoffRequested=!1,t.controlMode=`ai`,t.activeSpecPath=void 0,t}getSession(e){return this.sessions.get(e)}getSessionByBrowserId(e){let t=this.browserIdToSessionId.get(e);if(t)return this.sessions.get(t)}listSessions(){return Array.from(this.sessions.values())}removeSession(e){let t=this.sessions.get(e);return t?(this.browserIdToSessionId.delete(t.browserId),this.sessions.delete(e),!0):!1}setControlMode(e,t){let n=this.sessions.get(e);if(n)return n.controlMode=t,n}setActiveSpec(e,t){let n=this.sessions.get(e);if(n)return n.activeSpecPath=t,t&&(n.controlMode=`spec`),n}cleanupStaleSessions(e=this.defaultStaleThresholdMs){let t=Date.now(),n=0;for(let[r,i]of this.sessions)t-i.lastHeartbeatAt.getTime()>e&&(this.browserIdToSessionId.delete(i.browserId),this.sessions.delete(r),n++);return n}};Vt=j([(0,d.injectable)()],Vt);let Ht=class{constructor(e,t,n){this.bundler=e,this.pageProxy=t,this.sessionRegistry=n}async loadSpec(e){let t=await this.bundler.bundle(e),n=process,r=n.__pw_initiator__;try{return c.c(e),n.__pw_initiator__=void 0,await import(`${t.outputPath}?t=${Date.now()}`),c.s()}catch(t){throw c.p(),Error(`Failed to load spec "${e}": ${t instanceof Error?t.message:String(t)}`,{cause:t})}finally{n.__pw_initiator__=r,await t.cleanup()}}async executeSpec(e){let{specPath:t,sessionId:n,testFilter:r,specArgs:i,onHandoff:a}=e,o=Date.now(),s=[],l=0,u=0,d=!1,f=new Set,p=new Set;this.sessionRegistry.setActiveSpec(n,t);try{let e=await this.loadSpec(t),m=r?this.filterTests(e.allTests,r):e.allTests,h={page:this.pageProxy,requestHandoff:async e=>{if(!this.sessionRegistry.getSession(n))throw Error(`Session ${n} not found`);let t=await this.pageProxy.getSnapshot(),r=this.pageProxy.url();try{r=await this.pageProxy.currentUrlAsync()}catch{}this.sessionRegistry.requestHandoff({sessionId:n,reason:e,pageState:{url:r,snapshot:t}}),d=!0,a&&await a(n)},specArgs:i};for(let e of m){let{runtimeUse:t,unsupportedKeys:r,fixtureOverrides:i}=c.h(e.config.use);r.length>0&&f.add(`test.use() ignores unsupported fixture keys in extension mode: ${r.join(`, `)}`),Object.keys(t).length>0&&f.add(`test.use() runtime browser config is not applied in extension mode: ${Object.keys(t).join(`, `)}`);let a=e.serialScopeId,o=a!==void 0&&p.has(a);if(e.skip||o){s.push({title:e.title,fullTitle:e.fullTitle,passed:!0,error:null,duration:0}),l++;continue}let d=this.sessionRegistry.getSession(n);if(d?.handoffRequested)break;let m=Date.now();try{await e.fn({...h,...i}),s.push({title:e.title,fullTitle:e.fullTitle,passed:!0,error:null,duration:Date.now()-m,handoffTriggered:d?.handoffRequested}),l++}catch(t){let n=t instanceof Error?t.message:String(t);s.push({title:e.title,fullTitle:e.fullTitle,passed:!1,error:n,duration:Date.now()-m}),u++,a!==void 0&&p.add(a)}}return{specPath:t,totalTests:m.length,passed:l,failed:u,testResults:s,success:u===0,duration:Date.now()-o,handoffOccurred:d,sessionId:n,warnings:f.size>0?[...f]:void 0}}catch(e){return{specPath:t,totalTests:0,passed:0,failed:1,testResults:[{title:`Spec Loading`,fullTitle:`Failed to load: ${t}`,passed:!1,error:e instanceof Error?e.message:String(e),duration:Date.now()-o}],success:!1,duration:Date.now()-o,handoffOccurred:!1,sessionId:n,warnings:f.size>0?[...f]:void 0}}finally{this.sessionRegistry.setActiveSpec(n,void 0)}}filterTests(e,t){let n=[...e];if(t.onlyMarked){let e=n.filter(e=>e.only);e.length>0&&(n=e)}if(t.testName&&(n=n.filter(e=>e.title===t.testName)),t.testPattern){let e=new RegExp(t.testPattern,`i`);n=n.filter(t=>e.test(t.fullTitle))}if(t.describeFilter){let e=new RegExp(t.describeFilter,`i`);n=n.filter(t=>e.test(t.fullTitle))}return n}};Ht=j([(0,d.injectable)(),A(0,(0,d.inject)(O.SpecBundlerService)),A(1,(0,d.inject)(O.ExtensionPageProxy)),A(2,(0,d.inject)(O.ExtensionSessionRegistry)),k(`design:paramtypes`,[Object,Object,Object])],Ht);const Ut=`@agimon-ai/browse-tool`,Wt=new Set([`127.0.0.1`,`localhost`,`::1`,`0.0.0.0`]);function Gt(e){return e===`1`||e===`true`}function Kt(e){return Array.isArray(e)?JSON.stringify(e):typeof e==`string`||typeof e==`number`||typeof e==`boolean`?e:JSON.stringify(e)}function qt(e){if(!e)return;let t={};for(let[n,r]of Object.entries(e))r!=null&&(t[n]=Kt(r));return Object.keys(t).length>0?t:void 0}function Jt(e){if(!e)return;let t={};for(let n of e.split(`,`)){let[e,...r]=n.split(`=`),i=e?.trim(),a=r.join(`=`).trim();!i||!a||(t[i]=a)}return Object.keys(t).length>0?t:void 0}function Yt(e){if(!e)return;let t=Number(e);return Number.isFinite(t)&&t>0?t:void 0}function Xt(e,t){let n=e.endsWith(`/`)?e:`${e}/`;return n.endsWith(`/v1/${t}/`)||n.endsWith(`/v1/${t}`)?e:new URL(`v1/${t}`,n).toString()}function Zt(e,t){return(t===`traces`?e.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT:e.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT)||(e.OTEL_EXPORTER_OTLP_ENDPOINT?Xt(e.OTEL_EXPORTER_OTLP_ENDPOINT,t):void 0)}function Qt(e){return Wt.has(e)}function $t(e,t){try{let n=new URL(e),r=new URL(t);return!Qt(n.hostname)||Qt(r.hostname)||(n.hostname=r.hostname),n.toString()}catch{return e}}async function en(e,t,n){let r=await(0,ee.resolveNodeTelemetryEndpoints)({env:e,packageName:Ut,workspaceRoot:M(),healthCheck:n?.healthCheck});return t===`traces`?r.tracesEndpoint:r.logsEndpoint}async function tn(e,t,n,r){let i=t===`traces`?e.BROWSE_TOOL_OTEL_BROWSER_TRACES_ENDPOINT:e.BROWSE_TOOL_OTEL_BROWSER_LOGS_ENDPOINT;if(i)return $t(i,n);let a=e.BROWSE_TOOL_OTEL_BROWSER_ENDPOINT;if(a)return $t(Xt(a,t),n);let o=await en(e,t,r);return o?$t(o,n):void 0}async function nn(e,t,n){let r=e.BROWSE_TOOL_OTEL_BROWSER_SERVICE_NAME??`browse-tool-extension`,[i,a]=await Promise.all([tn(e,`traces`,t,n),tn(e,`logs`,t,n)]);return{enabled:!!(i||a),tracesEndpoint:i,logsEndpoint:a,serviceName:r}}function rn(e,t){let n=t===`traces`?e.OTEL_EXPORTER_OTLP_TRACES_HEADERS:e.OTEL_EXPORTER_OTLP_LOGS_HEADERS,r=t===`traces`?e.OTEL_EXPORTER_OTLP_TRACES_TIMEOUT:e.OTEL_EXPORTER_OTLP_LOGS_TIMEOUT;return{url:Zt(e,t),headers:Jt(n??e.OTEL_EXPORTER_OTLP_HEADERS),timeoutMillis:Yt(r??e.OTEL_EXPORTER_OTLP_TIMEOUT)}}function an(e){return e instanceof Error?e.message:String(e)}function on(e){if(e instanceof Error)return qt({"exception.type":e.name,"exception.message":e.message,"exception.stacktrace":e.stack})}let z=class{contextStore=new v.AsyncLocalStorage;tracesEnabled;logsEnabled;tracerProvider;tracer;logSinkReady;delegateReady;delegateHandle=null;constructor(e={}){let t=e.env??process.env,n=Gt(t.OTEL_SDK_DISABLED),r=Gt(t.BROWSE_TOOL_OTEL_ENABLED),i=rn(t,`traces`),a=!!e.traceExporter;this.tracesEnabled=!n&&(a||r||!!i.url),this.logsEnabled=!n;let o=e.serviceName??t.BROWSE_TOOL_OTEL_SERVICE_NAME??`browse-tool-http`,s=e.packageName??Ut,c=e.serviceVersion??t.npm_package_version,l={"service.name":o};c&&(l[`service.version`]=c);let u=(0,D.resourceFromAttributes)(l);if(this.tracesEnabled&&a){let t=e.traceExporter??new te.OTLPTraceExporter(i);this.tracerProvider=new ne.NodeTracerProvider({resource:u,spanProcessors:[e.traceExporter?new ne.SimpleSpanProcessor(t):new ne.BatchSpanProcessor(t)]})}this.tracer=this.tracerProvider?.getTracer(o,c)??E.trace.getTracer(o,c),this.delegateReady=!n&&!a?(0,ee.createNodeTelemetry)({env:t,packageName:s,serviceName:o,serviceVersion:c,workspaceRoot:M(),enableTraces:!0,enableLogs:!0}).then(e=>(this.delegateHandle=e,e)).catch(()=>null):Promise.resolve(null),this.logSinkReady=this.logsEnabled&&a?(0,ee.createNodeTelemetry)({env:t,packageName:s,serviceName:o,serviceVersion:c,workspaceRoot:M(),enableTraces:!1,enableLogs:!0}).catch(()=>null):this.delegateReady}isEnabled(){return this.tracesEnabled||this.logsEnabled}getActiveTraceContext(){let e=this.delegateHandle?.getTraceContext();if(e?.traceId||e?.spanId)return e;let t=this.contextStore.getStore();if(!t)return{};let n=E.trace.getSpan(t);if(!n)return{};let r=n.spanContext();return{traceId:r.traceId,spanId:r.spanId}}async runInSpan(e,t,n){let r=await this.delegateReady;if(r)return await r.runInSpan(e,t,n);if(!this.tracesEnabled||!this.tracerProvider)return await n(void 0);let i=this.contextStore.getStore()??E.context.active(),a=this.tracer.startSpan(e,{kind:t.kind??E.SpanKind.INTERNAL,attributes:qt(t.attributes)},i),o=E.trace.setSpan(i,a);return await this.contextStore.run(o,async()=>{try{return await n(a)}catch(e){throw a.recordException(e instanceof Error?e:Error(an(e))),a.setStatus({code:E.SpanStatusCode.ERROR,message:an(e)}),e}finally{a.end()}})}log(e,t,n={}){this.logsEnabled&&this.logSinkReady.then(r=>{if(!r)return;let i=n.context??this.contextStore.getStore()??E.context.active(),a=on(n.exception);r.logger[e](t,{attributes:qt({...n.attributes,...a}),context:i,exception:n.exception})})}async forceFlush(){let[e,t]=await Promise.all([this.logSinkReady,this.delegateReady]);await Promise.allSettled([this.tracerProvider?.forceFlush(),e?.flush(),t?.flush()])}async shutdown(){let[e,t]=await Promise.all([this.logSinkReady,this.delegateReady]);await Promise.allSettled([this.tracerProvider?.shutdown(),e?.shutdown(),t?.shutdown()])}};z=j([(0,d.injectable)(),k(`design:paramtypes`,[Object])],z);let B=class{pendingTasks=new Map;taskQueue=[];lastPollAt;lastResultAt;maxQueueSize=100;defaultTimeoutMs=P;constructor(e,t=new z,n){this.webSocketHub=e,this.telemetry=t,this.pageRegistry=n}async queueTask(e,t,n=this.defaultTimeoutMs,r){if(this.pendingTasks.size>=this.maxQueueSize)throw Error(`Task queue full (max ${this.maxQueueSize} tasks)`);let i=typeof t.pageId==`string`?t.pageId:void 0,a=r??t.browserId??this.resolveBrowserIdFromPage(i),o=a?void 0:this.resolveSingleConnectionBrowserId(),s={id:crypto.randomUUID(),tool:e,arguments:t,createdAt:new Date,timeoutMs:n,browserId:a,pageId:i,telemetry:this.getTaskTelemetryContext()};return new Promise((e,t)=>{let r=setTimeout(()=>{this.pendingTasks.delete(s.id);let e=this.taskQueue.findIndex(e=>e.id===s.id);e!==-1&&this.taskQueue.splice(e,1),t(Error(`Task ${s.id} timed out after ${n}ms`))},n);if(this.pendingTasks.set(s.id,{task:s,resolve:e,reject:t,timeoutId:r}),this.webSocketHub){let e=a??o;if(e&&this.pushTaskViaWebSocket(s,e))return}this.taskQueue.push(s)})}pushTaskViaWebSocket(e,t){if(!this.webSocketHub)return!1;let n={type:`task:push`,id:crypto.randomUUID(),payload:{taskId:e.id,tool:e.tool,arguments:e.arguments,telemetry:e.telemetry}};return this.webSocketHub.pushTask(t,n)}getNextTask(){return this.lastPollAt=new Date,this.taskQueue.shift()}submitResult(e){this.lastResultAt=new Date;let t=this.pendingTasks.get(e.taskId);if(t)return clearTimeout(t.timeoutId),this.pendingTasks.delete(e.taskId),t.resolve(e),t.task}getConnectionStatus(){let e=new Date,t=this.lastPollAt?e.getTime()-this.lastPollAt.getTime()<5e3:!1,n=this.webSocketHub?this.webSocketHub.getStats().totalConnections>0:!1;return{connected:t||n,lastPollAt:this.lastPollAt,lastResultAt:this.lastResultAt,pendingTasks:this.pendingTasks.size}}clearAllTasks(e){for(let[t,n]of this.pendingTasks)clearTimeout(n.timeoutId),n.reject(Error(e)),this.pendingTasks.delete(t);this.taskQueue.length=0}get queueSize(){return this.taskQueue.length}get pendingCount(){return this.pendingTasks.size}getTaskTelemetryContext(){let e=this.telemetry.getActiveTraceContext();if(!(!e.traceId&&!e.spanId))return{traceId:e.traceId,parentSpanId:e.spanId}}resolveBrowserIdFromPage(e){if(e)return this.pageRegistry?.get(e)?.browserId}resolveSingleConnectionBrowserId(){if(!this.webSocketHub)return;let e=this.webSocketHub.getStats();if(e.totalConnections!==0){if(new Set(e.connections.map(e=>e.browserId)).size>1)throw Error(`Ambiguous extension task target: provide browserId or pageId when multiple browsers are connected`);return e.connections[0]?.browserId}}};B=j([(0,d.injectable)(),A(0,(0,d.inject)(O.WebSocketHub)),A(0,(0,d.optional)()),A(1,(0,d.inject)(O.TelemetryService)),A(1,(0,d.optional)()),A(2,(0,d.inject)(O.PageRegistry)),A(2,(0,d.optional)()),k(`design:paramtypes`,[Object,Object,Object])],B);const sn=new Set(`browser_navigate.browser_go_back.browser_go_forward.browser_reload.browser_click.browser_fill.browser_type.browser_upload_file.browser_select.browser_hover.browser_drag.browser_press_key.browser_inspect_element.browser_resolve_locator.browser_pdf.browser_screenshot.browser_snapshot.browser_list_pages.browser_new_page.browser_select_page.browser_close_page.browser_wait_for.browser_evaluate_script.browser_resize_page.browser_handle_dialog.browser_emulate.browser_list_network_requests.browser_get_network_request.browser_list_console_messages.browser_expect`.split(`.`)),cn=new Set([`browser_launch`,`browser_list_profiles`,`browser_create_profile`,`browser_delete_profile`,`browser_save_profile_state`,`browser_start_trace`,`browser_stop_trace`,`run_spec`,`discover_specs`]);let V=class{constructor(e){this.taskQueue=e}isToolSupported(e){return sn.has(e)}isToolNotApplicable(e){return cn.has(e)}async executeTool(e,t){if(this.isToolNotApplicable(e))return{content:[{type:`text`,text:`Tool "${e}" is not available in Chrome extension mode. This tool requires Playwright and cannot be run through the extension.`}],isError:!0};if(!this.isToolSupported(e))return{content:[{type:`text`,text:`Tool "${e}" is not yet implemented in Chrome extension mode.`}],isError:!0};if(!this.taskQueue.getConnectionStatus().connected)return{content:[{type:`text`,text:`Chrome extension is not connected. Please ensure the extension is running and connected to the server.`}],isError:!0};try{let n=await this.taskQueue.queueTask(e,t);return n.success?n.result?this.persistScreenshotPath(e,t,n.result):{content:[{type:`text`,text:`Tool executed successfully`}]}:{content:[{type:`text`,text:n.error||n.result?.content?.[0]?.text||`Unknown error from Chrome extension`}],isError:!0}}catch(e){return{content:[{type:`text`,text:e instanceof Error?e.message:String(e)}],isError:!0}}}persistScreenshotPath(e,t,n){let r=typeof t.path==`string`?t.path.trim():``,i=n.content[0];return e!==`browser_screenshot`||!r||i?.type!==`image`||typeof i.data!=`string`?n:((0,p.mkdirSync)((0,f.dirname)(r),{recursive:!0}),(0,p.writeFileSync)(r,Buffer.from(i.data,`base64`)),{content:[{type:`text`,text:`Screenshot saved to: ${r}`}]})}getSupportedTools(){return Array.from(sn)}getNotApplicableTools(){return Array.from(cn)}};V=j([(0,d.injectable)(),A(0,(0,d.inject)(O.ExtensionTaskQueue)),k(`design:paramtypes`,[Object])],V);const ln=l.DEFAULT_PORT_RANGE.min,un=`PLAYWRIGHT_PORT`;function H(){return process.env.PLAYWRIGHT_HOST?.trim()||`localhost`}function dn(){let e=process.env[un];if(!e)return ln;let t=Number.parseInt(e,10);if(Number.isNaN(t)||t<=0||t>65535)throw Error(`Invalid ${un} value: ${e}`);return t}function U(e=void 0,t){let n=typeof e==`string`?e:e?.host??H(),r=e&&typeof e!=`string`?e.port:t??dn();return`http://${n===`0.0.0.0`?`127.0.0.1`:n===`::`?`::1`:n}:${r}`}let fn=class{baseUrl;defaultTimeout=P;constructor(){this.baseUrl=U()}setBaseUrl(e){this.baseUrl=e}getBaseUrl(){return this.baseUrl}async execute(e,t={}){let n=`${this.baseUrl}/execute`,r=new AbortController,i=setTimeout(()=>r.abort(),this.defaultTimeout);try{let a=await fetch(n,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({tool:e,arguments:t}),signal:r.signal});if(clearTimeout(i),!a.ok){let e=await a.text();return{content:[{type:`text`,text:`HTTP error ${a.status}: ${e}`}],isError:!0}}let o=await a.json();return o.success?o.result||{content:[{type:`text`,text:`No result returned`}]}:{content:[{type:`text`,text:o.error||o.result?.content?.[0]?.text||`Unknown error`}],isError:!0}}catch(e){return clearTimeout(i),e instanceof Error&&e.name===`AbortError`?{content:[{type:`text`,text:`Request timed out after ${this.defaultTimeout}ms`}],isError:!0}:{content:[{type:`text`,text:e instanceof Error?e.message:String(e)}],isError:!0}}}async isHealthy(){try{let e=await fetch(`${this.baseUrl}/health`,{headers:{"Content-Type":`application/json`}});return e.ok?(await e.json()).status===`healthy`:!1}catch{return!1}}};fn=j([(0,d.injectable)(),k(`design:paramtypes`,[])],fn);let W=class{defaultTimeout=5e3;async check(e,t=this.defaultTimeout){try{let n=`${U(H(),e)}/health`,r=new AbortController,i=setTimeout(()=>r.abort(),t);try{let t=await fetch(n,{signal:r.signal,headers:{"Content-Type":`application/json`}});if(clearTimeout(i),!t.ok)return{healthy:!1,error:`HTTP error ${t.status}: ${t.statusText}`};let a=await t.json();return a.status===`healthy`?{healthy:!0,port:e,serviceName:a.service,browserCount:a.browsers?.count}:{healthy:!1,error:`Unexpected health status: ${a.status||`unknown`}`}}catch(e){return clearTimeout(i),e instanceof Error?e.name===`AbortError`?{healthy:!1,error:`Health check timed out after ${t}ms`}:`code`in e&&e.code===`ECONNREFUSED`?{healthy:!1,error:`Connection refused - server not running`}:{healthy:!1,error:`Network error: ${e.message}`}:{healthy:!1,error:`Unknown error: ${String(e)}`}}}catch(e){return{healthy:!1,error:e instanceof Error?e.message:String(e)}}}};W=j([(0,d.injectable)()],W);var pn;const mn=(0,b.fileURLToPath)(require(`url`).pathToFileURL(__filename).href),hn=f.dirname(mn),G=`browse-tool-http`,gn=`tool`,_n=`http-serve`,vn=`ESRCH`,yn=`node`,bn=`cli.mjs`,xn=`cli.ts`,K=`[HttpServerManager]`;var Sn=class extends Error{code=`HTTP_SERVER_PORT_ERROR`;recovery=`Check if other services are occupying ports in the configured range.`;constructor(e,t){super(e,t),this.name=`HttpServerPortError`}},q=class extends Error{code=`HTTP_SERVER_STARTUP_ERROR`;recovery=`Check server logs, ensure CLI binary exists, and verify port availability.`;constructor(e,t){super(e,t),this.name=`HttpServerStartupError`}},Cn=class extends Error{code=`HTTP_SERVER_STOP_ERROR`;recovery=`Manually kill the process and clean up the port registry.`;causes;constructor(e,t=[],n){super(e,n),this.name=`HttpServerStopError`,this.causes=t}};const wn=(0,_.promisify)(g.execFile);function Tn(e){return(0,p.existsSync)(e)}async function En(e){try{let{stdout:t}=await wn(`git`,[`rev-parse`,`--git-common-dir`],{cwd:e}),n=t.trim(),r=f.isAbsolute(n)?n:f.join(e,n),i=f.dirname(r);return i===e?null:i}catch{return null}}let Dn=class{repositoryPath;environment;host;constructor(e,t,n=new u.ProcessRegistryService(process.env.PROCESS_REGISTRY_PATH)){this.healthCheck=e,this.portRegistry=t,this.processRegistry=n,this.environment=process.env.NODE_ENV||`development`,this.host=H()}async getRepositoryPath(){return this.repositoryPath||=M(),this.repositoryPath}buildErrorStatus(e){return{running:!1,error:e instanceof Error?e.message:String(e),errorCode:e instanceof Error&&`code`in e?e.code:void 0,errorRecovery:e instanceof Error&&`recovery`in e?e.recovery:void 0,errorCause:e instanceof Error&&e.cause instanceof Error?e.cause.message:void 0}}async listServiceEntries(){let e=await this.getRepositoryPath(),t={serviceName:G,serviceType:gn,environment:this.environment},n=await this.portRegistry.listAllocations({...t,repositoryPath:e});if(n.length>0)return n;let r=await En(e);if(r){let e=await this.portRegistry.listAllocations({...t,repositoryPath:r});if(e.length>0)return e}return _e()?this.portRegistry.listAllocations(t):[]}async releaseEntry(e){if(e.pid){let t=await this.processRegistry.releaseProcess({repositoryPath:e.repositoryPath,serviceName:G,serviceType:`service`,environment:e.environment,pid:e.pid,kill:!1,releasePort:!1});!t.success&&!t.error?.includes(`No matching process entry`)&&console.error(`${K} Failed to release process entry for port ${e.port}: ${t.error}`)}let t=await this.portRegistry.releasePort({repositoryPath:e.repositoryPath,serviceName:G,serviceType:gn,environment:e.environment,pid:e.pid});!t.success&&!t.error?.includes(`No matching registry entry`)&&console.error(`${K} Failed to release entry for port ${e.port}: ${t.error}`)}async discoverHealthyService(){let e=await this.listServiceEntries();for(let t of e)try{let e=await this.healthCheck.check(t.port);if(e.healthy&&e.serviceName===G)return{entry:t,browserCount:e.browserCount}}catch(e){console.error(`${K} Health check threw for port ${t.port}: ${e instanceof Error?e.message:String(e)}`)}return null}async discoverHealthyServiceOnPort(e){let t=await this.findRegistrationByPort(e);if(!t)return null;try{let n=await this.healthCheck.check(e);if(n.healthy&&n.serviceName===G)return{entry:t,browserCount:n.browserCount}}catch(t){console.error(`${K} Health check threw for requested port ${e}: ${t instanceof Error?t.message:String(t)}`)}return null}async cleanupStaleEntries(){let e=await this.listServiceEntries(),t=[];for(let n of e)try{let e;try{e=await this.healthCheck.check(n.port)}catch(e){console.error(`${K} Health check threw for port ${n.port}, skipping cleanup: ${e instanceof Error?e.message:String(e)}`);continue}e.healthy&&e.serviceName===G||(n.pid&&await this.killProcess(n.pid),await this.releaseEntry(n))}catch(e){let r=`Failed to clean up entry on port ${n.port}: ${e instanceof Error?e.message:String(e)}`;console.error(`${K} ${r}`),t.push(r)}t.length>0&&console.error(`${K} ${t.length} entry cleanup failure(s) during stale entry removal`)}async findRegistrationByPort(e){return(await this.listServiceEntries()).find(t=>t.port===e)??null}async findAvailablePort(e,t={}){let n=await this.getRepositoryPath(),r=await this.portRegistry.findAvailablePort({repositoryPath:n,serviceName:G,serviceType:gn,environment:this.environment,host:this.host,preferredPort:e,portRange:t.exactPort?{min:e,max:e}:void 0});if(!r.success||!r.port)throw new Sn(`No available port found from ${e}: ${r.error}`);return r.port}async ensureExactPortAvailable(e){let t=await this.findAvailablePort(e,{exactPort:!0});if(t!==e)throw new Sn(`Requested port ${e} is unavailable; next available port is ${t}`)}async resolveCliCommand(e){let t=f.resolve(hn,bn),n=f.resolve(hn,`..`,xn),r=[`--port`,e.toString(),`--host`,this.host];if(await Tn(t))return[yn,[t,_n,...r]];if(await Tn(n))return[`bun`,[`run`,n,_n,...r]];let i=f.resolve(hn,`..`,`..`),a=f.join(i,`dist`,bn),o=f.join(i,`src`,xn);if(await Tn(a))return[yn,[a,_n,...r]];if(await Tn(o))return[`bun`,[`run`,o,_n,...r]];throw new q(`Cannot find CLI at ${t}, ${n}, ${a}, or ${o}`)}async startHttpServer(e){let[t,n]=await this.resolveCliCommand(e),r=await this.getRepositoryPath(),i=(0,g.spawn)(t,n,{detached:!0,stdio:`ignore`,cwd:r,env:{...process.env,NODE_ENV:this.environment,[fe]:r,PLAYWRIGHT_SKIP_DYNAMIC_TOOL_COMMANDS:`1`}}),a=await new Promise(e=>{let t=setTimeout(()=>{i.removeAllListeners(`error`),e(null)},100);i.once(`error`,n=>{clearTimeout(t),e(n)})});if(a)throw new q(`Failed to spawn HTTP server process: ${a.message}`,{cause:a});i.unref();let o=i.pid;if(!o)throw new q(`Failed to spawn HTTP server - no PID returned`);return o}async killProcess(e){try{process.kill(e,0),process.kill(e,`SIGTERM`)}catch(t){if((t instanceof Error&&`code`in t?t.code:void 0)===vn){console.error(`${K} Process ${e} does not exist — skipping kill`);return}throw t}await new Promise(e=>setTimeout(e,1e3));try{process.kill(e,0),process.kill(e,`SIGKILL`)}catch(t){if((t instanceof Error&&`code`in t?t.code:void 0)===vn){console.error(`${K} Process ${e} exited after SIGTERM`);return}throw t}}async cleanupFailedSpawn(e,t){await this.killProcess(e);let n=await this.findRegistrationByPort(t);n&&n.pid===e&&await this.releaseEntry(n)}async attemptSpawn(e,t={}){let n=await this.findAvailablePort(e,t),r=await this.startHttpServer(n);await new Promise(e=>setTimeout(e,1e4));let i;try{i=await this.healthCheck.check(n)}catch(e){let t=e instanceof Error?e.message:String(e);return console.error(`${K} Health check threw on port ${n}: ${t}`),await this.cleanupFailedSpawn(r,n),{success:!1,reason:`health_check_error`,port:n,error:t}}if(!i.healthy)return await this.cleanupFailedSpawn(r,n),{success:!1,reason:`health_check_failed`,port:n,error:i.error};if(i.serviceName!==G)return await this.cleanupFailedSpawn(r,n),{success:!1,reason:`service_name_mismatch`,port:n,error:`Expected ${G}, got ${i.serviceName}`};let a=await this.findRegistrationByPort(n);return!a||a.pid!==r?(console.error(`${K} Ownership mismatch on port ${n}: expected pid ${r}, found ${a?.pid}`),await this.cleanupFailedSpawn(r,n),{success:!1,reason:`ownership_mismatch`,port:n}):(console.error(`${K} Server self-registered on port ${n} (pid: ${a.pid})`),{success:!0,port:n,pid:r,browserCount:i.browserCount})}async ensureRunning(e=dn(),t={}){try{if(t.exactPort){let t=await this.discoverHealthyServiceOnPort(e);if(t)return{running:!0,port:t.entry.port,pid:t.entry.pid,browserCount:t.browserCount,spawned:!1};await this.cleanupStaleEntries(),await this.ensureExactPortAvailable(e);let n=await this.attemptSpawn(e,{exactPort:!0});if(n.success)return{running:!0,port:n.port,pid:n.pid,browserCount:n.browserCount,spawned:!0};let r=new q(`Failed to start browse-tool HTTP server on requested port ${e}: ${n.reason}`);return this.buildErrorStatus(r)}let n=await this.discoverHealthyService();if(n)return{running:!0,port:n.entry.port,pid:n.entry.pid,browserCount:n.browserCount,spawned:!1};await this.cleanupStaleEntries();let r=e;for(let e=0;e<3;e+=1){let t=await this.attemptSpawn(r);if(t.success)return{running:!0,port:t.port,pid:t.pid,browserCount:t.browserCount,spawned:!0};console.error(`${K} Spawn attempt ${e+1} failed: ${t.reason} on port ${t.port}`),r=t.port+1}let i=new q(`Failed to start browse-tool HTTP server after 3 attempts`);return this.buildErrorStatus(i)}catch(e){return this.buildErrorStatus(e)}}async stop(){let e=await this.listServiceEntries();if(e.length===0)return!1;let t=[];for(let n of e)try{n.pid&&await this.killProcess(n.pid),await this.releaseEntry(n)}catch(e){let r=e instanceof Error?e:Error(`Failed to stop entry on port ${n.port}: ${String(e)}`);console.error(`${K} ${r.message}`),t.push(r)}if(t.length>0){let n=t.map(e=>e.message);throw new Cn(`Failed to stop ${t.length} of ${e.length} server(s): ${n.join(`; `)}`,t,{cause:t[0]})}return!0}async getStatus(){try{let e=await this.discoverHealthyService();if(e)return{running:!0,port:e.entry.port,pid:e.entry.pid,browserCount:e.browserCount};let t=await this.listServiceEntries();if(t.length>0){let e=t[0];return{running:!1,port:e.port,pid:e.pid,error:`Server registered but not healthy`,errorCode:`HTTP_SERVER_STALE_ENTRY`,errorRecovery:`Run ensureRunning() to start a fresh server or clean up stale entries.`}}return{running:!1,error:`No HTTP server registered`,errorCode:`HTTP_SERVER_NOT_REGISTERED`,errorRecovery:`Call ensureRunning() to discover or spawn a server.`}}catch(e){return this.buildErrorStatus(e)}}};Dn=j([(0,d.injectable)(),A(0,(0,d.inject)(O.HttpServerHealthCheck)),A(1,(0,d.inject)(O.PortRegistryService)),A(2,(0,d.inject)(O.ProcessRegistryService)),k(`design:paramtypes`,[Object,Object,typeof(pn=u.ProcessRegistryService!==void 0&&u.ProcessRegistryService)==`function`?pn:Object])],Dn);const On=900*1e3,kn=1800*1e3,An=`PLAYWRIGHT_BROWSER_IDLE_TIMEOUT_MINUTES`;function jn(){let e=process.env[An];if(!e)return kn;let t=Number.parseFloat(e);return!Number.isFinite(t)||t<=0?kn:Math.round(t*60*1e3)}let Mn=class{intervalId=null;browserIdleTimeoutMs=jn();constructor(e,t,n){this.browserService=e,this.pageRegistry=t,this.webSocketHub=n}start(){this.intervalId||(this.intervalId=setInterval(()=>{this.cleanup().catch(e=>{console.warn(`[IdleCleanup] Error during cleanup:`,e)})},6e4),this.intervalId&&typeof this.intervalId==`object`&&`unref`in this.intervalId&&this.intervalId.unref(),console.warn(`[IdleCleanup] Started (page timeout: ${On/6e4}min, browser timeout: ${this.browserIdleTimeoutMs/6e4}min)`))}stop(){this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null,console.warn(`[IdleCleanup] Stopped`))}async cleanup(){let e=Date.now();await this.cleanupIdlePages(e),await this.cleanupIdleBrowsers(e)}async cleanupIdlePages(e){let t=this.pageRegistry.list();for(let n of t){let t=e-n.lastAccessedAt.getTime();if(!(t<On)&&!(n.mode===`extension`&&this.isExtensionBrowserConnected(n.browserId))){console.warn(`[IdleCleanup] Closing idle page "${n.id}" (idle ${Math.round(t/6e4)}min)`);try{if(n.page)await n.page.close();else{let e=this.browserService.getBrowser(n.browserId);if(e&&(e.pageIds.delete(n.id),e.currentPageId===n.id)){let t=Array.from(e.pageIds);e.currentPageId=t.length>0?t[0]:null}this.pageRegistry.remove(n.id)}}catch(e){console.warn(`[IdleCleanup] Failed to close page "${n.id}":`,e)}}}}async cleanupIdleBrowsers(e){let t=this.browserService.listBrowsers();for(let n of t){let t=n.specOrigin?On:this.browserIdleTimeoutMs,r=e-n.lastAccessedAt.getTime();if(!(r<t)&&!((n.mode===`extension`||n.mode===`vm`)&&this.isExtensionBrowserConnected(n.id))){console.warn(`[IdleCleanup] Closing idle browser "${n.id}" (idle ${Math.round(r/6e4)}min)`);try{await this.browserService.closeBrowser(n.id)}catch(e){console.warn(`[IdleCleanup] Failed to close browser "${n.id}":`,e)}}}}isExtensionBrowserConnected(e){return this.webSocketHub?.hasConnection(e)??!1}};Mn=j([(0,d.injectable)(),A(0,(0,d.inject)(O.BrowserService)),A(1,(0,d.inject)(O.PageRegistry)),A(2,(0,d.inject)(O.WebSocketHub)),A(2,(0,d.optional)()),k(`design:paramtypes`,[Object,Object,Object])],Mn);let Nn=class{constructor(e){this.portRegistry=e}async findAvailablePort(e){let t=await this.portRegistry.findAvailablePort({repositoryPath:e.repositoryPath,serviceName:`browse-tool-http`,serviceType:`tool`,environment:e.environment,host:e.host,preferredPort:e.preferredPort,portRange:e.exactPort?{min:e.preferredPort,max:e.preferredPort}:void 0});if(!t.success||!t.port)throw Error(t.error||`Failed to acquire HTTP port from ${e.preferredPort}`);return t.port}async acquirePorts(e){let t,n=!1;try{if(e.reserveMcpPort){let n=e.preferredMcpPort??l.DEFAULT_PORT_RANGE.min,r=await this.portRegistry.reservePort({repositoryPath:e.repositoryPath,serviceName:e.mcpServiceName??`browse-tool-mcp-http`,serviceType:e.mcpServiceType??`tool`,environment:e.environment,preferredPort:n,pid:e.pid,host:e.host,force:!0,portRange:{min:n,max:n},metadata:e.mcpMetadata});if(!r.success||!r.record)throw Error(r.error||`Failed to reserve MCP port ${n}`);t=r.record}return{httpPort:await this.findAvailablePort({repositoryPath:e.repositoryPath,environment:e.environment,host:e.host,preferredPort:e.preferredHttpPort??l.DEFAULT_PORT_RANGE.min,exactPort:e.exactHttpPort}),mcpPort:t?.port,release:async()=>{n||!t||(n=!0,await this.portRegistry.releasePort({repositoryPath:t.repositoryPath,serviceName:t.serviceName,serviceType:t.serviceType,environment:t.environment,pid:t.pid}))}}}catch(e){throw t&&(n=!0,await this.portRegistry.releasePort({repositoryPath:t.repositoryPath,serviceName:t.serviceName,serviceType:t.serviceType,environment:t.environment,pid:t.pid})),e}}};Nn=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PortRegistryService)),k(`design:paramtypes`,[Object])],Nn);let Pn=class{browsers=new Map;pages=new Map;trackBrowser(e,t){this.browsers.has(e)||this.browsers.set(e,{browserId:e,mode:t,createdAt:new Date,pageIds:new Set})}trackPage(e,t,n){if(this.pages.has(e))return;this.pages.set(e,{pageId:e,browserId:t,url:n,createdAt:new Date});let r=this.browsers.get(t);r&&r.pageIds.add(e)}untrackBrowser(e){let t=this.browsers.get(e);if(t){for(let e of t.pageIds)this.pages.delete(e);this.browsers.delete(e)}}untrackPage(e){let t=this.pages.get(e);if(t){let n=this.browsers.get(t.browserId);n&&n.pageIds.delete(e),this.pages.delete(e)}}getBrowserIds(){return Array.from(this.browsers.keys())}getPageIds(){return Array.from(this.pages.keys())}getPageIdsForBrowser(e){let t=this.browsers.get(e);return t?Array.from(t.pageIds):[]}getSessionState(){return{browsers:Array.from(this.browsers.values()).map(e=>({browserId:e.browserId,mode:e.mode,createdAt:e.createdAt.toISOString(),pageIds:Array.from(e.pageIds)})),pages:Array.from(this.pages.values()).map(e=>({pageId:e.pageId,browserId:e.browserId,url:e.url,createdAt:e.createdAt.toISOString()})),totalBrowsers:this.browsers.size,totalPages:this.pages.size}}clear(){this.browsers.clear(),this.pages.clear()}};Pn=j([(0,d.injectable)()],Pn);let Fn=class{pageStates=new Map;pageInstances=new Map;startMonitoring(e,t){this.stopMonitoring(e);let n={networkRequests:new Map,consoleMessages:[],requestCounter:0,messageCounter:0,listeners:{}};n.listeners.request=e=>{let t=`req-${++n.requestCounter}`,r={id:t,url:e.url(),method:e.method(),resourceType:e.resourceType(),headers:e.headers(),postData:e.postData()??void 0,timestamp:new Date};n.networkRequests.set(t,r)},n.listeners.response=e=>{let t=e.request();for(let[,r]of n.networkRequests)if(r.url===t.url()&&!r.response){r.response={status:e.status(),statusText:e.statusText(),headers:e.headers(),timing:e.request().timing().responseEnd};break}},n.listeners.console=e=>{let t=`msg-${++n.messageCounter}`,r=e.location(),i={id:t,type:e.type(),text:e.text(),location:r.url?{url:r.url,lineNumber:r.lineNumber,columnNumber:r.columnNumber}:void 0,timestamp:new Date};n.consoleMessages.push(i)},t.on(`request`,n.listeners.request),t.on(`response`,n.listeners.response),t.on(`console`,n.listeners.console),this.pageStates.set(e,n),this.pageInstances.set(e,t),t.once(`close`,()=>{this.stopMonitoring(e)})}stopMonitoring(e){let t=this.pageStates.get(e),n=this.pageInstances.get(e);t&&n&&(t.listeners.request&&n.off(`request`,t.listeners.request),t.listeners.response&&n.off(`response`,t.listeners.response),t.listeners.console&&n.off(`console`,t.listeners.console)),this.pageStates.delete(e),this.pageInstances.delete(e)}getNetworkRequests(e){let t=this.pageStates.get(e);return t?Array.from(t.networkRequests.values()):[]}getNetworkRequest(e,t){let n=this.pageStates.get(e);if(n)return n.networkRequests.get(t)}getConsoleMessages(e,t){let n=this.pageStates.get(e);return n?t?n.consoleMessages.filter(e=>e.type===t):[...n.consoleMessages]:[]}clearData(e){let t=this.pageStates.get(e);t&&(t.networkRequests.clear(),t.consoleMessages=[],t.requestCounter=0,t.messageCounter=0)}};Fn=j([(0,d.injectable)()],Fn);let In=class{pages=new Map;pageIdCounter=0;constructor(e){this.webSocketHub=e}async register(e){let{page:t,browser:n,context:r,browserId:i,profileName:a,ownerId:o,mode:s,onClose:c}=e,l=`page-${++this.pageIdCounter}`,u={id:l,mode:s??`playwright`,page:t,browser:n,context:r,browserId:i,url:t.url(),title:await t.title(),profileName:a,ownerId:o??N(),createdAt:new Date,lastAccessedAt:new Date,onClose:c};return this.pages.set(l,u),t.once(`close`,()=>{try{c?.(l)}catch{}this.remove(l)}),l}registerExtensionPage(e,t,n,r=!0){let i=`page-${++this.pageIdCounter}`,a={id:i,mode:`extension`,page:void 0,browser:void 0,context:void 0,browserId:e,url:n??`about:blank`,title:`Extension Tab`,ownerId:N(),extensionTabId:t,createdAt:new Date,lastAccessedAt:new Date};return this.pages.set(i,a),r&&this.webSocketHub?.broadcastPageCreated(e,i,n),i}get(e){return this.pages.get(e)}remove(e){let t=this.pages.get(e);t&&(this.pages.delete(e),t.mode===`extension`&&this.webSocketHub?.broadcastPageRemoved(t.browserId,e))}list(){return Array.from(this.pages.values())}findByBrowser(e){return this.list().filter(t=>t.browserId===e)}findByProfile(e){return this.list().filter(t=>t.profileName===e)}findByOwner(e){return this.list().filter(t=>t.ownerId===e)}touchPage(e){let t=this.pages.get(e);t&&(t.lastAccessedAt=new Date)}async updateMetadata(e){let t=this.get(e);t&&t.page&&(t.url=t.page.url(),t.title=await t.page.title())}clear(){this.pages.clear(),this.pageIdCounter=0}};In=j([(0,d.injectable)(),A(0,(0,d.inject)(O.WebSocketHub)),A(0,(0,d.optional)()),k(`design:paramtypes`,[Object])],In);let Ln=class{pauseStates=new Map;getOrCreateState(e){let t=this.pauseStates.get(e);return t||(t={isPaused:!1,stepName:null,reason:null,pausedAt:null,resolver:null},this.pauseStates.set(e,t)),t}pause(e,t,n){let r=this.getOrCreateState(e);if(r.isPaused)throw Error(`Session "${e}" is already paused at step "${r.stepName}"`);return r.isPaused=!0,r.stepName=t,r.reason=n??null,r.pausedAt=new Date,new Promise(e=>{r.resolver=e})}resume(e){let t=this.pauseStates.get(e);if(!t||!t.isPaused)return!1;let n=t.resolver;return t.isPaused=!1,t.stepName=null,t.reason=null,t.pausedAt=null,t.resolver=null,n&&n(),!0}waitForResume(e){let t=this.pauseStates.get(e);return!t||!t.isPaused||!t.resolver?Promise.resolve():new Promise(e=>{let n=t.resolver;t.resolver=()=>{n&&n(),e()}})}getStatus(e){let t=this.pauseStates.get(e);return{sessionId:e,isPaused:t?.isPaused??!1,stepName:t?.stepName??null,reason:t?.reason??null,pausedAt:t?.pausedAt??null}}isPaused(e){return this.pauseStates.get(e)?.isPaused??!1}clear(e){let t=this.pauseStates.get(e);t?.resolver&&t.resolver(),this.pauseStates.delete(e)}};Ln=j([(0,d.injectable)()],Ln);let Rn=class{profilesDir;constructor(){this.profilesDir=process.env.PLAYWRIGHT_PROFILES_DIR||(0,f.join)((0,h.homedir)(),`.browse-tool`,`profiles`)}getProfilesDir(){return this.profilesDir}getProfileDir(e){return(0,f.join)(this.getProfilesDir(),e)}getProfilePath(e){return(0,f.join)(this.getProfileDir(e),`profile.json`)}getStorageStatePath(e){return(0,f.join)(this.getProfileDir(e),`storage-state.json`)}async ensureProfilesDir(){let e=this.getProfilesDir();(0,p.existsSync)(e)||await(0,m.mkdir)(e,{recursive:!0})}async list(){await this.ensureProfilesDir();let e=await(0,m.readdir)(this.getProfilesDir(),{withFileTypes:!0}),t=[];for(let n of e)if(n.isDirectory()){let e=await this.get(n.name);e&&t.push(e)}return t}async get(e){let t=this.getProfilePath(e);if(!(0,p.existsSync)(t))return null;let n=await(0,m.readFile)(t,`utf-8`);return JSON.parse(n)}async create(e){if(await this.get(e.name))throw Error(`Profile "${e.name}" already exists`);await(0,m.mkdir)(this.getProfileDir(e.name),{recursive:!0});let t=new Date().toISOString(),n={...e,createdAt:t,updatedAt:t};return await(0,m.writeFile)(this.getProfilePath(e.name),JSON.stringify(n,null,2)),n}async update(e,t){let n=await this.get(e);if(!n)throw Error(`Profile "${e}" not found`);let r={...n,...t,name:n.name,createdAt:n.createdAt,updatedAt:new Date().toISOString()};return await(0,m.writeFile)(this.getProfilePath(e),JSON.stringify(r,null,2)),r}async delete(e){if(!await this.get(e))throw Error(`Profile "${e}" not found`);await(0,m.rm)(this.getProfileDir(e),{recursive:!0})}async saveStorageState(e,t){if(!await this.get(e))throw Error(`Profile "${e}" not found`);await(0,m.writeFile)(this.getStorageStatePath(e),JSON.stringify(t,null,2))}async loadStorageState(e){let t=this.getStorageStatePath(e);if(!(0,p.existsSync)(t))return null;let n=await(0,m.readFile)(t,`utf-8`);return JSON.parse(n)}};Rn=j([(0,d.injectable)(),k(`design:paramtypes`,[])],Rn);const zn=`session`,Bn=/\$\{([A-Z0-9_]+)\}/gi;function Vn(e){return e.PLAYWRIGHT_PROFILES_DIR||f.default.join((0,h.homedir)(),`.browse-tool`,`profiles`)}function Hn(e){let t=e.env??process.env,n=e.proxyConfigDir??t.BROWSE_TOOL_PROXY_CONFIG_DIR;if(n)return f.default.resolve(n);let r=e.profilesDir??Vn(t);return f.default.join(f.default.dirname(f.default.resolve(r)),`proxy-config`)}function Un(e){if(!e||e.includes(`/`)||e.includes(`\\`)||e===`.`||e===`..`)throw Error(`Invalid proxy config name: ${e}`)}function Wn(e,t){Un(t);let n=f.default.resolve(e),r=f.default.resolve(n,`${t}.yaml`),i=f.default.relative(n,r);if(i.startsWith(`..`)||f.default.isAbsolute(i))throw Error(`Proxy config path escapes configured directory: ${t}`);return r}function Gn(e,t,n){return e.replace(Bn,(e,r)=>{let i=t[r];if(i===void 0)throw Error(`Missing environment variable "${r}" referenced by proxy config ${n}`);return i})}function Kn(e,t,n){let r=Wn(e,t);if(!(0,p.existsSync)(r))return;let i=(0,re.parse)((0,p.readFileSync)(r,`utf8`));if(!i||typeof i!=`object`||Array.isArray(i))throw Error(`Proxy config ${r} must be a YAML object`);let a=i,o={server:qn(a,`server`,r,n)},s=Jn(a,`username`,r,n),c=Jn(a,`password`,r,n),l=Jn(a,`bypass`,r,n);return s!==void 0&&(o.username=s),c!==void 0&&(o.password=c),l!==void 0&&(o.bypass=l),o}function qn(e,t,n,r){let i=Jn(e,t,n,r);if(!i)throw Error(`Proxy config ${n} must define a non-empty "${t}" string`);return i}function Jn(e,t,n,r){let i=e[t];if(i!=null){if(typeof i!=`string`)throw Error(`Proxy config ${n} field "${t}" must be a string`);return Gn(i,r,n)}}function Yn(e){return!!e&&e!==!0&&typeof e==`object`}let J=class{getProxyConfigDir(e={}){return Hn(e)}resolve(e){if(Yn(e.proxy))return e.proxy;let t=e.env??process.env,n=Hn(e);if(e.profileName){let r=Kn(n,e.profileName,t);if(r)return r}if(e.proxy===!0){let e=Kn(n,zn,t);if(!e)throw Error(`Proxy requested but ${Wn(n,zn)} was not found`);return e}return Kn(n,`default`,t)}};J=j([(0,d.injectable)()],J);function Xn(e){return new J().resolve(e)}let Zn=class{constructor(e){this.httpClient=e}async execute(e,t={}){return this.httpClient.execute(e,t)}async isServerAvailable(){return this.httpClient.isHealthy()}setServerPort(e){this.httpClient.setBaseUrl(U(void 0,e))}async listTools(){let e=this.httpClient.getBaseUrl(),t=await fetch(`${e}/tools`);if(!t.ok)throw Error(`Failed to list tools: ${t.statusText}`);let n=await t.json();if(n.error)throw Error(n.error);return n.tools}};Zn=j([(0,d.injectable)(),A(0,(0,d.inject)(O.HttpBrowserClient)),k(`design:paramtypes`,[Object])],Zn);let Qn=class{constructor(e){this.bundler=e}async runSetup(e,t,n){let r=null;try{r=await this.bundler.bundle(e);let i=await import(`${r.outputPath}?t=${Date.now()}`),a=await this.getSetupFunction(i,t,e)(n);return{state:this.validateSetupResult(a,e),success:!0}}catch(t){return{state:{},success:!1,error:`Setup failed for "${e}": ${t instanceof Error?t.message:String(t)}`}}finally{r&&await r.cleanup()}}getSetupFunction(e,t,n){if(t in e&&typeof e[t]==`function`)return e[t];if(t===`default`&&`default`in e){let t=e.default;if(typeof t==`function`)return t;throw Error(`Default export in "${n}" is not a function`)}let r=Object.keys(e).filter(t=>typeof e[t]==`function`);throw r.length===0?Error(`No function exports found in "${n}"`):Error(`Export "${t}" not found in "${n}". Available functions: ${r.join(`, `)}`)}validateSetupResult(e,t){if(e==null)return{};if(typeof e!=`object`||Array.isArray(e))throw Error(`Setup function in "${t}" must return an object or undefined, got ${typeof e}`);return e}};Qn=j([(0,d.injectable)(),A(0,(0,d.inject)(O.SpecBundlerService)),k(`design:paramtypes`,[Object])],Qn);const Y=`[SpecBundler]`,$n=RegExp(`^@playwright\\/test$`),er=`__PLAYWRIGHT_MCP_STUB_PATH__`,X={OUTDIR_NAME:`browse-tool-bundles`,PLUGIN_NAME:`playwright-test-alias`,EXTERNALS:[`playwright`,`playwright/test`,`playwright-core`]},tr={BUILD_FAILED:`SPEC_BUNDLER_BUILD_FAILED`,NO_OUTPUT:`SPEC_BUNDLER_NO_OUTPUT`,IO_FAILED:`SPEC_BUNDLER_IO_FAILED`},Z={UNEXPECTED_BUILD:`Unexpected build exception`,IO_MKDIR:`Failed to create output directory`,CLEANUP_FAILED:`Failed to clean up temporary file`};var Q=class extends Error{code=tr.BUILD_FAILED;recovery=`Check the spec file for syntax errors and ensure all imports are resolvable.`;constructor(e,t,n){super(`Failed to bundle spec "${(0,f.basename)(e)}": ${t}`,n),this.specPath=e,this.buildErrors=t,this.name=`SpecBundlerBuildError`}},nr=class extends Error{code=tr.IO_FAILED;recovery=`Check filesystem permissions and available disk space.`;constructor(e,t,n){super(`${e} at "${t}"`,n),this.operation=e,this.targetPath=t,this.name=`SpecBundlerIoError`}};let rr=class{stubPath;nodeModulesPath;constructor(){let e=(0,f.dirname)(require(`url`).pathToFileURL(__filename).href.replace(`file://`,``));this.stubPath=(0,f.join)(e,`stubs`,`playwright-test.mjs`),this.nodeModulesPath=this.findNodeModules(e)}findNodeModules(e){let t=e,n=(0,f.resolve)(`/`);for(;t!==n;){let e=(0,f.join)(t,`node_modules`);if((0,p.existsSync)((0,f.join)(e,`playwright`)))return e;t=(0,f.dirname)(t)}for(t=e;t!==n;){let e=(0,f.join)(t,`node_modules`);if((0,p.existsSync)(e))return e;t=(0,f.dirname)(t)}return(0,f.join)(e,`node_modules`)}async bundle(e){let t=(0,f.join)((0,h.tmpdir)(),X.OUTDIR_NAME);try{await(0,m.mkdir)(t,{recursive:!0})}catch(e){throw console.error(`${Y} ${Z.IO_MKDIR} "${t}":`,e),new nr(Z.IO_MKDIR,t,{cause:e})}let n=(0,f.join)(t,`node_modules`);if(!(0,p.existsSync)(n))try{await(0,m.symlink)(this.nodeModulesPath,n,`junction`)}catch{}return this.bundleWithEsbuild(e,t)}async bundleWithEsbuild(e,t){let n=(0,f.basename)(e).replace(/\.[^.]+$/,``),r=this.stubPath;try{let i=await(0,ie.build)({entryPoints:[e],outdir:t,bundle:!0,platform:`node`,format:`esm`,target:`node18`,banner:{js:`import { createRequire as __esbuildCreateRequire } from 'module'; const require = __esbuildCreateRequire(import.meta.url);`},external:[...X.EXTERNALS],define:{[er]:JSON.stringify(r)},plugins:[{name:X.PLUGIN_NAME,setup(e){e.onResolve({filter:$n},()=>({path:r}))}}],write:!0,logLevel:`silent`});if(i.errors.length>0){let t=i.errors.map(e=>e.text).join(`
|
|
49
|
-
`);throw console.error(`${Y} Build failed for "${e}": ${t}`),new Q(e,t,{cause:Error(t)})}let a=(0,f.join)(t,`${n}.js`);return{outputPath:a,cleanup:()=>this.safeUnlink(a)}}catch(t){if(t instanceof Q)throw t;let n=t instanceof Error?t.message:Z.UNEXPECTED_BUILD;throw console.error(`${Y} Build failed for "${e}":`,t),new Q(e,n,{cause:t})}}async bundleWithHooks(e){let t=(0,f.join)((0,h.tmpdir)(),X.OUTDIR_NAME);try{await(0,m.mkdir)(t,{recursive:!0})}catch(e){throw console.error(`${Y} ${Z.IO_MKDIR} "${t}":`,e),new nr(Z.IO_MKDIR,t,{cause:e})}let n=(0,f.join)(t,`node_modules`);if(!(0,p.existsSync)(n))try{await(0,m.symlink)(this.nodeModulesPath,n,`junction`)}catch{}return this.bundleSpecWithHooksEntry(e.specPath,e.hooksPath,t)}async bundleSpecWithHooksEntry(e,t,n){let r=(0,f.dirname)((0,f.resolve)(t)),i=this.stubPath,a=`spec-with-hooks`,o=(0,f.join)(n,`${a}-${(0,y.randomUUID)()}.js`),s=[`process.env.__HOOKS_SOURCE_DIR__ = ${JSON.stringify(r)};`,`import { setup as __hooksSetup } from ${JSON.stringify(t)};`,`await __hooksSetup();`,`delete process.env.__HOOKS_SOURCE_DIR__;`,`export * from ${JSON.stringify(e)};`].join(`
|
|
50
|
-
`);try{let t=await(0,ie.build)({stdin:{contents:s,resolveDir:(0,f.dirname)(e),loader:`ts`,sourcefile:`${a}.ts`},outfile:o,bundle:!0,platform:`node`,format:`esm`,target:`node18`,banner:{js:`import { createRequire as __esbuildCreateRequire } from 'module'; const require = __esbuildCreateRequire(import.meta.url);`},external:[...X.EXTERNALS],define:{[er]:JSON.stringify(i)},plugins:[{name:X.PLUGIN_NAME,setup(e){e.onResolve({filter:$n},()=>({path:i}))}}],write:!0,logLevel:`silent`});if(t.errors.length>0){let n=t.errors.map(e=>e.text).join(`
|
|
51
|
-
`);throw console.error(`${Y} Build failed for "${e}" (with hooks): ${n}`),new Q(e,n,{cause:Error(n)})}return{outputPath:o,cleanup:()=>this.safeUnlink(o)}}catch(t){if(t instanceof Q)throw t;let n=t instanceof Error?t.message:Z.UNEXPECTED_BUILD;throw console.error(`${Y} Build failed for "${e}" (with hooks):`,t),new Q(e,n,{cause:t})}}async safeUnlink(e){try{await(0,m.unlink)(e)}catch(t){console.warn(`${Y} ${Z.CLEANUP_FAILED} "${e}":`,t)}}};rr=j([(0,d.injectable)(),k(`design:paramtypes`,[])],rr);let ir=class{async discoverProjects(e){let t=e||process.cwd(),n=[],r=await this.findPlaywrightConfigs(t);for(let e of r){let t=(0,f.dirname)(e),r={name:(0,f.basename)(t),path:t,configPath:e,testDir:await this.getTestDirFromConfig(e),specs:[]};r.specs=await this.getProjectSpecs(t),n.push(r)}return n}async getProjectSpecs(e){let t=(0,f.join)(e,`playwright.config.ts`);if(!(0,p.existsSync)(t))throw Error(`No playwright.config.ts found in ${e}`);let n=(0,f.join)(e,await this.getTestDirFromConfig(t));if(!(0,p.existsSync)(n))return[];let r=[],i=await this.findSpecFiles(n);for(let t of i){let n=await this.getSpecInfo(t,e);r.push(n)}return r}async getSpecInfo(e,t){let n=(0,f.resolve)(e);if(!(0,p.existsSync)(n))throw Error(`Spec file not found: ${e}`);let r=(0,f.basename)(n);return{name:r,path:n,relativePath:t?(0,f.relative)(t,n):r,hasArgsSchema:await this.checkHasArgsSchema(n)}}async filterSpecs(e,t){let n=await this.discoverProjects(e),r=[];for(let e of n){if(t?.projectName&&e.name!==t.projectName)continue;let n=e.specs;if(t?.specPattern){let e=this.globToRegex(t.specPattern);n=n.filter(t=>e.test(t.name)||e.test(t.relativePath))}r=r.concat(n)}return r}async findPlaywrightConfigs(e){let t=[];return await this.walkDirectory(e,(e,n)=>{if(n){let t=(0,f.basename)(e);return!(t===`node_modules`||t.startsWith(`.`))}return(0,f.basename)(e)===`playwright.config.ts`&&t.push(e),!0}),t}async findSpecFiles(e){let t=[];return await this.walkDirectory(e,(e,n)=>n?(0,f.basename)(e)!==`node_modules`:(e.endsWith(`.spec.ts`)&&t.push(e),!0)),t}async walkDirectory(e,t){if(!(0,p.existsSync)(e))return;let n=await(0,m.readdir)(e,{withFileTypes:!0});for(let r of n){let n=(0,f.join)(e,r.name);r.isDirectory()?t(n,!0)&&await this.walkDirectory(n,t):t(n,!1)}}async getTestDirFromConfig(e){try{let t=(await(0,m.readFile)(e,`utf-8`)).match(/testDir:\s*['"`]([^'"`]+)['"`]/);if(t)return t[1].replace(/^\.\//,``)}catch{}return`tests`}async checkHasArgsSchema(e){try{let t=await(0,m.readFile)(e,`utf-8`);return/export\s+(const\s+)?argsSchema\b/.test(t)}catch{return!1}}globToRegex(e){let t=e.replace(/[.+^${}()|[\]\\]/g,`\\$&`).replace(/\*/g,`.*`).replace(/\?/g,`.`);return RegExp(`^${t}$`,`i`)}};ir=j([(0,d.injectable)()],ir);let ar=class{constructor(e){this.bundler=e}async extractSpecMetadata(e){let t=await this.bundler.bundle(e);try{let e=await import(`${t.outputPath}?t=${Date.now()}`),n=e.argsSchema??null;return{argsSchema:n,envPrefix:e.envPrefix??null,hasDynamicArgs:n!==null}}catch{return{argsSchema:null,envPrefix:null,hasDynamicArgs:!1}}finally{await t.cleanup()}}async loadPlaywrightConfig(e){try{let t=await(0,m.readFile)(e,`utf-8`),n={},r=t.match(/baseURL:\s*['"`]([^'"`]+)['"`]/);r&&(n.baseURL=r[1]);let i=t.match(/export\s+const\s+mcpConfig\s*=\s*\{([^}]+)\}/s);if(i){let e=i[1],t=e.match(/setupFile:\s*['"`]([^'"`]+)['"`]/);t&&(n.setupFile=t[1]);let r=e.match(/setupExport:\s*['"`]([^'"`]+)['"`]/);r&&(n.setupExport=r[1])}let a=t.match(/webServer:\s*\{([^}]+(?:\{[^}]*\}[^}]*)*)\}/s);if(a){let e=a[1],t=e.match(/command:\s*['"`]([^'"`]+)['"`]/),r=e.match(/url:\s*['"`]([^'"`]+)['"`]/),i=e.match(/reuseExistingServer:\s*(true|false)/),o=e.match(/timeout:\s*(\d+)/),s=e.match(/cwd:\s*['"`]([^'"`]+)['"`]/);t&&r&&(n.webServer={command:t[1],url:r[1],reuseExistingServer:i?i[1]===`true`:void 0,timeout:o?Number.parseInt(o[1],10):void 0,cwd:s?s[1]:void 0})}return n}catch{return{}}}parseArgsFromEnv(e,t){let n={},r=t.toUpperCase();for(let[e,t]of Object.entries(process.env))if(e.startsWith(r)&&t!==void 0){let i=this.envKeyToFieldName(e.slice(r.length));n[i]=this.parseEnvValue(t)}try{return e.parse(n)}catch{return n}}envKeyToFieldName(e){return e.toLowerCase().split(`_`).map((e,t)=>t===0?e:e.charAt(0).toUpperCase()+e.slice(1)).join(``)}parseEnvValue(e){if(e.toLowerCase()===`true`)return!0;if(e.toLowerCase()===`false`)return!1;let t=Number(e);if(!Number.isNaN(t)&&e.trim()!==``)return t;if(e.startsWith(`{`)&&e.endsWith(`}`)||e.startsWith(`[`)&&e.endsWith(`]`))try{return JSON.parse(e)}catch{}return e}};ar=j([(0,d.injectable)(),A(0,(0,d.inject)(O.SpecBundlerService)),k(`design:paramtypes`,[Object])],ar);function or(e,t){let{runtimeUse:n,unsupportedKeys:r}=c.h(t);return{options:{...e?{baseURL:e}:{},...n},warnings:r.length>0?[`test.use() ignores unsupported fixture keys: ${r.join(`, `)}`]:[]}}let sr=class{constructor(e){this.bundler=e}async loadSpec(e,t){let n=t?await this.bundler.bundleWithHooks({specPath:e,hooksPath:t}):await this.bundler.bundle(e),r=process,i=r.__pw_initiator__;try{return c.c(e),r.__pw_initiator__=void 0,await import(`${n.outputPath}?t=${Date.now()}`),c.s()}catch(t){throw c.p(),Error(`Failed to load spec "${e}": ${t instanceof Error?t.message:String(t)}`,{cause:t})}finally{r.__pw_initiator__=i,await n.cleanup()}}async executeSpec(e){let{specPath:t,browserType:n=`chromium`,headless:r=!0,hooksPath:i,baseURL:a,keepBrowserOpen:o}=e,s=Date.now(),l=[],u=0,d=0,f=new Set,p=new Set,m=[],h=await x.default[n].launch({channel:`chrome`,headless:r});try{let e=await this.loadSpec(t,i);for(let t of e.allTests){let{options:e,warnings:n}=or(a,t.config.use);for(let e of n)f.add(e);let r=t.serialScopeId,i=r!==void 0&&p.has(r);if(t.skip||i){l.push({title:t.title,fullTitle:t.fullTitle,passed:!0,error:null,duration:0}),u++;continue}let s=await h.newContext(e),g=await s.newPage(),_={page:g,context:s,browser:h,playwright:x.default},v=Date.now();try{await t.fn(_),l.push({title:t.title,fullTitle:t.fullTitle,passed:!0,error:null,duration:Date.now()-v}),u++}catch(e){if(e instanceof c.t||e instanceof Error&&`isSkip`in e)l.push({title:t.title,fullTitle:t.fullTitle,passed:!0,error:null,duration:Date.now()-v}),u++;else{let n=e instanceof Error?e.message:String(e);l.push({title:t.title,fullTitle:t.fullTitle,passed:!1,error:n,duration:Date.now()-v}),d++,r!==void 0&&p.add(r)}}finally{o?m.push({page:g,context:s}):await s.close().catch(()=>{})}}if(!o){for(let e of h.contexts())await e.close().catch(()=>{});await h.close()}return{specPath:t,totalTests:e.testCount,passed:u,failed:d,testResults:l,success:d===0,duration:Date.now()-s,warnings:f.size>0?[...f]:void 0,browser:o?h:void 0,keptPages:m.length>0?m:void 0}}catch(e){for(let e of h.contexts())await e.close().catch(()=>{});return await h.close().catch(()=>{}),{specPath:t,totalTests:0,passed:0,failed:1,testResults:[{title:`Spec Loading`,fullTitle:`Failed to load: ${t}`,passed:!1,error:e instanceof Error?e.message:String(e),duration:Date.now()-s}],success:!1,duration:Date.now()-s,warnings:f.size>0?[...f]:void 0}}}async executeSpecEnhanced(e){let{specPath:t,browserType:n=`chromium`,headless:r=!0,testFilter:i,specArgs:a,hooksPath:o,baseURL:s,keepBrowserOpen:l}=e,u=Date.now(),d=[],f=0,p=0,m=new Set,h=new Set,g=[],_=await x.default[n].launch({channel:`chrome`,headless:r});try{let e=await this.loadSpec(t,o),n=i?this.filterTests(e.allTests,i):e.allTests;for(let e of n){let{options:t,warnings:n}=or(s,e.config.use);for(let e of n)m.add(e);let r=e.serialScopeId,i=r!==void 0&&h.has(r);if(e.skip||i){d.push({title:e.title,fullTitle:e.fullTitle,passed:!0,error:null,duration:0}),f++;continue}let o=await _.newContext(t),u=await o.newPage(),v={page:u,context:o,browser:_,playwright:x.default,specArgs:a},y=Date.now();try{await e.fn(v),d.push({title:e.title,fullTitle:e.fullTitle,passed:!0,error:null,duration:Date.now()-y}),f++}catch(t){if(t instanceof c.t||t instanceof Error&&`isSkip`in t)d.push({title:e.title,fullTitle:e.fullTitle,passed:!0,error:null,duration:Date.now()-y}),f++;else{let n=t instanceof Error?t.message:String(t);d.push({title:e.title,fullTitle:e.fullTitle,passed:!1,error:n,duration:Date.now()-y}),p++,r!==void 0&&h.add(r)}}finally{l?g.push({page:u,context:o}):await o.close().catch(()=>{})}}if(!l){for(let e of _.contexts())await e.close().catch(()=>{});await _.close()}return{specPath:t,totalTests:n.length,passed:f,failed:p,testResults:d,success:p===0,duration:Date.now()-u,warnings:m.size>0?[...m]:void 0,browser:l?_:void 0,keptPages:g.length>0?g:void 0}}catch(e){for(let e of _.contexts())await e.close().catch(()=>{});return await _.close().catch(()=>{}),{specPath:t,totalTests:0,passed:0,failed:1,testResults:[{title:`Spec Loading`,fullTitle:`Failed to load: ${t}`,passed:!1,error:e instanceof Error?e.message:String(e),duration:Date.now()-u}],success:!1,duration:Date.now()-u,warnings:m.size>0?[...m]:void 0}}}filterTests(e,t){let n=[...e];if(t.onlyMarked){let e=n.filter(e=>e.only);e.length>0&&(n=e)}if(t.testName&&(n=n.filter(e=>e.title===t.testName)),t.testPattern){let e=new RegExp(t.testPattern,`i`);n=n.filter(t=>e.test(t.fullTitle))}if(t.describeFilter){let e=new RegExp(t.describeFilter,`i`);n=n.filter(t=>e.test(t.fullTitle))}return n}};sr=j([(0,d.injectable)(),A(0,(0,d.inject)(O.SpecBundlerService)),k(`design:paramtypes`,[Object])],sr);let cr=class{browsers=new Map;browserIdCounter=0;constructor(e,t,n){this.profileService=e,this.pageRegistry=t,this.lockManager=n}findChromePath(){let e=h.default.platform(),t={darwin:[`/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`,`/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary`,`${h.default.homedir()}/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`],win32:[`C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe`,`C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe`,`${process.env.LOCALAPPDATA}\\Google\\Chrome\\Application\\chrome.exe`],linux:[`/usr/bin/google-chrome`,`/usr/bin/google-chrome-stable`,`/usr/bin/chromium`,`/usr/bin/chromium-browser`,`/snap/bin/chromium`]},n=t[e]??t.linux;for(let e of n)if(p.default.existsSync(e))return e;return null}resolveExtensionPath(e){if(e&&p.default.existsSync(f.default.join(e,`manifest.json`)))return e;let t=f.default.dirname(new URL(require(`url`).pathToFileURL(__filename).href).pathname),n=[f.default.resolve(t,`../dist/extension`),f.default.resolve(t,`../../dist/extension`)];for(let e of n)if(p.default.existsSync(f.default.join(e,`manifest.json`)))return e;let r=f.default.resolve(process.cwd(),`dist/extension`);if(p.default.existsSync(f.default.join(r,`manifest.json`)))return r;throw Error(`Chrome extension not found. Build the extension first: pnpm build:extension`)}createUserDataDir(e){if(e){let t=f.default.join(this.profileService.getProfilesDir(),e,`stealth-user-data`);return p.default.mkdirSync(t,{recursive:!0}),t}let t=f.default.join(h.default.tmpdir(),`stealth-chrome-${Date.now()}`);return p.default.mkdirSync(t,{recursive:!0}),t}async launch(e={}){let t=this.findChromePath();if(!t)throw Error(`Chrome not found. Please install Google Chrome.
|
|
52
|
-
macOS: brew install --cask google-chrome
|
|
53
|
-
Linux: sudo apt install google-chrome-stable`);let n=this.resolveExtensionPath(e.extensionPath),r=this.createUserDataDir(e.profileName);await this.lockManager.ensureAvailable(r);let i=`stealth-browser-${++this.browserIdCounter}`,a=[n];e.proxy?.username&&e.proxy?.password&&a.push(ht(e.proxy.username,e.proxy.password));let o=a.join(`,`),s=(0,g.spawn)(t,[`--user-data-dir=${r}`,`--load-extension=${o}`,`--disable-extensions-except=${o}`,`--disable-blink-features=AutomationControlled`,`--disable-infobars`,`--no-first-run`,`--no-default-browser-check`,`--disable-background-timer-throttling`,`--disable-backgrounding-occluded-windows`,`--disable-renderer-backgrounding`,`--disable-component-update`,`--disable-features=TranslateUI`,`--disable-ipc-flooding-protection`,`--password-store=basic`,`--use-mock-keychain`,`--disable-dev-shm-usage`,`--disable-gpu-sandbox`,`--silent-debugger-extension-api`,...e.windowSize?[`--window-size=${e.windowSize.width},${e.windowSize.height}`]:[`--window-size=1280,720`],...e.proxy?gt(e.proxy):[],e.url??`about:blank`,...e.extraArgs??[]],{detached:!1,stdio:[`ignore`,`ignore`,`ignore`],env:{...process.env,DISPLAY:process.env.DISPLAY}});if(!s.pid)throw Error(`Failed to start Chrome process`);let c={id:i,process:s,pid:s.pid,userDataDir:r,extensionPath:n,profileName:e.profileName,createdAt:new Date,isRunning:!0};return s.on(`exit`,()=>{c.isRunning=!1}),s.on(`error`,e=>{console.error(`Chrome process ${i} error:`,e),c.isRunning=!1}),this.browsers.set(i,c),this.pageRegistry.registerExtensionPage(i),c}async close(e){let t=this.browsers.get(e);if(t&&(t.isRunning&&t.pid&&await we(t.pid),t.isRunning=!1,this.browsers.delete(e),!t.profileName&&t.userDataDir.includes(`stealth-chrome-`)))try{p.default.rmSync(t.userDataDir,{recursive:!0,force:!0})}catch{}}async closeAll(){let e=Array.from(this.browsers.keys()).map(e=>this.close(e));await Promise.all(e)}getBrowser(e){return this.browsers.get(e)}listBrowsers(){return Array.from(this.browsers.values()).filter(e=>e.isRunning)}};cr=j([(0,d.injectable)(),A(0,(0,d.inject)(O.ProfileService)),A(1,(0,d.inject)(O.PageRegistry)),A(2,(0,d.inject)(O.BrowserLockManager)),k(`design:paramtypes`,[Object,Object,Object])],cr);function lr(e){if(!e.isError)return;let t=e.content[0];return t?.type===`text`&&typeof t.text==`string`?t.text:`Unknown tool execution error`}let ur=class{constructor(e,t,n,r=new z){this.pageRegistry=e,this.browserService=t,this.delegator=n,this.telemetry=r}formatPageNotFoundMessage(e){let t=this.pageRegistry.list()??[];return t.length===0?`Page "${e}" not found. No pages are currently registered. Launch or select a browser page, then retry with the returned pageId.`:`Page "${e}" not found. Available pages: ${t.slice(0,5).map(e=>`${e.id} (${e.url||`about:blank`}, browser ${e.browserId})`).join(`; `)}${t.length>5?`; and ${t.length-5} more`:``}. Use browser_list_pages to refresh page IDs, then retry with a current pageId.`}formatBrowserNotFoundMessage(e){let t=this.browserService.listBrowsers()??[];return t.length===0?`Browser "${e}" not found. No browsers are currently registered. Launch a browser, then retry with the returned browserId.`:`Browser "${e}" not found. Available browsers: ${t.slice(0,5).map(e=>`${e.id} (${e.mode}, pages ${e.pageIds.size})`).join(`; `)}${t.length>5?`; and ${t.length-5} more`:``}. Use browser_list_pages to refresh browser IDs, then retry with a current browserId.`}async execute(e,t,n,r){return this.telemetry.runInSpan(`browse_tool.tool_executor.page`,{attributes:{"browse_tool.tool.name":e,"browse_tool.page.id":t}},async i=>{let a=this.pageRegistry.get(t);if(!a){let e=this.formatPageNotFoundMessage(t);return i?.setStatus({code:E.SpanStatusCode.ERROR,message:e}),{content:[{type:`text`,text:e}],isError:!0}}i?.setAttributes({"browse_tool.browser.id":a.browserId,"browse_tool.execution.mode":a.mode});let o=a.mode===`extension`?await this.delegator.executeTool(e,{pageId:t,browserId:a.browserId,...n}):await r(),s=lr(o);if(s)i?.setStatus({code:E.SpanStatusCode.ERROR,message:s});else if(a.mode===`extension`)try{await this.browserService.ensureExtensionRecordingActive(a.browserId,t)}catch(e){console.warn(`[ToolExecutor] Failed to start extension recording for browser "${a.browserId}" on page "${t}":`,e)}return o})}isExtensionMode(e){return this.pageRegistry.get(e)?.mode===`extension`}async executeForBrowser(e,t,n,r){return this.telemetry.runInSpan(`browse_tool.tool_executor.browser`,{attributes:{"browse_tool.tool.name":e,"browse_tool.browser.id":t}},async i=>{let a=this.browserService.getBrowser(t);if(!a){let e=this.formatBrowserNotFoundMessage(t);return i?.setStatus({code:E.SpanStatusCode.ERROR,message:e}),{content:[{type:`text`,text:e}],isError:!0}}i?.setAttributes({"browse_tool.execution.mode":a.mode});let o=a.mode===`extension`||a.mode===`vm`?await this.delegator.executeTool(e,{browserId:t,...n}):await r(),s=lr(o);return s&&i?.setStatus({code:E.SpanStatusCode.ERROR,message:s}),o})}isBrowserExtensionMode(e){let t=this.browserService.getBrowser(e);return t?.mode===`extension`||t?.mode===`vm`}};ur=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ExtensionToolDelegator)),A(3,(0,d.inject)(O.TelemetryService)),k(`design:paramtypes`,[Object,Object,Object,Object])],ur);let dr=class{serverProcess=null;currentUrl=null;cleanupRegistered=!1;constructor(){this.registerCleanup()}registerCleanup(){if(this.cleanupRegistered)return;this.cleanupRegistered=!0;let e=()=>this.cleanupSync();process.on(`exit`,e),process.on(`SIGINT`,e),process.on(`SIGTERM`,e)}async startServer(e,t){let{command:n,url:r,reuseExistingServer:i=!1,timeout:a=3e4,cwd:o}=e;if(i&&await this.isServerRunning(r))return this.currentUrl=r,{started:!1,url:r,reused:!0};await this.stopServer();let s=o?(0,f.resolve)(t,o):t,[c,...l]=this.parseCommand(n);if(this.serverProcess=(0,g.spawn)(c,l,{cwd:s,shell:!0,stdio:[`ignore`,`pipe`,`pipe`],detached:!1}),this.currentUrl=r,this.serverProcess.on(`error`,e=>{console.error(`Server process error: ${e.message}`)}),!await this.waitForServer(r,a))throw await this.stopServer(),Error(`Server failed to start within ${a}ms. URL: ${r}, Command: ${n}`);return{started:!0,url:r,reused:!1}}async waitForServer(e,t){let n=Date.now(),r=100;for(;Date.now()-n<t;){if(await this.isServerRunning(e))return!0;await this.sleep(r),r=Math.min(r*1.5,2e3)}return!1}async stopServer(){if(this.serverProcess)return new Promise(e=>{if(!this.serverProcess){e();return}let t=this.serverProcess;this.serverProcess=null,this.currentUrl=null;let n=setTimeout(()=>{try{t.kill(`SIGKILL`)}catch{}e()},5e3);t.on(`exit`,()=>{clearTimeout(n),e()});try{t.kill(`SIGTERM`)}catch{clearTimeout(n),e()}})}async isServerRunning(e){try{let t=new AbortController,n=setTimeout(()=>t.abort(),3e3),r=await fetch(e,{method:`HEAD`,signal:t.signal});return clearTimeout(n),r.status>=200&&r.status<400}catch{return!1}}parseCommand(e){let t=[],n=``,r=!1,i=``;for(let a of e)(a===`"`||a===`'`)&&!r?(r=!0,i=a):a===i&&r?(r=!1,i=``):a===` `&&!r?n&&=(t.push(n),``):n+=a;return n&&t.push(n),t}sleep(e){return new Promise(t=>setTimeout(t,e))}getCurrentUrl(){return this.currentUrl}cleanupSync(){if(this.serverProcess){try{this.serverProcess.kill(`SIGKILL`)}catch{}this.serverProcess=null,this.currentUrl=null}}};dr=j([(0,d.injectable)(),k(`design:paramtypes`,[])],dr);const fr=T.z.object({type:T.z.string(),text:T.z.string().optional(),data:T.z.string().optional(),mimeType:T.z.string().optional()}),pr=T.z.object({traceId:T.z.string().regex(/^[0-9a-f]{32}$/i).optional(),parentSpanId:T.z.string().regex(/^[0-9a-f]{16}$/i).optional()}),mr=T.z.object({content:T.z.array(fr),isError:T.z.boolean().optional()}),hr=T.z.object({type:T.z.literal(`task:push`),id:T.z.string(),payload:T.z.object({taskId:T.z.string(),tool:T.z.string(),arguments:T.z.record(T.z.string(),T.z.unknown()),pageId:T.z.string().optional(),telemetry:pr.optional()})}),gr=T.z.object({type:T.z.literal(`page:created`),payload:T.z.object({pageId:T.z.string(),browserId:T.z.string(),url:T.z.string().optional()})}),_r=T.z.object({type:T.z.literal(`page:removed`),payload:T.z.object({pageId:T.z.string()})}),vr=T.z.object({type:T.z.literal(`session:ack`),id:T.z.string(),payload:T.z.object({sessionId:T.z.string(),controlMode:T.z.string()})}),yr=T.z.object({type:T.z.literal(`pong`)}),br=T.z.object({type:T.z.literal(`error`),payload:T.z.object({message:T.z.string(),code:T.z.string().optional()})});T.z.discriminatedUnion(`type`,[hr,gr,_r,vr,yr,br]);const xr=T.z.object({type:T.z.literal(`session:register`),id:T.z.string(),payload:T.z.object({browserId:T.z.string(),tabId:T.z.number().optional(),url:T.z.string().optional()})}),Sr=T.z.object({type:T.z.literal(`task:result`),payload:T.z.object({taskId:T.z.string(),success:T.z.boolean(),result:mr.optional(),error:T.z.string().optional()})}),Cr=T.z.object({type:T.z.literal(`tab:mapped`),payload:T.z.object({pageId:T.z.string(),tabId:T.z.number()})}),wr=T.z.object({type:T.z.literal(`ping`)}),Tr=T.z.object({type:T.z.literal(`heartbeat`),payload:T.z.object({sessionId:T.z.string(),tabId:T.z.number().optional(),url:T.z.string().optional()})}),Er=T.z.discriminatedUnion(`type`,[xr,Sr,Cr,wr,Tr]);function Dr(e){let t=Er.safeParse(e);return t.success?t.data:null}let Or=class{connections=new Map;browserConnections=new Map;eventHandlers={};maxConnections=100;setEventHandlers(e){this.eventHandlers=e}addConnection(e,t){if(this.connections.size>=this.maxConnections)throw Error(`Maximum connections (${this.maxConnections}) reached`);let n=crypto.randomUUID(),r={id:n,ws:e,browserId:t,connectedAt:new Date,lastMessageAt:new Date};return this.connections.set(n,r),this.browserConnections.has(t)||this.browserConnections.set(t,new Set),this.browserConnections.get(t).add(n),console.log(`[WebSocketHub] Connection added: ${n} for browser ${t}`),n}removeConnection(e){let t=this.connections.get(e);if(!t)return;this.eventHandlers.onDisconnect?.(t);let n=this.browserConnections.get(t.browserId);n&&(n.delete(e),n.size===0&&this.browserConnections.delete(t.browserId)),this.connections.delete(e),console.log(`[WebSocketHub] Connection removed: ${e}`)}getConnection(e){return this.connections.get(e)}getConnectionsByBrowser(e){let t=this.browserConnections.get(e);return t?Array.from(t).map(e=>this.connections.get(e)).filter(e=>e!==void 0):[]}hasConnection(e){return this.browserConnections.has(e)&&(this.browserConnections.get(e)?.size??0)>0}reassignConnection(e,t){let n=this.connections.get(e);if(!n)return!1;let r=n.browserId;if(r===t)return!0;let i=this.browserConnections.get(r);return i&&(i.delete(e),i.size===0&&this.browserConnections.delete(r)),this.browserConnections.has(t)||this.browserConnections.set(t,new Set),this.browserConnections.get(t).add(e),n.browserId=t,!0}handleMessage(e,t){let n=this.connections.get(e);if(!n){console.warn(`[WebSocketHub] Message from unknown connection: ${e}`);return}n.lastMessageAt=new Date;try{let r=typeof t==`string`?t:new TextDecoder().decode(t),i=JSON.parse(r),a=Dr(i);if(!a){console.warn(`[WebSocketHub] Invalid message format from ${e}:`,i),this.sendError(n,`Invalid message format`);return}this.routeMessage(n,a)}catch(t){console.error(`[WebSocketHub] Error parsing message from ${e}:`,t),this.sendError(n,`Failed to parse message`)}}routeMessage(e,t){switch(t.type){case`session:register`:this.eventHandlers.onSessionRegister?.(e,t);break;case`task:result`:this.eventHandlers.onTaskResult?.(e,t);break;case`tab:mapped`:this.eventHandlers.onTabMapped?.(e,t);break;case`heartbeat`:this.eventHandlers.onHeartbeat?.(e,t);break;case`ping`:this.sendToConnection(e,{type:`pong`});break}}sendToConnection(e,t){try{return e.ws.send(JSON.stringify(t)),!0}catch(t){return console.error(`[WebSocketHub] Failed to send to ${e.id}:`,t),!1}}sendError(e,t,n){this.sendToConnection(e,{type:`error`,payload:{message:t,code:n}})}pushTask(e,t){let n=this.getConnectionsByBrowser(e);if(n.length===0)return console.warn(`[WebSocketHub] No connections for browser ${e}`),!1;let r=n[0];return this.sendToConnection(r,t)}broadcastPageCreated(e,t,n){let r={type:`page:created`,payload:{pageId:t,browserId:e,url:n}},i=this.getConnectionsByBrowser(e);for(let e of i)this.sendToConnection(e,r)}broadcastPageRemoved(e,t){let n={type:`page:removed`,payload:{pageId:t}},r=this.getConnectionsByBrowser(e);for(let e of r)this.sendToConnection(e,n)}sendSessionAck(e,t,n,r){e.sessionId=n;let i={type:`session:ack`,id:t,payload:{sessionId:n,controlMode:r}};this.sendToConnection(e,i)}getStats(){return{totalConnections:this.connections.size,browserCount:this.browserConnections.size,connections:Array.from(this.connections.values()).map(e=>({id:e.id,browserId:e.browserId,connectedAt:e.connectedAt}))}}closeAll(){for(let[e,t]of this.connections){try{t.ws.close()}catch{}this.removeConnection(e)}}};Or=j([(0,d.injectable)()],Or);let $=class{constructor(e,t,n){this.pageRegistry=e,this.browserService=t,this.toolExecutor=n}getPage(e){return this.pageRegistry.get(e)}resolvePage(e){let t=this.pageRegistry.get(e);if(!t)throw Error(`Page "${e}" not found`);if(!t.page)throw Error(`Page "${e}" is in extension mode and has no Playwright page`);return t.page}resolvePageEntry(e){let t=this.pageRegistry.get(e);if(!t)throw Error(`Page "${e}" not found`);return t}success(e){return{content:[{type:`text`,text:e}]}}successJson(e){return{content:[{type:`text`,text:JSON.stringify(e,null,2)}]}}successImage(e,t=`image/png`){return{content:[{type:`image`,data:e,mimeType:t}]}}error(e){return{content:[{type:`text`,text:e}],isError:!0}}async safeExecute(e){try{return await e()}catch(e){let t=e instanceof Error?e.message:String(e);return this.error(t)}}async executeWithMode(e,t,n,r){return this.toolExecutor.execute(e,t,n,r)}};$=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],$);var kr;const Ar=L.extend({pageId:T.z.string().describe(`Page ID to operate on`),limit:T.z.number().int().min(1).max(10).optional().default(5).describe(`Maximum number of candidates to return`)});async function jr(e,t,n=5){return(await Nr(e,t.frame)).evaluate(({selector:e,limit:t})=>{let n=e=>(e??``).replace(/\s+/g,` `).trim(),r=(e,t,r=!1)=>{if(!t)return!1;let i=n(e),a=n(t);return!i||!a?!1:r?i===a:i.toLowerCase().includes(a.toLowerCase())},i=e=>{if(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement){if(typeof e.labels?.length==`number`&&e.labels.length>0)return n(e.labels[0]?.textContent);if(e.id){let t=document.querySelector(`label[for="${CSS.escape(e.id)}"]`);if(t?.textContent)return n(t.textContent)}}let t=e.closest(`label`);return t?.textContent?n(t.textContent):null},a=e=>{let t=e.getAttribute(`aria-label`);if(t)return n(t);let r=e.getAttribute(`aria-labelledby`);if(r){let e=r.split(/\s+/).map(e=>n(document.getElementById(e)?.textContent)).filter(Boolean);if(e.length>0)return e.join(` `)}return i(e)||(e instanceof HTMLInputElement&&e.placeholder?n(e.placeholder):n(e.textContent))},o=e=>{let t=e.getAttribute(`role`);if(t)return t;if(e instanceof HTMLInputElement)return{button:`button`,checkbox:`checkbox`,email:`textbox`,number:`spinbutton`,password:`textbox`,radio:`radio`,range:`slider`,search:`searchbox`,submit:`button`,tel:`textbox`,text:`textbox`,url:`textbox`}[(e.type||`text`).toLowerCase()]||`textbox`;let n=e.tagName.toLowerCase();return{a:e.hasAttribute(`href`)?`link`:`none`,button:`button`,img:`img`,option:`option`,select:`combobox`,textarea:`textbox`}[n]||`none`},s=new Set([`html`,`body`,`head`,`script`,`style`,`noscript`,`template`,`meta`,`link`]),c=t=>{if(s.has(t.tagName.toLowerCase()))return null;let c=e.exact??!1,l=a(t),u=i(t),d=t.getAttribute(`placeholder`),f=n(t.textContent),p=o(t),m=t.getAttribute(`data-testid`)||t.getAttribute(`data-test-id`),h=t.getBoundingClientRect(),g=h.width>0&&h.height>0,_=!(t instanceof HTMLButtonElement||t instanceof HTMLInputElement||t instanceof HTMLSelectElement||t instanceof HTMLTextAreaElement)||!t.disabled,v=0,y=0,b=[],x=p!==`none`||t instanceof HTMLButtonElement||t instanceof HTMLInputElement||t instanceof HTMLSelectElement||t instanceof HTMLTextAreaElement||t instanceof HTMLAnchorElement&&t.hasAttribute(`href`);return e.role&&p===e.role&&(v+=30,y+=1,b.push(`role match`)),e.name&&(r(l,e.name,!0)?(v+=60,y+=1,b.push(`exact accessible name match`)):r(l,e.name,c)&&(v+=35,y+=1,b.push(`accessible name match`))),e.label&&(r(u,e.label,!0)?(v+=55,y+=1,b.push(`exact label match`)):r(u,e.label,c)&&(v+=30,y+=1,b.push(`label match`))),e.placeholder&&r(d,e.placeholder,c)&&(v+=25,y+=1,b.push(`placeholder match`)),e.text&&r(f,e.text,c)&&(v+=20,y+=1,b.push(`text match`)),e.testId&&m===e.testId&&(v+=50,y+=1,b.push(`test id match`)),x&&(v+=8,b.push(`interactive element`)),g&&(v+=10,b.push(`visible`)),_&&(v+=5,b.push(`enabled`)),y===0?null:{score:v,reasons:b,candidate:{tagName:t.tagName.toLowerCase(),id:t.id||``,className:t.getAttribute(`class`)||``,role:p,accessibleName:l,label:u,placeholder:d,testId:m,textContent:f.slice(0,500),visible:g,enabled:_,rect:{x:h.x,y:h.y,width:h.width,height:h.height}}}},l=Array.from(document.querySelectorAll(`*`)).map(e=>c(e)).filter(e=>e!==null&&e.score>0).sort((e,t)=>t.score-e.score).slice(0,t);return{totalCandidates:l.length,candidates:l}},{selector:t,limit:n})}let Mr=class extends ${static{kr=this}static TOOL_NAME=`browser_resolve_locator`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return Ar}getDefinition(){return{name:kr.TOOL_NAME,description:`Returns ranked locator candidates for a selector intent. Use this after a failed click/fill to recover using semantic matches instead of brittle guesses.`,inputSchema:T.z.toJSONSchema(Ar,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>this.executeWithMode(kr.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId),n=this.buildSelector(e),r=await jr(t,n,e.limit??5);return this.successJson({pageId:e.pageId,selector:n,...r})}))}buildSelector(e){return{uid:e.uid,role:e.role,name:e.name,label:e.label,placeholder:e.placeholder,testId:e.testId,text:e.text,exact:e.exact,selector:e.selector,xpath:e.xpath,frame:e.frame}}};Mr=kr=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],Mr);async function Nr(e,t){if(!t)return e;let n=await(await e.locator(t).first().elementHandle())?.contentFrame();if(!n)throw Error(`Frame not found: ${t}`);return n}var Pr;const Fr=L.extend({pageId:T.z.string().describe(`Optional page ID to override browser current page`),clickCount:T.z.number().optional().default(1).describe(`Number of clicks (default: 1, use 2 for double-click)`),button:T.z.enum([`left`,`middle`,`right`]).optional().default(`left`).describe(`Mouse button to use`),modifiers:T.z.array(T.z.enum([`Alt`,`Control`,`Meta`,`Shift`])).optional().describe(`Modifier keys to hold during click`),delay:T.z.number().optional().describe(`Time to wait between mousedown and mouseup in ms`),position:T.z.object({x:T.z.number(),y:T.z.number()}).optional().describe(`Position offset within the element`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds (default: ${P})`)});let Ir=class extends ${static{Pr=this}static TOOL_NAME=`browser_click`;constructor(e,t,n,r){super(e,t,n),this.elementLocator=r}getInputSchema(){return Fr}getDefinition(){return{name:Pr.TOOL_NAME,description:`Clicks on an element on the page. Supports CSS selector, XPath, text content, or accessibility snapshot UID for element identification.`,inputSchema:T.z.toJSONSchema(Fr,{reused:`inline`})}}async execute(e){return this.executeWithMode(Pr.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId),n=this.buildSelector(e);try{return await(await this.elementLocator.locate(t,n)).click({clickCount:e.clickCount,button:e.button,modifiers:e.modifiers,delay:e.delay,position:e.position,timeout:e.timeout}),this.success(`Clicked element successfully`)}catch(r){let i=r instanceof Error?r.message:String(r);if(!i.includes(`Element not found`))throw r;let a=await jr(t,n,5);return this.error(JSON.stringify({error:i,pageId:e.pageId,selector:n,...a},null,2))}})}buildSelector(e){return{uid:e.uid,role:e.role,name:e.name,label:e.label,placeholder:e.placeholder,testId:e.testId,text:e.text,exact:e.exact,selector:e.selector,xpath:e.xpath,frame:e.frame}}};Ir=Pr=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ElementLocatorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],Ir);var Lr;const Rr=T.z.object({browserId:T.z.string().describe(`The browser ID to close (returned by browser_launch)`)});let zr=class extends ${static{Lr=this}static TOOL_NAME=`browser_close`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return Rr}getDefinition(){return{name:Lr.TOOL_NAME,description:`Closes a browser instance by its ID. Terminates the browser process, cleans up all associated pages, and releases resources. Works for all modes: playwright, extension, vm (Docker), and stealth.`,inputSchema:T.z.toJSONSchema(Rr,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.browserService.getBrowser(e.browserId);if(!t)return this.error(`Browser "${e.browserId}" not found`);let n=t.pageIds.size,r=t.mode??`playwright`;return await this.browserService.closeBrowser(e.browserId),this.successJson({closedBrowserId:e.browserId,mode:r,pagesRemoved:n,message:`Browser "${e.browserId}" closed successfully`})})}};zr=Lr=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],zr);var Br;const Vr=T.z.object({pageId:T.z.string().describe(`Page ID to close`)});let Hr=class extends ${static{Br=this}static TOOL_NAME=`browser_close_page`;constructor(e,t,n,r){super(e,t,n),this.profileService=r}getInputSchema(){return Vr}getDefinition(){return{name:Br.TOOL_NAME,description:`Closes a page (tab) by its ID. Removes the page from the session and registry. If closed page was current, another page becomes current.`,inputSchema:T.z.toJSONSchema(Vr,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.pageRegistry.get(e.pageId);if(!t)return this.error(`Page "${e.pageId}" not found`);let{browserId:n,profileName:r,mode:i}=t,a=this.browserService.getBrowser(n);if(!a)return this.error(`Browser "${n}" not found`);let o=a.currentPageId===e.pageId,s=a.pageIds.size===1;if(i===`extension`||i===`vm`){let t=await this.executeWithMode(Br.TOOL_NAME,e.pageId,e,async()=>this.error(`Page "${e.pageId}" is not in extension mode`));if(t.isError)return t;if(await this.browserService.finalizeExtensionRecording(n,e.pageId),this.pageRegistry.remove(e.pageId),a.pageIds.delete(e.pageId),a.currentPageId===e.pageId&&(a.currentPageId=Array.from(a.pageIds)[0]??null),s)try{await this.browserService.closeBrowser(n)}catch(e){console.error(`Failed to close extension browser "${n}" after last tab shutdown:`,e)}return this.successJson({closedPageId:e.pageId,browserId:n,wasCurrentPage:o,newCurrentPageId:a.currentPageId,browserKeptOpen:!s,message:s?`Page "${e.pageId}" closed. Browser "${n}" was also closed because no tabs remained.`:`Page "${e.pageId}" closed successfully`})}if(!t.page)return this.error(`Page "${e.pageId}" is in extension mode and cannot be closed via Playwright`);await t.page.close();let c=this.browserService.getBrowser(n)?.currentPageId??null;if(s){if(r&&a.context)try{let e=await a.context.storageState();await this.profileService.saveStorageState(r,e)}catch(e){console.error(`Failed to auto-save profile "${r}":`,e)}if(!r)try{await this.browserService.closeBrowser(n)}catch(e){console.error(`Failed to auto-close browser "${n}":`,e)}}return this.successJson({closedPageId:e.pageId,browserId:n,wasCurrentPage:o,newCurrentPageId:c,browserKeptOpen:s&&!!r,message:s&&r?`Page "${e.pageId}" closed. Browser kept open for profile "${r}" reuse.`:`Page "${e.pageId}" closed successfully`})})}};Hr=Br=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ProfileService)),k(`design:paramtypes`,[Object,Object,Object,Object])],Hr);var Ur;const Wr=T.z.object({name:T.z.string().describe(`Unique profile name`),browserType:T.z.enum([`chromium`,`firefox`,`webkit`]).optional().default(`chromium`).describe(`Preferred browser type for the profile`),viewport:T.z.object({width:T.z.number(),height:T.z.number()}).optional(),userAgent:T.z.string().optional(),locale:T.z.string().optional(),timezone:T.z.string().optional(),colorScheme:T.z.enum([`light`,`dark`,`no-preference`]).optional()});let Gr=class{static{Ur=this}static TOOL_NAME=`browser_create_profile`;constructor(e){this.profileService=e}getInputSchema(){return Wr}getDefinition(){return{name:Ur.TOOL_NAME,description:`Creates a browser profile for persistent browser settings and session reuse.`,inputSchema:T.z.toJSONSchema(Wr,{reused:`inline`})}}async execute(e){try{let t=await this.profileService.create({name:e.name,browserType:e.browserType??`chromium`,viewport:e.viewport,userAgent:e.userAgent,locale:e.locale,timezone:e.timezone,colorScheme:e.colorScheme});return{content:[{type:`text`,text:JSON.stringify(t,null,2)}]}}catch(e){return{content:[{type:`text`,text:e instanceof Error?e.message:String(e)}],isError:!0}}}};Gr=Ur=j([(0,d.injectable)(),A(0,(0,d.inject)(O.ProfileService)),k(`design:paramtypes`,[Object])],Gr);var Kr;const qr=T.z.object({name:T.z.string().describe(`Name of the profile to delete`)});let Jr=class{static{Kr=this}static TOOL_NAME=`browser_delete_profile`;constructor(e){this.profileService=e}getInputSchema(){return qr}getDefinition(){return{name:Kr.TOOL_NAME,description:`Deletes a browser profile and all its associated storage state data.`,inputSchema:T.z.toJSONSchema(qr,{reused:`inline`})}}async execute(e){try{return await this.profileService.delete(e.name),{content:[{type:`text`,text:JSON.stringify({success:!0,message:`Profile "${e.name}" deleted successfully`},null,2)}]}}catch(e){return{content:[{type:`text`,text:`Error: ${e instanceof Error?e.message:`Unknown error`}`}],isError:!0}}}};Jr=Kr=j([(0,d.injectable)(),A(0,(0,d.inject)(O.ProfileService)),k(`design:paramtypes`,[Object])],Jr);var Yr;const Xr=T.z.object({basePath:T.z.string().describe(`Absolute path to search for e2e projects (required)`),projectName:T.z.string().optional().describe(`Filter by project name (e.g., 'boomlink-e2e')`),specPattern:T.z.string().optional().describe(`Filter specs by name pattern (supports * and ? wildcards, e.g., "auth*", "*.spec.ts")`),includeMetadata:T.z.boolean().optional().default(!0).describe(`Include metadata like hasArgsSchema detection (default: true)`)});let Zr=class{static{Yr=this}static TOOL_NAME=`discover_specs`;constructor(e){this.specDiscoveryService=e}getInputSchema(){return Xr}getDefinition(){return{name:Yr.TOOL_NAME,description:`Discovers e2e projects (directories with playwright.config.ts) and their spec files. Use to find available specs before running them.`,inputSchema:T.z.toJSONSchema(Xr,{reused:`inline`})}}async execute(e){if(!e.basePath||!(0,f.isAbsolute)(e.basePath))return{content:[{type:`text`,text:`Error: basePath must be an absolute path (e.g., /Users/me/project)`}],isError:!0};try{let t=e.basePath,n=e.includeMetadata!==!1;if(e.projectName||e.specPattern){let r=await this.specDiscoveryService.filterSpecs(t,{projectName:e.projectName,specPattern:e.specPattern}),i={filter:{basePath:t,projectName:e.projectName,specPattern:e.specPattern},totalSpecs:r.length,specs:r.map(e=>({name:e.name,path:e.path,relativePath:e.relativePath,...n&&{hasArgsSchema:e.hasArgsSchema}}))};return{content:[{type:`text`,text:JSON.stringify(i,null,2)}]}}let r=await this.specDiscoveryService.discoverProjects(t),i={basePath:t,totalProjects:r.length,totalSpecs:r.reduce((e,t)=>e+t.specs.length,0),projects:r.map(e=>({name:e.name,path:e.path,configPath:e.configPath,testDir:e.testDir,specCount:e.specs.length,specs:e.specs.map(e=>({name:e.name,path:e.path,relativePath:e.relativePath,...n&&{hasArgsSchema:e.hasArgsSchema}}))}))};return{content:[{type:`text`,text:JSON.stringify(i,null,2)}]}}catch(e){return{content:[{type:`text`,text:`Error discovering specs: ${e instanceof Error?e.message:`Unknown error`}`}],isError:!0}}}};Zr=Yr=j([(0,d.injectable)(),A(0,(0,d.inject)(O.SpecDiscoveryService)),k(`design:paramtypes`,[Object])],Zr);var Qr;const $r=L.extend({x:T.z.number().optional().describe(`X coordinate`),y:T.z.number().optional().describe(`Y coordinate`)}),ei=T.z.object({pageId:T.z.string().describe(`Page ID to operate on`),source:L.describe(`Source element selector`),target:$r.describe(`Target element selector or coordinates`),force:T.z.boolean().optional().default(!1).describe(`Bypass actionability checks`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let ti=class extends ${static{Qr=this}static TOOL_NAME=`browser_drag`;constructor(e,t,n,r){super(e,t,n),this.elementLocator=r}getInputSchema(){return ei}getDefinition(){return{name:Qr.TOOL_NAME,description:`Drags an element to a target location or element.`,inputSchema:T.z.toJSONSchema(ei,{reused:`inline`})}}async execute(e){return this.executeWithMode(Qr.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId),n=await this.elementLocator.locate(t,e.source);if(`x`in e.target&&`y`in e.target)await n.dragTo(t.locator(`body`),{targetPosition:{x:e.target.x,y:e.target.y},force:e.force,timeout:e.timeout});else{let r=await this.elementLocator.locate(t,e.target);await n.dragTo(r,{force:e.force,timeout:e.timeout})}return this.success(`Dragged element successfully`)})}};ti=Qr=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ElementLocatorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],ti);var ni;const ri=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),geolocation:T.z.object({latitude:T.z.number().min(-90).max(90),longitude:T.z.number().min(-180).max(180),accuracy:T.z.number().min(0).optional()}).optional().describe(`Geolocation coordinates to emulate`),locale:T.z.string().optional().describe(`Locale to emulate (e.g., "en-US", "de-DE")`),timezone:T.z.string().optional().describe(`Timezone to emulate (e.g., "America/New_York", "Europe/London")`),userAgent:T.z.string().optional().describe(`User agent string to use`),offline:T.z.boolean().optional().describe(`Whether to emulate offline mode`),colorScheme:T.z.enum([`light`,`dark`,`no-preference`]).optional().describe(`Color scheme preference`),reducedMotion:T.z.enum([`reduce`,`no-preference`]).optional().describe(`Reduced motion preference`)});let ii=class extends ${static{ni=this}static TOOL_NAME=`browser_emulate`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return ri}getDefinition(){return{name:ni.TOOL_NAME,description:`Emulates device characteristics like geolocation, timezone, locale, color scheme, and network conditions.`,inputSchema:T.z.toJSONSchema(ri,{reused:`inline`})}}async execute(e){return this.executeWithMode(ni.TOOL_NAME,e.pageId,e,async()=>this.safeExecute(async()=>{let t=this.resolvePageEntry(e.pageId),n=t.context,r=t.page;if(!n||!r)return this.error(`Page "${e.pageId}" is in extension mode and cannot be emulated via Playwright`);let i={};return e.geolocation&&(await n.setGeolocation(e.geolocation),i.geolocation=e.geolocation),e.offline!==void 0&&(await n.setOffline(e.offline),i.offline=e.offline),e.colorScheme&&(await r.emulateMedia({colorScheme:e.colorScheme}),i.colorScheme=e.colorScheme),e.reducedMotion&&(await r.emulateMedia({reducedMotion:e.reducedMotion}),i.reducedMotion=e.reducedMotion),e.locale&&(i.locale=e.locale,i.localeNote=`Locale changes may require a new browser context to take full effect`),e.timezone&&(i.timezone=e.timezone,i.timezoneNote=`Timezone changes may require a new browser context to take full effect`),e.userAgent&&(i.userAgent=e.userAgent,i.userAgentNote=`User agent changes may require a new browser context to take full effect`),this.successJson({success:!0,pageId:t.id,appliedSettings:i})}))}};ii=ni=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],ii);var ai;const oi=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),script:T.z.string().describe(`JavaScript code to execute. Can be a function body or expression (e.g., "document.title" or "() => window.location.href")`),arg:T.z.unknown().optional().describe(`Optional argument to pass to the script if it is a function`)});let si=class extends ${static{ai=this}static TOOL_NAME=`browser_evaluate_script`;static INLINE_SIZE_THRESHOLD=1e4;constructor(e,t,n){super(e,t,n)}getInputSchema(){return oi}getDefinition(){return{name:ai.TOOL_NAME,description:`Executes JavaScript code in the page context and returns the result. The script can be a function body or an expression.`,inputSchema:T.z.toJSONSchema(oi,{reused:`inline`})}}async execute(e){return this.executeWithMode(ai.TOOL_NAME,e.pageId,{script:e.script,arg:e.arg},async()=>{let t=await this.resolvePage(e.pageId).evaluate(e.script,e.arg),n;try{n=JSON.stringify(t,null,2)}catch{n=String(t)}if(Buffer.byteLength(n,`utf-8`)<=ai.INLINE_SIZE_THRESHOLD)return this.success(n);let r=`evaluate-${(0,y.randomUUID)()}.json`,i=(0,f.join)((0,h.tmpdir)(),r);return await(0,m.writeFile)(i,n,`utf-8`),this.success(`Result saved to: ${i}`)})}};si=ai=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],si);var ci;const li=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),selector:T.z.string().optional().describe(`CSS selector for the element to assert on (not required for page-level assertions like toHaveTitle, toHaveURL)`),assertion:T.z.enum([`toBeVisible`,`toBeHidden`,`toBeEnabled`,`toBeDisabled`,`toBeChecked`,`toBeEditable`,`toBeFocused`,`toBeEmpty`,`toHaveText`,`toContainText`,`toHaveValue`,`toHaveAttribute`,`toHaveClass`,`toHaveCount`,`toHaveCSS`,`toHaveId`,`toHaveTitle`,`toHaveURL`]).describe(`Type of assertion to perform`),expected:T.z.union([T.z.string(),T.z.number()]).optional().describe(`Expected value for assertions that require one (e.g., toHaveText, toHaveValue)`),attributeName:T.z.string().optional().describe(`Attribute name for toHaveAttribute or toHaveCSS assertion`),not:T.z.boolean().optional().describe(`Whether to negate the assertion (not.toBeVisible, etc.)`),timeout:T.z.number().optional().describe(`Timeout in milliseconds for the assertion (default: 5000)`)});let ui=class extends ${static{ci=this}static TOOL_NAME=`browser_expect`;constructor(e,t,n,r){super(e,t,n),this.extensionPageProxy=r}getInputSchema(){return li}getDefinition(){return{name:ci.TOOL_NAME,description:`Performs Playwright expect() assertions on page elements. Supports various assertion types like toBeVisible, toHaveText, toHaveValue, toBeEnabled, etc.`,inputSchema:T.z.toJSONSchema(li,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.resolvePageEntry(e.pageId),n=t.mode===`extension`?(this.extensionPageProxy.setTarget(t.id,t.browserId),this.extensionPageProxy):this.resolvePage(e.pageId),{selector:r,assertion:i,expected:a,attributeName:o,not:s,timeout:l=5e3}=e,u=[`toHaveTitle`,`toHaveURL`];if(!u.includes(i)&&!r)return this.error(`Selector is required for assertion type: ${i}`);try{if(u.includes(i)){let e=s?c.a(n).not:c.a(n);i===`toHaveTitle`?await e.toHaveTitle(a,{timeout:l}):i===`toHaveURL`&&await e.toHaveURL(a,{timeout:l})}else{let e=n.locator(r),t=s?c.a(e).not:c.a(e);switch(i){case`toBeVisible`:await t.toBeVisible({timeout:l});break;case`toBeHidden`:await t.toBeHidden({timeout:l});break;case`toBeEnabled`:await t.toBeEnabled({timeout:l});break;case`toBeDisabled`:await t.toBeDisabled({timeout:l});break;case`toBeChecked`:await t.toBeChecked({timeout:l});break;case`toBeEditable`:await t.toBeEditable({timeout:l});break;case`toBeFocused`:await t.toBeFocused({timeout:l});break;case`toBeEmpty`:await t.toBeEmpty({timeout:l});break;case`toHaveText`:await t.toHaveText(a,{timeout:l});break;case`toContainText`:await t.toContainText(a,{timeout:l});break;case`toHaveValue`:await t.toHaveValue(a,{timeout:l});break;case`toHaveAttribute`:if(!o)return this.error(`attributeName is required for toHaveAttribute assertion`);await t.toHaveAttribute(o,a,{timeout:l});break;case`toHaveClass`:await t.toHaveClass(a,{timeout:l});break;case`toHaveCount`:await t.toHaveCount(a,{timeout:l});break;case`toHaveCSS`:if(!o)return this.error(`attributeName (CSS property name) is required for toHaveCSS assertion`);await t.toHaveCSS(o,a,{timeout:l});break;case`toHaveId`:await t.toHaveId(a,{timeout:l});break;default:return this.error(`Unsupported assertion type: ${i}`)}}return this.successJson({success:!0,assertion:s?`not.${i}`:i,selector:r??null,passed:!0})}catch(e){let t=e instanceof Error?e.message:`Assertion failed`;return this.successJson({success:!0,assertion:s?`not.${i}`:i,selector:r??null,passed:!1,failureMessage:t})}})}};ui=ci=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ExtensionPageProxy)),k(`design:paramtypes`,[Object,Object,Object,Object])],ui);var di;const fi=L.extend({pageId:T.z.string().describe(`Page ID to operate on`),value:T.z.string().describe(`The text value to fill into the input`),force:T.z.boolean().optional().default(!1).describe(`Bypass actionability checks`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let pi=class extends ${static{di=this}static TOOL_NAME=`browser_fill`;constructor(e,t,n,r){super(e,t,n),this.elementLocator=r}getInputSchema(){return fi}getDefinition(){return{name:di.TOOL_NAME,description:`Fills an input field with text, clearing any existing content first. Use for form inputs, textareas, and contenteditable elements.`,inputSchema:T.z.toJSONSchema(fi,{reused:`inline`})}}async execute(e){return this.executeWithMode(di.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId),n=this.buildSelector(e);try{return await(await this.elementLocator.locate(t,n)).fill(e.value,{force:e.force,timeout:e.timeout}),this.success(`Filled element with: "${e.value}"`)}catch(r){let i=r instanceof Error?r.message:String(r);if(!i.includes(`Element not found`))throw r;let a=await jr(t,n,5);return this.error(JSON.stringify({error:i,pageId:e.pageId,selector:n,...a},null,2))}})}buildSelector(e){return{uid:e.uid,role:e.role,name:e.name,label:e.label,placeholder:e.placeholder,testId:e.testId,text:e.text,exact:e.exact,selector:e.selector,xpath:e.xpath,frame:e.frame}}};pi=di=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ElementLocatorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],pi);var mi;const hi=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),requestId:T.z.string().describe(`The ID of the network request to retrieve (e.g., "req-1")`)});let gi=class extends ${static{mi=this}static TOOL_NAME=`browser_get_network_request`;constructor(e,t,n,r){super(e,t,n),this.pageMonitor=r}getInputSchema(){return hi}getDefinition(){return{name:mi.TOOL_NAME,description:`Retrieves detailed information about a specific network request by ID, including headers, body, and response data.`,inputSchema:T.z.toJSONSchema(hi,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.resolvePageEntry(e.pageId);return this.executeWithMode(mi.TOOL_NAME,t.id,e,async()=>{let n=this.pageMonitor.getNetworkRequest(t.id,e.requestId);return n?this.successJson({id:n.id,url:n.url,method:n.method,resourceType:n.resourceType,headers:n.headers,postData:n.postData,timestamp:n.timestamp.toISOString(),response:n.response?{status:n.response.status,statusText:n.response.statusText,headers:n.response.headers,timing:n.response.timing}:null}):this.error(`Network request "${e.requestId}" not found for page "${t.id}"`)})})}};gi=mi=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.PageMonitorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],gi);var _i;const vi=T.z.object({pageId:T.z.string().describe(`Page ID to operate on`),waitUntil:T.z.enum([`load`,`domcontentloaded`,`networkidle`,`commit`]).optional().default(`load`).describe(`Wait until condition`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let yi=class extends ${static{_i=this}static TOOL_NAME=`browser_go_back`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return vi}getDefinition(){return{name:_i.TOOL_NAME,description:`Navigates the browser back in history.`,inputSchema:T.z.toJSONSchema(vi,{reused:`inline`})}}async execute(e){return this.executeWithMode(_i.TOOL_NAME,e.pageId,e,async()=>{let t=await this.resolvePage(e.pageId).goBack({waitUntil:e.waitUntil,timeout:e.timeout});if(t===null)return this.success(`No previous page in history`);let n=t.status();return this.success(`Navigated back (status: ${n})`)})}};yi=_i=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],yi);var bi;const xi=T.z.object({pageId:T.z.string().describe(`Page ID to operate on`),waitUntil:T.z.enum([`load`,`domcontentloaded`,`networkidle`,`commit`]).optional().default(`load`).describe(`Wait until condition`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let Si=class extends ${static{bi=this}static TOOL_NAME=`browser_go_forward`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return xi}getDefinition(){return{name:bi.TOOL_NAME,description:`Navigates the browser forward in history.`,inputSchema:T.z.toJSONSchema(xi,{reused:`inline`})}}async execute(e){return this.executeWithMode(bi.TOOL_NAME,e.pageId,e,async()=>{let t=await this.resolvePage(e.pageId).goForward({waitUntil:e.waitUntil,timeout:e.timeout});if(t===null)return this.success(`No next page in history`);let n=t.status();return this.success(`Navigated forward (status: ${n})`)})}};Si=bi=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],Si);var Ci;const wi=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),accept:T.z.boolean().describe(`Whether to accept (true) or dismiss (false) the dialog`),promptText:T.z.string().optional().describe(`Text to enter for prompt dialogs (only used when accept is true)`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds to wait for dialog (default: ${P})`)});let Ti=class extends ${static{Ci=this}static TOOL_NAME=`browser_handle_dialog`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return wi}getDefinition(){return{name:Ci.TOOL_NAME,description:`Handles JavaScript dialogs (alert, confirm, prompt) by accepting or dismissing them. Set up a listener before triggering the action that opens the dialog.`,inputSchema:T.z.toJSONSchema(wi,{reused:`inline`})}}async execute(e){return this.safeExecute(()=>this.executeWithMode(Ci.TOOL_NAME,e.pageId,{accept:e.accept,promptText:e.promptText,timeout:e.timeout},async()=>{let t=this.resolvePage(e.pageId),n=e.timeout??18e4,r=setTimeout(()=>{t.off(`dialog`,i)},n),i=async n=>{clearTimeout(r);try{e.accept?await n.accept(e.promptText):await n.dismiss()}finally{t.off(`dialog`,i)}};return t.once(`dialog`,i),this.successJson({armed:!0,action:e.accept?`accept`:`dismiss`,timeout:n,promptText:e.promptText??null})}))}};Ti=Ci=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],Ti);var Ei;const Di=L.extend({pageId:T.z.string().describe(`Page ID to operate on`),position:T.z.object({x:T.z.number(),y:T.z.number()}).optional().describe(`Position offset within element`),modifiers:T.z.array(T.z.enum([`Alt`,`Control`,`Meta`,`Shift`])).optional().describe(`Modifier keys to hold`),force:T.z.boolean().optional().default(!1).describe(`Bypass actionability checks`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let Oi=class extends ${static{Ei=this}static TOOL_NAME=`browser_hover`;constructor(e,t,n,r){super(e,t,n),this.elementLocator=r}getInputSchema(){return Di}getDefinition(){return{name:Ei.TOOL_NAME,description:`Hovers over an element to trigger hover states, tooltips, or dropdown menus.`,inputSchema:T.z.toJSONSchema(Di,{reused:`inline`})}}async execute(e){return this.executeWithMode(Ei.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId);return await(await this.elementLocator.locate(t,e)).hover({position:e.position,modifiers:e.modifiers,force:e.force,timeout:e.timeout}),this.success(`Hovered over element successfully`)})}};Oi=Ei=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ElementLocatorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],Oi);var ki;const Ai=L.extend({pageId:T.z.string().describe(`Page ID to operate on`)});let ji=class extends ${static{ki=this}static TOOL_NAME=`browser_inspect_element`;constructor(e,t,n,r){super(e,t,n),this.elementLocator=r}getInputSchema(){return Ai}getDefinition(){return{name:ki.TOOL_NAME,description:`Inspects a matching element and returns structured metadata such as role, accessible name, label, visibility, and bounds. Use this for selector debugging and agent recovery.`,inputSchema:T.z.toJSONSchema(Ai,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>this.executeWithMode(ki.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId),n=this.buildSelector(e),r=await this.elementLocator.locateAll(t,n),i=await r.count();if(i===0)return this.successJson({pageId:e.pageId,selector:n,matched:!1,matchCount:0,element:null});let a=r.first(),[o,s,c,l]=await Promise.all([a.isVisible().catch(()=>!1),a.isEnabled().catch(()=>!1),a.boundingBox().catch(()=>null),a.evaluate(e=>{let t=e=>(e??``).replace(/\s+/g,` `).trim(),n=e=>{if(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement){if(typeof e.labels?.length==`number`&&e.labels.length>0)return t(e.labels[0]?.textContent);if(e.id){let n=document.querySelector(`label[for="${CSS.escape(e.id)}"]`);if(n?.textContent)return t(n.textContent)}}let n=e.closest(`label`);return n?.textContent?t(n.textContent):null},r=e=>{let r=e.getAttribute(`aria-label`);if(r)return t(r);let i=e.getAttribute(`aria-labelledby`);if(i){let e=i.split(/\s+/).map(e=>t(document.getElementById(e)?.textContent)).filter(Boolean);if(e.length>0)return e.join(` `)}return n(e)||(e instanceof HTMLInputElement&&e.placeholder?t(e.placeholder):e instanceof HTMLImageElement&&e.alt?t(e.alt):t(e.textContent))},i=e=>{let t=e.getAttribute(`role`);if(t)return t;if(e instanceof HTMLInputElement)return{button:`button`,checkbox:`checkbox`,email:`textbox`,number:`spinbutton`,password:`textbox`,radio:`radio`,range:`slider`,search:`searchbox`,submit:`button`,tel:`textbox`,text:`textbox`,url:`textbox`}[(e.type||`text`).toLowerCase()]||`textbox`;let n=e.tagName.toLowerCase();return{a:e.hasAttribute(`href`)?`link`:`none`,button:`button`,img:`img`,option:`option`,select:`combobox`,textarea:`textbox`}[n]||`none`},a=e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement?e.value:null;return{tagName:e.tagName.toLowerCase(),id:e.id||``,className:e.getAttribute(`class`)||``,role:i(e),accessibleName:r(e),label:n(e),placeholder:e.getAttribute(`placeholder`),testId:e.getAttribute(`data-testid`)||e.getAttribute(`data-test-id`),textContent:t(e.textContent).slice(0,500),value:a}})]),u={...l,visible:o,enabled:s,rect:c?{x:c.x,y:c.y,width:c.width,height:c.height}:null};return this.successJson({pageId:e.pageId,selector:n,matched:!0,matchCount:i,element:u})}))}buildSelector(e){return{uid:e.uid,role:e.role,name:e.name,label:e.label,placeholder:e.placeholder,testId:e.testId,text:e.text,exact:e.exact,selector:e.selector,xpath:e.xpath,frame:e.frame}}};ji=ki=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ElementLocatorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],ji);var Mi;const Ni=`about:blank`,Pi=process.env.BROWSE_TOOL_DEBUG_EXTENSION_RECORDING===`1`,Fi=`[LaunchBrowserTool]`,Ii=new Set([`docker`,`docker-vm`,`docker-chromium`,`docker-chrome-testing`]),Li=(0,_.promisify)(g.execFile);async function Ri(e){try{let{stdout:t}=await Li(`git`,[`rev-parse`,`--git-common-dir`],{cwd:e}),n=t.trim(),r=f.default.isAbsolute(n)?n:f.default.join(e,n),i=f.default.dirname(r);return i===e?null:i}catch{return null}}const zi=T.z.object({server:T.z.string().describe(`Proxy server URL (e.g., "http://proxy.example.com:8080")`),username:T.z.string().optional().describe(`Proxy username for authentication`),password:T.z.string().optional().describe(`Proxy password for authentication`),bypass:T.z.string().optional().describe(`Comma-separated list of hosts to bypass the proxy (e.g., "localhost,127.0.0.1")`)}),Bi=T.z.object({mode:T.z.enum([`playwright`,`extension`,`vm`,`stealth`]).optional().default(`playwright`).describe(`Execution mode. "playwright" uses Playwright APIs. "extension" uses Playwright with Chrome extension. "vm" launches in an isolated runtime (for example via Docker). "stealth" spawns Chrome directly without automation flags.`),browserType:T.z.enum([`chromium`,`firefox`,`webkit`]).optional().default(`chromium`).describe(`Browser type to use (only applies to playwright mode)`),headless:T.z.boolean().optional().default(!0).describe(`Run browser in headless mode (only applies to playwright mode)`),url:T.z.string().optional().describe(`Optional URL to navigate to after launch`),profileName:T.z.string().optional().describe(`Optional profile name to use for browser settings and persistent storage`),vmCommand:T.z.string().optional().describe(`Executable/alias for vm mode launch (e.g., "docker" for isolated container runtime)`),vmCommandArgs:T.z.array(T.z.string()).optional().describe(`Browser arguments for vm command runtime`),vmDockerBrowser:T.z.enum([`cloakbrowser`,`chrome-for-testing`]).optional().describe(`Docker browser runtime for vm mode. "cloakbrowser" (default) uses stealth CloakBrowser with C++ anti-detection patches. "chrome-for-testing" uses Google Chrome for Testing.`),vmBrowserId:T.z.string().optional().describe(`Optional browserId override for vm mode`),startupDelayMs:T.z.number().optional().describe(`Optional startup delay in milliseconds before returning in vm mode`),vmServerUrl:T.z.string().optional().describe(`Explicit MCP server URL reachable from inside VM (e.g., "http://host.docker.internal:3200"). Overrides registry discovery.`),vmExtensionPath:T.z.string().optional().describe(`Explicit extension path that exists inside VM filesystem. Useful when VM does not share host workspace paths.`),vmEnableVnc:T.z.boolean().optional().default(!1).describe(`Expose VNC/noVNC ports when using Docker VM runtime`),vmVncPort:T.z.number().min(1).max(65535).optional().describe(`Host port for VNC (container port 5900) when vmEnableVnc is true`),vmNoVncPort:T.z.number().min(1).max(65535).optional().describe(`Host port for noVNC web UI (container port 7900) when vmEnableVnc is true`),vmVncPassword:T.z.string().optional().describe(`Optional VNC password for Docker VM runtime`),baseURL:T.z.string().optional().describe(`Base URL for relative navigation (e.g., page.goto("/") will use this as the base)`),videoDir:T.z.string().optional().describe(`Directory to save video recordings. If provided, video will be recorded for all pages. Videos are saved when the page or context is closed.`),proxy:T.z.union([zi,T.z.literal(!0)]).optional().describe(`Proxy configuration for routing browser traffic. Pass an object for explicit proxy settings, or true to load session.yaml from the proxy config directory.`)});let Vi=class{static{Mi=this}static TOOL_NAME=`browser_launch`;constructor(e,t,n=new J){this.browserService=e,this.stealthLauncher=t,this.proxyConfigService=n}getInputSchema(){return Bi}getDefinition(){return{name:Mi.TOOL_NAME,description:`Launches a new browser instance. Returns browserId and pageId for subsequent browser operations. Use mode "extension" for bot-detection-free automation via Chrome extension, or mode "vm" for isolated runtime launch.`,inputSchema:T.z.toJSONSchema(Bi,{reused:`inline`})}}async execute(e){let t=e.mode??`playwright`;try{let n={...e,proxy:this.proxyConfigService.resolve({proxy:e.proxy,profileName:e.profileName})};return t===`stealth`?await this.launchStealthMode(n):t===`vm`?await this.launchVmMode(n):t===`extension`?await this.launchExtensionMode(n):await this.launchPlaywrightMode(n)}catch(e){return{content:[{type:`text`,text:this.formatLaunchError(e)}],isError:!0}}}formatLaunchError(e){let t=e instanceof Error?e.message:String(e);return/Browser session conflict|already in use|profile ".*" is busy/i.test(t)?`${t}\n\nThis is a browser automation session conflict, not an application failure. Use browser_list_session to see reusable browserId/pageId values, retry after the active launch completes, or launch with a different profileName.`:t}async launchPlaywrightMode(e){let t=await this.browserService.launch({browserType:e.browserType,headless:e.headless,profileName:e.profileName,baseURL:e.baseURL,recordVideo:e.videoDir?{dir:e.videoDir}:void 0,proxy:e.proxy});return e.url&&await t.page.goto(e.url),{content:[{type:`text`,text:JSON.stringify({browserId:t.browserInstance.id,pageId:t.pageId,mode:`playwright`,url:e.url??t.page.url()},null,2)}]}}async launchStealthMode(e){let t=await this.stealthLauncher.launch({url:e.url,profileName:e.profileName,proxy:e.proxy});return{content:[{type:`text`,text:JSON.stringify({browserId:t.id,pageId:`page-${t.id}`,mode:`stealth`,url:e.url??`about:blank`,pid:t.pid},null,2)}]}}async launchExtensionMode(e){Pi&&console.warn(`[ExtensionRecordingDebug] launch tool mode=extension videoDir=${e.videoDir??``} url=${e.url??``}`);let t=e.profileName?`browser-${e.profileName}`:`browser-extension-${(0,y.randomUUID)()}`,n=e.url??Ni,r=await this.resolveExtensionBootstrapServerUrl(),i=this.buildVmBootstrapUrl(r,t,n),a=await this.browserService.launchWithExtension({url:i,baseURL:e.baseURL,profileName:e.profileName,recordVideo:e.videoDir?{dir:e.videoDir}:void 0,proxy:e.proxy,mode:`extension`,browserId:t});return{content:[{type:`text`,text:JSON.stringify({browserId:a.browserInstance.id,pageId:a.pageId,mode:`extension`,url:n,pid:a.process.pid},null,2)}]}}async launchVmMode(e){let t=e.vmBrowserId??`browser-vm-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,n=e.url??Ni,r=e.vmCommand??`docker`,i=await this.resolveVmBootstrapServerUrl(e.vmServerUrl,r),a=this.buildVmBootstrapUrl(i,t,n),o=this.buildVmDockerRuntimeOptions(e),s=await this.browserService.launchWithExtension({url:a,baseURL:e.baseURL,profileName:e.profileName,recordVideo:e.videoDir?{dir:e.videoDir}:void 0,proxy:e.proxy,extensionPath:e.vmExtensionPath??process.env.PLAYWRIGHT_VM_EXTENSION_PATH,mode:`vm`,vmDockerBrowser:e.vmDockerBrowser,command:r,commandArgs:e.vmCommandArgs,dockerRunArgs:o.runArgs,dockerEnv:o.env,dockerEnableVnc:o.enableVnc,browserId:t,startupDelayMs:e.startupDelayMs}),c=o.vnc?{vncUrl:o.vnc.vncUrl,noVncUrl:o.vnc.noVncUrl}:void 0;return{content:[{type:`text`,text:JSON.stringify({browserId:s.browserInstance.id,pageId:s.pageId,mode:`vm`,url:n,pid:s.process.pid,...c?{vm:c}:{}},null,2)}]}}buildVmBootstrapUrl(e,t,n){let r=new URL(e);return r.searchParams.set(`playwright_mcp_connect`,`1`),r.searchParams.set(`playwright_mcp_server`,e),r.searchParams.set(`playwright_mcp_browser_id`,t),r.searchParams.set(`playwright_mcp_navigate`,n),r.toString()}async resolveVmBootstrapServerUrl(e,t){let n=e??process.env.PLAYWRIGHT_VM_SERVER_URL;if(n)return this.normalizeVmServerUrl(n);let r=H(),i=await this.getRegisteredPort(r);return this.isDockerVmCommand(t??`docker`)?U(`host.docker.internal`,i.port):U(i.host,i.port)}async resolveExtensionBootstrapServerUrl(){let e=H(),t=await this.getRegisteredPort(e);return U(t.host,t.port)}normalizeVmServerUrl(e){let t;try{t=new URL(e)}catch{throw Error(`Invalid vmServerUrl: "${e}"`)}if(t.protocol!==`http:`&&t.protocol!==`https:`)throw Error(`vmServerUrl must use http or https: "${e}"`);return t.origin}isDockerVmCommand(e){return Ii.has(e.trim().toLowerCase())}buildVmDockerRuntimeOptions(e){let t=e.vmEnableVnc||e.vmVncPort!==void 0||e.vmNoVncPort!==void 0,n=[],r={},i;if(t){let t=e.vmVncPort??5901,r=e.vmNoVncPort??7901;n.push(`-p`,`${t}:5900`,`-p`,`${r}:7900`),i={vncUrl:`vnc://localhost:${t}`,noVncUrl:`http://localhost:${r}/`}}return e.vmVncPassword&&e.vmVncPassword.trim().length>0&&(r.SE_VNC_PASSWORD=e.vmVncPassword.trim()),{runArgs:n.length>0?n:void 0,env:Object.keys(r).length>0?r:void 0,enableVnc:t,vnc:i}}getFallbackPort(){try{return dn()}catch{return ln}}isValidPort(e){return typeof e==`number`&&Number.isInteger(e)&&e>0&&e<=65535}async getRegisteredPort(e){let t={host:e,port:this.getFallbackPort()},n=M(),r=process.env.NODE_ENV||`development`,i=new l.PortRegistryService(process.env.PORT_REGISTRY_PATH),a={serviceName:`browse-tool-http`,serviceType:`tool`,environment:r},o=async t=>{try{let n=await i.getPort({...a,repositoryPath:t});if(n.success&&n.record&&this.isValidPort(n.record.port))return{host:n.record.host||e,port:n.record.port}}catch(e){let n=e instanceof Error?e.message:String(e);console.warn(`${Fi} Registry lookup failed (path=${t}): ${n}`)}return null},s=await o(n);if(s)return s;let c=await Ri(n);if(c){let e=await o(c);if(e)return e}if(_e())try{let t=(await i.listAllocations(a)).filter(e=>this.isValidPort(e.port)).sort((e,t)=>new Date(t.updatedAt).getTime()-new Date(e.updatedAt).getTime())[0];if(t)return{host:t.host||e,port:t.port}}catch(e){let t=e instanceof Error?e.message:String(e);console.warn(`${Fi} Global registry lookup failed: ${t}`)}return t}};Vi=Mi=j([(0,d.injectable)(),A(0,(0,d.inject)(O.BrowserService)),A(1,(0,d.inject)(O.StealthLauncher)),A(2,(0,d.inject)(O.ProxyConfigService)),k(`design:paramtypes`,[Object,Object,Object])],Vi);var Hi;const Ui=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),type:T.z.enum([`log`,`info`,`warning`,`error`,`debug`,`trace`,`dir`,`dirxml`,`table`,`count`,`assert`]).optional().describe(`Filter by message type (log, info, warning, error, debug, trace, etc.)`),limit:T.z.number().min(1).optional().describe(`Maximum number of messages to return`)});let Wi=class extends ${static{Hi=this}static TOOL_NAME=`browser_list_console_messages`;constructor(e,t,n,r){super(e,t,n),this.pageMonitor=r}getInputSchema(){return Ui}getDefinition(){return{name:Hi.TOOL_NAME,description:`Lists all console messages captured during page automation. Requires monitoring to be started first.`,inputSchema:T.z.toJSONSchema(Ui,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.resolvePageEntry(e.pageId);return this.executeWithMode(Hi.TOOL_NAME,t.id,e,async()=>{let n=this.pageMonitor.getConsoleMessages(t.id,e.type);return e.limit&&(n=n.slice(0,e.limit)),this.successJson({pageId:t.id,messageCount:n.length,messages:n.map(e=>({id:e.id,type:e.type,text:e.text,location:e.location,timestamp:e.timestamp.toISOString()}))})})})}};Wi=Hi=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.PageMonitorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],Wi);var Gi;const Ki=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),resourceType:T.z.string().optional().describe(`Filter by resource type (document, xhr, fetch, script, stylesheet, image, etc.)`),urlPattern:T.z.string().optional().describe(`Filter by URL pattern (substring match)`),method:T.z.string().optional().describe(`Filter by HTTP method (GET, POST, etc.)`),limit:T.z.number().min(1).optional().describe(`Maximum number of requests to return`)});let qi=class extends ${static{Gi=this}static TOOL_NAME=`browser_list_network_requests`;constructor(e,t,n,r){super(e,t,n),this.pageMonitor=r}getInputSchema(){return Ki}getDefinition(){return{name:Gi.TOOL_NAME,description:`Lists all network requests captured during page automation. Requires monitoring to be started first.`,inputSchema:T.z.toJSONSchema(Ki,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.resolvePageEntry(e.pageId);return this.executeWithMode(Gi.TOOL_NAME,t.id,e,async()=>{let n=this.pageMonitor.getNetworkRequests(t.id);if(e.resourceType&&(n=n.filter(t=>t.resourceType===e.resourceType)),e.urlPattern){let t=e.urlPattern;n=n.filter(e=>e.url.includes(t))}return e.method&&(n=n.filter(t=>t.method===e.method)),e.limit&&(n=n.slice(0,e.limit)),this.successJson({pageId:t.id,requestCount:n.length,requests:n.map(e=>({id:e.id,url:e.url,method:e.method,resourceType:e.resourceType,status:e.response?.status,statusText:e.response?.statusText,timing:e.response?.timing,timestamp:e.timestamp.toISOString()}))})})})}};qi=Gi=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.PageMonitorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],qi);var Ji;const Yi=T.z.object({browserId:T.z.string().describe(`Browser ID to list pages for`)});let Xi=class extends ${static{Ji=this}static TOOL_NAME=`browser_list_pages`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return Yi}getDefinition(){return{name:Ji.TOOL_NAME,description:`Lists all pages (tabs) in a browser. Returns page IDs, URLs, titles, and indicates which page is currently active.`,inputSchema:T.z.toJSONSchema(Yi,{reused:`inline`})}}async execute(e){return this.toolExecutor.executeForBrowser(Ji.TOOL_NAME,e.browserId,e,async()=>{let t=this.browserService.getBrowser(e.browserId);if(!t)return this.error(`Browser "${e.browserId}" not found`);let n=this.pageRegistry.findByBrowser(e.browserId),r=await Promise.all(n.map(async e=>{await this.pageRegistry.updateMetadata(e.id);let n=this.pageRegistry.get(e.id);return{pageId:e.id,url:n?.url??e.url,title:n?.title??e.title,isActive:e.id===t.currentPageId,browserId:e.browserId,profileName:e.profileName,createdAt:e.createdAt.toISOString()}}));return this.successJson({browserId:e.browserId,currentPageId:t.currentPageId,pageCount:r.length,pages:r})})}};Xi=Ji=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],Xi);var Zi;const Qi=T.z.object({});let $i=class{static{Zi=this}static TOOL_NAME=`browser_list_profiles`;constructor(e){this.profileService=e}getInputSchema(){return Qi}getDefinition(){return{name:Zi.TOOL_NAME,description:`Lists all available browser profiles with their settings, browser type, viewport, and timestamps.`,inputSchema:T.z.toJSONSchema(Qi,{reused:`inline`})}}async execute(){try{let e=await this.profileService.list();return{content:[{type:`text`,text:JSON.stringify({profileCount:e.length,profiles:e.map(e=>({name:e.name,browserType:e.browserType,viewport:e.viewport??null,userAgent:e.userAgent??null,locale:e.locale??null,timezone:e.timezone??null,colorScheme:e.colorScheme??null,createdAt:e.createdAt,updatedAt:e.updatedAt}))},null,2)}]}}catch(e){return{content:[{type:`text`,text:`Error: ${e instanceof Error?e.message:`Unknown error`}`}],isError:!0}}}};$i=Zi=j([(0,d.injectable)(),A(0,(0,d.inject)(O.ProfileService)),k(`design:paramtypes`,[Object])],$i);var ea;const ta=T.z.object({});let na=class{static{ea=this}static TOOL_NAME=`browser_list_snippets`;constructor(e){this.snippetService=e}getInputSchema(){return ta}getDefinition(){return{name:ea.TOOL_NAME,description:`Lists saved code snippets by name, description, and snippetPath for reuse with browser_run_code.`,inputSchema:T.z.toJSONSchema(ta,{reused:`inline`})}}async execute(){try{let e=await this.snippetService.listSnippets();return{content:[{type:`text`,text:JSON.stringify({snippetCount:e.length,snippets:e},null,2)}]}}catch(e){return{content:[{type:`text`,text:`Error: ${e instanceof Error?e.message:`Unknown error`}`}],isError:!0}}}};na=ea=j([(0,d.injectable)(),A(0,(0,d.inject)(O.CodeSnippetService)),k(`design:paramtypes`,[Object])],na);var ra;const ia=T.z.object({pageId:T.z.string().describe(`Page ID to operate on`),url:T.z.string().describe(`URL to navigate to`),waitUntil:T.z.enum([`load`,`domcontentloaded`,`networkidle`,`commit`]).optional().default(`load`).describe(`Wait until condition`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`),referer:T.z.string().optional().describe(`Referer header value`)});let aa=class extends ${static{ra=this}static TOOL_NAME=`browser_navigate`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return ia}getDefinition(){return{name:ra.TOOL_NAME,description:`Navigates the browser to a specified URL.`,inputSchema:T.z.toJSONSchema(ia,{reused:`inline`})}}async execute(e){return this.executeWithMode(ra.TOOL_NAME,e.pageId,e,async()=>{let t=(await this.resolvePage(e.pageId).goto(e.url,{waitUntil:e.waitUntil,timeout:e.timeout,referer:e.referer}))?.status()??`unknown`;return this.success(`Navigated to ${e.url} (status: ${t})`)})}};aa=ra=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],aa);var oa;const sa=T.z.object({browserId:T.z.string().optional().describe(`Browser ID to create the page in (optional - uses default browser if not specified)`),url:T.z.string().optional().describe(`Optional URL to navigate to after creation`),setAsCurrent:T.z.boolean().optional().default(!0).describe(`Whether to set as current page for the browser (default: true)`)});let ca=class extends ${static{oa=this}static TOOL_NAME=`browser_new_page`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return sa}getDefinition(){return{name:oa.TOOL_NAME,description:`Creates a new page (tab) in a browser instance. Uses the default browser if browserId is not specified, launching one if needed.`,inputSchema:T.z.toJSONSchema(sa,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=e.browserId?this.browserService.getBrowser(e.browserId):await this.browserService.getOrCreateDefaultBrowser();if(!t)return this.error(`Browser "${e.browserId}" not found`);let n=t.id,r=e.setAsCurrent!==!1;if(t.mode===`extension`||t.mode===`vm`){let i=this.pageRegistry.registerExtensionPage(n,void 0,e.url,!1),a=await this.toolExecutor.executeForBrowser(oa.TOOL_NAME,n,{...e,pageId:i},async()=>this.error(`Browser "${n}" is not in extension mode`));if(a.isError)return this.pageRegistry.remove(i),a;t.pageIds.add(i),(r||!t.currentPageId)&&(t.currentPageId=i);let o=a.content[0]?.type===`text`?a.content[0].text:void 0,s={};if(typeof o==`string`)try{s=JSON.parse(o)}catch{s={}}let c=this.pageRegistry.get(i);return c&&(c.url=s.url??c.url,c.title=s.title??c.title,c.extensionTabId=s.tabId??c.extensionTabId),this.browserService.recordBrowserActivity(n,i),this.successJson({pageId:i,url:c?.url??e.url??`about:blank`,title:c?.title??``,browserId:n,isActive:t.currentPageId===i})}let{pageId:i,page:a}=await this.browserService.newPage(n);if(e.url)try{await a.goto(e.url),await this.pageRegistry.updateMetadata(i)}catch(e){throw await a.close(),e}r&&this.browserService.setCurrentPage(n,i);let o=this.pageRegistry.get(i);return this.successJson({pageId:i,url:o?.url??a.url(),title:o?.title??``,browserId:n,isActive:r})})}};ca=oa=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],ca);var la;const ua=T.z.object({pageId:T.z.string().describe(`Page ID to operate on`),format:T.z.enum([`Letter`,`Legal`,`Tabloid`,`Ledger`,`A0`,`A1`,`A2`,`A3`,`A4`,`A5`,`A6`]).optional().default(`Letter`).describe(`Paper format`),printBackground:T.z.boolean().optional().default(!1).describe(`Print background graphics`),landscape:T.z.boolean().optional().default(!1).describe(`Landscape orientation`),scale:T.z.number().min(.1).max(2).optional().default(1).describe(`Page scale (0.1 to 2)`),marginTop:T.z.string().optional().describe(`Top margin (e.g., "1cm", "0.5in")`),marginBottom:T.z.string().optional().describe(`Bottom margin`),marginLeft:T.z.string().optional().describe(`Left margin`),marginRight:T.z.string().optional().describe(`Right margin`),filename:T.z.string().optional().describe(`Custom filename without extension`)});let da=class extends ${static{la=this}static TOOL_NAME=`browser_pdf`;constructor(e,t,n,r){super(e,t,n),this.extensionTaskQueue=r}getInputSchema(){return ua}getDefinition(){return{name:la.TOOL_NAME,description:`Generates a PDF of the page with configurable format options. Saves to temp directory and returns the file path.`,inputSchema:T.z.toJSONSchema(ua,{reused:`inline`})}}async execute(e){let t=this.getPage(e.pageId);return t?t.mode===`extension`?this.safeExecute(async()=>{let n=e.filename??`page-${(0,y.randomUUID)()}`,r=(0,f.join)((0,h.tmpdir)(),`${n}.pdf`),i=await this.extensionTaskQueue.queueTask(la.TOOL_NAME,{pageId:e.pageId,format:e.format,printBackground:e.printBackground,landscape:e.landscape,scale:e.scale,marginTop:e.marginTop,marginBottom:e.marginBottom,marginLeft:e.marginLeft,marginRight:e.marginRight,filename:n},void 0,t.browserId),a=JSON.parse(i.result?.content?.[0]?.text??`{}`);if(!a.pdfBase64)throw Error(`Extension mode PDF generation returned no data`);return await(0,m.writeFile)(r,Buffer.from(a.pdfBase64,`base64`)),this.success(`PDF saved to: ${r}`)}):this.executeWithMode(la.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId),n=e.filename??`page-${(0,y.randomUUID)()}`,r=(0,f.join)((0,h.tmpdir)(),`${n}.pdf`);return await t.pdf({path:r,format:e.format??`Letter`,printBackground:e.printBackground??!1,landscape:e.landscape??!1,scale:e.scale??1,margin:{top:e.marginTop,bottom:e.marginBottom,left:e.marginLeft,right:e.marginRight}}),this.success(`PDF saved to: ${r}`)}):this.error(`Page "${e.pageId}" not found`)}};da=la=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ExtensionTaskQueue)),k(`design:paramtypes`,[Object,Object,Object,Object])],da);var fa;const pa=T.z.object({pageId:T.z.string().describe(`Page ID to operate on`),key:T.z.string().describe(`Key or key combination (e.g., "Enter", "Tab", "Control+a", "Meta+Shift+t", "ArrowDown")`),element:L.optional().describe(`Optional element to focus before pressing key`),delay:T.z.number().optional().describe(`Delay between keydown and keyup in ms`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let ma=class extends ${static{fa=this}static TOOL_NAME=`browser_press_key`;constructor(e,t,n,r){super(e,t,n),this.elementLocator=r}getInputSchema(){return pa}getDefinition(){return{name:fa.TOOL_NAME,description:`Presses a keyboard key or key combination. Supports modifier keys like Control, Shift, Alt, Meta.`,inputSchema:T.z.toJSONSchema(pa,{reused:`inline`})}}async execute(e){return this.executeWithMode(fa.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId);return e.element?await(await this.elementLocator.locate(t,e.element)).press(e.key,{delay:e.delay,timeout:e.timeout}):await t.keyboard.press(e.key,{delay:e.delay}),this.success(`Pressed key: "${e.key}"`)})}};ma=fa=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ElementLocatorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],ma);var ha;const ga=T.z.object({pageId:T.z.string().describe(`Page ID to operate on`),waitUntil:T.z.enum([`load`,`domcontentloaded`,`networkidle`,`commit`]).optional().default(`load`).describe(`Wait until condition`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let _a=class extends ${static{ha=this}static TOOL_NAME=`browser_reload`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return ga}getDefinition(){return{name:ha.TOOL_NAME,description:`Reloads the current page.`,inputSchema:T.z.toJSONSchema(ga,{reused:`inline`})}}async execute(e){return this.executeWithMode(ha.TOOL_NAME,e.pageId,e,async()=>{let t=(await this.resolvePage(e.pageId).reload({waitUntil:e.waitUntil,timeout:e.timeout}))?.status()??`unknown`;return this.success(`Page reloaded (status: ${t})`)})}};_a=ha=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],_a);var va;const ya=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),width:T.z.number().min(1).describe(`Viewport width in pixels`),height:T.z.number().min(1).describe(`Viewport height in pixels`),deviceScaleFactor:T.z.number().min(1).max(3).optional().describe(`Device scale factor (default: 1)`),isMobile:T.z.boolean().optional().describe(`Whether the meta viewport tag is taken into account`),hasTouch:T.z.boolean().optional().describe(`Whether the viewport supports touch events`)});let ba=class extends ${static{va=this}static TOOL_NAME=`browser_resize_page`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return ya}getDefinition(){return{name:va.TOOL_NAME,description:`Resizes the viewport of a page to specified width and height dimensions.`,inputSchema:T.z.toJSONSchema(ya,{reused:`inline`})}}async execute(e){return this.executeWithMode(va.TOOL_NAME,e.pageId,{width:e.width,height:e.height},async()=>{let t=this.resolvePage(e.pageId);if(typeof t.setViewportSize!=`function`)return this.error(`Page "${e.pageId}" does not support viewport resizing in the current browser mode`);await t.setViewportSize({width:e.width,height:e.height});let n=typeof t.viewportSize==`function`?t.viewportSize():void 0;return this.successJson({success:!0,viewport:{width:n?.width??e.width,height:n?.height??e.height}})})}};ba=va=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],ba);var xa;const Sa=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),code:T.z.string().optional().describe(`JavaScript code to execute. Accepts a function body with page/context/browser in scope or an async function receiving { page, context, browser }. Example body: "await page.goto('https://example.com'); return await page.title();". Example function: "async ({ page }) => await page.title()".`),snippetPath:T.z.string().optional().describe(`Relative snippet path inside the configured snippets directory. When provided, executes the saved snippet instead of inline code.`),saveAs:T.z.object({name:T.z.string().describe(`Human-readable snippet name. The filename is generated from a sanitized version of this value.`),description:T.z.string().describe(`Short description shown by browser_list_snippets.`)}).optional().describe(`Optional snippet metadata used to save the inline code into the configured snippets directory.`)});let Ca=class extends ${static{xa=this}static TOOL_NAME=`browser_run_code`;constructor(e,t,n,r,i){super(e,t,n),this.extensionPageProxy=r,this.snippetService=i}getInputSchema(){return Sa}getDefinition(){return{name:xa.TOOL_NAME,description:`Runs a Playwright code snippet. The code can be a function body with page/context/browser in scope, or an async function receiving { page, context, browser }. Return a value to include it in the response.`,inputSchema:T.z.toJSONSchema(Sa,{reused:`inline`})}}isFunctionExpressionCandidate(e){let t=e.trim();return/^(?:async\s*)?(?:\(\s*)?(?:\{\s*)?(?:page|context|browser)\b[\s\S]*=>/.test(t)||/^(?:async\s+)?function\s*\(/.test(t)}async executeInlineCode(e,t){if(this.isFunctionExpressionCandidate(e)){let n;try{n=Function(`return (${e}\n);`)()}catch{n=void 0}if(typeof n==`function`)return await n(t)}return await Function(`page`,`context`,`browser`,`return (async () => { ${e} })();`)(t.page,t.context,t.browser)}getSnippetCode(e){return this.isFunctionExpressionCandidate(e)?`return await (${e}\n)({ page, context, browser });`:e}async execute(e){return this.safeExecute(async()=>{if(!e.code&&!e.snippetPath)return this.error(`Either "code" or "snippetPath" is required`);if(e.saveAs&&!e.code)return this.error(`"saveAs" requires inline "code"`);let t=this.resolvePageEntry(e.pageId),{page:n,context:r,browser:i}=t,a=t.mode===`extension`?(this.extensionPageProxy.setTarget(t.id,t.browserId),this.extensionPageProxy):n,o=t.mode===`extension`?this.extensionPageProxy.context():r,s=t.mode===`extension`?{id:t.browserId,mode:this.browserService.getBrowser(t.browserId)?.mode??`extension`,pageId:t.id}:i,c;e.saveAs&&(c=await this.snippetService.saveSnippet({name:e.saveAs.name,description:e.saveAs.description,code:this.getSnippetCode(e.code)}));let l={page:a,context:o,browser:s},u=e.snippetPath?await this.snippetService.loadSnippet(e.snippetPath).then(e=>e.run(l)):await this.executeInlineCode(e.code,l);return u===void 0?c?this.successJson({savedSnippet:c}):this.success(`Code executed successfully`):this.successJson(c?{result:u,savedSnippet:c}:{result:u})})}};Ca=xa=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ExtensionPageProxy)),A(4,(0,d.inject)(O.CodeSnippetService)),k(`design:paramtypes`,[Object,Object,Object,Object,Object])],Ca);var wa;const Ta={width:1920,height:1080},Ea=T.z.object({specPath:T.z.string().describe(`Absolute path to the Playwright spec file to execute`),automationId:T.z.string().optional().describe(`Optional automation session ID (auto-generated if not provided)`),mode:T.z.enum([`playwright`,`extension`]).optional().default(`playwright`).describe(`Execution mode: playwright (standard) or extension (Chrome extension)`),browserId:T.z.string().optional().describe(`Browser ID to use (required for extension mode)`),pageId:T.z.string().optional().describe(`Page ID to use (required for extension mode)`),browserType:T.z.enum([`chromium`,`firefox`,`webkit`]).optional().default(`chromium`).describe(`Browser type to use (only applies to playwright mode)`),headless:T.z.boolean().optional().default(!0).describe(`Run browser in headless mode (only applies to playwright mode)`),baseURL:T.z.string().optional().describe(`Base URL for relative navigation (e.g., page.goto("/") will use this as the base)`),keepBrowserOpen:T.z.boolean().optional().default(!1).describe(`Keep browser open after spec execution completes for further interaction`),hooksPath:T.z.string().optional().describe(`Explicit absolute path to run-spec-hooks.ts file. Skips auto-discovery when provided. The source directory is injected as process.env.__HOOKS_SOURCE_DIR__ so hooks can resolve paths relative to their original location.`),env:T.z.record(T.z.string(),T.z.string()).optional().describe(`Environment variables to set before running the spec. Applied before hooks execution. Useful for passing config like API endpoints without relying on hooks file path resolution.`),testName:T.z.string().optional().describe(`Filter by exact test name`),testPattern:T.z.string().optional().describe(`Filter by regex pattern on full test title (e.g., "login.*success")`),onlyMarked:T.z.boolean().optional().default(!1).describe(`Run only tests marked with test.only()`),describeFilter:T.z.string().optional().describe(`Filter by describe block name pattern`),specArgs:T.z.record(T.z.string(),T.z.unknown()).optional().describe(`Arguments to pass to the spec (available as fixtures.specArgs)`),loadArgsFromEnv:T.z.boolean().optional().default(!1).describe(`Load specArgs from environment variables using spec's envPrefix export`),videoDir:T.z.string().optional().describe(`Absolute directory path to save video recordings. Supported in both playwright and extension modes.`)});let Da=class{static{wa=this}static TOOL_NAME=`run_spec`;constructor(e,t,n,r,i){this.automationRunner=e,this.specMetadataService=t,this.extensionSpecRunner=n,this.extensionPageProxy=r,this.specBundlerService=i}getInputSchema(){return Ea}getDefinition(){return{name:wa.TOOL_NAME,description:`Runs a Playwright spec file with optional test filtering and dynamic arguments. Supports filtering by test name, pattern, or test.only markers.`,inputSchema:T.z.toJSONSchema(Ea,{reused:`inline`})}}async execute(e){if(!(0,f.isAbsolute)(e.specPath))return{content:[{type:`text`,text:`Error: specPath must be an absolute path (e.g., /Users/me/project/tests/my-spec.ts)`}],isError:!0};if(e.hooksPath&&!(0,f.isAbsolute)(e.hooksPath))return{content:[{type:`text`,text:`Error: hooksPath must be an absolute path`}],isError:!0};if(e.videoDir&&!(0,f.isAbsolute)(e.videoDir))return{content:[{type:`text`,text:`Error: videoDir must be an absolute path`}],isError:!0};try{if(e.env)for(let[t,n]of Object.entries(e.env))process.env[t]=n;let t=this.resolveHooksPath(e.specPath,e.hooksPath),n=t?await this.runHooks(t):null;return n?.baseURL&&!e.baseURL&&(e.baseURL=n.baseURL),(e.mode??`playwright`)===`extension`?await this.executeExtensionMode(e):e.testName||e.testPattern||e.onlyMarked||e.describeFilter||e.loadArgsFromEnv?await this.executeEnhanced(e,t):await this.executeBasic(e,t)}catch(e){return{content:[{type:`text`,text:`Error: ${e instanceof Error?e.message:`Unknown error`}`}],isError:!0}}}findHooksFile(e){let t=(0,f.dirname)(e);for(let e=0;e<4;e++){let e=(0,f.join)(t,`run-spec-hooks.ts`);if((0,p.existsSync)(e))return e;let n=(0,f.dirname)(t);if(n===t)break;t=n}return null}async runHooks(e){let t=(0,f.dirname)((0,f.resolve)(e)),n=await this.specBundlerService.bundle(e);try{process.env.__HOOKS_SOURCE_DIR__=t;let e=await import(`${n.outputPath}?t=${Date.now()}`);return typeof e.setup==`function`?await e.setup():void 0}finally{process.env.__HOOKS_SOURCE_DIR__=void 0,await n.cleanup()}}resolveHooksPath(e,t){return t?(0,f.resolve)(t):this.findHooksFile(e)}async executeExtensionMode(e){if(!e.pageId||!e.browserId)return{content:[{type:`text`,text:`Error: pageId and browserId are required for extension mode`}],isError:!0};this.extensionPageProxy.setTarget(e.pageId,e.browserId);let t=e.specArgs??{};if(e.loadArgsFromEnv){let n=await this.specMetadataService.extractSpecMetadata(e.specPath);n.argsSchema&&n.envPrefix&&(t={...this.specMetadataService.parseArgsFromEnv(n.argsSchema,n.envPrefix),...t})}let n={};e.testName&&(n.testName=e.testName),e.testPattern&&(n.testPattern=e.testPattern),e.onlyMarked&&(n.onlyMarked=e.onlyMarked),e.describeFilter&&(n.describeFilter=e.describeFilter);let r=e.automationId??`ext-spec-${Date.now()}`,i=!!e.videoDir;i&&await this.extensionPageProxy.startRecording();let a;try{a=await this.extensionSpecRunner.executeSpec({specPath:e.specPath,sessionId:r,testFilter:Object.keys(n).length>0?n:void 0,specArgs:Object.keys(t).length>0?t:void 0})}finally{if(i&&e.videoDir)try{let t=await this.extensionPageProxy.stopRecording();if(t){(0,p.mkdirSync)(e.videoDir,{recursive:!0});let n=(0,f.join)(e.videoDir,`recording-${r}.webm`);(0,p.writeFileSync)(n,Buffer.from(t,`base64`)),a.videoPath=n}}catch{}}return this.formatExtensionResult(a)}async executeBasic(e,t){let n=await this.automationRunner.runSpec({specPath:e.specPath,automationId:e.automationId,browserId:e.browserId,pageId:e.pageId,keepBrowserOpen:e.keepBrowserOpen??!1,recordVideo:e.videoDir?{dir:e.videoDir,size:Ta}:void 0,browserOptions:{browserType:e.browserType,headless:e.headless,baseURL:e.baseURL},hooksPath:t??void 0});return this.formatResult(n,e.keepBrowserOpen??!1)}async executeEnhanced(e,t){let n=e.specArgs??{};if(e.loadArgsFromEnv){let t=await this.specMetadataService.extractSpecMetadata(e.specPath);t.argsSchema&&t.envPrefix&&(n={...this.specMetadataService.parseArgsFromEnv(t.argsSchema,t.envPrefix),...n})}let r={};e.testName&&(r.testName=e.testName),e.testPattern&&(r.testPattern=e.testPattern),e.onlyMarked&&(r.onlyMarked=e.onlyMarked),e.describeFilter&&(r.describeFilter=e.describeFilter);let i=await this.automationRunner.runSpecEnhanced({specPath:e.specPath,automationId:e.automationId,browserId:e.browserId,pageId:e.pageId,keepBrowserOpen:e.keepBrowserOpen??!1,recordVideo:e.videoDir?{dir:e.videoDir,size:Ta}:void 0,browserOptions:{browserType:e.browserType,headless:e.headless,baseURL:e.baseURL},testFilter:Object.keys(r).length>0?r:void 0,specArgs:Object.keys(n).length>0?n:void 0,hooksPath:t??void 0});return this.formatResult(i,e.keepBrowserOpen??!1)}formatResult(e,t){let n=e.status===`completed`?t?`Spec completed. Browser kept open for further interaction.`:`Spec completed.`:t?`Spec failed. Browser kept open for further interaction.`:`Spec failed.`,r=e.status!==`completed`||e.specResult?.success===!1;return{content:[{type:`text`,text:JSON.stringify({automationId:e.automationId,status:e.status,pageIds:e.pageIds,browserId:e.browserId,specResult:e.specResult,warnings:e.specResult?.warnings,message:n},null,2)}],...r?{isError:!0}:{}}}formatExtensionResult(e){let t=e.success?`Spec completed: ${e.passed}/${e.totalTests} tests passed`:`Spec failed: ${e.failed}/${e.totalTests} tests failed`;return{content:[{type:`text`,text:JSON.stringify({sessionId:e.sessionId,status:e.success?`completed`:`failed`,totalTests:e.totalTests,passed:e.passed,failed:e.failed,duration:e.duration,handoffOccurred:e.handoffOccurred,testResults:e.testResults,videoPath:e.videoPath,warnings:e.warnings,message:t},null,2)}],isError:!e.success}}};Da=wa=j([(0,d.injectable)(),A(0,(0,d.inject)(O.AutomationRunner)),A(1,(0,d.inject)(O.SpecMetadataService)),A(2,(0,d.inject)(O.ExtensionSpecRunner)),A(3,(0,d.inject)(O.ExtensionPageProxy)),A(4,(0,d.inject)(O.SpecBundlerService)),k(`design:paramtypes`,[Object,Object,Object,Object,Object])],Da);var Oa;const ka=T.z.object({profileName:T.z.string().describe(`Profile to save state into`),browserId:T.z.string().optional().describe(`Browser to read state from`)});let Aa=class{static{Oa=this}static TOOL_NAME=`browser_save_profile_state`;constructor(e,t,n){this.profileService=e,this.browserService=t,this.extensionPageProxy=n}getInputSchema(){return ka}getDefinition(){return{name:Oa.TOOL_NAME,description:`Saves the current browser state to the named profile for reuse across future launches.`,inputSchema:T.z.toJSONSchema(ka,{reused:`inline`})}}async execute(e){try{let t=e.browserId?this.browserService.getBrowser(e.browserId):this.browserService.getBrowserByProfile(e.profileName);if(!t)throw Error(`Browser for profile "${e.profileName}" not found`);if(t.context){let n=await t.context.storageState();await this.profileService.saveStorageState(e.profileName,n)}else{let n=t.currentPageId??[...t.pageIds][0];if(!n)throw Error(`Browser "${t.id}" has no active page`);this.extensionPageProxy.setTarget(n,t.id);let r=await this.extensionPageProxy.getStorageState();await this.profileService.saveStorageState(e.profileName,r)}return{content:[{type:`text`,text:JSON.stringify({profileName:e.profileName,browserId:t.id,saved:!0},null,2)}]}}catch(e){return{content:[{type:`text`,text:e instanceof Error?e.message:String(e)}],isError:!0}}}};Aa=Oa=j([(0,d.injectable)(),A(0,(0,d.inject)(O.ProfileService)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ExtensionPageProxy)),k(`design:paramtypes`,[Object,Object,Object])],Aa);var ja;const Ma=T.z.object({pageId:T.z.string().describe(`Page ID to operate on`),fullPage:T.z.boolean().optional().default(!1).describe(`Capture full page scrollable area`),selector:T.z.string().optional().describe(`CSS selector for element to screenshot`),clip:T.z.object({x:T.z.number().min(0).describe(`X coordinate of top-left corner`),y:T.z.number().min(0).describe(`Y coordinate of top-left corner`),width:T.z.number().min(1).describe(`Width of clipping region`),height:T.z.number().min(1).describe(`Height of clipping region`)}).optional().describe(`Clip region to capture by coordinates (cannot be used with selector)`),type:T.z.enum([`png`,`jpeg`]).optional().default(`png`).describe(`Image format`),quality:T.z.number().min(0).max(100).optional().describe(`Quality for jpeg format (0-100)`),omitBackground:T.z.boolean().optional().default(!1).describe(`Omit background for transparent PNG`),filename:T.z.string().optional().describe(`Custom filename without extension`),path:T.z.string().optional().describe(`Explicit output path for the screenshot`)});let Na=class extends ${static{ja=this}static TOOL_NAME=`browser_screenshot`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return Ma}getDefinition(){return{name:ja.TOOL_NAME,description:`Captures a screenshot of the page or specific element. Saves to temp directory and returns the file path.`,inputSchema:T.z.toJSONSchema(Ma,{reused:`inline`})}}async execute(e){return this.executeWithMode(ja.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId);if(e.clip&&e.selector)throw Error(`Cannot use both clip and selector options together`);let n=e.type??`png`,r=e.filename??`screenshot-${(0,y.randomUUID)()}`,i=e.path?.trim()?e.path:(0,f.join)((0,h.tmpdir)(),`${r}.${n}`);if(e.selector){if(typeof t.$!=`function`)return this.error(`Page "${e.pageId}" does not support selector screenshots`);let r=await t.$(e.selector);if(!r)throw Error(`Element not found: ${e.selector}`);if(typeof r.screenshot!=`function`)return this.error(`Element "${e.selector}" does not support screenshots`);await r.screenshot({path:i,type:n,omitBackground:e.omitBackground??!1,quality:n===`jpeg`?e.quality:void 0})}else{if(typeof t.screenshot!=`function`)return this.error(`Page "${e.pageId}" does not support screenshots in the current browser mode`);await t.screenshot({path:i,type:n,fullPage:e.fullPage??!1,omitBackground:e.omitBackground??!1,quality:n===`jpeg`?e.quality:void 0,clip:e.clip})}return this.success(`Screenshot saved to: ${i}`)})}};Na=ja=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],Na);var Pa;const Fa=T.z.object({pageId:T.z.string().describe(`Page ID to set as current`)});let Ia=class extends ${static{Pa=this}static TOOL_NAME=`browser_select_page`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return Fa}getDefinition(){return{name:Pa.TOOL_NAME,description:`Switches the active page for a browser. Updates the current page ID so subsequent operations use this page by default.`,inputSchema:T.z.toJSONSchema(Fa,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.pageRegistry.get(e.pageId);if(!t)return this.error(`Page "${e.pageId}" not found`);let{browserId:n}=t;if(t.mode===`extension`||t.mode===`vm`){let t=await this.executeWithMode(Pa.TOOL_NAME,e.pageId,e,async()=>this.error(`Page "${e.pageId}" is not in extension mode`));if(t.isError)return t}this.browserService.setCurrentPage(n,e.pageId),this.browserService.recordBrowserActivity(n,e.pageId),await this.pageRegistry.updateMetadata(e.pageId);let r=this.pageRegistry.get(e.pageId);return this.successJson({browserId:n,pageId:e.pageId,url:r?.url??t.url,title:r?.title??t.title,message:`Page "${e.pageId}" is now the active page for browser "${n}"`})})}};Ia=Pa=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],Ia);var La;const Ra=L.extend({pageId:T.z.string().describe(`Page ID to operate on`),values:T.z.array(T.z.string()).optional().describe(`Option value(s) to select`),labels:T.z.array(T.z.string()).optional().describe(`Option label(s) to select`),indexes:T.z.array(T.z.number()).optional().describe(`Option index(es) to select`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let za=class extends ${static{La=this}static TOOL_NAME=`browser_select`;constructor(e,t,n,r){super(e,t,n),this.elementLocator=r}getInputSchema(){return Ra}getDefinition(){return{name:La.TOOL_NAME,description:`Selects option(s) from a dropdown/select element. Can select by value, label, or index.`,inputSchema:T.z.toJSONSchema(Ra,{reused:`inline`})}}async execute(e){return this.executeWithMode(La.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId),n=await this.elementLocator.locate(t,e),r=[];e.values?r=e.values.map(e=>({value:e})):e.labels?r=e.labels.map(e=>({label:e})):e.indexes&&(r=e.indexes.map(e=>({index:e})));let i=await n.selectOption(r,{timeout:e.timeout});return this.success(`Selected ${i.length} option(s): ${i.join(`, `)}`)})}};za=La=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ElementLocatorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],za);var Ba;const Va=T.z.object({pageId:T.z.string().describe(`Page ID to operate on`),root:T.z.string().optional().describe(`CSS selector for root element to snapshot (defaults to body)`),sizeThreshold:T.z.number().optional().default(1e4).describe(`Size threshold in bytes; snapshots larger than this are saved to file (default: 10000)`)});let Ha=class extends ${static{Ba=this}static TOOL_NAME=`browser_snapshot`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return Va}getDefinition(){return{name:Ba.TOOL_NAME,description:`Returns the accessibility tree snapshot of the page. Preferred for LLM processing as it provides structured, semantic information about page content.`,inputSchema:T.z.toJSONSchema(Va,{reused:`inline`})}}async execute(e){return this.executeWithMode(Ba.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId),n=await this.captureSnapshot(t,e.root);if(!n)return this.success(`No accessibility tree available for this page`);let r=e.sizeThreshold??1e4;if(Buffer.byteLength(n,`utf-8`)<=r)return this.success(n);let i=`snapshot-${(0,y.randomUUID)()}.yaml`,a=(0,f.join)((0,h.tmpdir)(),i);return await(0,m.writeFile)(a,n,`utf-8`),this.success(`Snapshot saved to: ${a}`)})}async captureSnapshot(e,t){let n=t?[t]:[`body`,`html`];for(let t of n){let n=e.locator(t).first();if(await n.count()!==0)try{return await n.ariaSnapshot()}catch{}}if(!t){await e.waitForTimeout(250);for(let t of[`body`,`html`]){let n=e.locator(t).first();if(await n.count()!==0)try{return await n.ariaSnapshot()}catch{}}}throw Error(`Root element not found: ${t??`body/html`}`)}};Ha=Ba=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],Ha);var Ua;const Wa=T.z.object({pageId:T.z.string().describe(`The page ID to record.`),path:T.z.string().optional().describe(`Optional absolute output path for the .webm file. If omitted, browse-tool creates a temporary recording path.`)});let Ga=class extends ${static{Ua=this}static TOOL_NAME=`browser_start_recording`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return Wa}getDefinition(){return{name:Ua.TOOL_NAME,description:`Starts recording an existing extension/vm browser page and saves the artifact to the provided path or a temporary .webm file.`,inputSchema:T.z.toJSONSchema(Wa,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.resolvePageEntry(e.pageId);if(t.mode!==`extension`&&t.mode!==`vm`)return this.error(`Page "${e.pageId}" is in "${t.mode}" mode and cannot use extension recording`);if(e.path){if(!f.default.isAbsolute(e.path))return this.error(`Recording path must be an absolute path (e.g., /Users/me/project/recording.webm)`);if(f.default.extname(e.path).toLowerCase()!==`.webm`)return this.error(`Recording path must end with .webm`)}let n=await this.browserService.startPageRecording(t.browserId,t.id,e.path);return this.successJson({started:!0,browserId:t.browserId,pageId:t.id,outputPath:n.outputPath})})}};Ga=Ua=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],Ga);var Ka;const qa=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),name:T.z.string().optional().describe(`Name for the trace (used in trace filename)`),screenshots:T.z.boolean().optional().describe(`Whether to capture screenshots during tracing (default: true)`),snapshots:T.z.boolean().optional().describe(`Whether to capture snapshots during tracing (default: true)`),sources:T.z.boolean().optional().describe(`Whether to include source files in the trace (default: false)`)});let Ja=class extends ${static{Ka=this}static TOOL_NAME=`browser_start_trace`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return qa}getDefinition(){return{name:Ka.TOOL_NAME,description:`Starts Playwright performance tracing with configurable options for screenshots, snapshots, and sources. Use browser_stop_trace to stop and save the trace.`,inputSchema:T.z.toJSONSchema(qa,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.resolvePageEntry(e.pageId),n=t.context;if(!n)return this.error(`Page "${e.pageId}" is in extension mode and cannot start tracing`);let{name:r,screenshots:i=!0,snapshots:a=!0,sources:o=!1}=e;return await n.tracing.start({name:r,screenshots:i,snapshots:a,sources:o}),this.successJson({success:!0,message:`Tracing started`,pageId:t.id,options:{name:r??null,screenshots:i,snapshots:a,sources:o}})})}};Ja=Ka=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],Ja);var Ya;const Xa=T.z.object({pageId:T.z.string().describe(`The page ID whose recording should be stopped.`),includeBase64:T.z.boolean().optional().describe(`Whether to include the base64-encoded WebM payload in the response.`)});let Za=class extends ${static{Ya=this}static TOOL_NAME=`browser_stop_recording`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return Xa}getDefinition(){return{name:Ya.TOOL_NAME,description:`Stops an active extension/vm browser recording and returns the saved .webm path with optional base64 payload.`,inputSchema:T.z.toJSONSchema(Xa,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.resolvePageEntry(e.pageId);if(t.mode!==`extension`&&t.mode!==`vm`)return this.error(`Page "${e.pageId}" is in "${t.mode}" mode and cannot use extension recording`);let n=await this.browserService.stopPageRecording(t.browserId,t.id,{includeBase64:e.includeBase64});return this.successJson({stopped:!0,browserId:t.browserId,pageId:t.id,outputPath:n.outputPath,fileSizeBytes:n.fileSizeBytes,...n.videoBase64?{videoBase64:n.videoBase64}:{}})})}};Za=Ya=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],Za);var Qa;const $a=T.z.object({pageId:T.z.string().describe(`Optional page ID to override browser current page`),path:T.z.string().describe(`Absolute path where the trace file should be saved (e.g., "/Users/me/project/traces/trace.zip"). Must end with .zip extension.`)});let eo=class extends ${static{Qa=this}static TOOL_NAME=`browser_stop_trace`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return $a}getDefinition(){return{name:Qa.TOOL_NAME,description:`Stops Playwright performance tracing and saves the trace file to a specified path. The trace can be viewed using Playwright Trace Viewer.`,inputSchema:T.z.toJSONSchema($a,{reused:`inline`})}}async execute(e){return this.safeExecute(async()=>{let t=this.resolvePageEntry(e.pageId),n=t.context;if(!n)return this.error(`Page "${e.pageId}" is in extension mode and cannot stop tracing`);let r=e.path;return f.default.isAbsolute(r)?r.endsWith(`.zip`)?(await n.tracing.stop({path:r}),this.successJson({success:!0,message:`Tracing stopped and saved`,pageId:t.id,tracePath:r,viewCommand:`npx playwright show-trace ${r}`})):this.error(`Trace path must end with .zip extension`):this.error(`Trace path must be an absolute path (e.g., /Users/me/project/traces/trace.zip)`)})}};eo=Qa=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],eo);var to;const no=L.extend({pageId:T.z.string().describe(`Page ID to operate on`),text:T.z.string().describe(`The text to type character by character`),delay:T.z.number().optional().default(0).describe(`Delay between key presses in ms`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let ro=class extends ${static{to=this}static TOOL_NAME=`browser_type`;constructor(e,t,n,r){super(e,t,n),this.elementLocator=r}getInputSchema(){return no}getDefinition(){return{name:to.TOOL_NAME,description:`Types text into an element character by character, simulating real keyboard input. Does not clear existing content. Use browser_fill for replacing content.`,inputSchema:T.z.toJSONSchema(no,{reused:`inline`})}}async execute(e){return this.executeWithMode(to.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId);return await(await this.elementLocator.locate(t,{selector:e.selector,xpath:e.xpath,uid:e.uid,frame:e.frame})).pressSequentially(e.text,{delay:e.delay,timeout:e.timeout}),this.success(`Typed: "${e.text}"`)})}};ro=to=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ElementLocatorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],ro);var io;const ao=L.extend({pageId:T.z.string().describe(`Page ID to operate on`),files:T.z.union([T.z.string(),T.z.array(T.z.string())]).describe(`File path(s) to upload`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let oo=class extends ${static{io=this}static TOOL_NAME=`browser_upload_file`;constructor(e,t,n,r){super(e,t,n),this.elementLocator=r}getInputSchema(){return ao}getDefinition(){return{name:io.TOOL_NAME,description:`Uploads file(s) to a file input element.`,inputSchema:T.z.toJSONSchema(ao,{reused:`inline`})}}async execute(e){return this.executeWithMode(io.TOOL_NAME,e.pageId,e,async()=>{let t=this.resolvePage(e.pageId);await(await this.elementLocator.locate(t,e)).setInputFiles(e.files,{timeout:e.timeout});let n=Array.isArray(e.files)?e.files.length:1;return this.success(`Uploaded ${n} file(s) successfully`)})}};oo=io=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),A(3,(0,d.inject)(O.ElementLocatorService)),k(`design:paramtypes`,[Object,Object,Object,Object])],oo);var so;const co=T.z.object({pageId:T.z.string().describe(`Page ID to operate on`),type:T.z.enum([`element`,`text`,`timeout`,`loadState`]).optional().describe(`Type of wait condition`),selector:T.z.string().optional().describe(`CSS selector for element wait`),text:T.z.string().optional().describe(`Text to wait for (supports regex patterns)`),duration:T.z.number().optional().describe(`Duration in milliseconds for timeout wait`),state:T.z.enum([`load`,`domcontentloaded`,`networkidle`]).optional().describe(`Load state to wait for`),elementState:T.z.enum([`attached`,`detached`,`visible`,`hidden`]).optional().default(`visible`).describe(`Element state to wait for`),timeout:T.z.number().optional().default(P).describe(`Timeout in milliseconds`)});let lo=class extends ${static{so=this}static TOOL_NAME=`browser_wait_for`;constructor(e,t,n){super(e,t,n)}getInputSchema(){return co}getDefinition(){return{name:so.TOOL_NAME,description:`Waits for a condition to be met: element visibility, text appearance, timeout duration, or page load state.`,inputSchema:T.z.toJSONSchema(co,{reused:`inline`})}}async execute(e){let t=e.type??(e.text?`text`:e.selector?`element`:e.duration===void 0?e.state?`loadState`:void 0:`timeout`);if(!t)return this.error(`Unable to infer wait type. Provide "type" or one of selector, text, duration, or state.`);if(t===`element`&&!e.selector)return this.error(`selector is required for element wait`);if(t===`text`&&!e.text)return this.error(`text is required for text wait`);if(t===`timeout`){let t=e.duration??1e3;return await new Promise(e=>setTimeout(e,t)),this.success(`Waited for ${t}ms`)}return this.executeWithMode(so.TOOL_NAME,e.pageId,{...e,type:t},async()=>{let n=this.resolvePage(e.pageId);switch(t){case`element`:if(!e.selector)throw Error(`selector is required for element wait`);return await n.locator(e.selector).waitFor({state:e.elementState??`visible`,timeout:e.timeout}),this.success(`Element "${e.selector}" is ${e.elementState??`visible`}`);case`text`:if(!e.text)throw Error(`text is required for text wait`);return await n.getByText(e.text).waitFor({state:`visible`,timeout:e.timeout}),this.success(`Text "${e.text}" appeared on page`);case`loadState`:{let t=e.state??`load`;return await n.waitForLoadState(t,{timeout:e.timeout}),this.success(`Page reached "${t}" state`)}default:throw Error(`Unknown wait type: ${t}`)}})}};lo=so=j([(0,d.injectable)(),A(0,(0,d.inject)(O.PageRegistry)),A(1,(0,d.inject)(O.BrowserService)),A(2,(0,d.inject)(O.ToolExecutor)),k(`design:paramtypes`,[Object,Object,Object])],lo);const uo=new d.ContainerModule(e=>{e.bind(O.PortRegistryService).toDynamicValue(()=>new l.PortRegistryService(process.env.PORT_REGISTRY_PATH)).inSingletonScope(),e.bind(O.ProcessRegistryService).toDynamicValue(()=>new u.ProcessRegistryService(process.env.PROCESS_REGISTRY_PATH)).inSingletonScope(),e.bind(O.HttpServerHealthCheck).to(W).inSingletonScope(),e.bind(O.HttpServerManager).to(Dn).inSingletonScope(),e.bind(O.McpPortAllocationService).to(Nn).inSingletonScope(),e.bind(O.McpSessionTracker).to(Pn).inSingletonScope()}),fo=new d.ContainerModule(e=>{e.bind(O.ProfileService).to(Rn).inSingletonScope(),e.bind(O.ProxyConfigService).to(J).inSingletonScope(),e.bind(O.PageRegistry).to(In).inSingletonScope(),e.bind(O.BrowserProcessRegistry).to(De).inSingletonScope(),e.bind(O.BrowserService).to(Et).inSingletonScope(),e.bind(O.BrowserLockManager).to(de).inSingletonScope(),e.bind(O.ChromeForTestingService).to(Ue).inSingletonScope(),e.bind(O.ElementLocatorService).to(Pt).inSingletonScope(),e.bind(O.PageMonitorService).to(Fn).inSingletonScope(),e.bind(O.PauseController).to(Ln).inSingletonScope(),e.bind(O.SpecBundlerService).to(rr).inSingletonScope(),e.bind(O.SpecDiscoveryService).to(ir).inSingletonScope(),e.bind(O.SpecMetadataService).to(ar).inSingletonScope(),e.bind(O.SetupRunner).to(Qn).inSingletonScope(),e.bind(O.WebServerManager).to(dr).inSingletonScope(),e.bind(O.SpecRunner).to(sr).inSingletonScope(),e.bind(O.AutomationRunner).to(le).inSingletonScope(),e.bind(O.TelemetryService).to(z).inSingletonScope(),e.bind(O.CodeSnippetService).toDynamicValue(()=>new jt(process.env.BROWSE_TOOL_SNIPPETS_DIR)).inSingletonScope(),e.bind(O.ExtensionTaskQueue).to(B).inSingletonScope(),e.bind(O.ExtensionToolDelegator).to(V).inSingletonScope(),e.bind(O.ToolExecutor).to(ur).inSingletonScope(),e.bind(O.ExtensionSessionRegistry).to(Vt).inSingletonScope(),e.bind(O.ExtensionPageProxy).to(zt).inSingletonScope(),e.bind(O.ExtensionSpecRunner).to(Ht).inSingletonScope(),e.bind(O.StealthLauncher).to(cr).inSingletonScope(),e.bind(O.HttpServerHealthCheck).to(W).inSingletonScope(),e.bind(O.HttpBrowserClient).to(fn).inSingletonScope(),e.bind(O.RemoteToolExecutor).to(Zn).inSingletonScope(),e.bind(O.McpPortAllocationService).to(Nn).inSingletonScope(),e.bind(O.ProcessRegistryService).toDynamicValue(()=>new u.ProcessRegistryService(process.env.PROCESS_REGISTRY_PATH)).inSingletonScope(),e.bind(O.WebSocketHub).to(Or).inSingletonScope(),e.bind(O.IdleCleanupService).to(Mn).inSingletonScope()}),po=new d.ContainerModule(e=>{e.bind(O.Tool).to(Ir).inSingletonScope(),e.bind(O.Tool).to(pi).inSingletonScope(),e.bind(O.Tool).to(ro).inSingletonScope(),e.bind(O.Tool).to(za).inSingletonScope(),e.bind(O.Tool).to(Oi).inSingletonScope(),e.bind(O.Tool).to(ti).inSingletonScope(),e.bind(O.Tool).to(ma).inSingletonScope(),e.bind(O.Tool).to(ji).inSingletonScope(),e.bind(O.Tool).to(oo).inSingletonScope(),e.bind(O.Tool).to(aa).inSingletonScope(),e.bind(O.Tool).to(yi).inSingletonScope(),e.bind(O.Tool).to(Si).inSingletonScope(),e.bind(O.Tool).to(_a).inSingletonScope(),e.bind(O.Tool).to(Mr).inSingletonScope(),e.bind(O.Tool).to(lo).inSingletonScope(),e.bind(O.Tool).to(Ha).inSingletonScope(),e.bind(O.Tool).to(Na).inSingletonScope(),e.bind(O.Tool).to(da).inSingletonScope(),e.bind(O.Tool).to(Xi).inSingletonScope(),e.bind(O.Tool).to(ca).inSingletonScope(),e.bind(O.Tool).to(Ia).inSingletonScope(),e.bind(O.Tool).to(Hr).inSingletonScope(),e.bind(O.Tool).to(Ti).inSingletonScope(),e.bind(O.Tool).to(qi).inSingletonScope(),e.bind(O.Tool).to(gi).inSingletonScope(),e.bind(O.Tool).to(Wi).inSingletonScope(),e.bind(O.Tool).to(si).inSingletonScope(),e.bind(O.Tool).to(ii).inSingletonScope(),e.bind(O.Tool).to(ba).inSingletonScope(),e.bind(O.Tool).to(ui).inSingletonScope(),e.bind(O.Tool).to(Ga).inSingletonScope(),e.bind(O.Tool).to(Za).inSingletonScope(),e.bind(O.Tool).to(Ja).inSingletonScope(),e.bind(O.Tool).to(eo).inSingletonScope(),e.bind(O.Tool).to($i).inSingletonScope(),e.bind(O.Tool).to(na).inSingletonScope(),e.bind(O.Tool).to(Gr).inSingletonScope(),e.bind(O.Tool).to(Jr).inSingletonScope(),e.bind(O.Tool).to(Aa).inSingletonScope(),e.bind(O.Tool).to(Da).inSingletonScope(),e.bind(O.Tool).to(Zr).inSingletonScope(),e.bind(O.Tool).to(Vi).inSingletonScope(),e.bind(O.Tool).to(zr).inSingletonScope(),e.bind(O.Tool).to(Ca).inSingletonScope()});function mo(){let e=new d.Container({defaultScope:`Singleton`});return e.load(uo),e}function ho(){let e=new d.Container({defaultScope:`Singleton`});return e.load(fo,po),e}const go=new d.ContainerModule(e=>{e.bind(O.PortRegistryService).toDynamicValue(()=>new l.PortRegistryService(process.env.PORT_REGISTRY_PATH)).inSingletonScope(),e.bind(O.ProfileService).to(Rn).inSingletonScope(),e.bind(O.ProxyConfigService).to(J).inSingletonScope(),e.bind(O.PageRegistry).to(In).inSingletonScope(),e.bind(O.BrowserService).to(Et).inSingletonScope(),e.bind(O.ElementLocatorService).to(Pt).inSingletonScope(),e.bind(O.PageMonitorService).to(Fn).inSingletonScope(),e.bind(O.PauseController).to(Ln).inSingletonScope(),e.bind(O.SpecBundlerService).to(rr).inSingletonScope(),e.bind(O.SpecDiscoveryService).to(ir).inSingletonScope(),e.bind(O.SpecMetadataService).to(ar).inSingletonScope(),e.bind(O.SetupRunner).to(Qn).inSingletonScope(),e.bind(O.WebServerManager).to(dr).inSingletonScope(),e.bind(O.SpecRunner).to(sr).inSingletonScope(),e.bind(O.AutomationRunner).to(le).inSingletonScope(),e.bind(O.HttpServerHealthCheck).to(W).inSingletonScope(),e.bind(O.HttpServerManager).to(Dn).inSingletonScope(),e.bind(O.HttpBrowserClient).to(fn).inSingletonScope(),e.bind(O.RemoteToolExecutor).to(Zn).inSingletonScope(),e.bind(O.McpPortAllocationService).to(Nn).inSingletonScope(),e.bind(O.ProcessRegistryService).toDynamicValue(()=>new u.ProcessRegistryService(process.env.PROCESS_REGISTRY_PATH)).inSingletonScope(),e.bind(O.ExtensionTaskQueue).to(B).inSingletonScope(),e.bind(O.ExtensionToolDelegator).to(V).inSingletonScope(),e.bind(O.ToolExecutor).to(ur).inSingletonScope(),e.bind(O.StealthLauncher).to(cr).inSingletonScope(),e.bind(O.BrowserLockManager).to(de).inSingletonScope(),e.bind(O.ExtensionSessionRegistry).to(Vt).inSingletonScope(),e.bind(O.ExtensionPageProxy).to(zt).inSingletonScope(),e.bind(O.ExtensionSpecRunner).to(Ht).inSingletonScope(),e.bind(O.McpSessionTracker).to(Pn).inSingletonScope(),e.bind(O.ChromeForTestingService).to(Ue).inSingletonScope(),e.bind(O.IdleCleanupService).to(Mn).inSingletonScope()}),_o=new d.Container({defaultScope:`Singleton`});_o.load(go,po);function vo(){let e=new d.Container({defaultScope:`Singleton`});return e.load(go,po),e}var yo=class extends Error{code=`TRANSPORT_CONNECTION_ERROR`;recovery=`Check that stdio streams are available and try again.`;constructor(e,t){super(e,t),this.name=`TransportConnectionError`}},bo=class{server;transport=null;constructor(e){this.server=e}async start(){let e=new oe.StdioServerTransport;try{await this.server.connect(e),this.transport=e,console.error(`browse-tool MCP server started on stdio`)}catch(t){try{await e.close()}catch{}let n=new yo(`Failed to establish stdio transport connection`,{cause:t});throw console.error(`[${n.code}] ${n.message}. Recovery: ${n.recovery}`),n}}async stop(){this.transport&&=(await this.transport.close(),null)}},xo=class extends Error{code=`TRANSPORT_CONNECTION_ERROR`;recovery=`Check that the HTTP host and port are available and try again.`;constructor(e,t){super(e,t),this.name=`TransportConnectionError`}};async function So(e){let t=[];for await(let n of e)t.push(typeof n==`string`?Buffer.from(n):n);if(t.length===0)return;let n=Buffer.concat(t).toString(`utf8`).trim();if(n)return JSON.parse(n)}function Co(e,t,n){e.headersSent||(e.writeHead(t,{"Content-Type":`application/json`}),e.end(JSON.stringify(n)))}var wo=class{sessions=new Map;closingSessions=new Set;get(e){return this.sessions.get(e)}set(e,t){this.sessions.set(e,t)}has(e){return this.sessions.has(e)}remove(e){this.sessions.delete(e)}isClosing(e){return this.closingSessions.has(e)}async closeSession(e){let t=this.sessions.get(e);if(t){this.closingSessions.add(e),this.sessions.delete(e),t.server.close();try{await t.onClose?.()}finally{this.closingSessions.delete(e)}}}async clear(){let e=[...this.sessions.keys()];for(let t of e)await this.closeSession(t)}},To=class{sessionFactory;config;sessionManager=new wo;httpServer=null;constructor(e,t){this.sessionFactory=e,this.config={host:t.host,port:t.port,path:t.path??`/mcp`}}async createSession(e){let t=this.sessionFactory({headers:e.headers}),n=new ce.StreamableHTTPServerTransport({sessionIdGenerator:()=>(0,y.randomUUID)(),enableJsonResponse:!0,onsessioninitialized:e=>{this.sessionManager.set(e,{server:t.server,transport:n,onClose:t.onClose})},onsessionclosed:async e=>{await this.sessionManager.closeSession(e)}});return n.onclose=()=>{n.sessionId&&(this.sessionManager.isClosing(n.sessionId)||this.sessionManager.remove(n.sessionId))},await t.server.connect(n),n}async handleMcpRequest(e,t){let n=e.headers[`mcp-session-id`],r=Array.isArray(n)?n[0]:n,i;if(e.method===`POST`)try{i=await So(e)}catch(e){Co(t,400,{jsonrpc:`2.0`,error:{code:-32700,message:e instanceof Error?e.message:`Invalid JSON body`},id:null});return}let a;if(r?a=this.sessionManager.get(r)?.transport:e.method===`POST`&&(0,ae.isInitializeRequest)(i)&&(a=await this.createSession(e)),!a){Co(t,r?404:400,{jsonrpc:`2.0`,error:{code:-32e3,message:r?`Session not found`:`Bad Request: No valid session ID provided`},id:null});return}await a.handleRequest(e,t,i)}async start(){if(this.httpServer)throw new xo(`HTTP transport server is already running`);await new Promise((e,t)=>{let n=(0,se.createServer)((e,t)=>{(async()=>{try{let n=new URL(e.url??`/`,`http://${e.headers.host??this.config.host}`).pathname;if(n===`/health`&&e.method===`GET`){Co(t,200,{status:`ok`,transport:`streamable-http`});return}if(n!==this.config.path){Co(t,404,{error:`Not found`});return}await this.handleMcpRequest(e,t)}catch(e){Co(t,500,{jsonrpc:`2.0`,error:{code:-32603,message:(e instanceof Error?e:Error(String(e))).message},id:null})}})()});n.once(`error`,e=>{t(new xo(`Failed to establish streamable HTTP transport connection`,{cause:e}))}),n.listen(this.config.port,this.config.host,()=>{this.httpServer=n,process.stderr.write(`browse-tool MCP server started on http://${this.config.host}:${this.config.port}${this.config.path}\n`),process.stderr.write(`Health check: http://${this.config.host}:${this.config.port}/health\n`),e()})})}async stop(){if(await this.sessionManager.clear(),!this.httpServer)return;let e=this.httpServer;this.httpServer=null,await new Promise((t,n)=>{e.close(e=>{if(e){n(e);return}t()})})}};Object.defineProperty(exports,`A`,{enumerable:!0,get:function(){return M}}),Object.defineProperty(exports,`C`,{enumerable:!0,get:function(){return je}}),Object.defineProperty(exports,`D`,{enumerable:!0,get:function(){return be}}),Object.defineProperty(exports,`E`,{enumerable:!0,get:function(){return N}}),Object.defineProperty(exports,`M`,{enumerable:!0,get:function(){return O}}),Object.defineProperty(exports,`N`,{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,`O`,{enumerable:!0,get:function(){return ye}}),Object.defineProperty(exports,`S`,{enumerable:!0,get:function(){return ke}}),Object.defineProperty(exports,`T`,{enumerable:!0,get:function(){return`x-browse-owner`}}),Object.defineProperty(exports,`_`,{enumerable:!0,get:function(){return nn}}),Object.defineProperty(exports,`a`,{enumerable:!0,get:function(){return ho}}),Object.defineProperty(exports,`b`,{enumerable:!0,get:function(){return at}}),Object.defineProperty(exports,`c`,{enumerable:!0,get:function(){return Xn}}),Object.defineProperty(exports,`d`,{enumerable:!0,get:function(){return ln}}),Object.defineProperty(exports,`f`,{enumerable:!0,get:function(){return U}}),Object.defineProperty(exports,`g`,{enumerable:!0,get:function(){return z}}),Object.defineProperty(exports,`h`,{enumerable:!0,get:function(){return B}}),Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return vo}}),Object.defineProperty(exports,`j`,{enumerable:!0,get:function(){return ge}}),Object.defineProperty(exports,`k`,{enumerable:!0,get:function(){return fe}}),Object.defineProperty(exports,`l`,{enumerable:!0,get:function(){return Pn}}),Object.defineProperty(exports,`m`,{enumerable:!0,get:function(){return V}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return bo}}),Object.defineProperty(exports,`o`,{enumerable:!0,get:function(){return mo}}),Object.defineProperty(exports,`p`,{enumerable:!0,get:function(){return H}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return _o}}),Object.defineProperty(exports,`s`,{enumerable:!0,get:function(){return`BROWSE_TOOL_PROXY_CONFIG_DIR`}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return To}}),Object.defineProperty(exports,`u`,{enumerable:!0,get:function(){return An}}),Object.defineProperty(exports,`v`,{enumerable:!0,get:function(){return zt}}),Object.defineProperty(exports,`w`,{enumerable:!0,get:function(){return Oe}}),Object.defineProperty(exports,`x`,{enumerable:!0,get:function(){return Le}}),Object.defineProperty(exports,`y`,{enumerable:!0,get:function(){return lt}});
|