momentic 0.0.30 → 0.0.31

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/bin/cli.js CHANGED
@@ -21,22 +21,22 @@ import{Command as Za,Option as ie}from"commander";import el from"dedent";import
21
21
  ${m}${i}</${a}>
22
22
  `}if(e.neighbors!==void 0&&e.neighbors>0&&this.parent){let m=this.parent.children.findIndex(y=>y.id===this.id),f=m>0?this.parent.children[m-1]?.serialize({...e,neighbors:0}):"",w=m<this.parent.children.length-1?this.parent.children[m+1]?.serialize({...e,neighbors:0}):"";return`${f||""}
23
23
  ${u}
24
- ${w||""}`}return u}},eo=class{constructor(e,o,r){this.root=e;this.a11yIdNodeMap=o;this.dataMomenticIdMap=r}serialize(){return this.root?this.root.serialize():""}};function oi(t){return t.name?.value?`"${t.name.value}"`:t.role?.value&&t.role.value!=="none"&&t.role.value!=="generic"?`"${t.role.value}"`:`"${t.nodeId}"`}function vr(t,e,o){if(!e&&t.parentId)throw new Error(`Got no parent for accessibility node ${t.nodeId}: ${JSON.stringify(t)}`);let r=new Zt({id:parseInt(t.nodeId),role:t.role?.value||"",name:t.name?.value||"",content:t.value?.value||"",properties:t.properties,children:[],pathFromRoot:(e?`${e.pathFromRoot} `:"")+oi(t),backendNodeID:t.backendDOMNodeId,ignoredByCDP:t.ignored}),n=t.childIds??[];for(let a of n){if(!a)continue;let l=o.get(parseInt(a));if(!l)continue;let c=vr(l,r,o);c.length&&(r.children=r.children.concat(c))}if(r.role==="StaticText"&&(r.children=[]),r.children.length===1&&r.children[0].role==="StaticText"){let a=r.name,l=r.children[0]?.name;(a===l||!l)&&(r.children=[])}let s=[];for(let a=r.children.length-1;a>=0;a--){let l=r.children[a];if(l.role!=="StaticText"){s.push(l);continue}if(a===0||r.children[a-1].role!=="StaticText"){s.push(l);continue}r.children[a-1].name+=` ${l.name}`}if(r.children=s.reverse(),r.role==="generic"&&r.children.length===1){let a=r.children[0];if(r.name&&!to.includes(a.role)&&r.name===a.name)return r.children}if(!r.isInteresting()&&t.parentId)return r.children;for(let a of r.children)a.parent=r;return[r]}function Er(t,e,o,r,n=1){t.id=n,n+=1,e.set(t.id,t),t.dataMomenticId?o.set(t.dataMomenticId,t):to.includes(t.role)||r.debug({node:t.serialize({neighbors:1,maxLevel:1})},"Node has no data-momentic-id");for(let s of t.children)n=Er(s,e,o,r,n);return n}function Ar(t,e){if(!t.root)throw new Error("a11y tree has null root");t.allNodes=t.allNodes.filter(i=>i.ignored?!i.ignoredReasons?.find(l=>Zs.includes(l.name)):!0);let o=new Map;for(let i of t.allNodes)o.set(parseInt(i.nodeId),i);let r=vr(t.root,null,o);if(r.length>1)throw new Error(`Something went horribly wrong processing the a11y tree, we got: ${JSON.stringify(r)}`);if(r.length===0)throw new rt;let n=new Map,s=new Map;return Er(r[0],n,s,e),new eo(r[0],n,s)}var Ke=(t,e)=>{e.id=t.id,e.content=t.content,e.name=t.name,e.role=t.role,e.numChildren=t.children.length,e.serializedForm=t.serialize({noID:!0,maxLevel:1,neighbors:1}),e.nodeOnlySerializedForm=t.serialize({noID:!0,noChildren:!0,noProperties:!0})},oo=(t,e)=>{let o=1;t.role===e.role&&o++;let r=["name","content"];for(let n of r){if(!t[n]?.trim())continue;let s=Qt(t[n],e[n])/Math.min(t[n].length,e[n].length);s===0?o+=2:s<=.1&&o++}if(e.numChildren!==void 0&&(t.children.length===e.numChildren&&e.numChildren>0?o++:(e.numChildren>0&&t.children.length===0||Math.abs(t.children.length-e.numChildren)>2)&&o--),e.nodeOnlySerializedForm){let n=t.serialize({noID:!0,noChildren:!0,noProperties:!0});Qt(n,e.nodeOnlySerializedForm)/Math.min(n.length,e.nodeOnlySerializedForm.length)<=.1&&o++}if(e.serializedForm){let n=t.serialize({noID:!0,maxLevel:1,neighbors:1}),s=Qt(n,e.serializedForm)/Math.min(n.length,e.serializedForm.length);s===0?o+=2:s<=.1&&o++}return o};var Ae={r:147,g:196,b:125,a:.55},Cr={showInfo:!1,showRulers:!1,showStyles:!1,showAccessibilityInfo:!1,showExtensionLines:!1,contrastAlgorithm:"aa",contentColor:Ae,paddingColor:Ae,borderColor:Ae,marginColor:Ae,eventTargetColor:Ae,shapeColor:Ae,shapeMarginColor:Ae};var B=(t=1e3)=>new Promise(e=>setTimeout(()=>e(),t));function Tr(){window.cursor||(window.cursor=document.createElement("img"),window.cursor.setAttribute("src","data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjMyIiB2aWV3Qm94PSIwIDAgMzIgMzIiIHdpZHRoPSIzMiIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48ZyBmaWxsPSJub25lIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDEwIDcpIj48cGF0aCBkPSJtNi4xNDggMTguNDczIDEuODYzLTEuMDAzIDEuNjE1LS44MzktMi41NjgtNC44MTZoNC4zMzJsLTExLjM3OS0xMS40MDh2MTYuMDE1bDMuMzE2LTMuMjIxeiIgZmlsbD0iI2ZmZiIvPjxwYXRoIGQ9Im02LjQzMSAxNyAxLjc2NS0uOTQxLTIuNzc1LTUuMjAyaDMuNjA0bC04LjAyNS04LjA0M3YxMS4xODhsMi41My0yLjQ0MnoiIGZpbGw9IiMwMDAiLz48L2c+PC9zdmc+"),window.cursor.setAttribute("id","momentic_cursor"),window.cursor.setAttribute("style","position: absolute; z-index: 99999999999; pointer-events: none; left:0; top:0; height:4%;"),window.cursor.style.filter="invert(0%) sepia(6%) saturate(24%) hue-rotate(315deg) brightness(89%) contrast(110%)",document.body.appendChild(cursor),document.onmousemove=function(t){t=t||window.event,document.getElementById("momentic_cursor").style.left=t.pageX+"px",document.getElementById("momentic_cursor").style.top=t.pageY+"px"})}function Rr(){window.globalHintManager||(window.globalHintManager=new window.HintManager),window.globalHintManager.capture()}function xr(){window.globalHintManager&&window.globalHintManager.reset()}function Ir(){let t=document.body.getElementsByTagName("*"),e=1;for(let o=0;o<t.length;o++){let r=e.toString();for(;[6].some(s=>r.includes(s.toString()));)e++,r=e.toString();let n=t[o];n?.setAttribute("data-momentic-id",`${e}`),n?.setAttribute("aria-keyshortcuts",`${e}`),e++}}var ri=new Set(["document","script","XMLHttpRequest","fetch","xhr"]),ni=new Set(["script","document"]),si=["intercom.io","googletagmanager.com","google-analytics.com","www.gstatic.com","gstatic.com","apis.google.com","sentry.io","newrelic.com","p.retool.com","m.stripe.com","m.stripe.network","js.stripe.com","assets.trybento.co","udon.trybento.co","cdn.lr-in-prod.com","r.lr-in-prod.com","content.product-usage.assembledhq.com","data.product-usage.assembledhq.com","static.zdassets.com","o.clarity.ms/collect"],ii=["api.stripe.com","supabase.co"],ai=[];async function Or(t){for(let e of ai)await t.route(e,o=>o.abort())}function ro(t){return`${t.resourceType()} ${t.method()} ${t.url()}`}function Lr(t){return t=t.replace(/^www\./,""),t}function Mr(t){return ii.some(e=>t.includes(e))}function Nr(t,e){if(!ri.has(t.resourceType()))return!1;let o=new URL(e),r=new URL(t.url());return si.some(n=>r.hostname.includes(n))?!1:ni.has(t.resourceType())||t.method()!=="GET"?!0:Lr(r.hostname).includes(Lr(o.hostname))}var Pr=()=>{window.clickListener&&document.removeEventListener("click",window.clickListener,{capture:!0}),window.clickListener=async t=>{console.log("[Momentic] Window click listener fired",t.target),await window.captureClick(t.target)},document.addEventListener("click",window.clickListener,{capture:!0})};var Dr=()=>{window.pressListener&&document.removeEventListener("keydown",window.pressListener,{capture:!0}),window.pressListener=async t=>{console.log("[Momentic] Window press listener fired"),await window.captureKeystroke({key:t.key})},document.addEventListener("keydown",window.pressListener,{capture:!0})};var wt=Ei(vi);wt.use(Ci());wt.use(Ai({provider:{id:"2captcha",token:process.env.TWO_CAPTCHA_KEY},visualFeedback:!0}));var O=class t{browser;context;page;a11yIdToNodeMap=new Map;dataMomenticIdToNodeMap=new Map;cdpClient;logger;localMode;activeFrame;transformer;mostRecentBrowserState;baseURL;constructor({browser:e,context:o,page:r,baseUrl:n,logger:s,localMode:i,cdpClient:a}){this.browser=e,this.context=o,this.cdpClient=a,this.page=r,this.baseURL=n,this.logger=s,this.localMode=!!i}static USER_AGENT=zr["Desktop Chrome"].userAgent;static VIEWPORT={width:1920,height:1080};static async init({baseUrl:e,logger:o,browserArgs:r,contextArgs:n,takeScreenshots:s,onScreenshot:i,onClose:a,localMode:l,localAppUrl:c,extensionPath:d,timeout:u=8e3}){let h={headless:!0,handleSIGTERM:!1,chromiumSandbox:!1,...r??{}},m={viewport:t.VIEWPORT,deviceScaleFactor:process.platform==="darwin"?2:1,userAgent:zr["Desktop Chrome"].userAgent,geolocation:{latitude:37.7749,longitude:-122.4194},locale:"en-US",timezoneId:"America/Los_Angeles",...n??{}},f=null,w,y;if(l){let R=bi(wi(),"momentic","chromium");w=await wt.launchPersistentContext(R,{...h,...m,ignoreDefaultArgs:["--enable-automation","--enable-strict-mixed-content-checking"],ignoreHTTPSErrors:!0,bypassCSP:!0,args:["--allow-insecure-localhost","--disable-site-isolation-for-policy","--disable-site-isolation-trials",`--unsafely-treat-insecure-origin-as-secure=${c}`,`--load-extension=${d}`,"--test-type=browser"],baseURL:e}),y=w.pages()[0],a&&y.on("close",()=>{a()})}else f=await wt.launch(h),w=await f.newContext({...m,baseURL:e}),y=await w.newPage();await Or(w);let E=new t({browser:f,context:w,page:y,baseUrl:e,logger:o,localMode:l,cdpClient:await w.newCDPSession(y)}),b=!1;(async()=>{try{await E.navigate({url:e,wrapPossibleNavigation:!1,initialNavigation:!0})}catch(R){o.error({err:R},"Failed to initialize chrome browser")}finally{b=!0}})();let v=async()=>{if(s)try{i?.({viewport:await E.viewport(),buffer:await E.screenshot()})}catch(R){o.error({err:R},"Failed to take screenshot")}};v();let C=setInterval(()=>{v()},250),j=Date.now();for(;!b&&Date.now()-j<u;)await B(250);return clearInterval(C),b||o.warn("Timeout elapsed waiting for browser to initialize - are you sure this page is accessible?"),E}async getUserPageOrFrame(){if(this.localMode&&this.activeFrame){let e=null,o=0;for(;!e&&o<5;)e=this.page.frame(this.activeFrame),await B(100),o++;if(!e)throw new T("InternalWebAgentError",`Failed to find frame ${this.activeFrame} on page ${this.page.url()}`);return e}return this.page}async initCDPSession(){let e=!1,o=2,r=async()=>{try{this.cdpClient=await this.context.newCDPSession(this.page),await this.cdpClient.send("Accessibility.enable"),await this.cdpClient.send("DOM.enable"),await this.cdpClient.send("Overlay.enable"),e=!0}catch(n){if(o>0)return this.logger.error({err:n},"Failed to initialize CDP session, re-creating CDP client"),await B(250),o--,r();throw n}};if(await Promise.race([r(),B(3e3)]),!e)throw new T("InternalWebAgentError","Timeout elapsed waiting for CDP session to initialize")}setLogger(e){this.logger=e}ping(){if(this.closed)throw new Error("Page has been closed");if(this.browser&&!this.browser.isConnected())throw new Error("Browser is not connected")}setActiveFrame(e){this.activeFrame=e}async reset(e){this.a11yIdToNodeMap.clear(),this.dataMomenticIdToNodeMap.clear();let o=this.context.pages();this.page=o[0];for(let r=1;r<o.length;r++)await o[r].close();if(e.clearCookies&&await this.context.clearCookies(),!this.page.isClosed()){if(e.clearStorage)try{await(await this.getUserPageOrFrame()).evaluate(async()=>{window.localStorage.clear(),window.sessionStorage.clear(),await indexedDB.databases().then(r=>{r.forEach(n=>{n.name&&indexedDB.deleteDatabase(n.name)})})})}catch(r){this.logger.warn({err:r},"Failed clearing site data, continuing...")}await this.navigate({url:e.url??this.baseURL,wrapPossibleNavigation:!1,initialNavigation:!0})}}async pageSetup(){try{this.localMode||await this.page.evaluate(Tr)}catch{}}async wait(e){await B(e)}async toggleHints(e){let o=await this.getUserPageOrFrame();e.state==="on"?(await o.addStyleTag({content:qt.css}),await o.addScriptTag({content:qt.js}),await o.evaluate(Rr)):await o.evaluate(xr)}async showHints(){await this.toggleHints({state:"on"});let e=async()=>{try{await this.toggleHints({state:"off"})}catch(o){this.logger.debug({err:o},"Failed to remove vision hints")}};setTimeout(()=>{e()},3e3)}async cleanup(){await this.page.close(),await this.context.close(),this.browser&&await this.browser.close()}get closed(){return this.page.isClosed()||!!this.browser&&!this.browser.isConnected()}async html(){return(await this.getUserPageOrFrame()).content()}async url(){return(await this.getUserPageOrFrame()).url()}async screenshotWithHints(e=100,o="device",r="/tmp/screenshots/test.jpg"){let n=r?.split("."),s=n?.slice(0,-1).join("."),i=n?.slice(-1)[0],a=Buffer.from("");await this.showHints();let l=await this.screenshot(e,o,r?`${s}-after-hint.${i}`:void 0);return{before:a,after:l}}async screenshot(e=100,o="device",r){let n={fullPage:!1,quality:e,scale:o,type:"jpeg",caret:"initial",path:r};return!this.localMode||!this.activeFrame?this.page.screenshot(n):this.page.locator(`iframe[name="${this.activeFrame}"]`).screenshot(n)}async viewport(){if(this.localMode&&this.activeFrame){let o=await this.page.locator(`iframe[name="${this.activeFrame}"]`).boundingBox();if(!o)throw new Error(`Failed to get bounding box for frame: ${this.activeFrame}`);return o}let e=this.page.viewportSize();if(!e)throw new Error("failed to get viewport");return e}async navigate({url:e,wrapPossibleNavigation:o=!0,initialNavigation:r=!1}){this.logger.debug(`Navigating to ${e}`);let n=Date.now(),s=async()=>{try{await(await this.getUserPageOrFrame()).goto(e,{waitUntil:"load",timeout:8e3}),this.logger.debug({url:e},`Got load event in ${Math.floor(Date.now()-n)}ms`)}catch{this.logger.warn({url:e},"Timeout elapsed waiting for page to fire load event, continuing anyways...")}};o?await this.wrapPossibleNavigation(s):await s();let i=await this.url();if(yr.has(i)&&process.env.NODE_ENV==="production")throw new T("ActionFailureError",`${e} took too long to load \u{1F61E}. Please ensure the site and your internet are working.`,{},!0);if(r&&(await this.initCDPSession(),await this.pageSetup(),await this.attachPageLoadListeners(),this.localMode))try{await this.exposeRecordingBindings()}catch(a){a instanceof Error&&a.message.includes("already registered")||this.logger.error({err:a},"Failed to expose recording bindings during navigation")}this.logger.info({url:e},"Navigation complete")}async type(e,o={}){let{clearContent:r=!0,pressKeysSequentially:n=!1}=o;r&&(process.platform==="darwin"?await this.page.keyboard.press("Meta+A"):await this.page.keyboard.press("Control+A"),await this.page.keyboard.press("Backspace")),n?await this.page.keyboard.type(e):await this.page.keyboard.insertText(e)}async clickByA11yID(e,o={}){let r=this.a11yIdToNodeMap.get(e);if(!r)throw new Error(`Could not find DOM node during click: ${e}`);let n=await this.clickUsingCDP(r,o);return await this.highlightNode(n),r.serialize({noChildren:!0,noProperties:!0,noID:!0})}async selectOptionByA11yID(e,o){let r=this.a11yIdToNodeMap.get(e);if(!r)throw new Error(`Could not find DOM node while selecting option: ${e}`);if(!r.backendNodeID)throw new Error(`Select target missing backend node id: ${r.getLogForm()}`);return await(await this.getLocatorFromBackendID(r.backendNodeID)).selectOption(o,{timeout:8e3}),await this.highlightNode(r),r.serialize({noChildren:!0,noProperties:!0,noID:!0})}async scrollIntoView(e){let o=await this.resolveCachedTargetToID(e),r=this.a11yIdToNodeMap.get(o);if(!r)throw new Error(`Could not find node in DOM with a11y id: ${o}`);if(!r.backendNodeID)throw new Error(`Focus target missing backend node id: ${r.getLogForm()}`);await(await this.getLocatorFromBackendID(r.backendNodeID)).scrollIntoViewIfNeeded({timeout:8e3})}async highlight(e){try{let o=await this.resolveCachedTargetToID(e),r=this.a11yIdToNodeMap.get(o);if(!r)throw new Error(`Could not find DOM node during highlight: ${o}`);if(!r.backendNodeID)throw new Error(`Highlight target missing backend node id: ${r.getLogForm()}`);await this.highlightNode(r)}catch(o){this.logger.warn({err:o,target:e},"Failed to highlight target")}}async highlightNode(e){try{await this.cdpClient.send("Overlay.highlightNode",{highlightConfig:Cr,backendNodeId:e.backendNodeID})}catch{this.logger.warn("Failed to add node highlight, a page navigation likely occurred. This is non-fatal for tests.")}let o=async()=>{try{await this.cdpClient.send("Overlay.hideHighlight",{backendNodeId:e.backendNodeID})}catch(r){this.logger.debug({err:r},"Failed to remove node highlight")}};setTimeout(()=>{o()},3e3)}async wrapPossibleNavigation(e,o=8e3,r=!0){let n=Date.now(),s=await this.url(),i=Date.now(),a=new Map,l=new Map,c=v=>{let C=ro(v.request());l.set(C,(l.get(C)??0)+1);let j=v.status();j>=500&&this.logger.warn({request:C,status:j},"Received 500 level response")},d=v=>{if(!Nr(v,s))return;let C=ro(v);a.set(C,(a.get(C)??0)+1),i=Date.now()};this.page.on("response",c),this.page.on("request",d);let u=[];r&&(u=this.context.pages().map(v=>v.url()));let h=!1,m=e().catch(v=>(h=!0,v instanceof Error?v:new Error(`${v}`)));await B(250);let f=async v=>{let C=await v;if(C instanceof Error)throw C;return C},w=new Set,y=!1,b=await(async()=>{for(;!h&&!(!y&&Date.now()-n>o);){if(await B(250),y=!1,w=new Set,Date.now()-i<=1250)continue;let v=!1;for(let C of a.keys())a.get(C)!==l.get(C)&&(Mr(C)&&(y=!0),v=!0,w.add(C));if(!v)return this.logger.debug({url:await this.url(),requests:JSON.stringify(Array.from(a.entries()))},`Network idle in ${Math.floor(Date.now()-n)}ms`),!0}return!h&&w.size>0&&this.logger.warn({url:await this.url(),unfinishedRequests:JSON.stringify(Array.from(w.entries()))},"Timeout elapsed waiting for network idle, continuing anyways..."),!1})();if(this.page.off("response",c),this.page.off("request",d),!b)return f(m);let M=await this.url();if(!h&&Ee(M,s)){this.logger.debug({startUrl:s,newUrl:M},"Detected url change in wrapPossibleNavigation, waiting for load state");let v=Math.max(o-(Date.now()-n),0);if(v>0)try{await(await this.getUserPageOrFrame()).waitForLoadState("load",{timeout:v})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for load state, continuing anyways...")}}if(r){let v=this.context.pages().map(C=>C.url());if(v.length>u.length)for(let C of v)C!==M&&await this.switchToPage(C)}return f(m)}async resolveCachedTargetToID(e){if(!tt(e)){let a=this.a11yIdToNodeMap.get(e.id);if(!a)throw new Error(`Resolving target failed, fresh value did not exist in node map: ${e.id}`);return Ke(a,e),e.id}let o=(await this.getA11yTree()).serialize();this.logger.debug({tree:o},"Refreshed a11y tree before resolving target");let r=this.a11yIdToNodeMap.get(e.id);if(r){let a=oo(r,e);if(a>=5)return this.logger.debug({target:e,proposedNode:r.getLogForm(),comparisonScore:a},"Resolved cached a11y target to node with exact same id"),Ke(r,e),e.id}let n=1/0,s=1/0,i;for(let a of this.a11yIdToNodeMap.values()){let l=oo(a,e);if(l>=5)return this.logger.debug({newNode:a.getLogForm(),target:e,comparisonScore:l},"Resolved cached a11y target to new node with field comparison"),Ke(a,e),a.id;if(!e.serializedForm)continue;let c=a.serialize({noID:!0,maxLevel:1,neighbors:1});if(Math.abs(c.length-e.serializedForm.length)>15)continue;let d=Si(e.serializedForm,c),u=d/Math.min(e.serializedForm.length,c.length);d<n&&u<.2&&(n=d,s=u,i=a)}if(i&&n<15)return this.logger.debug({newNode:i.getLogForm(),target:e,distance:n,ratio:s},"Resolved cached a11y target to new node with pure levenshtein distance"),Ke(i,e),i.id;throw new Error(`Could not find any relevant node given cached target: ${JSON.stringify({target:e,closestLevenshteinDistance:n,closestNode:i})}`)}async click(e,o={}){let r=await this.resolveCachedTargetToID(e);return await this.wrapPossibleNavigation(()=>this.clickByA11yID(r,o))}async hover(e){let o=await this.resolveCachedTargetToID(e),r=this.a11yIdToNodeMap.get(o);if(!r)throw new Error(`Could not find DOM node for hover: ${o}`);if(!r.backendNodeID)throw new Error(`Hover target missing backend node id: ${r.getLogForm()}`);return await(await this.getLocatorFromBackendID(r.backendNodeID)).hover({timeout:8e3}),await this.highlightNode(r),r.serialize({noChildren:!0,noProperties:!0,noID:!0})}async selectOption(e,o){let r=await this.resolveCachedTargetToID(e);return this.selectOptionByA11yID(r,o)}async press(e){await this.wrapPossibleNavigation(()=>this.page.keyboard.press(e))}async refresh(){if(this.localMode&&this.activeFrame){let o=(await this.getUserPageOrFrame()).url();await this.navigate({url:o,wrapPossibleNavigation:!0})}else try{await this.page.reload({timeout:3e3})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for page on reload, continuing anyways...")}}async getA11yTree(e=!1){let o=null,r=async()=>{let n=0,s=await this.url();for(;!o&&n<3;)try{let i=await this.getRawA11yTree(e);if(!i.root||i.allNodes.length<=1)throw new Error("No a11y tree found on page");o=Ar(i,this.logger)}catch(i){if(this.logger.error({err:i,url:s},"Error fetching a11y tree"),n===0)await B(1e3),n++;else throw new Error(`Max retries exceeded fetching a11y tree: ${i}`)}if(!o||!o.root)throw new Error("Accessibility tree appears empty");this.a11yIdToNodeMap=o.a11yIdNodeMap,this.dataMomenticIdToNodeMap=o.dataMomenticIdMap};if(await Promise.race([r(),B(8e3)]),o)return o;throw new T("ActionFailureError",`Getting accessibility tree timed out after ${8e3}ms`)}getA11yIdFromDataMomenticId(e){return this.dataMomenticIdToNodeMap.get(e)?.id}async getRawA11yTree(e=!1){let o=await this.url(),r=Date.now(),n=()=>{r=Date.now()};this.cdpClient.addListener("Accessibility.nodesUpdated",n);let s=!1,i=()=>{this.logger.info({url:o},"Load event fired on page"),s=!0,r=Date.now()};this.cdpClient.addListener("Accessibility.loadComplete",i);let a=Date.now(),l=!e;for(;!e&&Date.now()-a<3e3;){if(await B(250),!s&&Date.now()-a<1e3){process.env.NODE_ENV!=="production"&&this.logger.debug({url:o},"A11y tree not loaded yet, waiting...");continue}if(Date.now()-r>=1250){l=!1;break}this.logger.debug({url:o},"A11y tree not stable yet, waiting...")}this.logger.debug({duration:Date.now()-a,eventReceived:s,timeoutTriggered:l,skipWait:e},"A11y wait phase completed"),await(await this.getUserPageOrFrame()).evaluate(Ir);let c;if(this.localMode&&this.activeFrame){let{result:{objectId:u}}=await this.cdpClient.send("Runtime.evaluate",{expression:`document.querySelector("#${this.activeFrame}")`}),h=await this.cdpClient.send("DOM.describeNode",{objectId:u});c=(await this.cdpClient.send("Accessibility.getRootAXNode",{frameId:h.node.frameId})).node.backendDOMNodeId}else{let{node:u}=await this.cdpClient.send("Accessibility.getRootAXNode");c=u.backendDOMNodeId}let{nodes:d}=await this.cdpClient.send("Accessibility.queryAXTree",{backendNodeId:c});return this.cdpClient.removeListener("Accessibility.loadComplete",i),this.cdpClient.removeListener("Accessibility.nodesUpdated",n),{root:d[0],allNodes:d}}async clickUsingVisualCoordinates(e){let o=await this.getElementLocation(e);if(!o)throw new Error(`Could not find element location with backend node id: ${e}`);this.logger.debug({location:o},"Executing mouse click"),await this.page.mouse.click(o.centerX,o.centerY)}async getIDAttributeUsingCDP(e){await this.cdpClient.send("DOM.getDocument",{depth:0});let o=await this.cdpClient.send("DOM.requestNode",{objectId:e}),n=(await this.cdpClient.send("DOM.getAttributes",{nodeId:o.nodeId})).attributes,s=n.findIndex(i=>i===_e);if(s===-1||!n[s+1])throw new Error(`Could not find ${_e} for object ${e}`);return n[s+1]}async getLocatorFromBackendID(e){let o=await this.cdpClient.send("DOM.resolveNode",{backendNodeId:e});if(!o||!o.object.objectId)throw new Error(`Could not resolve backend node ${e}`);let r;try{r=await this.getIDAttributeUsingCDP(o.object.objectId)}catch(n){throw this.logger.error({err:n,object:JSON.stringify(o.object)},"Failed to get ID attribute"),n}return(await this.getUserPageOrFrame()).locator(`[${_e}="${r}"]`)}async clickUsingCDP(e,o={}){let r=0,n=e;for(;r<Sr;){if(!n||n.role==="RootWebArea")throw new T("ActionFailureError",`Attempted to click node with no clickable surrounding elements: ${e.getLogForm()}`);if(n.role==="StaticText"){n=n.parent;continue}let s=n.backendNodeID;if(!s){this.logger.warn({node:n.getLogForm()},"Click candidate had no backend node ID"),n=n.parent;continue}try{let i=await this.getLocatorFromBackendID(s);return o.doubleClick?await i.dblclick({timeout:8e3}):await i.click({timeout:8e3,button:o.rightClick?"right":"left",force:o.force}),n.id!==e.id&&this.logger.info({oldNode:e.getLogForm(),newNode:n.getLogForm()},"Redirected click successfully to new element"),n}catch(i){this.logger.error({err:i,node:n.getLogForm()},"Failed click or click timed out"),r++,n=n.parent}}throw new Error(`Max click redirection attempts exhausted on original element: ${e.getLogForm()}`)}async getElementLocation(e){let o=await this.cdpClient.send("DOMSnapshot.captureSnapshot",{computedStyles:[],includeDOMRects:!0,includePaintOrder:!0}),r=await this.page.evaluate(()=>window.devicePixelRatio);process.platform==="darwin"&&r===1&&(r=2);let n=o.documents[0],s=n.layout,i=n.nodes,a=i.nodeName||[],l=i.backendNodeId||[],c=s.nodeIndex,d=s.bounds,u=-1;for(let b=0;b<a.length;b++)if(l[b]===e){u=c.indexOf(b);break}if(u===-1)throw new Error(`Could not find any backend node with ID ${e}`);let[h=0,m=0,f=0,w=0]=d[u];h/=r,m/=r,f/=r,w/=r;let y=h+f/2,E=m+w/2;return{centerX:y,centerY:E}}async scroll(e,o,r,n){let s=o==="left"?-1:1,i=n==="up"?-1:1;this.activeFrame?await(await this.getUserPageOrFrame()).evaluate(([l,c,d,u])=>window.scrollTo(window.scrollX+(l??window.innerWidth)*d,window.scrollY+(c??window.innerHeight)*u),[e,r,s,i]):await this.page.mouse.wheel((e??t.VIEWPORT.width)*s,(r??t.VIEWPORT.height)*i)}async scrollUp(e){await this.scroll(0,null,e??null,"up")}async scrollDown(e){await this.scroll(0,null,e??null,"down")}async scrollLeft(e){await this.scroll(e??null,"left",0,null)}async scrollRight(e){await this.scroll(e??null,"right",0,null)}async goForward(){await this.wrapPossibleNavigation(async()=>this.localMode&&this.activeFrame?(await this.getUserPageOrFrame()).evaluate(e=>{let o=e().contentWindow;o?o.history.forward():console.error("Failed to get content window for frame")},()=>document.querySelector(`iframe[name="${this.activeFrame}"]`)):this.page.goForward({timeout:8e3}))}async goBack(){await this.wrapPossibleNavigation(async()=>this.localMode&&this.activeFrame?(await this.getUserPageOrFrame()).evaluate(e=>{let o=e().contentWindow;o?o.history.back():console.error("Failed to get content window for frame")},()=>document.querySelector(`iframe[name="${this.activeFrame}"]`)):this.page.goBack({timeout:8e3}))}async switchToPage(e){let o=this.context.pages();for(let r=0;r<o.length;r++){let n=o[r];if(n.url().includes(e)){this.logger.info(`Switching to tab ${r} with url ${n.url()}`),this.page=n,await this.pageSetup(),await this.attachPageLoadListeners();try{await n.waitForLoadState("load",{timeout:3e3})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for load state during tab switch, continuing anyways...")}await this.initCDPSession();return}}throw new Error(`Could not find page with url containing ${e}`)}async attachPageLoadListeners(){if(this.localMode)return;let e=async o=>{o.parentFrame()||await this.pageSetup()};this.page.off("framenavigated",e),this.page.on("framenavigated",e)}async setCookie(e){let o=Po(e);await this.context.addCookies([o])}async setLocalStorage(e,o){await(await this.getUserPageOrFrame()).evaluate(([n,s])=>{n&&localStorage.setItem(n,s||"")},[e,o])}async solveCaptcha(){await this.getA11yTree();let e;for(let a of this.a11yIdToNodeMap.values())if(a.role==="image"&&a.name.toLowerCase().includes("captcha")){if(!a.backendNodeID)continue;e=await this.getLocatorFromBackendID(a.backendNodeID);break}if(!e){let a=await(await this.getUserPageOrFrame()).solveRecaptchas();if(!a.captchas||!a.captchas.length)throw new Error("No captchas found on the page");return}let o=await e.screenshot({type:"jpeg",animations:"allow",quality:100}),r=await fetch("https://api.2captcha.com/createTask",{method:"POST",body:JSON.stringify({clientKey:process.env.TWO_CAPTCHA_KEY,task:{type:"ImageToTextTask",body:o.toString("base64"),case:!0},languagePool:"en"})});if(!r.ok){let a=`Captcha solver API returned error response: ${r.statusText}`;throw this.logger.error({text:await r.text()},a),new Error(a)}let{taskId:n}=await r.json(),s=Date.now(),i="";for(;Date.now()-s<6e4;){await B(2500);let a=await fetch("https://api.2captcha.com/getTaskResult",{method:"POST",body:JSON.stringify({clientKey:process.env.TWO_CAPTCHA_KEY,taskId:n})});if(!a.ok){let c=`Captcha solution API returned error response: ${a.statusText}`;throw this.logger.error({text:await a.text()},c),new Error(c)}let l=await a.json();if(l.errorId){let c=`Captcha solution API returned error ID ${l.errorId}`;throw this.logger.error(c),new Error(c)}if(l.status==="ready"){i=l.solution.text;break}}if(!i)throw new Error("Captcha solution timed out");return i}getActiveFrame(){return this.activeFrame}async exposeRecordingBindings(){await this.context.exposeBinding("captureClick",async(e,o)=>{if(!this.transformer){this.logger.warn("Click captured without transformer");return}let r,n;try{r=await o.getAttribute("id"),n=await o.getAttribute(_e)}catch(l){throw this.logger.error({err:l},"Failed to get element attributes for click processing"),l}if(this.logger.info({id:r,dataMomenticId:n},"Click captured on element"),!n){this.logger.error({url:await this.url(),element:await o.evaluate(l=>l.outerHTML)},`Could not find ${_e} for recorded click`);return}let s=this.getA11yIdFromDataMomenticId(parseInt(n));if(!s){this.logger.warn({url:await this.url(),element:await o.evaluate(l=>l.outerHTML)},"Could not find a11y id for recorded click");return}let i={id:s};await this.resolveCachedTargetToID(i),(async()=>{try{let l=this.mostRecentBrowserState??(await this.getA11yTree(!0)).serialize();await this.transformer?.recordClick(i,l)}catch(l){this.logger.error({err:l},"Failed transforming captured click")}})()},{handle:!0}),await this.context.exposeBinding("captureKeystroke",async({},{key:e})=>{this.transformer&&(this.logger.info({key:e},"Captured keypress"),this.transformer.recordKeystroke(e))})}async startRecording(e,o){this.transformer=o;let r=await this.getA11yTree(!0);this.mostRecentBrowserState=r.serialize();let n=null,s=[async()=>{this.transformer=void 0}],i=async l=>{n&&clearTimeout(n),n=setInterval(()=>{if(!this.transformer||e.aborted)return;(async()=>{let d=0;for(;d<2;)try{let u=await this.getA11yTree(!0);this.mostRecentBrowserState=u.serialize();break}catch(u){this.logger.error({err:u},"Failed to get a11y tree in frame navigation listener"),d++}})()},250),s.push(async()=>{n&&(clearInterval(n),n=null)}),await l.evaluate(Pr),s.push(async()=>{await l.evaluate(()=>{document.removeEventListener("click",window.clickListener,{capture:!0})})}),await l.evaluate(Dr),s.push(async()=>{await l.evaluate(()=>{document.removeEventListener("keydown",window.pressListener,{capture:!0})})})};await i(await this.getUserPageOrFrame());let a=async l=>{l.name().startsWith(_t)&&(e.aborted||!this.transformer||(this.logger.info("Re-adding window recording event listeners on navigation"),await i(l)))};this.page.on("framenavigated",a),s.push(async()=>{this.page.off("framenavigated",a)}),e.addEventListener("abort",async()=>{for(let l of s)try{await l()}catch(c){this.logger.warn({err:c},"Recording cleanup function failed, continuing...")}})}async getSelectOptions(e){let o=this.a11yIdToNodeMap.get(e);if(!o)throw new Error(`Could not find node with a11y id ${e}`);if(!o.backendNodeID)throw new Error(`Node with a11y id ${e} has no backend node ID`);return await(await this.getLocatorFromBackendID(o.backendNodeID)).evaluate(s=>Array.from(s.querySelectorAll("option")).map(a=>a.value))}};var Fe=async({controller:t,step:e,logger:o,advanced:r,takeScreenshots:n,...s})=>{s.onStarted?.(),t.resetHistory();let i={...e,startedAt:new Date,userAgent:O.USER_AGENT,finishedAt:new Date,results:[],status:"SUCCESS"};try{let a=0;e.commands=e.commands||[];let l=e.commands&&e.commands.length>0;for(;;){if(a>12)throw new Error(`Exceeded max number of commands per step (${12})`);if(t.isClosed())throw new Error("Cancelling remaining steps in AI action because the controller is now closed");let c,d=new Date,u;if(n){let m=await t.browser.screenshot();u=await s.onSaveScreenshot(m)}if(l){if(c=e.commands[a],!c)throw new Error(`Saved command at index ${a} is undefined.`)}else{o.info(`Generating new sub-command ${a} within AI step`);let m;try{m=await t.promptToCommand(e.type,e.text,r.disableAICaching),c=m.command}catch(w){c={type:"FAILURE",thoughts:w instanceof Error?w.message:`${w}`}}finally{if(c.type==="FAILURE")if(a===0){i.finishedAt=new Date,i.status="FAILED",i.message=c.thoughts;break}else{let w="Stopping command generation prematurely since no progress can be made";o.warn({command:c},w),c={type:"SUCCESS",thoughts:w}}}(async()=>{let w=a,y=e,E=c;if("target"in E&&E.target){await new Promise(b=>setTimeout(()=>b(),250));try{let b=await Ti(t,m.context,E.target);E.target.elementDescriptor=b,y.commands[a]=E,s.onCommandExecuted?.({commandIndex:w,command:E})}catch(b){o.error({err:b,currentIndex:w,currentStep:y},"Failed to generate element description, continuing...")}}})()}s.onCommandGenerated?.({commandIndex:a,message:Nt[c.type]||`Unknown command (${c.type})`});let h={beforeScreenshot:u,beforeUrl:await t.browser.url(),startedAt:d,viewport:await t.browser.viewport(),finishedAt:new Date,status:"SUCCESS"};o.info(`Executing sub-command ${a} within AI step: ${Ne(c)}`);try{let m=await t.executeCommand(c,r.disableAICaching,l);o.info(`AI sub-command ${a} completed successfully`),h.elementInteracted=m.elementInteracted;let f;if(n){let y=await t.browser.screenshot();f=await s.onSaveScreenshot(y)}h.afterScreenshot=f,h.afterUrl=await t.browser.url(),h.finishedAt=new Date;let w={status:"SUCCESS",startedAt:h.startedAt,finishedAt:h.finishedAt,type:"PRESET_ACTION",data:m.data,command:c,results:[h]};if(i.results.push(w),e.commands[a]=c,s.onCommandExecuted?.({commandIndex:a,output:{...w,message:c.thoughts??m.thoughts??"Successfully executed preset action."},command:c}),c.type==="SUCCESS"){i.finishedAt=new Date,i.status="SUCCESS",i.message=m.thoughts??"All commands completed.";break}if(m.succeedImmediately&&!l){i.finishedAt=new Date,i.status="SUCCESS",i.message=m.succeedImmediatelyReason,c={type:"SUCCESS",thoughts:m.succeedImmediatelyReason},e.commands.push(c),s.onCommandExecuted?.({commandIndex:a+1,output:{...w,message:i.message},command:c}),i.results.push({...w,command:c});break}}catch(m){if(l){l=!1,a=0,i.results=[];continue}let f=m instanceof Error?m.message:`${m}`;h.status="FAILED",h.message=f,h.finishedAt=new Date,h.afterUrl=await t.browser.url();let w;try{w=await t.browser.screenshot()}catch(y){o.warn({err:y},"Failed to take screenshot after error, skipping")}h.afterScreenshot=w,i.results.push({status:"FAILED",startedAt:h.startedAt,finishedAt:h.finishedAt,type:"PRESET_ACTION",command:c,results:[h],message:f}),i.status="FAILED",i.finishedAt=new Date,i.message=f;break}a++}}catch(a){i.message=a instanceof Error?a.message:`${a}`,i.finishedAt=new Date,i.status="FAILED"}return i.status==="SUCCESS"?s.onSuccess?.({message:i.message||"AI step succeeded.",startedAt:i.startedAt.getTime(),durationMs:i.finishedAt.getTime()-i.startedAt.getTime(),output:i}):s.onFailure?.({message:i.message||"AI step errored.",startedAt:i.startedAt.getTime(),durationMs:i.finishedAt.getTime()-i.startedAt.getTime(),output:i}),i};async function Ti(t,e,o){let r=o.a11yData?.id;if(!r)throw new Error("Attempted to get reverse mapping for command with no a11y id target");return t.getReverseMappedTarget(e,r,!0)}var ze=async({controller:t,step:e,advanced:o,takeScreenshots:r,...n})=>{n.onStarted?.();let s=new Date,i=await t.browser.url(),a;if(r){let l=await t.browser.screenshot();a=await n.onSaveScreenshot(l)}try{let l=await t.executePresetStep(e.command,o.disableAICaching),c;if(r){let f=await t.browser.screenshot();c=await n.onSaveScreenshot(f)}let d=new Date,u={...e,startedAt:s,finishedAt:d,status:"SUCCESS",data:l.data,results:[]},h="Successfully executed preset action.";"assertion"in e.command&&(h=l.thoughts||"Assertion passed.");let m={beforeUrl:i,beforeScreenshot:a,afterUrl:await t.browser.url(),afterScreenshot:c,startedAt:s,finishedAt:d,viewport:await t.browser.viewport(),status:"SUCCESS"};return u.status="SUCCESS",u.results=[m],u.message=h,n.onSuccess?.({message:h,startedAt:s.getTime(),durationMs:d.getTime()-s.getTime(),command:e.command,output:u}),u}catch(l){n.logger.error({err:l},`Failed executing preset step ${e.command.type}`);let c=new Date,d=l instanceof Error?l.message:`${l}`,u=await t.browser.screenshot();if("cancelOnFailure"in e.command&&e.command.cancelOnFailure){let m={...e,startedAt:s,finishedAt:c,status:"CANCELLED",message:d,results:[{beforeUrl:i,beforeScreenshot:a,afterUrl:await t.browser.url(),afterScreenshot:u,startedAt:s,finishedAt:c,viewport:await t.browser.viewport(),status:"CANCELLED",message:d}]};return n.onCancelled?.({message:d,startedAt:s.getTime(),durationMs:c.getTime()-s.getTime(),output:m}),m}let h={...e,startedAt:s,finishedAt:c,status:"FAILED",message:d,results:[{beforeUrl:i,beforeScreenshot:a,afterUrl:await t.browser.url(),afterScreenshot:u,startedAt:s,finishedAt:c,viewport:await t.browser.viewport(),status:"FAILED",message:d}]};return n.onFailure?.({message:d,startedAt:s.getTime(),durationMs:c.getTime()-s.getTime(),output:h}),h}};var bt=async({controller:t,step:e,advanced:o,logger:r,takeScreenshots:n,...s})=>{s.onStarted?.();let i={type:"MODULE",moduleId:e.moduleId,startedAt:new Date,userAgent:O.USER_AGENT,results:[],finishedAt:new Date,status:"SUCCESS"};for(let a=0;a<e.steps.length;a++){let l=e.steps[a];if(t.isClosed())throw i.status="CANCELLED",new Error("Cancelling remaining steps in module because the controller is now closed");r.debug({i:a,moduleStep:l},"Starting module step"),r.info(`Starting module sub-step ${a+1}/${e.steps.length}: ${ut(l)}`);let c;switch(l.type){case"PRESET_ACTION":c=await ze({controller:t,step:l,advanced:o,logger:r,takeScreenshots:n,onSaveScreenshot:s.onSaveScreenshot,onStarted(){s.onStepStarted?.({index:a})},onSuccess({message:u,startedAt:h,durationMs:m,output:f}){s.onStepSuccess?.({index:a,message:u,startedAt:h,durationMs:m,output:f})},onFailure({message:u,startedAt:h,durationMs:m,output:f}){s.onStepFailure?.({index:a,message:u,startedAt:h,durationMs:m,output:f})},onCancelled({message:u,startedAt:h,durationMs:m,output:f}){s.onStepCancelled?.({index:a,message:u,startedAt:h,durationMs:m,output:f})}});break;case"AI_ACTION":c=await Fe({controller:t,step:l,advanced:o,logger:r,takeScreenshots:n,onSaveScreenshot:s.onSaveScreenshot,onStarted(){s.onStepStarted?.({index:a})},onSuccess({message:u,startedAt:h,durationMs:m,output:f}){s.onStepSuccess?.({index:a,message:u,startedAt:h,durationMs:m,output:f})},onFailure({message:u,startedAt:h,durationMs:m,output:f}){s.onStepFailure?.({index:a,message:u,startedAt:h,durationMs:m,output:f})},onCommandGenerated({commandIndex:u,message:h}){s.onCommandGenerated?.({index:a,commandIndex:u,message:h})},onCommandExecuted({commandIndex:u,command:h,output:m}){s.onCommandExecuted?.({index:a,commandIndex:u,command:h,output:m})}});break;default:return(u=>{throw"If Typescript complains about the line below, you missed a case or break in the switch above"})(l)}if(i.results.push(c),c.status==="FAILED"||c.status==="CANCELLED"){i.status=c.status,i.finishedAt=new Date,i.message=c.message;for(let d=a+1;d<e.steps.length;d++){let h={...e.steps[d],status:"CANCELLED",startedAt:new Date,finishedAt:new Date,userAgent:O.USER_AGENT,results:[],message:"Cancelled due to previous failure."};i.results.push(h)}break}}return i.status==="SUCCESS"?s.onSuccess?.({message:"Executed module step.",startedAt:i.startedAt.getTime(),durationMs:i.finishedAt.getTime()-i.startedAt.getTime(),output:i}):i.status==="FAILED"?s.onFailure?.({message:"Failed to execute module step.",startedAt:i.startedAt.getTime(),durationMs:i.finishedAt.getTime()-i.startedAt.getTime(),output:i}):i.status==="CANCELLED"&&s.onCancelled?.({message:"Module step cancelled.",startedAt:i.startedAt.getTime(),durationMs:i.finishedAt.getTime()-i.startedAt.getTime(),output:i}),i};import{v4 as wp}from"uuid";var bp=process.env.AWS_ACCESS_KEY_ID,vp=process.env.AWS_SECRET_ACCESS_KEY,Ep=process.env.S3_REGION,Ap=process.env.S3_BUCKET;var $r=async({test:t,runId:e,controller:o,logger:r,takeScreenshots:n,onUpdateRun:s,onSaveScreenshot:i,onTestSuccess:a})=>{try{let l=await Ri({test:t,runId:e,controller:o,logger:r,takeScreenshots:n,onUpdateRun:s,onSaveScreenshot:i});if(l==="PASSED"||l==="CANCELLED")return await a?.(t),l;throw new T("InternalPlatformError",l.message||"An unknown platform error occurred")}catch(l){throw l instanceof T||(l=new T("InternalPlatformError",l instanceof Error?l.message:`${l}`,{cause:l})),l}finally{await o.browser.cleanup()}},Ri=async({test:t,runId:e,controller:o,logger:r,takeScreenshots:n,onUpdateRun:s,onSaveScreenshot:i})=>{let a=ce.parse(t.advanced),l=r.child({runId:e,testId:t.id});l.info("Starting test run"),await s({status:"RUNNING",startedAt:new Date});let c,d="PASSED",u=[];for(let h=0;h<t.steps.length;h++){let m=t.steps[h];l.info(`Starting step ${h+1}/${t.steps.length}: ${ut(m)}`);let f;switch(m.type){case"PRESET_ACTION":f=await ze({controller:o,step:m,advanced:a,logger:l,takeScreenshots:n,onSaveScreenshot:i});break;case"AI_ACTION":f=await Fe({controller:o,step:m,advanced:a,logger:l,takeScreenshots:n,onSaveScreenshot:i});break;case"RESOLVED_MODULE":f=await bt({controller:o,step:m,advanced:a,logger:l,takeScreenshots:n,onSaveScreenshot:i});break;default:return(y=>{throw"If Typescript complains about the line below, you missed a case or break in the switch above"})(m)}if(u.push(f),await s({results:u}),f.status==="SUCCESS"){l.info(`Step ${h+1}/${t.steps.length} succeeded`);continue}if(f.status!=="FAILED"&&f.status!=="CANCELLED")throw new T("InternalPlatformError",`Received unexpected non-terminal status from step: ${f.status}`);l.info({message:u[u.length-1]?.message},`Step ${h+1}/${t.steps.length} ended with status: ${f.status}`),c=f,d=f.status;for(let w=h+1;w<t.steps.length;w++){let y=t.steps[w];if(y.type==="RESOLVED_MODULE"){let E={type:"MODULE",moduleId:y.moduleId,startedAt:new Date,userAgent:O.USER_AGENT,results:y.steps.map(b=>({...b,status:"CANCELLED",startedAt:new Date,finishedAt:new Date,userAgent:O.USER_AGENT,results:[]})),finishedAt:new Date,status:"CANCELLED"};u.push(E)}else{let E={...y,status:"CANCELLED",startedAt:new Date,finishedAt:new Date,userAgent:O.USER_AGENT,results:[]};u.push(E)}}break}return await s({status:d,finishedAt:new Date,results:u}),d==="FAILED"?c:d};var Hr={currentlyExecutingRequests:0},xi=t=>async e=>{try{Hr.currentlyExecutingRequests++,await Ii({...t,...e})}finally{Hr.currentlyExecutingRequests--}},Ii=async({socket:t,steps:e,baseUrl:o,testMetadata:r,reInitialize:n,indicesFilter:s,metadata:i,logger:a,rootController:l,localApp:c})=>{let{testId:d,sessionId:u,orgId:h}=i,m={testId:d,orgId:h},f=a.child(m),w=a.child({package:"web-agent",...m}),y=!0;c==="iframe"&&(y=!1);let E=ce.parse(r.advanced??{}),b=F.getController(u);if(!b)throw new Error("No controller found");b.setOpen();let M=O.USER_AGENT;f.info({reInitialize:n,steps:e,indicesFilter:s},"Starting execution");let v={url:o,clearCookies:!0,clearStorage:!0};if(n&&(l?(l.browser.baseURL=o,l.browser.setActiveFrame(dt),await l.resetState(v),b.setLogger(w),b.browser.setLogger(w)):await b.resetState(v),b.setOpen()),f.info(`Session restarted for test ${d} at ${o}`),t.emit("session",{url:o,userAgent:M,viewport:await b.browser.viewport(),localApp:c}),s?.length){let C=Oi(e,s);f.info({step:C},"Starting individual step"),t.emit("started",{indices:s}),await Br({indices:s,step:C,controller:b,advanced:E,socket:t,logger:f,takeScreenshots:y}),t.emit("finished");return}for(let C=0;C<e.length&&!(b.browser.closed||b.isClosed()||t.disconnected);C++){let j=e[C];if(f.info({index:C,step:j},"Starting step"),!await Br({indices:[C],step:j,controller:b,advanced:E,socket:t,logger:f,takeScreenshots:y}))break}t.emit("finished")},Li=(t,e)=>{switch(t.type){case"RESOLVED_MODULE":return t.steps[e];case"AI_ACTION":let o=t.commands[e];return{type:"PRESET_ACTION",command:o};case"PRESET_ACTION":default:throw new Error(`Cannot get a child step from step type ${t.type}`)}},Oi=(t,e)=>{let o=t[e[0]];for(let r=1;r<e.length;r++)o=Li(o,e[r]);return o},so=async t=>t,Br=async t=>{try{return await Mi(t)}catch(e){throw e instanceof T&&e.emitToUser&&t.socket.emit("error",{message:e.message}),e}},Mi=async({indices:t,step:e,controller:o,advanced:r,socket:n,logger:s,takeScreenshots:i})=>{let a=()=>{n.emit("started",{indices:t})},l=({message:u,startedAt:h,durationMs:m,command:f,output:w})=>{n.emit("success",{indices:t,message:u,startedAt:h,durationMs:m,command:f,output:ee.parse(w)})},c=({message:u,startedAt:h,durationMs:m,output:f})=>{n.emit("failure",{indices:t,message:u,startedAt:h,durationMs:m,output:ee.parse(f)})},d=({message:u,startedAt:h,durationMs:m,output:f})=>{n.emit("cancelled",{indices:t,message:u,startedAt:h,durationMs:m,output:ee.parse(f)})};switch(e.type){case"PRESET_ACTION":return(await ze({controller:o,step:e,advanced:r,logger:s,takeScreenshots:i,onSaveScreenshot:so,onStarted:a,onSuccess:l,onFailure:c,onCancelled:d})).status==="SUCCESS";case"AI_ACTION":return(await Fe({controller:o,step:e,advanced:r,logger:s,takeScreenshots:i,onSaveScreenshot:so,onStarted:a,onSuccess:l,onFailure:c,onCommandGenerated({commandIndex:m,message:f}){n.emit("commandGenerated",{indices:[...t,m],message:f})},onCommandExecuted({commandIndex:m,command:f,output:w}){n.emit("commandExecuted",{indices:[...t,m],command:f,output:ee.parse(w)})}})).status==="SUCCESS";case"RESOLVED_MODULE":return(await bt({controller:o,step:e,advanced:r,logger:s,takeScreenshots:i,onSaveScreenshot:so,onStarted:a,onSuccess:l,onFailure:c,onCancelled:d,onStepStarted({index:m}){n.emit("started",{indices:[...t,m]})},onStepSuccess({index:m,message:f,startedAt:w,durationMs:y,output:E}){n.emit("success",{indices:[...t,m],message:f,startedAt:w,durationMs:y,output:ee.parse(E)})},onStepFailure({index:m,message:f,startedAt:w,durationMs:y,output:E}){n.emit("failure",{indices:[...t,m],message:f,startedAt:w,durationMs:y,output:ee.parse(E)})},onStepCancelled({index:m,message:f,startedAt:w,durationMs:y,output:E}){n.emit("cancelled",{indices:[...t,m],message:f,startedAt:w,durationMs:y,output:ee.parse(E)})},onCommandGenerated({commandIndex:m,index:f,message:w}){n.emit("commandGenerated",{indices:[...t,f,m],message:w})},onCommandExecuted({index:m,commandIndex:f,command:w,output:y}){n.emit("commandExecuted",{indices:[...t,m,f],command:w,output:ee.parse(y)})}})).status==="SUCCESS";default:return(h=>{throw"If Typescript complains about the line below, you missed a case or break in the switch above"})(e)}};var Gr={event:"execute",createHandler:xi};var Ni=({metadata:t,logger:e,socket:o})=>{let{sessionId:r}=t;return async({description:n,useVision:s,testMetadata:i,isSelect:a},l)=>{let c=F.getController(r);if(!c)throw new Error("No controller found");let d=ce.parse(i.advanced??{}),u,h;try{if(u=await c.locateElement(n,s,d.disableAICaching),h={id:u.id},await c.browser.resolveCachedTargetToID(h),a)try{u.options=await c.browser.getSelectOptions(u.id)}catch(m){e.warn({err:m},"Failed getting select options"),o.emit("error",{message:`Failed getting select options: ${m}`})}}catch(m){e.error({err:m},"Error locating element"),o.emit("error",{message:m instanceof Error?m.message:`${m}`}),l({found:!1});return}e.info({locator:u,a11yData:h},"Located element result");try{await c.browser.scrollIntoView({id:u.id}),await c.browser.highlight({id:u.id})}catch(m){e.error({err:m,locator:u},"Error highlighting element"),l({found:!1});return}l({found:!0,locator:u,a11yData:h})}},jr={event:"locate",createHandler:Ni};var Pi=({socket:t,metadata:e,logger:o})=>{let{sessionId:r}=e;return async({action:n,indices:s},i)=>{try{let a=F.getController(r);if(!a)throw new Error("No controller found");await a.toggleRecordMode({action:n,indices:s,onStepRecord:(l,c)=>{t.emit("stepRecorded",{step:l,indices:c})}}),i()}catch(a){o.error({err:a},"Error in record socket handler"),i(a instanceof Error?a.message:`${a}`)}}},Wr={event:"record",createHandler:Pi};var Di=({socket:t,metadata:e,rootController:o,logger:r,localApp:n})=>async({baseUrl:s})=>{let{testId:i,sessionId:a}=e;r.info({testId:i,sessionId:a,baseUrl:s},"Reset event received");let l;if(o)await o.resetState({clearCookies:!0,clearStorage:!0,url:s}),l=o.browser.baseURL;else{let d=F.getController(a);if(!d){t.emit("error",{message:"No session to reset"});return}await d.browser.reset({clearCookies:!0,clearStorage:!0,url:s}),l=d.browser.baseURL}let c=O.USER_AGENT;r.info(`Session reset for test ${i} at ${l}`),t.emit("session",{url:l,userAgent:c,viewport:O.VIEWPORT,localApp:n})},Vr={event:"reset",createHandler:Di};import{v4 as ji}from"uuid";var _i={type:"a11y",version:"1.0.0",useHistory:"diff",useGoalSplitter:!0},Te=_i;import{existsSync as zi}from"fs";import ki from"p-timeout";async function Kr({code,fragment,timeoutMs=3e3}){let evalCode=code;if(fragment){let t=`__momentic${Math.round(Math.random()*1e4)}`;evalCode=`
24
+ ${w||""}`}return u}},eo=class{constructor(e,o,r){this.root=e;this.a11yIdNodeMap=o;this.dataMomenticIdMap=r}serialize(){return this.root?this.root.serialize():""}};function oi(t){return t.name?.value?`"${t.name.value}"`:t.role?.value&&t.role.value!=="none"&&t.role.value!=="generic"?`"${t.role.value}"`:`"${t.nodeId}"`}function vr(t,e,o){if(!e&&t.parentId)throw new Error(`Got no parent for accessibility node ${t.nodeId}: ${JSON.stringify(t)}`);let r=new Zt({id:parseInt(t.nodeId),role:t.role?.value||"",name:t.name?.value||"",content:t.value?.value||"",properties:t.properties,children:[],pathFromRoot:(e?`${e.pathFromRoot} `:"")+oi(t),backendNodeID:t.backendDOMNodeId,ignoredByCDP:t.ignored}),n=t.childIds??[];for(let a of n){if(!a)continue;let l=o.get(parseInt(a));if(!l)continue;let c=vr(l,r,o);c.length&&(r.children=r.children.concat(c))}if(r.role==="StaticText"&&(r.children=[]),r.children.length===1&&r.children[0].role==="StaticText"){let a=r.name,l=r.children[0]?.name;(a===l||!l)&&(r.children=[])}let s=[];for(let a=r.children.length-1;a>=0;a--){let l=r.children[a];if(l.role!=="StaticText"){s.push(l);continue}if(a===0||r.children[a-1].role!=="StaticText"){s.push(l);continue}r.children[a-1].name+=` ${l.name}`}if(r.children=s.reverse(),r.role==="generic"&&r.children.length===1){let a=r.children[0];if(r.name&&!to.includes(a.role)&&r.name===a.name)return r.children}if(!r.isInteresting()&&t.parentId)return r.children;for(let a of r.children)a.parent=r;return[r]}function Er(t,e,o,r,n=1){t.id=n,n+=1,e.set(t.id,t),t.dataMomenticId?o.set(t.dataMomenticId,t):to.includes(t.role)||r.debug({node:t.serialize({neighbors:1,maxLevel:1})},"Node has no data-momentic-id");for(let s of t.children)n=Er(s,e,o,r,n);return n}function Ar(t,e){if(!t.root)throw new Error("a11y tree has null root");t.allNodes=t.allNodes.filter(i=>i.ignored?!i.ignoredReasons?.find(l=>Zs.includes(l.name)):!0);let o=new Map;for(let i of t.allNodes)o.set(parseInt(i.nodeId),i);let r=vr(t.root,null,o);if(r.length>1)throw new Error(`Something went horribly wrong processing the a11y tree, we got: ${JSON.stringify(r)}`);if(r.length===0)throw new rt;let n=new Map,s=new Map;return Er(r[0],n,s,e),new eo(r[0],n,s)}var Ke=(t,e)=>{e.id=t.id,e.content=t.content,e.name=t.name,e.role=t.role,e.numChildren=t.children.length,e.serializedForm=t.serialize({noID:!0,maxLevel:1,neighbors:1}),e.nodeOnlySerializedForm=t.serialize({noID:!0,noChildren:!0,noProperties:!0})},oo=(t,e)=>{let o=1;t.role===e.role&&o++;let r=["name","content"];for(let n of r){if(!t[n]?.trim())continue;let s=Qt(t[n],e[n])/Math.min(t[n].length,e[n].length);s===0?o+=2:s<=.1&&o++}if(e.numChildren!==void 0&&(t.children.length===e.numChildren&&e.numChildren>0?o++:(e.numChildren>0&&t.children.length===0||Math.abs(t.children.length-e.numChildren)>2)&&o--),e.nodeOnlySerializedForm){let n=t.serialize({noID:!0,noChildren:!0,noProperties:!0});Qt(n,e.nodeOnlySerializedForm)/Math.min(n.length,e.nodeOnlySerializedForm.length)<=.1&&o++}if(e.serializedForm){let n=t.serialize({noID:!0,maxLevel:1,neighbors:1}),s=Qt(n,e.serializedForm)/Math.min(n.length,e.serializedForm.length);s===0?o+=2:s<=.1&&o++}return o};var Ae={r:147,g:196,b:125,a:.55},Cr={showInfo:!1,showRulers:!1,showStyles:!1,showAccessibilityInfo:!1,showExtensionLines:!1,contrastAlgorithm:"aa",contentColor:Ae,paddingColor:Ae,borderColor:Ae,marginColor:Ae,eventTargetColor:Ae,shapeColor:Ae,shapeMarginColor:Ae};var B=(t=1e3)=>new Promise(e=>setTimeout(()=>e(),t));function Tr(){window.cursor||(window.cursor=document.createElement("img"),window.cursor.setAttribute("src","data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjMyIiB2aWV3Qm94PSIwIDAgMzIgMzIiIHdpZHRoPSIzMiIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48ZyBmaWxsPSJub25lIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDEwIDcpIj48cGF0aCBkPSJtNi4xNDggMTguNDczIDEuODYzLTEuMDAzIDEuNjE1LS44MzktMi41NjgtNC44MTZoNC4zMzJsLTExLjM3OS0xMS40MDh2MTYuMDE1bDMuMzE2LTMuMjIxeiIgZmlsbD0iI2ZmZiIvPjxwYXRoIGQ9Im02LjQzMSAxNyAxLjc2NS0uOTQxLTIuNzc1LTUuMjAyaDMuNjA0bC04LjAyNS04LjA0M3YxMS4xODhsMi41My0yLjQ0MnoiIGZpbGw9IiMwMDAiLz48L2c+PC9zdmc+"),window.cursor.setAttribute("id","momentic_cursor"),window.cursor.setAttribute("style","position: absolute; z-index: 99999999999; pointer-events: none; left:0; top:0; height:4%;"),window.cursor.style.filter="invert(0%) sepia(6%) saturate(24%) hue-rotate(315deg) brightness(89%) contrast(110%)",document.body.appendChild(cursor),document.onmousemove=function(t){t=t||window.event,document.getElementById("momentic_cursor").style.left=t.pageX+"px",document.getElementById("momentic_cursor").style.top=t.pageY+"px"})}function Rr(){window.globalHintManager||(window.globalHintManager=new window.HintManager),window.globalHintManager.capture()}function xr(){window.globalHintManager&&window.globalHintManager.reset()}function Ir(){let t=document.body.getElementsByTagName("*"),e=1;for(let o=0;o<t.length;o++){let r=e.toString();for(;[6].some(s=>r.includes(s.toString()));)e++,r=e.toString();let n=t[o];n?.setAttribute("data-momentic-id",`${e}`),n?.setAttribute("aria-keyshortcuts",`${e}`),e++}}var ri=new Set(["document","script","XMLHttpRequest","fetch","xhr"]),ni=new Set(["script","document"]),si=["intercom.io","googletagmanager.com","google-analytics.com","www.gstatic.com","gstatic.com","apis.google.com","sentry.io","newrelic.com","p.retool.com","m.stripe.com","m.stripe.network","js.stripe.com","assets.trybento.co","udon.trybento.co","cdn.lr-in-prod.com","r.lr-in-prod.com","content.product-usage.assembledhq.com","data.product-usage.assembledhq.com","static.zdassets.com","o.clarity.ms/collect"],ii=["api.stripe.com","supabase.co"],ai=[];async function Or(t){for(let e of ai)await t.route(e,o=>o.abort())}function ro(t){return`${t.resourceType()} ${t.method()} ${t.url()}`}function Lr(t){return t=t.replace(/^www\./,""),t}function Mr(t){return ii.some(e=>t.includes(e))}function Nr(t,e){if(!ri.has(t.resourceType()))return!1;let o=new URL(e),r=new URL(t.url());return si.some(n=>r.hostname.includes(n))?!1:ni.has(t.resourceType())||t.method()!=="GET"?!0:Lr(r.hostname).includes(Lr(o.hostname))}var Pr=()=>{window.clickListener&&document.removeEventListener("click",window.clickListener,{capture:!0}),window.clickListener=async t=>{console.log("[Momentic] Window click listener fired",t.target),await window.captureClick(t.target)},document.addEventListener("click",window.clickListener,{capture:!0})};var Dr=()=>{window.pressListener&&document.removeEventListener("keydown",window.pressListener,{capture:!0}),window.pressListener=async t=>{console.log("[Momentic] Window press listener fired"),await window.captureKeystroke({key:t.key})},document.addEventListener("keydown",window.pressListener,{capture:!0})};var wt=Ei(vi);wt.use(Ci());wt.use(Ai({provider:{id:"2captcha",token:process.env.TWO_CAPTCHA_KEY},visualFeedback:!0}));var O=class t{browser;context;page;a11yIdToNodeMap=new Map;dataMomenticIdToNodeMap=new Map;cdpClient;logger;localMode;activeFrame;transformer;mostRecentBrowserState;baseURL;constructor({browser:e,context:o,page:r,baseUrl:n,logger:s,localMode:i,cdpClient:a}){this.browser=e,this.context=o,this.cdpClient=a,this.page=r,this.baseURL=n,this.logger=s,this.localMode=!!i}static USER_AGENT=zr["Desktop Chrome"].userAgent;static VIEWPORT={width:1920,height:1080};static async init({baseUrl:e,logger:o,browserArgs:r,contextArgs:n,takeScreenshots:s,onScreenshot:i,onClose:a,localMode:l,localAppUrl:c,extensionPath:d,timeout:u=8e3}){let h={headless:!0,handleSIGTERM:!1,chromiumSandbox:!1,...r??{}},m={viewport:t.VIEWPORT,deviceScaleFactor:process.platform==="darwin"?2:1,userAgent:zr["Desktop Chrome"].userAgent,geolocation:{latitude:37.7749,longitude:-122.4194},locale:"en-US",timezoneId:"America/Los_Angeles",...n??{}},f=null,w,y;if(l){let R=bi(wi(),"momentic","chromium");w=await wt.launchPersistentContext(R,{...h,...m,ignoreDefaultArgs:["--enable-automation","--enable-strict-mixed-content-checking"],ignoreHTTPSErrors:!0,bypassCSP:!0,args:["--allow-insecure-localhost","--disable-site-isolation-for-policy","--disable-site-isolation-trials",`--unsafely-treat-insecure-origin-as-secure=${c}`,`--load-extension=${d}`,"--test-type=browser"],baseURL:e}),y=w.pages()[0],a&&y.on("close",()=>{a()})}else f=await wt.launch(h),w=await f.newContext({...m,baseURL:e}),y=await w.newPage();await Or(w);let E=new t({browser:f,context:w,page:y,baseUrl:e,logger:o,localMode:l,cdpClient:await w.newCDPSession(y)}),b=!1;(async()=>{try{await E.navigate({url:e,wrapPossibleNavigation:!1,initialNavigation:!0})}catch(R){o.error({err:R},"Failed to initialize chrome browser")}finally{b=!0}})();let v=async()=>{if(s)try{i?.({viewport:await E.viewport(),buffer:await E.screenshot()})}catch(R){o.error({err:R},"Failed to take screenshot")}};v();let C=setInterval(()=>{v()},250),j=Date.now();for(;!b&&Date.now()-j<u;)await B(250);return clearInterval(C),b||o.warn("Timeout elapsed waiting for browser to initialize - are you sure this page is accessible?"),E}async getUserPageOrFrame(){if(this.localMode&&this.activeFrame){let e=null,o=0;for(;!e&&o<5;)e=this.page.frame(this.activeFrame),await B(100),o++;if(!e)throw new T("InternalWebAgentError",`Failed to find frame ${this.activeFrame} on page ${this.page.url()}`);return e}return this.page}async initCDPSession(){let e=!1,o=2,r=async()=>{try{this.cdpClient=await this.context.newCDPSession(this.page),await this.cdpClient.send("Accessibility.enable"),await this.cdpClient.send("DOM.enable"),await this.cdpClient.send("Overlay.enable"),e=!0}catch(n){if(o>0)return this.logger.error({err:n},"Failed to initialize CDP session, re-creating CDP client"),await B(250),o--,r();throw n}};if(await Promise.race([r(),B(3e3)]),!e)throw new T("InternalWebAgentError","Timeout elapsed waiting for CDP session to initialize")}setLogger(e){this.logger=e}ping(){if(this.closed)throw new Error("Page has been closed");if(this.browser&&!this.browser.isConnected())throw new Error("Browser is not connected")}setActiveFrame(e){this.activeFrame=e}async reset(e){this.a11yIdToNodeMap.clear(),this.dataMomenticIdToNodeMap.clear();let o=this.context.pages();this.page=o[0];for(let r=1;r<o.length;r++)await o[r].close();if(e.clearCookies&&await this.context.clearCookies(),!this.page.isClosed()){if(e.clearStorage)try{await(await this.getUserPageOrFrame()).evaluate(async()=>{window.localStorage.clear(),window.sessionStorage.clear(),await indexedDB.databases().then(r=>{r.forEach(n=>{n.name&&indexedDB.deleteDatabase(n.name)})})})}catch(r){this.logger.warn({err:r},"Failed clearing site data, continuing...")}await this.navigate({url:e.url??this.baseURL,wrapPossibleNavigation:!1,initialNavigation:!0})}}async pageSetup(){try{this.localMode||await this.page.evaluate(Tr)}catch{}}async wait(e){await B(e)}async toggleHints(e){let o=await this.getUserPageOrFrame();e.state==="on"?(await o.addStyleTag({content:qt.css}),await o.addScriptTag({content:qt.js}),await o.evaluate(Rr)):await o.evaluate(xr)}async showHints(){await this.toggleHints({state:"on"});let e=async()=>{try{await this.toggleHints({state:"off"})}catch(o){this.logger.debug({err:o},"Failed to remove vision hints")}};setTimeout(()=>{e()},3e3)}async cleanup(){await this.page.close(),await this.context.close(),this.browser&&await this.browser.close()}get closed(){return this.page.isClosed()||!!this.browser&&!this.browser.isConnected()}async html(){return(await this.getUserPageOrFrame()).content()}async url(){return(await this.getUserPageOrFrame()).url()}async screenshotWithHints(e=100,o="device",r="/tmp/screenshots/test.jpg"){let n=r?.split("."),s=n?.slice(0,-1).join("."),i=n?.slice(-1)[0],a=Buffer.from("");await this.showHints();let l=await this.screenshot(e,o,r?`${s}-after-hint.${i}`:void 0);return{before:a,after:l}}async screenshot(e=100,o="device",r){let n={fullPage:!1,quality:e,scale:o,type:"jpeg",caret:"initial",path:r};return!this.localMode||!this.activeFrame?this.page.screenshot(n):this.page.locator(`iframe[name="${this.activeFrame}"]`).screenshot(n)}async viewport(){if(this.localMode&&this.activeFrame){let o=await this.page.locator(`iframe[name="${this.activeFrame}"]`).boundingBox();if(!o)throw new Error(`Failed to get bounding box for frame: ${this.activeFrame}`);return o}let e=this.page.viewportSize();if(!e)throw new Error("failed to get viewport");return e}async navigate({url:e,wrapPossibleNavigation:o=!0,initialNavigation:r=!1}){this.logger.debug(`Navigating to ${e}`);let n=Date.now(),s=async()=>{try{await(await this.getUserPageOrFrame()).goto(e,{waitUntil:"load",timeout:8e3}),this.logger.debug({url:e},`Got load event in ${Math.floor(Date.now()-n)}ms`)}catch{this.logger.warn({url:e},"Timeout elapsed waiting for page to fire load event, continuing anyways...")}};o?await this.wrapPossibleNavigation(s):await s();let i=await this.url();if(yr.has(i)&&process.env.NODE_ENV==="production")throw new T("ActionFailureError",`${e} took too long to load \u{1F61E}. Please ensure the site and your internet are working.`,{},!0);if(r&&(await this.initCDPSession(),await this.pageSetup(),await this.attachPageLoadListeners(),this.localMode))try{await this.exposeRecordingBindings()}catch(a){a instanceof Error&&a.message.includes("already registered")||this.logger.error({err:a},"Failed to expose recording bindings during navigation")}this.logger.info({url:e},"Navigation complete")}async type(e,o={}){let{clearContent:r=!0,pressKeysSequentially:n=!1}=o;r&&(process.platform==="darwin"?await this.page.keyboard.press("Meta+A"):await this.page.keyboard.press("Control+A"),await this.page.keyboard.press("Backspace")),n?await this.page.keyboard.type(e):await this.page.keyboard.insertText(e)}async clickByA11yID(e,o={}){let r=this.a11yIdToNodeMap.get(e);if(!r)throw new Error(`Could not find DOM node during click: ${e}`);let n=await this.clickUsingCDP(r,o);return await this.highlightNode(n),r.serialize({noChildren:!0,noProperties:!0,noID:!0})}async selectOptionByA11yID(e,o){let r=this.a11yIdToNodeMap.get(e);if(!r)throw new Error(`Could not find DOM node while selecting option: ${e}`);if(!r.backendNodeID)throw new Error(`Select target missing backend node id: ${r.getLogForm()}`);return await(await this.getLocatorFromBackendID(r.backendNodeID)).selectOption(o,{timeout:8e3}),await this.highlightNode(r),r.serialize({noChildren:!0,noProperties:!0,noID:!0})}async scrollIntoView(e){let o=await this.resolveCachedTargetToID(e),r=this.a11yIdToNodeMap.get(o);if(!r)throw new Error(`Could not find node in DOM with a11y id: ${o}`);if(!r.backendNodeID)throw new Error(`Focus target missing backend node id: ${r.getLogForm()}`);await(await this.getLocatorFromBackendID(r.backendNodeID)).scrollIntoViewIfNeeded({timeout:8e3})}async highlight(e){try{let o=await this.resolveCachedTargetToID(e),r=this.a11yIdToNodeMap.get(o);if(!r)throw new Error(`Could not find DOM node during highlight: ${o}`);if(!r.backendNodeID)throw new Error(`Highlight target missing backend node id: ${r.getLogForm()}`);await this.highlightNode(r)}catch(o){this.logger.warn({err:o,target:e},"Failed to highlight target")}}async highlightNode(e){try{await this.cdpClient.send("Overlay.highlightNode",{highlightConfig:Cr,backendNodeId:e.backendNodeID})}catch{this.logger.warn("Failed to add node highlight, a page navigation likely occurred. This is non-fatal for tests.")}let o=async()=>{try{await this.cdpClient.send("Overlay.hideHighlight",{backendNodeId:e.backendNodeID})}catch(r){this.logger.debug({err:r},"Failed to remove node highlight")}};setTimeout(()=>{o()},3e3)}async wrapPossibleNavigation(e,o=8e3,r=!0){let n=Date.now(),s=await this.url(),i=Date.now(),a=new Map,l=new Map,c=v=>{let C=ro(v.request());l.set(C,(l.get(C)??0)+1);let j=v.status();j>=500&&this.logger.warn({request:C,status:j},"Received 500 level response")},d=v=>{if(!Nr(v,s))return;let C=ro(v);a.set(C,(a.get(C)??0)+1),i=Date.now()};this.page.on("response",c),this.page.on("request",d);let u=[];r&&(u=this.context.pages().map(v=>v.url()));let h=!1,m=e().catch(v=>(h=!0,v instanceof Error?v:new Error(`${v}`)));await B(250);let f=async v=>{let C=await v;if(C instanceof Error)throw C;return C},w=new Set,y=!1,b=await(async()=>{for(;!h&&!(!y&&Date.now()-n>o);){if(await B(250),y=!1,w=new Set,Date.now()-i<=1250)continue;let v=!1;for(let C of a.keys())a.get(C)!==l.get(C)&&(Mr(C)&&(y=!0),v=!0,w.add(C));if(!v)return this.logger.debug({url:await this.url(),requests:JSON.stringify(Array.from(a.entries()))},`Network idle in ${Math.floor(Date.now()-n)}ms`),!0}return!h&&w.size>0&&this.logger.warn({url:await this.url(),unfinishedRequests:JSON.stringify(Array.from(w.entries()))},"Timeout elapsed waiting for network idle, continuing anyways..."),!1})();if(this.page.off("response",c),this.page.off("request",d),!b)return f(m);let M=await this.url();if(!h&&Ee(M,s)){this.logger.debug({startUrl:s,newUrl:M},"Detected url change in wrapPossibleNavigation, waiting for load state");let v=Math.max(o-(Date.now()-n),0);if(v>0)try{await(await this.getUserPageOrFrame()).waitForLoadState("load",{timeout:v})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for load state, continuing anyways...")}}if(r){let v=this.context.pages().map(C=>C.url());if(v.length>u.length)for(let C of v)C!==M&&await this.switchToPage(C)}return f(m)}async resolveCachedTargetToID(e){if(!tt(e)){let a=this.a11yIdToNodeMap.get(e.id);if(!a)throw new Error(`Resolving target failed, fresh value did not exist in node map: ${e.id}`);return Ke(a,e),e.id}let o=(await this.getA11yTree()).serialize();this.logger.debug({tree:o},"Refreshed a11y tree before resolving target");let r=this.a11yIdToNodeMap.get(e.id);if(r){let a=oo(r,e);if(a>=5)return this.logger.debug({target:e,proposedNode:r.getLogForm(),comparisonScore:a},"Resolved cached a11y target to node with exact same id"),Ke(r,e),e.id}let n=1/0,s=1/0,i;for(let a of this.a11yIdToNodeMap.values()){let l=oo(a,e);if(l>=5)return this.logger.debug({newNode:a.getLogForm(),target:e,comparisonScore:l},"Resolved cached a11y target to new node with field comparison"),Ke(a,e),a.id;if(!e.serializedForm)continue;let c=a.serialize({noID:!0,maxLevel:1,neighbors:1});if(Math.abs(c.length-e.serializedForm.length)>15)continue;let d=Si(e.serializedForm,c),u=d/Math.min(e.serializedForm.length,c.length);d<n&&u<.2&&(n=d,s=u,i=a)}if(i&&n<15)return this.logger.debug({newNode:i.getLogForm(),target:e,distance:n,ratio:s},"Resolved cached a11y target to new node with pure levenshtein distance"),Ke(i,e),i.id;throw new Error(`Could not find any relevant node given cached target: ${JSON.stringify({target:e,closestLevenshteinDistance:n,closestNode:i})}`)}async click(e,o={}){let r=await this.resolveCachedTargetToID(e);return await this.wrapPossibleNavigation(()=>this.clickByA11yID(r,o))}async hover(e){let o=await this.resolveCachedTargetToID(e),r=this.a11yIdToNodeMap.get(o);if(!r)throw new Error(`Could not find DOM node for hover: ${o}`);if(!r.backendNodeID)throw new Error(`Hover target missing backend node id: ${r.getLogForm()}`);return await(await this.getLocatorFromBackendID(r.backendNodeID)).hover({timeout:8e3}),await this.highlightNode(r),r.serialize({noChildren:!0,noProperties:!0,noID:!0})}async selectOption(e,o){let r=await this.resolveCachedTargetToID(e);return this.selectOptionByA11yID(r,o)}async press(e){await this.wrapPossibleNavigation(()=>this.page.keyboard.press(e))}async refresh(){if(this.localMode&&this.activeFrame){let o=(await this.getUserPageOrFrame()).url();await this.navigate({url:o,wrapPossibleNavigation:!0})}else try{await this.page.reload({timeout:3e3})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for page on reload, continuing anyways...")}}async getA11yTree(e=!1){let o=null,r=async()=>{let n=0,s=await this.url();for(;!o&&n<3;)try{let i=await this.getRawA11yTree(e);if(!i.root||i.allNodes.length<=1)throw new Error("No a11y tree found on page");o=Ar(i,this.logger)}catch(i){if(this.logger.error({err:i,url:s},"Error fetching a11y tree"),n===0)await B(1e3),n++;else throw new Error(`Max retries exceeded fetching a11y tree: ${i}`)}if(!o||!o.root)throw new Error("Accessibility tree appears empty");this.a11yIdToNodeMap=o.a11yIdNodeMap,this.dataMomenticIdToNodeMap=o.dataMomenticIdMap};if(await Promise.race([r(),B(8e3)]),o)return o;throw new T("ActionFailureError",`Getting accessibility tree timed out after ${8e3}ms`)}getA11yIdFromDataMomenticId(e){return this.dataMomenticIdToNodeMap.get(e)?.id}async getRawA11yTree(e=!1){let o=await this.url(),r=Date.now(),n=()=>{r=Date.now()};this.cdpClient.addListener("Accessibility.nodesUpdated",n);let s=!1,i=()=>{this.logger.info({url:o},"Load event fired on page"),s=!0,r=Date.now()};this.cdpClient.addListener("Accessibility.loadComplete",i);let a=Date.now(),l=!e;for(;!e&&Date.now()-a<3e3;){if(await B(250),!s&&Date.now()-a<1e3){process.env.NODE_ENV!=="production"&&this.logger.debug({url:o},"A11y tree not loaded yet, waiting...");continue}if(Date.now()-r>=1250){l=!1;break}this.logger.debug({url:o},"A11y tree not stable yet, waiting...")}this.logger.debug({duration:Date.now()-a,eventReceived:s,timeoutTriggered:l,skipWait:e},"A11y wait phase completed"),await(await this.getUserPageOrFrame()).evaluate(Ir);let c;if(this.localMode&&this.activeFrame){let{result:{objectId:u}}=await this.cdpClient.send("Runtime.evaluate",{expression:`document.querySelector("#${this.activeFrame}")`}),h=await this.cdpClient.send("DOM.describeNode",{objectId:u});c=(await this.cdpClient.send("Accessibility.getRootAXNode",{frameId:h.node.frameId})).node.backendDOMNodeId}else{let{node:u}=await this.cdpClient.send("Accessibility.getRootAXNode");c=u.backendDOMNodeId}let{nodes:d}=await this.cdpClient.send("Accessibility.queryAXTree",{backendNodeId:c});return this.cdpClient.removeListener("Accessibility.loadComplete",i),this.cdpClient.removeListener("Accessibility.nodesUpdated",n),{root:d[0],allNodes:d}}async clickUsingVisualCoordinates(e){let o=await this.getElementLocation(e);if(!o)throw new Error(`Could not find element location with backend node id: ${e}`);this.logger.debug({location:o},"Executing mouse click"),await this.page.mouse.click(o.centerX,o.centerY)}async getIDAttributeUsingCDP(e){await this.cdpClient.send("DOM.getDocument",{depth:0});let o=await this.cdpClient.send("DOM.requestNode",{objectId:e}),n=(await this.cdpClient.send("DOM.getAttributes",{nodeId:o.nodeId})).attributes,s=n.findIndex(i=>i===_e);if(s===-1||!n[s+1])throw new Error(`Could not find ${_e} for object ${e}`);return n[s+1]}async getLocatorFromBackendID(e){let o=await this.cdpClient.send("DOM.resolveNode",{backendNodeId:e});if(!o||!o.object.objectId)throw new Error(`Could not resolve backend node ${e}`);let r;try{r=await this.getIDAttributeUsingCDP(o.object.objectId)}catch(n){throw this.logger.error({err:n,object:JSON.stringify(o.object)},"Failed to get ID attribute"),n}return(await this.getUserPageOrFrame()).locator(`[${_e}="${r}"]`)}async clickUsingCDP(e,o={}){let r=0,n=e;for(;r<Sr;){if(!n||n.role==="RootWebArea")throw new T("ActionFailureError",`Attempted to click node with no clickable surrounding elements: ${e.getLogForm()}`);if(n.role==="StaticText"){n=n.parent;continue}let s=n.backendNodeID;if(!s){this.logger.warn({node:n.getLogForm()},"Click candidate had no backend node ID"),n=n.parent;continue}try{let i=await this.getLocatorFromBackendID(s);return o.doubleClick?await i.dblclick({timeout:8e3}):await i.click({timeout:8e3,button:o.rightClick?"right":"left",force:o.force}),n.id!==e.id&&this.logger.info({oldNode:e.getLogForm(),newNode:n.getLogForm()},"Redirected click successfully to new element"),n}catch(i){this.logger.error({err:i,node:n.getLogForm()},"Failed click or click timed out"),r++,n=n.parent}}throw new Error(`Max click redirection attempts exhausted on original element: ${e.getLogForm()}`)}async getElementLocation(e){let o=await this.cdpClient.send("DOMSnapshot.captureSnapshot",{computedStyles:[],includeDOMRects:!0,includePaintOrder:!0}),r=await this.page.evaluate(()=>window.devicePixelRatio);process.platform==="darwin"&&r===1&&(r=2);let n=o.documents[0],s=n.layout,i=n.nodes,a=i.nodeName||[],l=i.backendNodeId||[],c=s.nodeIndex,d=s.bounds,u=-1;for(let b=0;b<a.length;b++)if(l[b]===e){u=c.indexOf(b);break}if(u===-1)throw new Error(`Could not find any backend node with ID ${e}`);let[h=0,m=0,f=0,w=0]=d[u];h/=r,m/=r,f/=r,w/=r;let y=h+f/2,E=m+w/2;return{centerX:y,centerY:E}}async scroll(e,o,r,n){let s=o==="left"?-1:1,i=n==="up"?-1:1;this.activeFrame?await(await this.getUserPageOrFrame()).evaluate(([l,c,d,u])=>window.scrollTo(window.scrollX+(l??window.innerWidth)*d,window.scrollY+(c??window.innerHeight)*u),[e,r,s,i]):await this.page.mouse.wheel((e??t.VIEWPORT.width)*s,(r??t.VIEWPORT.height)*i)}async scrollUp(e){await this.scroll(0,null,e??null,"up")}async scrollDown(e){await this.scroll(0,null,e??null,"down")}async scrollLeft(e){await this.scroll(e??null,"left",0,null)}async scrollRight(e){await this.scroll(e??null,"right",0,null)}async goForward(){await this.wrapPossibleNavigation(async()=>this.localMode&&this.activeFrame?(await this.getUserPageOrFrame()).evaluate(e=>{let o=e().contentWindow;o?o.history.forward():console.error("Failed to get content window for frame")},()=>document.querySelector(`iframe[name="${this.activeFrame}"]`)):this.page.goForward({timeout:8e3}))}async goBack(){await this.wrapPossibleNavigation(async()=>this.localMode&&this.activeFrame?(await this.getUserPageOrFrame()).evaluate(e=>{let o=e().contentWindow;o?o.history.back():console.error("Failed to get content window for frame")},()=>document.querySelector(`iframe[name="${this.activeFrame}"]`)):this.page.goBack({timeout:8e3}))}async switchToPage(e){let o=this.context.pages();for(let r=0;r<o.length;r++){let n=o[r];if(n.url().includes(e)){this.logger.info(`Switching to tab ${r} with url ${n.url()}`),this.page=n,await this.pageSetup(),await this.attachPageLoadListeners();try{await n.waitForLoadState("load",{timeout:3e3})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for load state during tab switch, continuing anyways...")}await this.initCDPSession();return}}throw new Error(`Could not find page with url containing ${e}`)}async attachPageLoadListeners(){if(this.localMode)return;let e=async o=>{o.parentFrame()||await this.pageSetup()};this.page.off("framenavigated",e),this.page.on("framenavigated",e)}async setCookie(e){let o=Po(e);await this.context.addCookies([o])}async setLocalStorage(e,o){await(await this.getUserPageOrFrame()).evaluate(([n,s])=>{n&&localStorage.setItem(n,s||"")},[e,o])}async solveCaptcha(){await this.getA11yTree();let e;for(let a of this.a11yIdToNodeMap.values())if(a.role==="image"&&a.name.toLowerCase().includes("captcha")){if(!a.backendNodeID)continue;e=await this.getLocatorFromBackendID(a.backendNodeID);break}if(!e){let a=await(await this.getUserPageOrFrame()).solveRecaptchas();if(!a.captchas||!a.captchas.length)throw new Error("No captchas found on the page");return}let o=await e.screenshot({type:"jpeg",animations:"allow",quality:100}),r=await fetch("https://api.2captcha.com/createTask",{method:"POST",body:JSON.stringify({clientKey:process.env.TWO_CAPTCHA_KEY,task:{type:"ImageToTextTask",body:o.toString("base64"),case:!0},languagePool:"en"})});if(!r.ok){let a=`Captcha solver API returned error response: ${r.statusText}`;throw this.logger.error({text:await r.text()},a),new Error(a)}let{taskId:n}=await r.json(),s=Date.now(),i="";for(;Date.now()-s<6e4;){await B(2500);let a=await fetch("https://api.2captcha.com/getTaskResult",{method:"POST",body:JSON.stringify({clientKey:process.env.TWO_CAPTCHA_KEY,taskId:n})});if(!a.ok){let c=`Captcha solution API returned error response: ${a.statusText}`;throw this.logger.error({text:await a.text()},c),new Error(c)}let l=await a.json();if(l.errorId){let c=`Captcha solution API returned error ID ${l.errorId}`;throw this.logger.error(c),new Error(c)}if(l.status==="ready"){i=l.solution.text;break}}if(!i)throw new Error("Captcha solution timed out");return i}getActiveFrame(){return this.activeFrame}async exposeRecordingBindings(){await this.context.exposeBinding("captureClick",async(e,o)=>{if(!this.transformer){this.logger.warn("Click captured without transformer");return}let r,n;try{r=await o.getAttribute("id"),n=await o.getAttribute(_e)}catch(l){throw this.logger.error({err:l},"Failed to get element attributes for click processing"),l}if(this.logger.info({id:r,dataMomenticId:n},"Click captured on element"),!n){this.logger.error({url:await this.url(),element:await o.evaluate(l=>l.outerHTML)},`Could not find ${_e} for recorded click`);return}let s=this.getA11yIdFromDataMomenticId(parseInt(n));if(!s){this.logger.warn({url:await this.url(),element:await o.evaluate(l=>l.outerHTML)},"Could not find a11y id for recorded click");return}let i={id:s};await this.resolveCachedTargetToID(i),(async()=>{try{let l=this.mostRecentBrowserState??(await this.getA11yTree(!0)).serialize();await this.transformer?.recordClick(i,l)}catch(l){this.logger.error({err:l},"Failed transforming captured click")}})()},{handle:!0}),await this.context.exposeBinding("captureKeystroke",async({},{key:e})=>{this.transformer&&(this.logger.info({key:e},"Captured keypress"),this.transformer.recordKeystroke(e))})}async startRecording(e,o){this.transformer=o;let r=await this.getA11yTree(!0);this.mostRecentBrowserState=r.serialize();let n=null,s=[async()=>{this.transformer=void 0}],i=async l=>{n&&clearTimeout(n),n=setInterval(()=>{if(!this.transformer||e.aborted)return;(async()=>{let d=0;for(;d<2;)try{let u=await this.getA11yTree(!0);this.mostRecentBrowserState=u.serialize();break}catch(u){this.logger.error({err:u},"Failed to get a11y tree in frame navigation listener"),d++}})()},250),s.push(async()=>{n&&(clearInterval(n),n=null)}),await l.evaluate(Pr),s.push(async()=>{await l.evaluate(()=>{document.removeEventListener("click",window.clickListener,{capture:!0})})}),await l.evaluate(Dr),s.push(async()=>{await l.evaluate(()=>{document.removeEventListener("keydown",window.pressListener,{capture:!0})})})};await i(await this.getUserPageOrFrame());let a=async l=>{l.name().startsWith(_t)&&(e.aborted||!this.transformer||(this.logger.info("Re-adding window recording event listeners on navigation"),await i(l)))};this.page.on("framenavigated",a),s.push(async()=>{this.page.off("framenavigated",a)}),e.addEventListener("abort",async()=>{for(let l of s)try{await l()}catch(c){this.logger.warn({err:c},"Recording cleanup function failed, continuing...")}})}async getSelectOptions(e){let o=this.a11yIdToNodeMap.get(e);if(!o)throw new Error(`Could not find node with a11y id ${e}`);if(!o.backendNodeID)throw new Error(`Node with a11y id ${e} has no backend node ID`);return await(await this.getLocatorFromBackendID(o.backendNodeID)).evaluate(s=>Array.from(s.querySelectorAll("option")).map(a=>a.value))}};var Fe=async({controller:t,step:e,logger:o,advanced:r,takeScreenshots:n,...s})=>{s.onStarted?.(),t.resetHistory();let i={...e,startedAt:new Date,userAgent:O.USER_AGENT,finishedAt:new Date,results:[],status:"SUCCESS"};try{let a=0;e.commands=e.commands||[];let l=e.commands&&e.commands.length>0;for(;;){if(a>12)throw new Error(`Exceeded max number of commands per step (${12})`);if(t.isClosed())throw new Error("Cancelling remaining steps in AI action because the controller is now closed");let c,d=new Date,u;if(n){let m=await t.browser.screenshot();u=await s.onSaveScreenshot(m)}if(l){if(c=e.commands[a],!c)throw new Error(`Saved command at index ${a} is undefined.`)}else{o.info(`Generating new sub-command ${a} within AI step`);let m;try{m=await t.promptToCommand(e.type,e.text,r.disableAICaching),c=m.command}catch(w){c={type:"FAILURE",thoughts:w instanceof Error?w.message:`${w}`}}finally{if(c.type==="FAILURE")if(a===0){i.finishedAt=new Date,i.status="FAILED",i.message=c.thoughts;break}else{let w="Stopping command generation prematurely since no progress can be made";o.warn({command:c},w),c={type:"SUCCESS",thoughts:w}}}(async()=>{let w=a,y=e,E=c;if("target"in E&&E.target){await new Promise(b=>setTimeout(()=>b(),250));try{let b=await Ti(t,m.context,E.target);E.target.elementDescriptor=b,y.commands[a]=E,s.onCommandExecuted?.({commandIndex:w,command:E})}catch(b){o.error({err:b,currentIndex:w,currentStep:y},"Failed to generate element description, continuing...")}}})()}s.onCommandGenerated?.({commandIndex:a,message:Nt[c.type]||`Unknown command (${c.type})`});let h={beforeScreenshot:u,beforeUrl:await t.browser.url(),startedAt:d,viewport:await t.browser.viewport(),finishedAt:new Date,status:"SUCCESS"};o.info(`Executing sub-command ${a} within AI step: ${Ne(c)}`);try{let m=await t.executeCommand(c,r.disableAICaching,l);o.info(`AI sub-command ${a} completed successfully`),h.elementInteracted=m.elementInteracted;let f;if(n){let y=await t.browser.screenshot();f=await s.onSaveScreenshot(y)}h.afterScreenshot=f,h.afterUrl=await t.browser.url(),h.finishedAt=new Date;let w={status:"SUCCESS",startedAt:h.startedAt,finishedAt:h.finishedAt,type:"PRESET_ACTION",data:m.data,command:c,results:[h]};if(i.results.push(w),e.commands[a]=c,s.onCommandExecuted?.({commandIndex:a,output:{...w,message:c.thoughts??m.thoughts??"Successfully executed preset action."},command:c}),c.type==="SUCCESS"){i.finishedAt=new Date,i.status="SUCCESS",i.message=m.thoughts??"All commands completed.";break}if(m.succeedImmediately&&!l){i.finishedAt=new Date,i.status="SUCCESS",i.message=m.succeedImmediatelyReason,c={type:"SUCCESS",thoughts:m.succeedImmediatelyReason},e.commands.push(c),s.onCommandExecuted?.({commandIndex:a+1,output:{...w,message:i.message},command:c}),i.results.push({...w,command:c});break}}catch(m){if(l){l=!1,a=0,i.results=[];continue}let f=m instanceof Error?m.message:`${m}`;h.status="FAILED",h.message=f,h.finishedAt=new Date,h.afterUrl=await t.browser.url();let w;try{w=await t.browser.screenshot()}catch(y){o.warn({err:y},"Failed to take screenshot after error, skipping")}h.afterScreenshot=w,i.results.push({status:"FAILED",startedAt:h.startedAt,finishedAt:h.finishedAt,type:"PRESET_ACTION",command:c,results:[h],message:f}),i.status="FAILED",i.finishedAt=new Date,i.message=f;break}a++}}catch(a){i.message=a instanceof Error?a.message:`${a}`,i.finishedAt=new Date,i.status="FAILED"}return i.status==="SUCCESS"?s.onSuccess?.({message:i.message||"AI step succeeded.",startedAt:i.startedAt.getTime(),durationMs:i.finishedAt.getTime()-i.startedAt.getTime(),output:i}):s.onFailure?.({message:i.message||"AI step errored.",startedAt:i.startedAt.getTime(),durationMs:i.finishedAt.getTime()-i.startedAt.getTime(),output:i}),i};async function Ti(t,e,o){let r=o.a11yData?.id;if(!r)throw new Error("Attempted to get reverse mapping for command with no a11y id target");return t.getReverseMappedTarget(e,r,!0)}var ze=async({controller:t,step:e,advanced:o,takeScreenshots:r,...n})=>{n.onStarted?.();let s=new Date,i=await t.browser.url(),a;if(r){let l=await t.browser.screenshot();a=await n.onSaveScreenshot(l)}try{let l=await t.executePresetStep(e.command,o.disableAICaching),c;if(r){let f=await t.browser.screenshot();c=await n.onSaveScreenshot(f)}let d=new Date,u={...e,startedAt:s,finishedAt:d,status:"SUCCESS",data:l.data,results:[]},h="Successfully executed preset action.";"assertion"in e.command&&(h=l.thoughts||"Assertion passed.");let m={beforeUrl:i,beforeScreenshot:a,afterUrl:await t.browser.url(),afterScreenshot:c,startedAt:s,finishedAt:d,viewport:await t.browser.viewport(),status:"SUCCESS"};return u.status="SUCCESS",u.results=[m],u.message=h,n.onSuccess?.({message:h,startedAt:s.getTime(),durationMs:d.getTime()-s.getTime(),command:e.command,output:u}),u}catch(l){n.logger.error({err:l},`Failed executing preset step ${e.command.type}`);let c=new Date,d=l instanceof Error?l.message:`${l}`,u=await t.browser.screenshot();if("cancelOnFailure"in e.command&&e.command.cancelOnFailure){let m={...e,startedAt:s,finishedAt:c,status:"CANCELLED",message:d,results:[{beforeUrl:i,beforeScreenshot:a,afterUrl:await t.browser.url(),afterScreenshot:u,startedAt:s,finishedAt:c,viewport:await t.browser.viewport(),status:"CANCELLED",message:d}]};return n.onCancelled?.({message:d,startedAt:s.getTime(),durationMs:c.getTime()-s.getTime(),output:m}),m}let h={...e,startedAt:s,finishedAt:c,status:"FAILED",message:d,results:[{beforeUrl:i,beforeScreenshot:a,afterUrl:await t.browser.url(),afterScreenshot:u,startedAt:s,finishedAt:c,viewport:await t.browser.viewport(),status:"FAILED",message:d}]};return n.onFailure?.({message:d,startedAt:s.getTime(),durationMs:c.getTime()-s.getTime(),output:h}),h}};var bt=async({controller:t,step:e,advanced:o,logger:r,takeScreenshots:n,...s})=>{s.onStarted?.();let i={type:"MODULE",moduleId:e.moduleId,startedAt:new Date,userAgent:O.USER_AGENT,results:[],finishedAt:new Date,status:"SUCCESS"};for(let a=0;a<e.steps.length;a++){let l=e.steps[a];if(t.isClosed())throw i.status="CANCELLED",new Error("Cancelling remaining steps in module because the controller is now closed");r.debug({i:a,moduleStep:l},"Starting module step"),r.info(`Starting module sub-step ${a+1}/${e.steps.length}: ${ut(l)}`);let c;switch(l.type){case"PRESET_ACTION":c=await ze({controller:t,step:l,advanced:o,logger:r,takeScreenshots:n,onSaveScreenshot:s.onSaveScreenshot,onStarted(){s.onStepStarted?.({index:a})},onSuccess({message:u,startedAt:h,durationMs:m,output:f}){s.onStepSuccess?.({index:a,message:u,startedAt:h,durationMs:m,output:f})},onFailure({message:u,startedAt:h,durationMs:m,output:f}){s.onStepFailure?.({index:a,message:u,startedAt:h,durationMs:m,output:f})},onCancelled({message:u,startedAt:h,durationMs:m,output:f}){s.onStepCancelled?.({index:a,message:u,startedAt:h,durationMs:m,output:f})}});break;case"AI_ACTION":c=await Fe({controller:t,step:l,advanced:o,logger:r,takeScreenshots:n,onSaveScreenshot:s.onSaveScreenshot,onStarted(){s.onStepStarted?.({index:a})},onSuccess({message:u,startedAt:h,durationMs:m,output:f}){s.onStepSuccess?.({index:a,message:u,startedAt:h,durationMs:m,output:f})},onFailure({message:u,startedAt:h,durationMs:m,output:f}){s.onStepFailure?.({index:a,message:u,startedAt:h,durationMs:m,output:f})},onCommandGenerated({commandIndex:u,message:h}){s.onCommandGenerated?.({index:a,commandIndex:u,message:h})},onCommandExecuted({commandIndex:u,command:h,output:m}){s.onCommandExecuted?.({index:a,commandIndex:u,command:h,output:m})}});break;default:return(u=>{throw"If Typescript complains about the line below, you missed a case or break in the switch above"})(l)}if(i.results.push(c),c.status==="FAILED"||c.status==="CANCELLED"){i.status=c.status,i.finishedAt=new Date,i.message=c.message;for(let d=a+1;d<e.steps.length;d++){let h={...e.steps[d],status:"CANCELLED",startedAt:new Date,finishedAt:new Date,userAgent:O.USER_AGENT,results:[],message:"Cancelled due to previous failure."};i.results.push(h)}break}}return i.status==="SUCCESS"?s.onSuccess?.({message:"Executed module step.",startedAt:i.startedAt.getTime(),durationMs:i.finishedAt.getTime()-i.startedAt.getTime(),output:i}):i.status==="FAILED"?s.onFailure?.({message:"Failed to execute module step.",startedAt:i.startedAt.getTime(),durationMs:i.finishedAt.getTime()-i.startedAt.getTime(),output:i}):i.status==="CANCELLED"&&s.onCancelled?.({message:"Module step cancelled.",startedAt:i.startedAt.getTime(),durationMs:i.finishedAt.getTime()-i.startedAt.getTime(),output:i}),i};import{v4 as wp}from"uuid";var bp=process.env.AWS_ACCESS_KEY_ID,vp=process.env.AWS_SECRET_ACCESS_KEY,Ep=process.env.S3_REGION,Ap=process.env.S3_BUCKET;var $r=async({test:t,runId:e,controller:o,logger:r,takeScreenshots:n,onUpdateRun:s,onSaveScreenshot:i,onTestSuccess:a})=>{try{let l=await Ri({test:t,runId:e,controller:o,logger:r,takeScreenshots:n,onUpdateRun:s,onSaveScreenshot:i});if(l==="PASSED"||l==="CANCELLED")return await a?.(t),l;throw new T("InternalPlatformError",l.message||"An unknown platform error occurred")}catch(l){throw l instanceof T||(l=new T("InternalPlatformError",l instanceof Error?l.message:`${l}`,{cause:l})),l}finally{await o.browser.cleanup()}},Ri=async({test:t,runId:e,controller:o,logger:r,takeScreenshots:n,onUpdateRun:s,onSaveScreenshot:i})=>{let a=ce.parse(t.advanced),l=r.child({runId:e,testId:t.id});l.info("Starting test run"),await s({status:"RUNNING",startedAt:new Date});let c,d="PASSED",u=[];for(let h=0;h<t.steps.length;h++){let m=t.steps[h];l.info(`Starting step ${h+1}/${t.steps.length}: ${ut(m)}`);let f;switch(m.type){case"PRESET_ACTION":f=await ze({controller:o,step:m,advanced:a,logger:l,takeScreenshots:n,onSaveScreenshot:i});break;case"AI_ACTION":f=await Fe({controller:o,step:m,advanced:a,logger:l,takeScreenshots:n,onSaveScreenshot:i});break;case"RESOLVED_MODULE":f=await bt({controller:o,step:m,advanced:a,logger:l,takeScreenshots:n,onSaveScreenshot:i});break;default:return(y=>{throw"If Typescript complains about the line below, you missed a case or break in the switch above"})(m)}if(u.push(f),await s({results:u}),f.status==="SUCCESS"){l.info(`Step ${h+1}/${t.steps.length} succeeded`);continue}if(f.status!=="FAILED"&&f.status!=="CANCELLED")throw new T("InternalPlatformError",`Received unexpected non-terminal status from step: ${f.status}`);l.info({message:u[u.length-1]?.message},`Step ${h+1}/${t.steps.length} ended with status: ${f.status}`),c=f,d=f.status;for(let w=h+1;w<t.steps.length;w++){let y=t.steps[w];if(y.type==="RESOLVED_MODULE"){let E={type:"MODULE",moduleId:y.moduleId,startedAt:new Date,userAgent:O.USER_AGENT,results:y.steps.map(b=>({...b,status:"CANCELLED",startedAt:new Date,finishedAt:new Date,userAgent:O.USER_AGENT,results:[]})),finishedAt:new Date,status:"CANCELLED"};u.push(E)}else{let E={...y,status:"CANCELLED",startedAt:new Date,finishedAt:new Date,userAgent:O.USER_AGENT,results:[]};u.push(E)}}break}return await s({status:d,finishedAt:new Date,results:u}),d==="FAILED"?c:d};var Hr={currentlyExecutingRequests:0},xi=t=>async e=>{try{Hr.currentlyExecutingRequests++,await Ii({...t,...e})}finally{Hr.currentlyExecutingRequests--}},Ii=async({socket:t,steps:e,baseUrl:o,testMetadata:r,reInitialize:n,indicesFilter:s,metadata:i,logger:a,rootController:l,localApp:c})=>{let{testId:d,sessionId:u,orgId:h}=i,m={testId:d,orgId:h},f=a.child(m),w=a.child({package:"web-agent",...m}),y=!0;c==="iframe"&&(y=!1);let E=ce.parse(r.advanced??{}),b=F.getController(u);if(!b)throw new Error("No controller found");b.setOpen();let M=O.USER_AGENT;f.info({reInitialize:n,steps:e,indicesFilter:s},"Starting execution");let v={url:o,clearCookies:!0,clearStorage:!0};if(n&&(l?(l.browser.baseURL=o,l.browser.setActiveFrame(dt),await l.resetState(v),b.setLogger(w),b.browser.setLogger(w)):await b.resetState(v),b.setOpen()),f.info(`Session restarted for test ${d} at ${o}`),t.emit("session",{url:o,userAgent:M,viewport:await b.browser.viewport(),localApp:c}),s?.length){let C=Oi(e,s);f.info({step:C},"Starting individual step"),t.emit("started",{indices:s}),await Br({indices:s,step:C,controller:b,advanced:E,socket:t,logger:f,takeScreenshots:y}),t.emit("finished");return}for(let C=0;C<e.length&&!(b.browser.closed||b.isClosed()||t.disconnected);C++){let j=e[C];if(f.info({index:C,step:j},"Starting step"),!await Br({indices:[C],step:j,controller:b,advanced:E,socket:t,logger:f,takeScreenshots:y}))break}t.emit("finished")},Li=(t,e)=>{switch(t.type){case"RESOLVED_MODULE":return t.steps[e];case"AI_ACTION":let o=t.commands[e];return{type:"PRESET_ACTION",command:o};case"PRESET_ACTION":default:throw new Error(`Cannot get a child step from step type ${t.type}`)}},Oi=(t,e)=>{let o=t[e[0]];for(let r=1;r<e.length;r++)o=Li(o,e[r]);return o},so=async t=>t,Br=async t=>{try{return await Mi(t)}catch(e){throw e instanceof T&&e.emitToUser&&t.socket.emit("error",{message:e.message}),e}},Mi=async({indices:t,step:e,controller:o,advanced:r,socket:n,logger:s,takeScreenshots:i})=>{let a=()=>{n.emit("started",{indices:t})},l=({message:u,startedAt:h,durationMs:m,command:f,output:w})=>{n.emit("success",{indices:t,message:u,startedAt:h,durationMs:m,command:f,output:ee.parse(w)})},c=({message:u,startedAt:h,durationMs:m,output:f})=>{n.emit("failure",{indices:t,message:u,startedAt:h,durationMs:m,output:ee.parse(f)})},d=({message:u,startedAt:h,durationMs:m,output:f})=>{n.emit("cancelled",{indices:t,message:u,startedAt:h,durationMs:m,output:ee.parse(f)})};switch(e.type){case"PRESET_ACTION":return(await ze({controller:o,step:e,advanced:r,logger:s,takeScreenshots:i,onSaveScreenshot:so,onStarted:a,onSuccess:l,onFailure:c,onCancelled:d})).status==="SUCCESS";case"AI_ACTION":return(await Fe({controller:o,step:e,advanced:r,logger:s,takeScreenshots:i,onSaveScreenshot:so,onStarted:a,onSuccess:l,onFailure:c,onCommandGenerated({commandIndex:m,message:f}){n.emit("commandGenerated",{indices:[...t,m],message:f})},onCommandExecuted({commandIndex:m,command:f,output:w}){n.emit("commandExecuted",{indices:[...t,m],command:f,output:ee.parse(w)})}})).status==="SUCCESS";case"RESOLVED_MODULE":return(await bt({controller:o,step:e,advanced:r,logger:s,takeScreenshots:i,onSaveScreenshot:so,onStarted:a,onSuccess:l,onFailure:c,onCancelled:d,onStepStarted({index:m}){n.emit("started",{indices:[...t,m]})},onStepSuccess({index:m,message:f,startedAt:w,durationMs:y,output:E}){n.emit("success",{indices:[...t,m],message:f,startedAt:w,durationMs:y,output:ee.parse(E)})},onStepFailure({index:m,message:f,startedAt:w,durationMs:y,output:E}){n.emit("failure",{indices:[...t,m],message:f,startedAt:w,durationMs:y,output:ee.parse(E)})},onStepCancelled({index:m,message:f,startedAt:w,durationMs:y,output:E}){n.emit("cancelled",{indices:[...t,m],message:f,startedAt:w,durationMs:y,output:ee.parse(E)})},onCommandGenerated({commandIndex:m,index:f,message:w}){n.emit("commandGenerated",{indices:[...t,f,m],message:w})},onCommandExecuted({index:m,commandIndex:f,command:w,output:y}){n.emit("commandExecuted",{indices:[...t,m,f],command:w,output:ee.parse(y)})}})).status==="SUCCESS";default:return(h=>{throw"If Typescript complains about the line below, you missed a case or break in the switch above"})(e)}};var Gr={event:"execute",createHandler:xi};var Ni=({metadata:t,logger:e,socket:o})=>{let{sessionId:r}=t;return async({description:n,useVision:s,testMetadata:i,isSelect:a},l)=>{let c=F.getController(r);if(!c)throw new Error("No controller found");let d=ce.parse(i.advanced??{}),u,h;try{if(u=await c.locateElement(n,s,d.disableAICaching),h={id:u.id},await c.browser.resolveCachedTargetToID(h),a)try{u.options=await c.browser.getSelectOptions(u.id)}catch(m){e.warn({err:m},"Failed getting select options"),o.emit("error",{message:`Failed getting select options: ${m}`})}}catch(m){e.error({err:m},"Error locating element"),o.emit("error",{message:m instanceof Error?m.message:`${m}`}),l({found:!1});return}e.info({locator:u,a11yData:h},"Located element result");try{await c.browser.scrollIntoView({id:u.id}),await c.browser.highlight({id:u.id})}catch(m){e.error({err:m,locator:u},"Error highlighting element"),l({found:!1});return}l({found:!0,locator:u,a11yData:h})}},jr={event:"locate",createHandler:Ni};var Pi=({socket:t,metadata:e,logger:o})=>{let{sessionId:r}=e;return async({action:n,indices:s},i)=>{try{let a=F.getController(r);if(!a)throw new Error("No controller found");await a.toggleRecordMode({action:n,indices:s,onStepRecord:(l,c)=>{t.emit("stepRecorded",{step:l,indices:c})}}),i()}catch(a){o.error({err:a},"Error in record socket handler"),i(a instanceof Error?a.message:`${a}`)}}},Wr={event:"record",createHandler:Pi};var Di=({socket:t,metadata:e,rootController:o,logger:r,localApp:n})=>async({baseUrl:s})=>{let{testId:i,sessionId:a}=e;r.info({testId:i,sessionId:a,baseUrl:s},"Reset event received");let l;if(o)await o.resetState({clearCookies:!0,clearStorage:!0,url:s}),l=o.browser.baseURL;else{let d=F.getController(a);if(!d){t.emit("error",{message:"No session to reset"});return}await d.browser.reset({clearCookies:!0,clearStorage:!0,url:s}),l=d.browser.baseURL}let c=O.USER_AGENT;r.info(`Session reset for test ${i} at ${l}`),t.emit("session",{url:l,userAgent:c,viewport:O.VIEWPORT,localApp:n})},Vr={event:"reset",createHandler:Di};import{v4 as ji}from"uuid";var _i={type:"a11y",version:"1.0.0",useHistory:"diff",useGoalSplitter:!0},Te=_i;import{existsSync as zi}from"fs";import{faker as Ch}from"@faker-js/faker";import Rh from"axios";import Ih from"moment";import ki from"p-timeout";async function Kr({code,fragment,timeoutMs=1e4}){let evalCode=code;if(fragment){let t=`__momentic${Math.round(Math.random()*1e4)}`;evalCode=`
25
25
  const ${t} = async () => {
26
26
  ${code}
27
27
  }
28
28
  ${t}()
29
- `}let result,runCode=async()=>{result=await Promise.resolve(eval(evalCode))};return await ki(runCode(),{milliseconds:timeoutMs,message:`Timeout of ${timeoutMs}ms exceeded for code execution`}),result}import{z as vt}from"zod";var qr=process.env.GCP_JS_EVAL_FUNCTION_ENDPOINT,Fi=vt.object({result:vt.unknown(),error:vt.string().optional(),success:vt.boolean()});async function Yr({code:t,fragment:e,timeoutMs:o=3e3}){let r=new AbortController;if(!qr)throw new Error("GCP_JS_EVAL_FUNCTION_ENDPOINT environment variable not set");let n=setTimeout(()=>{r.abort()},o),s=await fetch(qr,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({code:t,fragment:e}),signal:r.signal});if(!s.ok)throw new Error(`Code evaluation server returned error code ${s.status}`);clearTimeout(n);let i;try{i=Fi.parse(await s.json())}catch(a){throw new Error(`Failed to parse response: ${a}`)}if(i.error)throw new Error(i.error);return i.result}var Ui=$i();function $i(){if(process.env.NODE_ENV!=="production")return!1;if(process.env.NAMESPACE)return!0;try{if(zi("/var/run/secrets/kubernetes.io/serviceaccount/token"))return!0}catch{}return!1}async function Xr(t){let e;return Ui?(t.logger.info({code:t.code,fragment:t.fragment},"Running code remotely"),e=Yr):(t.logger.info({code:t.code,fragment:t.fragment},"Running code locally"),e=Kr),e(t)}import Hi from"dedent";import Bi from"diff-lines";var Et=class t{static ACTION_DEBOUNCE_DELAY=1e3;onStepRecord;signal;generator;indices;keyBuffer=[];timer=null;keyBufferMode="normal";lastStep=null;constructor({signal:e,onStepRecord:o,generator:r,initialIndices:n}){this.signal=e,this.onStepRecord=(s,i)=>{this.lastStep={step:s,indices:i},o(s,i)},this.generator=r,this.indices=n}async recordClick(e,o){if(this.signal.aborted)return;let r=ot("CLICK"),n={type:"PRESET_ACTION",command:{...r,target:{elementDescriptor:`Generating description for: ${e.nodeOnlySerializedForm?.trim()??`element with id ${e.id}`}`,a11yData:e}}},s=Array.from(this.indices);this.indices[this.indices.length-1]++,this.onStepRecord(n,s);let i=await this.generator.getReverseMappedDescription({goal:`${e.id}`,browserState:o},!1);n.command.target.elementDescriptor=i.phrase,this.onStepRecord(n,s)}flushKeyBufferToCommand(){let e;if(this.keyBufferMode==="normal"){let n=ot("TYPE");e={type:"PRESET_ACTION",command:{...n,target:void 0,value:this.keyBuffer.join("")}}}else{let n=ot("PRESS");e={type:"PRESET_ACTION",command:{...n,value:this.keyBuffer.join("+")}}}let o,r=this.lastStep?.step.command;if(r?.type===e.command.type){let n=r.value,s=e.command.value;e={type:"PRESET_ACTION",command:{...r,value:e.command.type==="PRESS"?`${n}+${s}`:`${n}${s}`}},o=this.lastStep.indices}else r?.type==="CLICK"&&e.command.type==="TYPE"?(this.lastStep.step.command={...e.command,target:r.target},e=this.lastStep.step,o=this.lastStep.indices):(o=Array.from(this.indices),this.indices[this.indices.length-1]++);this.onStepRecord(e,o),this.keyBuffer=[],this.timer=null}recordKeystroke(e){if(e==="Shift"||e==="CapsLock"||this.signal.aborted)return;this.timer!==null&&clearTimeout(this.timer);let o="normal";e.length>1&&(o="special"),this.keyBuffer.length!==0&&o!==this.keyBufferMode&&this.flushKeyBufferToCommand(),this.keyBufferMode=o,this.keyBuffer.push(e),this.timer=setTimeout(()=>{this.flushKeyBufferToCommand()},t.ACTION_DEBOUNCE_DELAY)}};var Gi=1e4,se=class{browser;pendingInstructions;generator;commandHistory;config;closed=!1;logger;recordAbortController=null;constructor({browser:e,config:o,generator:r,logger:n}){this.browser=e,this.generator=r,this.config=o,this.logger=n,this.pendingInstructions=[],this.commandHistory=[]}get history(){return this.commandHistory.filter(e=>e.state==="DONE")}get lastExecutedCommand(){let e=this.history;return e.length===0?null:e[e.length-1]}setOpen(){this.closed=!1}isClosed(){return this.closed}setLogger(e){this.logger=e}resetHistory(){this.commandHistory=[],this.pendingInstructions=[]}async resetState(e){this.resetHistory(),this.closed=!0,await this.browser.reset(e)}async getBrowserState(){let o=await(await this.browser.getA11yTree()).serialize();return this.logger.debug({tree:o,activeFrame:this.browser.getActiveFrame()},"Got a11y tree"),o}getSerializedHistory(e,o){let r;return this.config.useHistory==="diff"?r=this.getDiffHistory(e,o):r=this.getListHistory(),r}async splitUserGoal(e,o,r){if(e==="AI_ACTION"&&o.match(/[,!;.]|(?:and)|(?:then)/)&&this.config.useGoalSplitter){let n=await this.generator.getGranularGoals({goal:o,url:await this.browser.url()},r);this.pendingInstructions=n.reverse()}else this.pendingInstructions=[o]}async promptToCommand(e,o,r){try{return await this.promptToCommandHelper(e,o,r)}catch(n){throw n instanceof T?n:new T("InternalWebAgentError",n instanceof Error?n.message:`${n}`,{cause:n})}}async promptToCommandHelper(e,o,r){if(this.pendingInstructions.length===0){if(!o.trim())throw new Error("Cannot generate commands for empty goal");await this.splitUserGoal(e,o,r)}let n=this.pendingInstructions[this.pendingInstructions.length-1];this.logger.info({goal:n},"Starting prompt translation");let s=Date.now(),i=await this.browser.url(),a=await this.getBrowserState();this.logger.info({duration:Date.now()-s,url:i},"Got browser state");let l=this.commandHistory.length;this.commandHistory.push({state:"PENDING",browserStateBeforeCommand:a,urlBeforeCommand:i,type:e});let c=this.getSerializedHistory(i,a),d={url:i,numPrevious:l,browserState:a,history:c,goal:n,lastCommand:this.lastExecutedCommand},u=await this.generator.getProposedCommand(d,r);if(this.logger.info({type:u.type,thoughts:u.thoughts},"Got proposed command"),u.type==="SUCCESS"){let h=this.pendingInstructions.pop();if(this.logger.info({finishedInstruction:h,remainingInstructions:this.pendingInstructions},"Removing pending instruction due to SUCCESS"),this.pendingInstructions.length!==0)return this.commandHistory=[],this.promptToCommand(e,"",r)}else u.type==="FAILURE"&&(this.logger.info({remainingInstructions:this.pendingInstructions},"Removing pending instructions due to FAILURE"),this.pendingInstructions=[]);return{context:d,command:u}}async locateElement(e,o,r){if(!e)throw new T("ActionFailureError","Cannot locate element with empty description");let n=await this.getBrowserState(),s;if(o){let{before:i,after:a}=await this.browser.screenshotWithHints();if(s=await this.generator.getElementLocationWithVision({goal:e,screenshot:i,hintActivatedScreenshot:a},r),s.id>0){let l=this.browser.getA11yIdFromDataMomenticId(s.id);if(!l)throw new T("InternalWebAgentError",`Unable to find corresponding DOM node for id ${s.id}`);s.id=l}}else s=await this.generator.getElementLocation({browserState:n,goal:e},r);if(s.id<0)throw new T("ActionFailureError",`Unable to locate element: ${s.thoughts?s.thoughts:"please ensure the element is visible and conforms to Accessibility guidelines"}`);return s}getDiffHistory(e,o){let r=this.history.filter(s=>s.type==="AI_ACTION");if(r.length===0)return"<NONE/>";let n=[`
29
+ `}let result,runCode=async()=>{result=await Promise.resolve(eval(evalCode))};return await ki(runCode(),{milliseconds:timeoutMs,message:`Timeout of ${timeoutMs}ms exceeded for code execution`}),result}import{z as vt}from"zod";var qr=process.env.GCP_JS_EVAL_FUNCTION_ENDPOINT,Fi=vt.object({result:vt.unknown(),error:vt.string().optional(),success:vt.boolean()});async function Yr({code:t,fragment:e,timeoutMs:o=1e4}){let r=new AbortController;if(!qr)throw new Error("GCP_JS_EVAL_FUNCTION_ENDPOINT environment variable not set");let n=setTimeout(()=>{r.abort()},o),s=await fetch(qr,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({code:t,fragment:e}),signal:r.signal});if(!s.ok)throw new Error(`Code evaluation server returned error code ${s.status}`);clearTimeout(n);let i;try{i=Fi.parse(await s.json())}catch(a){throw new Error(`Failed to parse response: ${a}`)}if(i.error)throw new Error(i.error);return i.result}var Ui=$i();function $i(){if(process.env.NODE_ENV!=="production")return!1;if(process.env.NAMESPACE)return!0;try{if(zi("/var/run/secrets/kubernetes.io/serviceaccount/token"))return!0}catch{}return!1}async function Xr(t){let e;return Ui?(t.logger.info({code:t.code,fragment:t.fragment},"Running code remotely"),e=Yr):(t.logger.info({code:t.code,fragment:t.fragment},"Running code locally"),e=Kr),e(t)}import Hi from"dedent";import Bi from"diff-lines";var Et=class t{static ACTION_DEBOUNCE_DELAY=1e3;onStepRecord;signal;generator;indices;keyBuffer=[];timer=null;keyBufferMode="normal";lastStep=null;constructor({signal:e,onStepRecord:o,generator:r,initialIndices:n}){this.signal=e,this.onStepRecord=(s,i)=>{this.lastStep={step:s,indices:i},o(s,i)},this.generator=r,this.indices=n}async recordClick(e,o){if(this.signal.aborted)return;let r=ot("CLICK"),n={type:"PRESET_ACTION",command:{...r,target:{elementDescriptor:`Generating description for: ${e.nodeOnlySerializedForm?.trim()??`element with id ${e.id}`}`,a11yData:e}}},s=Array.from(this.indices);this.indices[this.indices.length-1]++,this.onStepRecord(n,s);let i=await this.generator.getReverseMappedDescription({goal:`${e.id}`,browserState:o},!1);n.command.target.elementDescriptor=i.phrase,this.onStepRecord(n,s)}flushKeyBufferToCommand(){let e;if(this.keyBufferMode==="normal"){let n=ot("TYPE");e={type:"PRESET_ACTION",command:{...n,target:void 0,value:this.keyBuffer.join("")}}}else{let n=ot("PRESS");e={type:"PRESET_ACTION",command:{...n,value:this.keyBuffer.join("+")}}}let o,r=this.lastStep?.step.command;if(r?.type===e.command.type){let n=r.value,s=e.command.value;e={type:"PRESET_ACTION",command:{...r,value:e.command.type==="PRESS"?`${n}+${s}`:`${n}${s}`}},o=this.lastStep.indices}else r?.type==="CLICK"&&e.command.type==="TYPE"?(this.lastStep.step.command={...e.command,target:r.target},e=this.lastStep.step,o=this.lastStep.indices):(o=Array.from(this.indices),this.indices[this.indices.length-1]++);this.onStepRecord(e,o),this.keyBuffer=[],this.timer=null}recordKeystroke(e){if(e==="Shift"||e==="CapsLock"||this.signal.aborted)return;this.timer!==null&&clearTimeout(this.timer);let o="normal";e.length>1&&(o="special"),this.keyBuffer.length!==0&&o!==this.keyBufferMode&&this.flushKeyBufferToCommand(),this.keyBufferMode=o,this.keyBuffer.push(e),this.timer=setTimeout(()=>{this.flushKeyBufferToCommand()},t.ACTION_DEBOUNCE_DELAY)}};var Gi=1e4,se=class{browser;pendingInstructions;generator;commandHistory;config;closed=!1;logger;recordAbortController=null;constructor({browser:e,config:o,generator:r,logger:n}){this.browser=e,this.generator=r,this.config=o,this.logger=n,this.pendingInstructions=[],this.commandHistory=[]}get history(){return this.commandHistory.filter(e=>e.state==="DONE")}get lastExecutedCommand(){let e=this.history;return e.length===0?null:e[e.length-1]}setOpen(){this.closed=!1}isClosed(){return this.closed}setLogger(e){this.logger=e}resetHistory(){this.commandHistory=[],this.pendingInstructions=[]}async resetState(e){this.resetHistory(),this.closed=!0,await this.browser.reset(e)}async getBrowserState(){let o=await(await this.browser.getA11yTree()).serialize();return this.logger.debug({tree:o,activeFrame:this.browser.getActiveFrame()},"Got a11y tree"),o}getSerializedHistory(e,o){let r;return this.config.useHistory==="diff"?r=this.getDiffHistory(e,o):r=this.getListHistory(),r}async splitUserGoal(e,o,r){if(e==="AI_ACTION"&&o.match(/[,!;.]|(?:and)|(?:then)/)&&this.config.useGoalSplitter){let n=await this.generator.getGranularGoals({goal:o,url:await this.browser.url()},r);this.pendingInstructions=n.reverse()}else this.pendingInstructions=[o]}async promptToCommand(e,o,r){try{return await this.promptToCommandHelper(e,o,r)}catch(n){throw n instanceof T?n:new T("InternalWebAgentError",n instanceof Error?n.message:`${n}`,{cause:n})}}async promptToCommandHelper(e,o,r){if(this.pendingInstructions.length===0){if(!o.trim())throw new Error("Cannot generate commands for empty goal");await this.splitUserGoal(e,o,r)}let n=this.pendingInstructions[this.pendingInstructions.length-1];this.logger.info({goal:n},"Starting prompt translation");let s=Date.now(),i=await this.browser.url(),a=await this.getBrowserState();this.logger.info({duration:Date.now()-s,url:i},"Got browser state");let l=this.commandHistory.length;this.commandHistory.push({state:"PENDING",browserStateBeforeCommand:a,urlBeforeCommand:i,type:e});let c=this.getSerializedHistory(i,a),d={url:i,numPrevious:l,browserState:a,history:c,goal:n,lastCommand:this.lastExecutedCommand},u=await this.generator.getProposedCommand(d,r);if(this.logger.info({type:u.type,thoughts:u.thoughts},"Got proposed command"),u.type==="SUCCESS"){let h=this.pendingInstructions.pop();if(this.logger.info({finishedInstruction:h,remainingInstructions:this.pendingInstructions},"Removing pending instruction due to SUCCESS"),this.pendingInstructions.length!==0)return this.commandHistory=[],this.promptToCommand(e,"",r)}else u.type==="FAILURE"&&(this.logger.info({remainingInstructions:this.pendingInstructions},"Removing pending instructions due to FAILURE"),this.pendingInstructions=[]);return{context:d,command:u}}async locateElement(e,o,r){if(!e)throw new T("ActionFailureError","Cannot locate element with empty description");let n=await this.getBrowserState(),s;if(o){let{before:i,after:a}=await this.browser.screenshotWithHints();if(s=await this.generator.getElementLocationWithVision({goal:e,screenshot:i,hintActivatedScreenshot:a},r),s.id>0){let l=this.browser.getA11yIdFromDataMomenticId(s.id);if(!l)throw new T("InternalWebAgentError",`Unable to find corresponding DOM node for id ${s.id}`);s.id=l}}else s=await this.generator.getElementLocation({browserState:n,goal:e},r);if(s.id<0)throw new T("ActionFailureError",`Unable to locate element: ${s.thoughts?s.thoughts:"please ensure the element is visible and conforms to Accessibility guidelines"}`);return s}getDiffHistory(e,o){let r=this.history.filter(s=>s.type==="AI_ACTION");if(r.length===0)return"<NONE/>";let n=[`
30
30
  You have already executed the following commands successfully (most recent listed first)`,"-".repeat(10)];return r.reverse().forEach((s,i)=>{if(n.push(`COMMAND ${r.length-i}${i===0?" (command just executed)":""}: ${s.serializedCommand}`),i===0)if(Ee(s.urlBeforeCommand,e))n.push(` URL CHANGE: '${s.urlBeforeCommand}' -> '${e}'`);else{let a=Bi(s.browserStateBeforeCommand,o,{n_surrounding:1});a?a.length<Gi?(n.push("PAGE CONTENT CHANGE:"),a.split(`
31
31
  `).forEach(l=>n.push(` ${l}`))):n.push("PAGE CONTENT CHANGE: <TOO_LONG_TO_DISPLAY/>"):n.push("PAGE CONTENT CHANGE: <NONE/>")}n.push("-".repeat(10))}),n.push(`STARTING URL: ${this.browser.baseURL}`),n.join(`
32
32
  `)}getListHistory(){return Hi`Here are the commands that you have successfully executed:
33
33
  ${this.commandHistory.filter(e=>e.type==="AI_ACTION").map(e=>`- ${e.serializedCommand}`).join(`
34
- `)}`}async executeCommand(e,o,r=!1){let n=this.commandHistory[this.commandHistory.length-1];if(!r&&(!n||n.state!=="PENDING"))throw new T("InternalWebAgentError","Executing command but there is no pending entry in the history");if(this.closed)throw new Error("Attempting to execute command on a closed controller");let s=Date.now(),i=await this.executePresetStep(e,o),a=Date.now()-s;return this.logger.debug({result:i,duration:a},"Got execution result"),i.succeedImmediately&&!r&&(this.pendingInstructions.pop(),this.pendingInstructions.length>0&&(i.succeedImmediately=!1)),i.elementInteracted&&"target"in e&&e.target&&!e.target.elementDescriptor&&(e.target.elementDescriptor=i.elementInteracted.trim()),r||(n.generatedStep=e,n.serializedCommand=Ne(e),n.state="DONE"),i}async executeAssertion(e){let o=await this.getBrowserState(),r=await this.browser.url(),n;if(e.useVision)n={goal:e.assertion,url:r,screenshot:await this.browser.screenshot(),browserState:"",history:"",numPrevious:-1,lastCommand:null};else{let i=this.getSerializedHistory(r,o);n={goal:e.assertion,url:r,browserState:o,history:i,lastCommand:this.lastExecutedCommand,numPrevious:this.commandHistory.length}}let s=await this.generator.getAssertionResult(n,e.useVision,e.disableCache);if(s.relevantElements&&Promise.all(s.relevantElements.map(i=>this.browser.highlight({id:i}))),!s.result)throw new T("AssertionFailureError",s.thoughts);return{succeedImmediately:!1,thoughts:s.thoughts,urlAfterCommand:r}}async wrapElementTargetingCommand(e,o,r,n,s=1){if(!e.a11yData&&!e.elementDescriptor)throw new T("ActionFailureError","Cannot target element with no target data or element descriptor");let i=e.a11yData&&tt(e.a11yData);e.a11yData||(this.logger.info("No cached locator data for target, prompting AI for fresh location"),s--,e.a11yData=et.parse(await this.locateElement(e.elementDescriptor,o,r)));try{let a=await n(e.a11yData);return i?this.logger.info({target:e},"Successfully used cached target to perform action"):this.logger.debug({target:e},"Successfully generated and used new a11y target information"),a}catch(a){if(s>0&&e.elementDescriptor)return this.logger.warn({err:a,target:e},"Failed to execute action with cached target, retrying with AI"),e.a11yData=void 0,this.wrapElementTargetingCommand(e,o,r,n,s);if(a instanceof T)throw a;let l=`Failed to find ${e.elementDescriptor?`${e.elementDescriptor}`:"element"}: ${a instanceof Error?a.message:a}`;throw this.logger.error({err:a,target:e},l),new T("ActionFailureError",l,{cause:a})}}async executePresetStep(e,o){try{return await this.executePresetStepHelper(e,o)}catch(r){throw r instanceof T?r:new T("InternalWebAgentError",r instanceof Error?r.message:`${r}`,{cause:r})}}async executePresetStepHelper(e,o){switch(e.type){case"SUCCESS":return e.condition?.assertion.trim()?this.executeAssertion(e.condition):{succeedImmediately:!1,urlAfterCommand:await this.browser.url()};case"AI_ASSERTION":return this.executeAssertion(e);case"AI_WAIT":{let l=Date.now(),c=(e.timeout??10)*1e3,d,u,h=0;for(;Date.now()-l<c;)try{d=await this.executeAssertion({...e,type:"AI_ASSERTION"});break}catch(m){u=m instanceof Error?m:new Error(`${m}`),this.logger.warn({err:m},`AI_WAIT assert attempt ${h} failed, retrying...`),h++}if(!d){let m=`AI wait still failing after ${c}ms.`;throw u&&(m+=` Latest result: ${u.message}`),new T("AssertionFailureError",m)}return d}case"NAVIGATE":if(!Yt(e.url)&&!Xt(e.url,this.browser.baseURL))throw new T("ActionFailureError",`Invalid URL: ${e.url}`);await this.browser.navigate({url:e.url});break;case"CAPTCHA":let r=await this.browser.solveCaptcha();r&&(await this.wrapElementTargetingCommand({elementDescriptor:"the captcha image solution input"},e.useVision,o,l=>this.browser.click(l)),await this.browser.type(r,{clearContent:!0,pressKeysSequentially:!1}));break;case"GO_BACK":await this.browser.goBack();break;case"GO_FORWARD":await this.browser.goForward();break;case"SCROLL_LEFT":case"SCROLL_RIGHT":case"SCROLL_DOWN":case"SCROLL_UP":let n;switch(e.target&&(e.target.elementDescriptor.trim()||e.target.a11yData)&&(n=await this.wrapElementTargetingCommand(e.target,e.useVision,o,l=>this.browser.hover(l))),e.type){case"SCROLL_UP":await this.browser.scrollUp(e.deltaY);break;case"SCROLL_DOWN":await this.browser.scrollDown(e.deltaY);break;case"SCROLL_LEFT":await this.browser.scrollLeft(e.deltaX);break;case"SCROLL_RIGHT":await this.browser.scrollRight(e.deltaX);break}return{succeedImmediately:!1,urlAfterCommand:await this.browser.url(),elementInteracted:n};case"WAIT":await this.browser.wait(e.delay*1e3);break;case"REFRESH":await this.browser.refresh();break;case"CLICK":{let l=await this.browser.url(),c=await this.wrapElementTargetingCommand(e.target,e.useVision,o,u=>this.browser.click(u,{doubleClick:e.doubleClick,rightClick:e.rightClick,force:e.force})),d={urlAfterCommand:await this.browser.url(),succeedImmediately:!1,elementInteracted:c};return Ee(l,d.urlAfterCommand)&&(d.succeedImmediately=!0,d.succeedImmediatelyReason="URL changed"),d}case"SELECT_OPTION":{let l=await this.wrapElementTargetingCommand(e.target,!1,o,c=>this.browser.selectOption(c,e.option));return{succeedImmediately:!1,urlAfterCommand:await this.browser.url(),elementInteracted:l}}case"TAB":await this.browser.switchToPage(e.url);break;case"COOKIE":if(!e.value)break;await this.browser.setCookie(e.value);break;case"LOCAL_STORAGE":if(!e.value||!e.key)break;await this.browser.setLocalStorage(e.key,e.value);break;case"JAVASCRIPT":{let l;try{l=await Xr({code:e.code,fragment:!!e.fragment,timeoutMs:e.timeout?e.timeout*1e3:void 0,logger:this.logger})}catch(c){throw new T("ActionFailureError",c instanceof Error?c.message:`${c}`,{cause:c})}try{l=JSON.parse(JSON.stringify(l))}catch(c){throw new T("ActionFailureError",`Return value is not serializable: ${c instanceof Error?c.message:`${c}`}`,{cause:c})}return{urlAfterCommand:await this.browser.url(),succeedImmediately:!1,data:{result:l}}}case"TYPE":{let l=await this.browser.url(),c;e.target&&(c=await this.wrapElementTargetingCommand(e.target,e.useVision,o,u=>this.browser.click(u,{force:e.force}))),await this.browser.type(e.value,{clearContent:e.clearContent,pressKeysSequentially:e.pressKeysSequentially}),e.pressEnter&&await this.browser.press("Enter");let d={urlAfterCommand:await this.browser.url(),succeedImmediately:!1,elementInteracted:c};return Ee(l,d.urlAfterCommand)&&(d.succeedImmediately=!0,d.succeedImmediatelyReason="URL changed"),d}case"HOVER":{let l=await this.wrapElementTargetingCommand(e.target,e.useVision,o,c=>this.browser.hover(c));return{succeedImmediately:!1,urlAfterCommand:await this.browser.url(),elementInteracted:l}}case"PRESS":let s=await this.browser.url();await this.browser.press(e.value);let i={urlAfterCommand:await this.browser.url(),succeedImmediately:!1};return Ee(s,i.urlAfterCommand)&&(i.succeedImmediately=!0,i.succeedImmediatelyReason="URL changed"),i;case"REQUEST":{let l=e.timeout??30,c=null,d=new AbortController,u=Object.fromEntries(Object.entries(e.headers||{}).filter(([M,v])=>M&&v)),h=new URLSearchParams;Object.entries(e.params||{}).filter(([M,v])=>M&&v).forEach(([M,v])=>{h.append(M,v)});let m;if(Yt(e.url)&&(m=e.url),Xt(e.url,this.browser.baseURL)&&(m=new URL(e.url,this.browser.baseURL).toString()),!m)throw new T("ActionFailureError",`Invalid URL: ${e.url}`);let f=async()=>{try{c=await fetch(`${m}?${h.toString()}`,{headers:u,method:e.method,body:e.body,signal:d.signal})}catch(M){this.logger.error({err:M},"Failed to make fetch request")}},w=async()=>{await new Promise(M=>setTimeout(M,l*1e3)),d.abort()};await Promise.race([w(),f()]);let y=c;if(!y)throw new T("ActionFailureError",`Fetch request timed out after ${l} seconds`);if(!y.ok)throw new T("ActionFailureError",`Fetch request failed with status ${y.status}`);let E={};y.headers.forEach((M,v)=>{E[v]=M});let b={status:y.status,headers:E};return y.headers.get("content-type")?.includes("json")?b.json=await y.json():y.headers.get("content-type")?.includes("text")&&(b.text=await y.text()),{succeedImmediately:!1,urlAfterCommand:await this.browser.url(),data:b}}default:return(l=>{throw"If Typescript complains about the line below, you missed a case or break in the switch above"})(e)}return{succeedImmediately:!1,urlAfterCommand:await this.browser.url()}}async getReverseMappedTarget(e,o,r){return(await this.generator.getReverseMappedDescription({browserState:e.browserState,goal:`${o}`},r)).phrase}async toggleRecordMode({action:e,indices:o,onStepRecord:r}){if(e==="STOP"){this.recordAbortController?.abort();return}if(!o||!r)throw new Error("Indices or step recording callback not provided to toggleRecordMode");this.recordAbortController=new AbortController;let n=new Et({signal:this.recordAbortController.signal,initialIndices:o,generator:this.generator,onStepRecord:r});await this.browser.startRecording(this.recordAbortController.signal,n)}};async function Jr({socket:t,generator:e,logger:o,rootController:r,localApp:n}){let s=o.child({package:"web-agent"}),i=!0;n==="iframe"&&(i=!1);let a=t.handshake.query.testId,l=t.handshake.query.baseUrl;if(!a)throw new Error("Socket connection is missing testID");let c=t.handshake.query.orgId,d=ji(),u=f=>{t.emit("screenshot",{...f,url:l})},h=r;if(h)h.browser.setActiveFrame(dt),h.browser.baseURL=l,await h.browser.reset({clearCookies:!0,clearStorage:!0});else{let f=await O.init({baseUrl:l,logger:s,takeScreenshots:i,onScreenshot:u});h=new se({browser:f,generator:e,config:Te,logger:s})}i&&To(t,d,o),F.registerSession(h,d);let m=O.USER_AGENT;return t.emit("session",{url:l,userAgent:m,viewport:await h.browser.viewport(),localApp:n}),{sessionId:d,testId:a,orgId:c}}var Qr=[Gr,Ro,Vr,jr,Wr];var Zr=t=>{let{logger:e}=t,o=new Wi(t.baseServer,{cors:{origin:"*",methods:["GET","POST"]}});return o.on("connection",async r=>{let n;try{n=await Jr({...t,socket:r})}catch(s){e.error({event:"connection",type:"websocket",err:s},"Failed to setup connection"),r.emit("error",{message:`${s}`});return}Qr.forEach(s=>Vi(s,{socket:r,metadata:n,...t}))}),o},Vi=(t,e)=>{let o=t.createHandler(e),r=(...n)=>{e.logger.debug({event:t.event,metadata:e.metadata,args:n},"Websocket event");let s=i=>{e.logger.error({event:t.event,type:"websocket",args:n,err:i instanceof Error?i:new Error(`${i}`)},"Unhandled exception in socket handler"),e.socket.emit("error",{message:`${i}`})};try{let i=o.apply(void 0,n);i&&typeof i.catch=="function"&&i.catch(s)}catch(i){s(i)}};e.socket.on(t.event,r)};import Ki from"fetch-retry";var qi=Ki(global.fetch),Re="v1",xe=class{baseURL;apiKey;constructor(e){this.baseURL=e.baseURL,this.apiKey=e.apiKey}async getElementLocation(e,o){let r=await this.sendRequest(`/${Re}/web-agent/locate-element`,{browserState:e.browserState,goal:e.goal,disableCache:o});return Ht.parse(r)}async getElementLocationWithVision(e,o){let r=await this.sendRequest(`/${Re}/web-agent/visual-locate`,{goal:e.goal,screenshot:e.screenshot?.toString("base64"),hintActivatedScreenshot:e.hintActivatedScreenshot?.toString("base64"),disableCache:o});return Ht.parse(r)}async getAssertionResult(e,o,r){if(o){let s=await this.sendRequest(`/${Re}/web-agent/assertion`,{url:e.url,goal:e.goal,screenshot:e.screenshot?.toString("base64"),disableCache:r,vision:!0});return $t.parse(s)}let n=await this.sendRequest(`/${Re}/web-agent/assertion`,{url:e.url,browserState:e.browserState,goal:e.goal,history:e.history,numPrevious:e.numPrevious,lastCommand:e.lastCommand,disableCache:r,vision:!1});return $t.parse(n)}async getProposedCommand(e,o){let r=await this.sendRequest(`/${Re}/web-agent/next-command`,{url:e.url,browserState:e.browserState,goal:e.goal,history:e.history,numPrevious:e.numPrevious,lastCommand:e.lastCommand,disableCache:o});return Ko.parse(r)}async getGranularGoals(e,o){let r=await this.sendRequest(`/${Re}/web-agent/split-goal`,{url:e.url,goal:e.goal,disableCache:o});return qo.parse(r)}async getReverseMappedDescription(e,o){let r=await this.sendRequest(`/${Re}/web-agent/reverse-mapped-description`,{goal:e.goal,browserState:e.browserState,disableCache:o});return Yo.parse(r)}async sendRequest(e,o){let r=await qi(`${this.baseURL}${e}`,{retries:1,retryDelay:1e3,method:"POST",body:JSON.stringify(o),headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`}});if(!r.ok)throw new Error(`Request to ${e} failed with status ${r.status}: ${await r.text()}`);return r.json()}};import{existsSync as Yi,readFileSync as Xi,writeFileSync as io}from"fs";import ao from"path";import{parse as Ji,stringify as Qi}from"yaml";var k=process.env.MOMENTIC_DIR??de;function en(t,e){let o=sr(t);for(let[s,i]of Object.entries(o.modules)){let a=We(s);io(ao.join(k,oe,`${a}.yaml`),i,"utf-8")}let r=We(e),n=ao.join(k,`${r}.yaml`);return io(n,o.test,"utf-8"),`${r}.yaml`}function tn(t,e){if(!Yi(e))throw new Error(`Test not found at path: ${ao}`);let o=Xi(e,"utf-8"),n={...Ji(o),...t},s=Qi(n);io(e,s,"utf-8")}import{readFileSync as At,readdirSync as on,writeFileSync as rn}from"fs";import{join as lo}from"path";import{v4 as Zi}from"uuid";import{parse as Ct,stringify as nn}from"yaml";var qe=lo(k,oe);function sn(t,e){let o=gt(qe,e).path,r=At(o,"utf-8"),s={...Ct(r),...t},i=nn(s);rn(o,i,"utf-8")}function an(t,e){let o=We(t),r=lo(qe,`${o}.yaml`),n={fileType:"momentic/module",schemaVersion:z,moduleId:Zi(),name:t,steps:e},s=nn(n);return rn(r,s,"utf-8"),{type:"RESOLVED_MODULE",moduleId:n.moduleId,name:n.name,steps:n.steps}}function ln(){let t=[];for(let e in on(qe)){if(!e.endsWith(".yaml"))continue;let o=At(e,"utf-8"),r=Ct(o),n={name:r.name,moduleId:r.moduleId,numSteps:r.steps.length};t.push(n)}return t}function cn(){let t=[];for(let e of on(qe)){if(!e.endsWith(".yaml"))continue;let o=lo(qe,e),r=At(o,"utf-8"),n=Ct(r);try{let s=Gt.parse(n);t.push(s)}catch(s){g.warn({err:s},"Error parsing module, skipping...")}}return t}function dn(t){let e=gt(oe,t).path,o=At(e,"utf-8"),r=Ct(o);return Gt.parse(r)}import{Router as na}from"express";import{existsSync as ea,readFileSync as ta,readdirSync as oa}from"fs";import{join as mn}from"path";import{parse as ra}from"yaml";var co=mn(k,ct);function un(){let t=[];if(!ea(co))return[];for(let e of oa(co)){if(!e.endsWith(".yaml"))continue;let o=mn(co,e),r=ta(o,"utf-8"),n=ra(r);try{let s=V.parse(n);t.push(s)}catch(s){g.warn({err:s},"Error parsing environment, skipping...")}}return t}function G(t){return function(...e){let o=e[e.length-1],r=t(...e);Promise.resolve(r).catch(o)}}var pn=na();pn.get("/",G((t,e)=>{let o=un();e.status(200).json(o)}));var hn=pn;import{Router as sa}from"express";var Ye=sa();Ye.get("/",G((t,e)=>{let r=cn().map(n=>({...n,type:"RESOLVED_MODULE"}));e.status(200).json(r)}));Ye.get("/metadata",G((t,e)=>{let o=ln();e.status(200).json(o)}));Ye.post("/",G((t,e)=>{let o;try{o=jo.parse(t.body)}catch(n){e.status(400).json({error:`Invalid request body: ${n}`});return}try{je(o.name)}catch(n){e.status(400).json({error:`Invalid module name: ${n}`});return}let r=an(o.name,o.steps);e.status(201).json(r)}));Ye.get("/:moduleId",G((t,e)=>{if(!t.params.moduleId){e.status(400).json({error:"Missing moduleId in url path."});return}let o=dn(t.params.moduleId);e.json(o)}));var fn=Ye;import{Router as ia}from"express";import{existsSync as aa}from"fs";import{join as mo}from"path";import{v4 as la}from"uuid";var Xe=ia();Xe.get("/",G((t,e)=>{let o=Ve(k).filter(r=>r.localOnly);e.status(200).json(o)}));Xe.post("/",G((t,e)=>{let o;try{o=Go.parse(t.body)}catch(a){e.status(400).json({error:`Invalid request body: ${a}`});return}try{je(o.name)}catch(a){e.status(400).json({error:a.message});return}let n={id:la(),name:o.name,baseUrl:o.baseUrl,schemaVersion:z,advanced:{disableAICaching:!1,availableAsModule:!0},retries:0,steps:[],localOnly:!0};o.environment&&(n.envs=[{name:o.environment,defaultOnLocal:!0}]);let s=en(n,o.name),i={...n,testPath:s};e.status(201).json(i)}));Xe.get("/:testPath",G(async(t,e)=>{if(!t.params.testPath){e.status(400).json({error:"Missing testPath in url path."});return}let o=mo(k,t.params.testPath);if(!aa(o)){e.status(400).json({error:`Test not found at path: ${o}`});return}let r=await yt(o,mo(k,oe));e.status(200).json(r)}));Xe.patch("/:testPath",G((t,e)=>{if(!t.params.testPath){e.status(400).json({error:"Missing testPath in url path."});return}let o;try{o=Bo.parse(t.body)}catch(s){e.status(400).json({error:`Invalid request body: ${s}`});return}let r=[],n={};for(let s of o.steps){if(s.type!=="RESOLVED_MODULE"){r.push(s);continue}r.push({type:"MODULE",moduleId:s.moduleId}),n[s.moduleId]=s.steps}for(let[s,i]of Object.entries(n))sn({steps:i},s);tn({steps:r},mo(k,t.params.testPath)),e.status(201).json({message:"ok"})}));var gn=Xe;var uo="https://api.momentic.ai",Sn=t=>{uo=t},wn=()=>uo,ca,bn=t=>{ca=t};var yn,vn=async t=>{if(yn)return;let e=await fetch(`${uo}/v1/auth/check`,{headers:{"Content-Type":"application/json",Authorization:`Bearer ${t}`}});if(!e.ok)throw new Error(`Error checking API key with server (code ${e.status}): ${await e.text()}`);let o=await e.json(),{orgId:r}=rr.parse(o);yn=r};import En,{multistream as da}from"pino";import ma from"pino-pretty";var po=new Map,ua=process.env.NODE_ENV==="production",ho=class t{consoleLogger;ddClientToken;hostname;bindingAttributes;site="https://http-intake.logs.us5.datadoghq.com/api/v2/logs";constructor({bindings:e,clientToken:o,hostname:r}){this.ddClientToken=o,this.hostname=r,this.bindingAttributes={...e,env:process.env.NODE_ENV};let n={base:this.bindingAttributes,errorKey:"err",level:"debug"};this.consoleLogger=ua?En(n):En(n,da([{stream:ma({colorize:!0})}]))}child(e){return new t({clientToken:this.ddClientToken,bindings:{...this.bindingAttributes,...e,hostname:this.hostname}})}flush(e){this.consoleLogger.flush(e)}log(e,o,r,...n){this.consoleLogger[e](o,r,...n);let s=Object.assign({},this.bindingAttributes,o&&typeof o=="object"?o:{});n.length>0&&(s.args=n),(async()=>{try{let i=await fetch(this.site,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","DD-API-KEY":this.ddClientToken},body:JSON.stringify([{ddsource:this.bindingAttributes.app,ddtags:Object.keys(s).map(a=>`${a}:${s[a]}`).join(","),hostname:this.hostname??"vercel",message:r||"",service:"momentic"}])});if(!i.ok)throw new Error(`Failed to log to DD: ${i.statusText})}`)}catch(i){this.consoleLogger.warn({obj:o,msg:r,args:n,err:i},"Failed to log to DD")}})()}debug(e,o,...r){this.log("debug",e,o,...r)}info(e,o,...r){this.log("info",e,o,...r)}warn(e,o,...r){this.log("warn",e,o,...r)}error(e,o,...r){this.log("error",e,o,...r)}bindings(){return this.bindingAttributes}},An=({app:t,clientToken:e,hostname:o})=>{if(!process.env.DD_CLIENT_TOKEN&&!process.env.NEXT_PUBLIC_DD_CLIENT_TOKEN&&!e)throw new Error("Missing DD_CLIENT_TOKEN");return po.has(t)||po.set(t,new ho({bindings:{app:t},hostname:o,clientToken:e||process.env.NEXT_PUBLIC_DD_CLIENT_TOKEN||process.env.DD_CLIENT_TOKEN})),po.get(t)};import{hostname as pa}from"os";var X=An({app:"desktop-server",clientToken:"pubcfd7516a5c0ba852b42675cd97bee027",hostname:pa()});async function Tn({momenticServerUrl:t,apiKey:e,mode:o,serverPort:r,appPort:n,staticDir:s,assetsDir:i}){if(!ga(k)||!ya(k).isDirectory()){let m=ba.isAbsolute(k);throw new Error(`Root folder ${k} does not exist${m?"":` in the current directory ("${process.cwd()}")`}. Please run \`npx momentic init\` if you wish to setup Momentic here for the first time.`)}t&&Sn(t),g.info("Checking API key"),await vn(e);let l=`http://localhost:${r}`;n&&(l=`http://localhost:${n}`);let c=Ea(s);await new Promise(m=>{c.listen(r,()=>{g.info(`Server is running at http://localhost:${r}`),m()})});let u=new xe({baseURL:wn(),apiKey:e});if(o==="web"){await Cn("web",c,u,X),await wa(l);return}let h=await Aa({appUrl:l,apiGenerator:u,extensionPath:va(i,"extension"),onClose:async()=>{g.info("Browser closed, closing app."),await h.browser.cleanup(),c.close(()=>{process.exit(0)})}});await Cn("iframe",c,u,X,h),process.on("uncaughtException",m=>{X.error({err:m},"Uncaught exception in desktop-server")}),process.on("unhandledRejection",m=>{X.error({err:m},"Unhandled rejection in desktop-server")})}function Ea(t){let e=fo();e.use(fa()),e.use(ha.json({limit:"50mb"}));let o=fo.Router();if(o.use("/tests",gn),o.use("/modules",fn),o.use("/environments",hn),e.use("/api",o),e.use((n,s,i)=>{n.path!=="/healthcheck"&&X.debug({url:n.url,path:n.path,query:n.query,method:n.method,body:n.body,headers:n.rawHeaders,client:n.ip},"Received desktop-server request"),s.on("close",()=>{s.statusCode>=400&&X.error({url:n.url,method:n.method,statusCode:s.statusCode},"Request completed in error")}),i()}),e.use((n,s,i,a)=>{X.error({stack:n.stack,msg:n.message,url:s.url,method:s.method},"Unhandled exception leading to 500 on desktop-server"),i.status(500).send("Internal Server Error"),a(n)}),t){let n=fo.static(t);e.use("/",n),e.use("*",n)}return Sa.createServer(e)}async function Aa({appUrl:t,apiGenerator:e,extensionPath:o,onClose:r}){let n=await O.init({baseUrl:t,logger:X,browserArgs:{headless:!1,handleSIGTERM:!0},contextArgs:{bypassCSP:!0,viewport:null,deviceScaleFactor:void 0},localMode:!0,localAppUrl:t,extensionPath:o,onClose:r}),s=new se({browser:n,config:Te,generator:e,logger:X});return bn(s),s}async function Cn(t,e,o,r,n){Zr({baseServer:e,generator:o,logger:r,localApp:t,rootController:n})}import{existsSync as tl}from"fs";import Jn,{dirname as ol}from"path";import{fileURLToPath as rl}from"url";var Rn="0.0.30";var K="v1",J=class{baseURL;apiKey;constructor(e){this.baseURL=e.baseURL,this.apiKey=e.apiKey}async getRun(e){let o=await this.sendRequest(`/${K}/runs/${e}`,{method:"GET"});return tr.parse(o)}async createRun(e){let o=await this.sendRequest(`/${K}/runs`,{method:"POST",body:e});return er.parse(o)}async updateRun(e,o){await this.sendRequest(`/${K}/runs/${e}`,{method:"PATCH",body:o})}async getTest(e){let o=await this.sendRequest(`/${K}/tests/${e}`,{method:"GET"});return Jo.parse(o)}async getAllTestIds(){let e=await this.sendRequest(`/${K}/tests`,{method:"GET"});return Qo.parse(e)}async getTestYAMLExport(e){let o=await this.sendRequest(`/${K}/tests/export`,{method:"POST",body:e});return Zo.parse(o)}async updateTestWithYAML(e){await this.sendRequest(`/${K}/tests/update`,{method:"POST",body:e})}async queueTests(e){let o=await this.sendRequest(`/${K}/tests/queue`,{method:"POST",body:e});return Xo.parse(o)}async uploadScreenshot(e){let o=await this.sendRequest(`/${K}/screenshots`,{method:"POST",body:e});return or.parse(o)}async getAllEnvironments(){let e=await this.sendRequest(`/${K}/environments`,{method:"GET"});return nr.parse(e)}async updateEnvironments(e){await this.sendRequest(`/${K}/environments`,{method:"POST",body:e})}async sendRequest(e,o){let r=await fetch(`${this.baseURL}${e}`,{method:o.method,body:o.body?JSON.stringify(o.body):void 0,headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`}});if(!r.ok)throw new Error(`Request to ${e} failed with status ${r.status}: ${await r.text()}`);return r.status===204?r.text():r.json()}};import{createHash as Ln}from"crypto";import{existsSync as On,mkdirSync as Oa,readFileSync as wo,writeFileSync as Mn}from"fs";import{join as Nn}from"path";import{parse as Ma}from"yaml";import{existsSync as go,mkdirSync as Je,readdirSync as In,statSync as yo}from"fs";import{homedir as xa}from"os";import{join as Qe,resolve as Ia}from"path";import Ta from"chalk";import Ra from"readline/promises";async function Q(t,e){if(process.env.CI)return!0;let o=Ra.createInterface({input:process.stdin,output:process.stdout});t=`${t} (y/N) `;let r=e?Ta.bold.yellow(t):t,n=await o.question(r);return o.close(),n.toLowerCase()==="y"}var Tt=de,Ie=Qe(de,oe),So=Qe(de,Ho),q=Qe(de,ct),La=Qe(xa(),"momentic","chromium");function xn(t){return go(t)&&yo(t).isDirectory()}async function me(t,e=!1){xn(Tt)||(!e&&!await Q(`A '${de}' folder was not found in the current directory. Setup the required Momentic folder structure?`)&&(g.error("Setup cancelled"),process.exit(1)),Je(Tt),Je(Ie),Je(So),Je(q),Je(La,{recursive:!0})),(!xn(q)||In(q).length===0)&&(!e&&!await Q("You do not appear to have any environments configured locally. Pull environments from Momentic Cloud?")&&(g.error("Setup cancelled"),process.exit(1)),await Rt({client:t,all:!0,skipPrompts:e})),g.info("Setup complete!")}function Ze(t,e,o=new Set){for(let r of t){let n=Ia(r);if(n&&go(n)&&yo(n).isDirectory()){let s=In(n).map(i=>Qe(n,i));Ze(s,e,o);continue}if(n.endsWith(".yaml")){if(!go(n)||!yo(n).isFile())throw new Error(`File not found or unreadable: ${n}`);if(!e(n))continue;o.add(n)}}return o}async function Rt({envNames:t,client:e,all:o,skipPrompts:r}){let n=await e.getAllEnvironments();t&&!o&&(n=n.filter(a=>t?.includes(a.name))),On(q)||Oa(q);let s=0,i=0;for(let a of n){let l=Nn(q,`${a.name}.yaml`);if(!On(l))Mn(l,jt(a)),s++;else{let c=Ln("sha256").update(wo(l)).digest("hex"),d=jt(a),u=Ln("sha256").update(d).digest("hex");if(c!==u){if(!r&&!await Q(`Environment ${a.name} already exists on disk but needs to be updated. Overwrite?`)){g.error("Pull cancelled");return}Mn(l,d),i++}}}g.info(`Pulled ${s} new environments and updated ${i} existing environments`)}function Na(t){return t.endsWith(".yaml")?wo(t,"utf8").includes("momentic/environment")?!0:(g.warn(`Skipping YAML that is not a Momentic environment: ${t}`),!1):!1}async function Pn({client:t,names:e,all:o,yes:r}){let n;o?n=[q]:n=e.map(a=>Nn(q,`${a.toLowerCase()}.yaml`));let s=Ze(n,Na);g.info(`Found ${s.size} environments(s) to push:`),s.forEach(a=>g.info(` - ${a}`)),g.info("Loading file contents");let i=[];for(let a of s){let l=Ma(wo(a,"utf-8"));try{let c=V.parse(l);i.push(c)}catch(c){g.error({err:c},`${a} failed to parse as a valid environment file`),process.exit(1)}}if(!r&&!await Q("Pushing environments can overwrite variables on cloud and instantly affect scheduled runs. Continue?",!0)){g.error("Push cancelled");return}g.info(`Pushing ${s.size} environment(s)`),await t.updateEnvironments(i),g.info("Push successful!")}import{registry as bo}from"playwright-core/lib/server";function Pa(t){let e=[],o=[];for(let r of t){let n=bo.findExecutable(r);!n||n.installType==="none"?e.push(r):o.push(n)}return o}async function Dn(){let t=Pa(["chromium"]);await bo.installDeps(t,!1),await bo.install(t,!1)}import{Argument as xt,Option as Le}from"commander";var ue=new Le("--api-key <key>","API key for authentication. If not supplied, attempts to read the MOMENTIC_API_KEY env var.").env("MOMENTIC_API_KEY").makeOptionMandatory(!0),pe=new Le("--server <server>","Momentic server to use. Leave unchanged unless using Momentic on-premise.").default("https://api.momentic.ai"),he=new Le("-y, --yes","Skip confirmation prompts.").env("CI").default(!1),_n=new Le("--no-report","Skip reporting test results to Momentic Cloud when running with the --local flag.").default(!0),kn=new Le("--env <env>","Name of the environment to use when running tests."),vo=new Le("-a --all","Select all tests in your organization from Momentic Cloud. Cannot be used together with <tests> arguments.").default(!1).preset(!0),Eo=new Le("-a --all","Select all environments in your organization from Momentic Cloud. Cannot be used together with <paths> arguments.").default(!1).preset(!0),Fn=new xt("<tests...>",`One or more test paths to pull from Momentic Cloud.
34
+ `)}`}async executeCommand(e,o,r=!1){let n=this.commandHistory[this.commandHistory.length-1];if(!r&&(!n||n.state!=="PENDING"))throw new T("InternalWebAgentError","Executing command but there is no pending entry in the history");if(this.closed)throw new Error("Attempting to execute command on a closed controller");let s=Date.now(),i=await this.executePresetStep(e,o),a=Date.now()-s;return this.logger.debug({result:i,duration:a},"Got execution result"),i.succeedImmediately&&!r&&(this.pendingInstructions.pop(),this.pendingInstructions.length>0&&(i.succeedImmediately=!1)),i.elementInteracted&&"target"in e&&e.target&&!e.target.elementDescriptor&&(e.target.elementDescriptor=i.elementInteracted.trim()),r||(n.generatedStep=e,n.serializedCommand=Ne(e),n.state="DONE"),i}async executeAssertion(e){let o=await this.getBrowserState(),r=await this.browser.url(),n;if(e.useVision)n={goal:e.assertion,url:r,screenshot:await this.browser.screenshot(),browserState:"",history:"",numPrevious:-1,lastCommand:null};else{let i=this.getSerializedHistory(r,o);n={goal:e.assertion,url:r,browserState:o,history:i,lastCommand:this.lastExecutedCommand,numPrevious:this.commandHistory.length}}let s=await this.generator.getAssertionResult(n,e.useVision,e.disableCache);if(s.relevantElements&&Promise.all(s.relevantElements.map(i=>this.browser.highlight({id:i}))),!s.result)throw new T("AssertionFailureError",s.thoughts);return{succeedImmediately:!1,thoughts:s.thoughts,urlAfterCommand:r}}async wrapElementTargetingCommand(e,o,r,n,s=1){if(!e.a11yData&&!e.elementDescriptor)throw new T("ActionFailureError","Cannot target element with no target data or element descriptor");let i=e.a11yData&&tt(e.a11yData);e.a11yData||(this.logger.info("No cached locator data for target, prompting AI for fresh location"),s--,e.a11yData=et.parse(await this.locateElement(e.elementDescriptor,o,r)));try{let a=await n(e.a11yData);return i?this.logger.info({target:e},"Successfully used cached target to perform action"):this.logger.debug({target:e},"Successfully generated and used new a11y target information"),a}catch(a){if(s>0&&e.elementDescriptor)return this.logger.warn({err:a,target:e},"Failed to execute action with cached target, retrying with AI"),e.a11yData=void 0,this.wrapElementTargetingCommand(e,o,r,n,s);if(a instanceof T)throw a;let l=`Failed to find ${e.elementDescriptor?`${e.elementDescriptor}`:"element"}: ${a instanceof Error?a.message:a}`;throw this.logger.error({err:a,target:e},l),new T("ActionFailureError",l,{cause:a})}}async executePresetStep(e,o){try{return await this.executePresetStepHelper(e,o)}catch(r){throw r instanceof T?r:new T("InternalWebAgentError",r instanceof Error?r.message:`${r}`,{cause:r})}}async executePresetStepHelper(e,o){switch(e.type){case"SUCCESS":return e.condition?.assertion.trim()?this.executeAssertion(e.condition):{succeedImmediately:!1,urlAfterCommand:await this.browser.url()};case"AI_ASSERTION":return this.executeAssertion(e);case"AI_WAIT":{let l=Date.now(),c=(e.timeout??10)*1e3,d,u,h=0;for(;Date.now()-l<c;)try{d=await this.executeAssertion({...e,type:"AI_ASSERTION"});break}catch(m){u=m instanceof Error?m:new Error(`${m}`),this.logger.warn({err:m},`AI_WAIT assert attempt ${h} failed, retrying...`),h++}if(!d){let m=`AI wait still failing after ${c}ms.`;throw u&&(m+=` Latest result: ${u.message}`),new T("AssertionFailureError",m)}return d}case"NAVIGATE":if(!Yt(e.url)&&!Xt(e.url,this.browser.baseURL))throw new T("ActionFailureError",`Invalid URL: ${e.url}`);await this.browser.navigate({url:e.url});break;case"CAPTCHA":let r=await this.browser.solveCaptcha();r&&(await this.wrapElementTargetingCommand({elementDescriptor:"the captcha image solution input"},e.useVision,o,l=>this.browser.click(l)),await this.browser.type(r,{clearContent:!0,pressKeysSequentially:!1}));break;case"GO_BACK":await this.browser.goBack();break;case"GO_FORWARD":await this.browser.goForward();break;case"SCROLL_LEFT":case"SCROLL_RIGHT":case"SCROLL_DOWN":case"SCROLL_UP":let n;switch(e.target&&(e.target.elementDescriptor.trim()||e.target.a11yData)&&(n=await this.wrapElementTargetingCommand(e.target,e.useVision,o,l=>this.browser.hover(l))),e.type){case"SCROLL_UP":await this.browser.scrollUp(e.deltaY);break;case"SCROLL_DOWN":await this.browser.scrollDown(e.deltaY);break;case"SCROLL_LEFT":await this.browser.scrollLeft(e.deltaX);break;case"SCROLL_RIGHT":await this.browser.scrollRight(e.deltaX);break}return{succeedImmediately:!1,urlAfterCommand:await this.browser.url(),elementInteracted:n};case"WAIT":await this.browser.wait(e.delay*1e3);break;case"REFRESH":await this.browser.refresh();break;case"CLICK":{let l=await this.browser.url(),c=await this.wrapElementTargetingCommand(e.target,e.useVision,o,u=>this.browser.click(u,{doubleClick:e.doubleClick,rightClick:e.rightClick,force:e.force})),d={urlAfterCommand:await this.browser.url(),succeedImmediately:!1,elementInteracted:c};return Ee(l,d.urlAfterCommand)&&(d.succeedImmediately=!0,d.succeedImmediatelyReason="URL changed"),d}case"SELECT_OPTION":{let l=await this.wrapElementTargetingCommand(e.target,!1,o,c=>this.browser.selectOption(c,e.option));return{succeedImmediately:!1,urlAfterCommand:await this.browser.url(),elementInteracted:l}}case"TAB":await this.browser.switchToPage(e.url);break;case"COOKIE":if(!e.value)break;await this.browser.setCookie(e.value);break;case"LOCAL_STORAGE":if(!e.value||!e.key)break;await this.browser.setLocalStorage(e.key,e.value);break;case"JAVASCRIPT":{let l;try{l=await Xr({code:e.code,fragment:!!e.fragment,timeoutMs:e.timeout?e.timeout*1e3:void 0,logger:this.logger})}catch(c){throw new T("ActionFailureError",c instanceof Error?c.message:`${c}`,{cause:c})}try{l=JSON.parse(JSON.stringify(l))}catch(c){throw new T("ActionFailureError",`Return value is not serializable: ${c instanceof Error?c.message:`${c}`}`,{cause:c})}return{urlAfterCommand:await this.browser.url(),succeedImmediately:!1,data:{result:l}}}case"TYPE":{let l=await this.browser.url(),c;e.target&&(c=await this.wrapElementTargetingCommand(e.target,e.useVision,o,u=>this.browser.click(u,{force:e.force}))),await this.browser.type(e.value,{clearContent:e.clearContent,pressKeysSequentially:e.pressKeysSequentially}),e.pressEnter&&await this.browser.press("Enter");let d={urlAfterCommand:await this.browser.url(),succeedImmediately:!1,elementInteracted:c};return Ee(l,d.urlAfterCommand)&&(d.succeedImmediately=!0,d.succeedImmediatelyReason="URL changed"),d}case"HOVER":{let l=await this.wrapElementTargetingCommand(e.target,e.useVision,o,c=>this.browser.hover(c));return{succeedImmediately:!1,urlAfterCommand:await this.browser.url(),elementInteracted:l}}case"PRESS":let s=await this.browser.url();await this.browser.press(e.value);let i={urlAfterCommand:await this.browser.url(),succeedImmediately:!1};return Ee(s,i.urlAfterCommand)&&(i.succeedImmediately=!0,i.succeedImmediatelyReason="URL changed"),i;case"REQUEST":{let l=e.timeout??30,c=null,d=new AbortController,u=Object.fromEntries(Object.entries(e.headers||{}).filter(([M,v])=>M&&v)),h=new URLSearchParams;Object.entries(e.params||{}).filter(([M,v])=>M&&v).forEach(([M,v])=>{h.append(M,v)});let m;if(Yt(e.url)&&(m=e.url),Xt(e.url,this.browser.baseURL)&&(m=new URL(e.url,this.browser.baseURL).toString()),!m)throw new T("ActionFailureError",`Invalid URL: ${e.url}`);let f=async()=>{try{c=await fetch(`${m}?${h.toString()}`,{headers:u,method:e.method,body:e.body,signal:d.signal})}catch(M){this.logger.error({err:M},"Failed to make fetch request")}},w=async()=>{await new Promise(M=>setTimeout(M,l*1e3)),d.abort()};await Promise.race([w(),f()]);let y=c;if(!y)throw new T("ActionFailureError",`Fetch request timed out after ${l} seconds`);if(!y.ok)throw new T("ActionFailureError",`Fetch request failed with status ${y.status}`);let E={};y.headers.forEach((M,v)=>{E[v]=M});let b={status:y.status,headers:E};return y.headers.get("content-type")?.includes("json")?b.json=await y.json():y.headers.get("content-type")?.includes("text")&&(b.text=await y.text()),{succeedImmediately:!1,urlAfterCommand:await this.browser.url(),data:b}}default:return(l=>{throw"If Typescript complains about the line below, you missed a case or break in the switch above"})(e)}return{succeedImmediately:!1,urlAfterCommand:await this.browser.url()}}async getReverseMappedTarget(e,o,r){return(await this.generator.getReverseMappedDescription({browserState:e.browserState,goal:`${o}`},r)).phrase}async toggleRecordMode({action:e,indices:o,onStepRecord:r}){if(e==="STOP"){this.recordAbortController?.abort();return}if(!o||!r)throw new Error("Indices or step recording callback not provided to toggleRecordMode");this.recordAbortController=new AbortController;let n=new Et({signal:this.recordAbortController.signal,initialIndices:o,generator:this.generator,onStepRecord:r});await this.browser.startRecording(this.recordAbortController.signal,n)}};async function Jr({socket:t,generator:e,logger:o,rootController:r,localApp:n}){let s=o.child({package:"web-agent"}),i=!0;n==="iframe"&&(i=!1);let a=t.handshake.query.testId,l=t.handshake.query.baseUrl;if(!a)throw new Error("Socket connection is missing testID");let c=t.handshake.query.orgId,d=ji(),u=f=>{t.emit("screenshot",{...f,url:l})},h=r;if(h)h.browser.setActiveFrame(dt),h.browser.baseURL=l,await h.browser.reset({clearCookies:!0,clearStorage:!0});else{let f=await O.init({baseUrl:l,logger:s,takeScreenshots:i,onScreenshot:u});h=new se({browser:f,generator:e,config:Te,logger:s})}i&&To(t,d,o),F.registerSession(h,d);let m=O.USER_AGENT;return t.emit("session",{url:l,userAgent:m,viewport:await h.browser.viewport(),localApp:n}),{sessionId:d,testId:a,orgId:c}}var Qr=[Gr,Ro,Vr,jr,Wr];var Zr=t=>{let{logger:e}=t,o=new Wi(t.baseServer,{cors:{origin:"*",methods:["GET","POST"]}});return o.on("connection",async r=>{let n;try{n=await Jr({...t,socket:r})}catch(s){e.error({event:"connection",type:"websocket",err:s},"Failed to setup connection"),r.emit("error",{message:`${s}`});return}Qr.forEach(s=>Vi(s,{socket:r,metadata:n,...t}))}),o},Vi=(t,e)=>{let o=t.createHandler(e),r=(...n)=>{e.logger.debug({event:t.event,metadata:e.metadata,args:n},"Websocket event");let s=i=>{e.logger.error({event:t.event,type:"websocket",args:n,err:i instanceof Error?i:new Error(`${i}`)},"Unhandled exception in socket handler"),e.socket.emit("error",{message:`${i}`})};try{let i=o.apply(void 0,n);i&&typeof i.catch=="function"&&i.catch(s)}catch(i){s(i)}};e.socket.on(t.event,r)};import Ki from"fetch-retry";var qi=Ki(global.fetch),Re="v1",xe=class{baseURL;apiKey;constructor(e){this.baseURL=e.baseURL,this.apiKey=e.apiKey}async getElementLocation(e,o){let r=await this.sendRequest(`/${Re}/web-agent/locate-element`,{browserState:e.browserState,goal:e.goal,disableCache:o});return Ht.parse(r)}async getElementLocationWithVision(e,o){let r=await this.sendRequest(`/${Re}/web-agent/visual-locate`,{goal:e.goal,screenshot:e.screenshot?.toString("base64"),hintActivatedScreenshot:e.hintActivatedScreenshot?.toString("base64"),disableCache:o});return Ht.parse(r)}async getAssertionResult(e,o,r){if(o){let s=await this.sendRequest(`/${Re}/web-agent/assertion`,{url:e.url,goal:e.goal,screenshot:e.screenshot?.toString("base64"),disableCache:r,vision:!0});return $t.parse(s)}let n=await this.sendRequest(`/${Re}/web-agent/assertion`,{url:e.url,browserState:e.browserState,goal:e.goal,history:e.history,numPrevious:e.numPrevious,lastCommand:e.lastCommand,disableCache:r,vision:!1});return $t.parse(n)}async getProposedCommand(e,o){let r=await this.sendRequest(`/${Re}/web-agent/next-command`,{url:e.url,browserState:e.browserState,goal:e.goal,history:e.history,numPrevious:e.numPrevious,lastCommand:e.lastCommand,disableCache:o});return Ko.parse(r)}async getGranularGoals(e,o){let r=await this.sendRequest(`/${Re}/web-agent/split-goal`,{url:e.url,goal:e.goal,disableCache:o});return qo.parse(r)}async getReverseMappedDescription(e,o){let r=await this.sendRequest(`/${Re}/web-agent/reverse-mapped-description`,{goal:e.goal,browserState:e.browserState,disableCache:o});return Yo.parse(r)}async sendRequest(e,o){let r=await qi(`${this.baseURL}${e}`,{retries:1,retryDelay:1e3,method:"POST",body:JSON.stringify(o),headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`}});if(!r.ok)throw new Error(`Request to ${e} failed with status ${r.status}: ${await r.text()}`);return r.json()}};import{existsSync as Yi,readFileSync as Xi,writeFileSync as io}from"fs";import ao from"path";import{parse as Ji,stringify as Qi}from"yaml";var k=process.env.MOMENTIC_DIR??de;function en(t,e){let o=sr(t);for(let[s,i]of Object.entries(o.modules)){let a=We(s);io(ao.join(k,oe,`${a}.yaml`),i,"utf-8")}let r=We(e),n=ao.join(k,`${r}.yaml`);return io(n,o.test,"utf-8"),`${r}.yaml`}function tn(t,e){if(!Yi(e))throw new Error(`Test not found at path: ${ao}`);let o=Xi(e,"utf-8"),n={...Ji(o),...t},s=Qi(n);io(e,s,"utf-8")}import{readFileSync as At,readdirSync as on,writeFileSync as rn}from"fs";import{join as lo}from"path";import{v4 as Zi}from"uuid";import{parse as Ct,stringify as nn}from"yaml";var qe=lo(k,oe);function sn(t,e){let o=gt(qe,e).path,r=At(o,"utf-8"),s={...Ct(r),...t},i=nn(s);rn(o,i,"utf-8")}function an(t,e){let o=We(t),r=lo(qe,`${o}.yaml`),n={fileType:"momentic/module",schemaVersion:z,moduleId:Zi(),name:t,steps:e},s=nn(n);return rn(r,s,"utf-8"),{type:"RESOLVED_MODULE",moduleId:n.moduleId,name:n.name,steps:n.steps}}function ln(){let t=[];for(let e in on(qe)){if(!e.endsWith(".yaml"))continue;let o=At(e,"utf-8"),r=Ct(o),n={name:r.name,moduleId:r.moduleId,numSteps:r.steps.length};t.push(n)}return t}function cn(){let t=[];for(let e of on(qe)){if(!e.endsWith(".yaml"))continue;let o=lo(qe,e),r=At(o,"utf-8"),n=Ct(r);try{let s=Gt.parse(n);t.push(s)}catch(s){g.warn({err:s},"Error parsing module, skipping...")}}return t}function dn(t){let e=gt(oe,t).path,o=At(e,"utf-8"),r=Ct(o);return Gt.parse(r)}import{Router as na}from"express";import{existsSync as ea,readFileSync as ta,readdirSync as oa}from"fs";import{join as mn}from"path";import{parse as ra}from"yaml";var co=mn(k,ct);function un(){let t=[];if(!ea(co))return[];for(let e of oa(co)){if(!e.endsWith(".yaml"))continue;let o=mn(co,e),r=ta(o,"utf-8"),n=ra(r);try{let s=V.parse(n);t.push(s)}catch(s){g.warn({err:s},"Error parsing environment, skipping...")}}return t}function G(t){return function(...e){let o=e[e.length-1],r=t(...e);Promise.resolve(r).catch(o)}}var pn=na();pn.get("/",G((t,e)=>{let o=un();e.status(200).json(o)}));var hn=pn;import{Router as sa}from"express";var Ye=sa();Ye.get("/",G((t,e)=>{let r=cn().map(n=>({...n,type:"RESOLVED_MODULE"}));e.status(200).json(r)}));Ye.get("/metadata",G((t,e)=>{let o=ln();e.status(200).json(o)}));Ye.post("/",G((t,e)=>{let o;try{o=jo.parse(t.body)}catch(n){e.status(400).json({error:`Invalid request body: ${n}`});return}try{je(o.name)}catch(n){e.status(400).json({error:`Invalid module name: ${n}`});return}let r=an(o.name,o.steps);e.status(201).json(r)}));Ye.get("/:moduleId",G((t,e)=>{if(!t.params.moduleId){e.status(400).json({error:"Missing moduleId in url path."});return}let o=dn(t.params.moduleId);e.json(o)}));var fn=Ye;import{Router as ia}from"express";import{existsSync as aa}from"fs";import{join as mo}from"path";import{v4 as la}from"uuid";var Xe=ia();Xe.get("/",G((t,e)=>{let o=Ve(k).filter(r=>r.localOnly);e.status(200).json(o)}));Xe.post("/",G((t,e)=>{let o;try{o=Go.parse(t.body)}catch(a){e.status(400).json({error:`Invalid request body: ${a}`});return}try{je(o.name)}catch(a){e.status(400).json({error:a.message});return}let n={id:la(),name:o.name,baseUrl:o.baseUrl,schemaVersion:z,advanced:{disableAICaching:!1,availableAsModule:!0},retries:0,steps:[],localOnly:!0};o.environment&&(n.envs=[{name:o.environment,defaultOnLocal:!0}]);let s=en(n,o.name),i={...n,testPath:s};e.status(201).json(i)}));Xe.get("/:testPath",G(async(t,e)=>{if(!t.params.testPath){e.status(400).json({error:"Missing testPath in url path."});return}let o=mo(k,t.params.testPath);if(!aa(o)){e.status(400).json({error:`Test not found at path: ${o}`});return}let r=await yt(o,mo(k,oe));e.status(200).json(r)}));Xe.patch("/:testPath",G((t,e)=>{if(!t.params.testPath){e.status(400).json({error:"Missing testPath in url path."});return}let o;try{o=Bo.parse(t.body)}catch(s){e.status(400).json({error:`Invalid request body: ${s}`});return}let r=[],n={};for(let s of o.steps){if(s.type!=="RESOLVED_MODULE"){r.push(s);continue}r.push({type:"MODULE",moduleId:s.moduleId}),n[s.moduleId]=s.steps}for(let[s,i]of Object.entries(n))sn({steps:i},s);tn({steps:r},mo(k,t.params.testPath)),e.status(201).json({message:"ok"})}));var gn=Xe;var uo="https://api.momentic.ai",Sn=t=>{uo=t},wn=()=>uo,ca,bn=t=>{ca=t};var yn,vn=async t=>{if(yn)return;let e=await fetch(`${uo}/v1/auth/check`,{headers:{"Content-Type":"application/json",Authorization:`Bearer ${t}`}});if(!e.ok)throw new Error(`Error checking API key with server (code ${e.status}): ${await e.text()}`);let o=await e.json(),{orgId:r}=rr.parse(o);yn=r};import En,{multistream as da}from"pino";import ma from"pino-pretty";var po=new Map,ua=process.env.NODE_ENV==="production",ho=class t{consoleLogger;ddClientToken;hostname;bindingAttributes;site="https://http-intake.logs.us5.datadoghq.com/api/v2/logs";constructor({bindings:e,clientToken:o,hostname:r}){this.ddClientToken=o,this.hostname=r,this.bindingAttributes={...e,env:process.env.NODE_ENV};let n={base:this.bindingAttributes,errorKey:"err",level:"debug"};this.consoleLogger=ua?En(n):En(n,da([{stream:ma({colorize:!0})}]))}child(e){return new t({clientToken:this.ddClientToken,bindings:{...this.bindingAttributes,...e,hostname:this.hostname}})}flush(e){this.consoleLogger.flush(e)}log(e,o,r,...n){this.consoleLogger[e](o,r,...n);let s=Object.assign({},this.bindingAttributes,o&&typeof o=="object"?o:{});n.length>0&&(s.args=n),(async()=>{try{let i=await fetch(this.site,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","DD-API-KEY":this.ddClientToken},body:JSON.stringify([{ddsource:this.bindingAttributes.app,ddtags:Object.keys(s).map(a=>`${a}:${s[a]}`).join(","),hostname:this.hostname??"vercel",message:r||"",service:"momentic"}])});if(!i.ok)throw new Error(`Failed to log to DD: ${i.statusText})}`)}catch(i){this.consoleLogger.warn({obj:o,msg:r,args:n,err:i},"Failed to log to DD")}})()}debug(e,o,...r){this.log("debug",e,o,...r)}info(e,o,...r){this.log("info",e,o,...r)}warn(e,o,...r){this.log("warn",e,o,...r)}error(e,o,...r){this.log("error",e,o,...r)}bindings(){return this.bindingAttributes}},An=({app:t,clientToken:e,hostname:o})=>{if(!process.env.DD_CLIENT_TOKEN&&!process.env.NEXT_PUBLIC_DD_CLIENT_TOKEN&&!e)throw new Error("Missing DD_CLIENT_TOKEN");return po.has(t)||po.set(t,new ho({bindings:{app:t},hostname:o,clientToken:e||process.env.NEXT_PUBLIC_DD_CLIENT_TOKEN||process.env.DD_CLIENT_TOKEN})),po.get(t)};import{hostname as pa}from"os";var X=An({app:"desktop-server",clientToken:"pubcfd7516a5c0ba852b42675cd97bee027",hostname:pa()});async function Tn({momenticServerUrl:t,apiKey:e,mode:o,serverPort:r,appPort:n,staticDir:s,assetsDir:i}){if(!ga(k)||!ya(k).isDirectory()){let m=ba.isAbsolute(k);throw new Error(`Root folder ${k} does not exist${m?"":` in the current directory ("${process.cwd()}")`}. Please run \`npx momentic init\` if you wish to setup Momentic here for the first time.`)}t&&Sn(t),g.info("Checking API key"),await vn(e);let l=`http://localhost:${r}`;n&&(l=`http://localhost:${n}`);let c=Ea(s);await new Promise(m=>{c.listen(r,()=>{g.info(`Server is running at http://localhost:${r}`),m()})});let u=new xe({baseURL:wn(),apiKey:e});if(o==="web"){await Cn("web",c,u,X),await wa(l);return}let h=await Aa({appUrl:l,apiGenerator:u,extensionPath:va(i,"extension"),onClose:async()=>{g.info("Browser closed, closing app."),await h.browser.cleanup(),c.close(()=>{process.exit(0)})}});await Cn("iframe",c,u,X,h),process.on("uncaughtException",m=>{X.error({err:m},"Uncaught exception in desktop-server")}),process.on("unhandledRejection",m=>{X.error({err:m},"Unhandled rejection in desktop-server")})}function Ea(t){let e=fo();e.use(fa()),e.use(ha.json({limit:"50mb"}));let o=fo.Router();if(o.use("/tests",gn),o.use("/modules",fn),o.use("/environments",hn),e.use("/api",o),e.use((n,s,i)=>{n.path!=="/healthcheck"&&X.debug({url:n.url,path:n.path,query:n.query,method:n.method,body:n.body,headers:n.rawHeaders,client:n.ip},"Received desktop-server request"),s.on("close",()=>{s.statusCode>=400&&X.error({url:n.url,method:n.method,statusCode:s.statusCode},"Request completed in error")}),i()}),e.use((n,s,i,a)=>{X.error({stack:n.stack,msg:n.message,url:s.url,method:s.method},"Unhandled exception leading to 500 on desktop-server"),i.status(500).send("Internal Server Error"),a(n)}),t){let n=fo.static(t);e.use("/",n),e.use("*",n)}return Sa.createServer(e)}async function Aa({appUrl:t,apiGenerator:e,extensionPath:o,onClose:r}){let n=await O.init({baseUrl:t,logger:X,browserArgs:{headless:!1,handleSIGTERM:!0},contextArgs:{bypassCSP:!0,viewport:null,deviceScaleFactor:void 0},localMode:!0,localAppUrl:t,extensionPath:o,onClose:r}),s=new se({browser:n,config:Te,generator:e,logger:X});return bn(s),s}async function Cn(t,e,o,r,n){Zr({baseServer:e,generator:o,logger:r,localApp:t,rootController:n})}import{existsSync as tl}from"fs";import Jn,{dirname as ol}from"path";import{fileURLToPath as rl}from"url";var Rn="0.0.31";var K="v1",J=class{baseURL;apiKey;constructor(e){this.baseURL=e.baseURL,this.apiKey=e.apiKey}async getRun(e){let o=await this.sendRequest(`/${K}/runs/${e}`,{method:"GET"});return tr.parse(o)}async createRun(e){let o=await this.sendRequest(`/${K}/runs`,{method:"POST",body:e});return er.parse(o)}async updateRun(e,o){await this.sendRequest(`/${K}/runs/${e}`,{method:"PATCH",body:o})}async getTest(e){let o=await this.sendRequest(`/${K}/tests/${e}`,{method:"GET"});return Jo.parse(o)}async getAllTestIds(){let e=await this.sendRequest(`/${K}/tests`,{method:"GET"});return Qo.parse(e)}async getTestYAMLExport(e){let o=await this.sendRequest(`/${K}/tests/export`,{method:"POST",body:e});return Zo.parse(o)}async updateTestWithYAML(e){await this.sendRequest(`/${K}/tests/update`,{method:"POST",body:e})}async queueTests(e){let o=await this.sendRequest(`/${K}/tests/queue`,{method:"POST",body:e});return Xo.parse(o)}async uploadScreenshot(e){let o=await this.sendRequest(`/${K}/screenshots`,{method:"POST",body:e});return or.parse(o)}async getAllEnvironments(){let e=await this.sendRequest(`/${K}/environments`,{method:"GET"});return nr.parse(e)}async updateEnvironments(e){await this.sendRequest(`/${K}/environments`,{method:"POST",body:e})}async sendRequest(e,o){let r=await fetch(`${this.baseURL}${e}`,{method:o.method,body:o.body?JSON.stringify(o.body):void 0,headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`}});if(!r.ok)throw new Error(`Request to ${e} failed with status ${r.status}: ${await r.text()}`);return r.status===204?r.text():r.json()}};import{createHash as Ln}from"crypto";import{existsSync as On,mkdirSync as Oa,readFileSync as wo,writeFileSync as Mn}from"fs";import{join as Nn}from"path";import{parse as Ma}from"yaml";import{existsSync as go,mkdirSync as Je,readdirSync as In,statSync as yo}from"fs";import{homedir as xa}from"os";import{join as Qe,resolve as Ia}from"path";import Ta from"chalk";import Ra from"readline/promises";async function Q(t,e){if(process.env.CI)return!0;let o=Ra.createInterface({input:process.stdin,output:process.stdout});t=`${t} (y/N) `;let r=e?Ta.bold.yellow(t):t,n=await o.question(r);return o.close(),n.toLowerCase()==="y"}var Tt=de,Ie=Qe(de,oe),So=Qe(de,Ho),q=Qe(de,ct),La=Qe(xa(),"momentic","chromium");function xn(t){return go(t)&&yo(t).isDirectory()}async function me(t,e=!1){xn(Tt)||(!e&&!await Q(`A '${de}' folder was not found in the current directory. Setup the required Momentic folder structure?`)&&(g.error("Setup cancelled"),process.exit(1)),Je(Tt),Je(Ie),Je(So),Je(q),Je(La,{recursive:!0})),(!xn(q)||In(q).length===0)&&(!e&&!await Q("You do not appear to have any environments configured locally. Pull environments from Momentic Cloud?")&&(g.error("Setup cancelled"),process.exit(1)),await Rt({client:t,all:!0,skipPrompts:e})),g.info("Setup complete!")}function Ze(t,e,o=new Set){for(let r of t){let n=Ia(r);if(n&&go(n)&&yo(n).isDirectory()){let s=In(n).map(i=>Qe(n,i));Ze(s,e,o);continue}if(n.endsWith(".yaml")){if(!go(n)||!yo(n).isFile())throw new Error(`File not found or unreadable: ${n}`);if(!e(n))continue;o.add(n)}}return o}async function Rt({envNames:t,client:e,all:o,skipPrompts:r}){let n=await e.getAllEnvironments();t&&!o&&(n=n.filter(a=>t?.includes(a.name))),On(q)||Oa(q);let s=0,i=0;for(let a of n){let l=Nn(q,`${a.name}.yaml`);if(!On(l))Mn(l,jt(a)),s++;else{let c=Ln("sha256").update(wo(l)).digest("hex"),d=jt(a),u=Ln("sha256").update(d).digest("hex");if(c!==u){if(!r&&!await Q(`Environment ${a.name} already exists on disk but needs to be updated. Overwrite?`)){g.error("Pull cancelled");return}Mn(l,d),i++}}}g.info(`Pulled ${s} new environments and updated ${i} existing environments`)}function Na(t){return t.endsWith(".yaml")?wo(t,"utf8").includes("momentic/environment")?!0:(g.warn(`Skipping YAML that is not a Momentic environment: ${t}`),!1):!1}async function Pn({client:t,names:e,all:o,yes:r}){let n;o?n=[q]:n=e.map(a=>Nn(q,`${a.toLowerCase()}.yaml`));let s=Ze(n,Na);g.info(`Found ${s.size} environments(s) to push:`),s.forEach(a=>g.info(` - ${a}`)),g.info("Loading file contents");let i=[];for(let a of s){let l=Ma(wo(a,"utf-8"));try{let c=V.parse(l);i.push(c)}catch(c){g.error({err:c},`${a} failed to parse as a valid environment file`),process.exit(1)}}if(!r&&!await Q("Pushing environments can overwrite variables on cloud and instantly affect scheduled runs. Continue?",!0)){g.error("Push cancelled");return}g.info(`Pushing ${s.size} environment(s)`),await t.updateEnvironments(i),g.info("Push successful!")}import{registry as bo}from"playwright-core/lib/server";function Pa(t){let e=[],o=[];for(let r of t){let n=bo.findExecutable(r);!n||n.installType==="none"?e.push(r):o.push(n)}return o}async function Dn(){let t=Pa(["chromium"]);await bo.installDeps(t,!1),await bo.install(t,!1)}import{Argument as xt,Option as Le}from"commander";var ue=new Le("--api-key <key>","API key for authentication. If not supplied, attempts to read the MOMENTIC_API_KEY env var.").env("MOMENTIC_API_KEY").makeOptionMandatory(!0),pe=new Le("--server <server>","Momentic server to use. Leave unchanged unless using Momentic on-premise.").default("https://api.momentic.ai"),he=new Le("-y, --yes","Skip confirmation prompts.").env("CI").default(!1),_n=new Le("--no-report","Skip reporting test results to Momentic Cloud when running with the --local flag.").default(!0),kn=new Le("--env <env>","Name of the environment to use when running tests."),vo=new Le("-a --all","Select all tests in your organization from Momentic Cloud. Cannot be used together with <tests> arguments.").default(!1).preset(!0),Eo=new Le("-a --all","Select all environments in your organization from Momentic Cloud. Cannot be used together with <paths> arguments.").default(!1).preset(!0),Fn=new xt("<tests...>",`One or more test paths to pull from Momentic Cloud.
35
35
 
36
36
  A test path is a lowercased version of your test name where spaces are replaced with underscores: 'npx momentic pull hello-world'.`).argOptional(),zn=new xt("<tests...>",`One or more test identifiers.
37
37
 
38
38
  To use tests stored on your local file system, pass '--local' and specify file paths to Momentic YAML files or folders that exist locally: 'npx momentic run --local momentic/hello-world.yaml'.
39
39
 
40
- To use tests stored remotely on Momentic Cloud, pass '--remote' and specify one or more test paths. A test path is a lowercased version of your test name where spaces are replaced with underscores: 'npx momentic run --remote hello-world'.`).argRequired(),Un=new xt("<paths...>","File paths pointing to one or more Momentic YAML files. Paths to directories are supported as well."),Ao=new xt("<envs...>","One or more environment names to push").argOptional();import{existsSync as _a,writeFileSync as $n}from"fs";import{join as Hn}from"path";async function jn({testsToFetch:t,client:e,all:o}){let{tests:r,modules:n}=await e.getTestYAMLExport({paths:t,all:o}),s=Object.keys(r).length;for(let[a,l]of Object.entries(r)){let c=Hn(Tt,Gn(a));if(!await Bn(c)){g.error("Pull cancelled");return}$n(c,l,"utf-8"),g.info(`Wrote '${c}'`)}let i=Object.keys(n).length;for(let[a,l]of Object.entries(n)){let c=Hn(Ie,Gn(a));if(!await Bn(c)){g.error("Pull cancelled");return}$n(c,l,"utf-8"),g.info(`Wrote '${c}'`)}g.info(`Pulled ${s} test${s>1?"s":""}${i?` and ${i} module${i>1?"s":""}`:""}!`)}async function Bn(t){return _a(t)?Q(`File '${t.replace(/(\s+)/g,"\\$1")}' already exists. Overwrite existing content?`,!0):!0}function Gn(t){return`${t.toLowerCase().replaceAll(" ","-")}.yaml`}import{existsSync as fy,readFileSync as ka}from"fs";async function Wn(t){let e=Ze(t.paths,Fa);g.info(`Found ${e.size} test(s) to push:`),e.forEach(n=>g.info(` - ${n}`)),g.info("Loading file contents and resolving dependent modules");let o=Array.from(e).map(n=>Kt(n,Ie)),r=new Set(o.flatMap(n=>Object.keys(n.modules)));if(g.info(`Resolved ${e.size} test(s) and ${r.size} module(s)`),!t.yes&&!await Q("Pushing tests overwrites tests on production and will instantly affect scheduled runs. Continue?",!0)){g.error("Push cancelled");return}await t.client.updateTestWithYAML(o),g.info("Update successful!")}function Fa(t){if(!t.endsWith(".yaml"))return!1;let e=ka(t,"utf8");return e.includes("momentic/test")?e.match(/localOnly:.*true/)?(g.warn(`Skipping local-only test: ${t}`),!1):!0:(g.warn(`Skipping YAML that is not a Momentic test: ${t}`),!1)}import{existsSync as qn,readFileSync as qa,statSync as Ya}from"fs";import{join as Xa}from"path";import Ja from"wait-on";import{parse as Qa}from"yaml";import za from"@actions/exec";import Ua from"@actions/io";import $a from"quote";import Ha from"string-argv";async function Vn(t,e=!0){let o=Ha(t),r=await Ua.which(o[0],!0),n=o.slice(1),s=za.exec($a(r),n,{delay:100});if(e)return s}import{v4 as Ka}from"uuid";import{existsSync as Ba,readFileSync as Ga}from"fs";import{join as ja}from"path";import{parse as Wa}from"yaml";function Va(t){let e=ja(So,`${t}.yaml`);if(!Ba(e))throw new Error(`Fixture '${t}' does not exist at expected path ${e}`);let o;try{o=Wa(Ga(e,"utf-8").replace(/\r\n|\r/g,`
40
+ To use tests stored remotely on Momentic Cloud, pass '--remote' and specify one or more test paths. A test path is a lowercased version of your test name where spaces are replaced with underscores: 'npx momentic run --remote hello-world'.`).argRequired(),Un=new xt("<paths...>","File paths pointing to one or more Momentic YAML files. Paths to directories are supported as well."),Ao=new xt("<envs...>","One or more environment names to push").argOptional();import{existsSync as _a,writeFileSync as $n}from"fs";import{join as Hn}from"path";async function jn({testsToFetch:t,client:e,all:o}){let{tests:r,modules:n}=await e.getTestYAMLExport({paths:t,all:o}),s=Object.keys(r).length;for(let[a,l]of Object.entries(r)){let c=Hn(Tt,Gn(a));if(!await Bn(c)){g.error("Pull cancelled");return}$n(c,l,"utf-8"),g.info(`Wrote '${c}'`)}let i=Object.keys(n).length;for(let[a,l]of Object.entries(n)){let c=Hn(Ie,Gn(a));if(!await Bn(c)){g.error("Pull cancelled");return}$n(c,l,"utf-8"),g.info(`Wrote '${c}'`)}g.info(`Pulled ${s} test${s>1?"s":""}${i?` and ${i} module${i>1?"s":""}`:""}!`)}async function Bn(t){return _a(t)?Q(`File '${t.replace(/(\s+)/g,"\\$1")}' already exists. Overwrite existing content?`,!0):!0}function Gn(t){return`${t.toLowerCase().replaceAll(" ","-")}.yaml`}import{existsSync as vy,readFileSync as ka}from"fs";async function Wn(t){let e=Ze(t.paths,Fa);g.info(`Found ${e.size} test(s) to push:`),e.forEach(n=>g.info(` - ${n}`)),g.info("Loading file contents and resolving dependent modules");let o=Array.from(e).map(n=>Kt(n,Ie)),r=new Set(o.flatMap(n=>Object.keys(n.modules)));if(g.info(`Resolved ${e.size} test(s) and ${r.size} module(s)`),!t.yes&&!await Q("Pushing tests overwrites tests on production and will instantly affect scheduled runs. Continue?",!0)){g.error("Push cancelled");return}await t.client.updateTestWithYAML(o),g.info("Update successful!")}function Fa(t){if(!t.endsWith(".yaml"))return!1;let e=ka(t,"utf8");return e.includes("momentic/test")?e.match(/localOnly:.*true/)?(g.warn(`Skipping local-only test: ${t}`),!1):!0:(g.warn(`Skipping YAML that is not a Momentic test: ${t}`),!1)}import{existsSync as qn,readFileSync as qa,statSync as Ya}from"fs";import{join as Xa}from"path";import Ja from"wait-on";import{parse as Qa}from"yaml";import za from"@actions/exec";import Ua from"@actions/io";import $a from"quote";import Ha from"string-argv";async function Vn(t,e=!0){let o=Ha(t),r=await Ua.which(o[0],!0),n=o.slice(1),s=za.exec($a(r),n,{delay:100});if(e)return s}import{v4 as Ka}from"uuid";import{existsSync as Ba,readFileSync as Ga}from"fs";import{join as ja}from"path";import{parse as Wa}from"yaml";function Va(t){let e=ja(So,`${t}.yaml`);if(!Ba(e))throw new Error(`Fixture '${t}' does not exist at expected path ${e}`);let o;try{o=Wa(Ga(e,"utf-8").replace(/\r\n|\r/g,`
41
41
  `))}catch(n){throw new Error(`Fixture at path ${e} does not parse as valid YAML: ${n}`)}let r;try{r=Fo.parse(o)}catch(n){throw new Error(`Fixture at path ${e} does not conform to expected schema: ${n}`)}return r}async function Co(t,e){let o=Va(t),r=e==="setup"?o.setup:o.teardown;if(!r){g.warn(`Fixture '${t}' does not have any steps in the ${e} phase, skipping...`);return}let n=r.timeout,s=async function(){for(let c=0;c<r.steps.length;c++){let d=r.steps[c],u=d.run,h=async m=>{try{await gr(`set -ex; ${u}`)}catch(f){if(m){let w=`Fixture '${t}' ${e} step ${c+1} errored in the background. The test will continue, but this may affect test execution and results: ${f}`;g.error(w)}else throw new Error(`Fixture '${t}' ${e} step ${c+1} errored: ${f}`)}};d.waitForCompletion===!1?h(!0):await h(!1)}};if(g.info(`Running ${e} phase of fixture '${t}'`),!n){await s();return}let i,a,l=async function(){return new Promise((c,d)=>{a=c,i=setTimeout(()=>{a=void 0,d(`Fixture '${t}' ${e} phase timed out after ${n} seconds`)},n*1e3)})};try{await Promise.race([s(),l()])}catch(c){throw c}finally{a&&a(),clearTimeout(i)}}async function Kn({path:t,apiClient:e,generator:o,useLocalFiles:r,noReport:n,environment:s,newBaseUrl:i}){let a;r?(g.info(`Reading ${t} from local filesystem`),a=await yt(t,Ie)):(g.info(`Fetching ${t} from Momentic Cloud server (${e.baseURL})`),a=await e.getTest(t)),a.schemaVersion>z&&g.warn(`Test ${t} has schema version ${a.schemaVersion}, which is greater than what is currently supported by this SDK. Please update your momentic package version to avoid unexpected behavior.`);let l=[],c={};s&&(l=a.envs?.find(E=>E.name===s.name)?.fixtures??[],c=s.variables);for(let y of l)await Co(y,"setup");let d;if(a.baseUrl){if(d=a.baseUrl,i){let y=new URL(a.baseUrl),E=new URL(i);y.hostname=E.hostname,y.protocol=E.protocol,y.port=E.port,d=y.toString()}}else d=c[at],d||(g.error(`Test ${a.name} has no baseURL and the environment has no ${at} variable either`),process.exit(1));let u=await O.init({baseUrl:d,logger:g}),h=new se({browser:u,generator:o,config:Te,logger:g}),m,f;if(n)f=Ka();else try{m=await e.createRun({testId:a.id,trigger:"CLI"}),f=m.id}catch(y){throw g.error(y),new Error(`Are you sure test ${a.name} exists on the server?`)}let w="FAILED";try{w=await $r({test:a,runId:f,controller:h,logger:g,takeScreenshots:!0,onSaveScreenshot:async y=>{if(n)return"";let{key:E}=await e.uploadScreenshot({screenshot:y.toString("base64")});return E},onUpdateRun:async y=>{n||await e.updateRun(f,y)}})}catch(y){throw n||await e.updateRun(f,{status:"FAILED",finishedAt:new Date}),n||g.error(`Test '${a.name}' failed. You can view a playback of this run at ${e.baseURL}/runs/${f}`),y}finally{try{for(let y of l)await Co(y,"teardown")}catch(y){g.error(`Failed to run teardown fixtures: ${y}`)}}return w}async function Yn({tests:t,start:e,waitOn:o,waitOnTimeout:r,client:n,all:s,noReport:i,env:a,parallelization:l=1}){e&&(g.info(`Running start command: ${e}`),await Vn(e,!1)),o&&(g.info(`Waiting for ${o} to be accessible (timeout: ${r}s)`),await Ja({resources:[o],timeout:r*1e3}));let c;if(a){let b=Xa(q,`${a}.yaml`),M=Qa(qa(b,"utf-8"));try{c=V.parse(M),g.info(`Running in the ${a} environment with the following variables:`),g.info(c.variables)}catch(v){g.error({err:v},`${b} failed to parse as a valid environment file`),process.exit(1)}}let d=new xe({baseURL:n.baseURL,apiKey:n.apiKey}),u=!1;(t.some(b=>b.endsWith(".yaml"))||t.some(b=>qn(b)))&&(u=!0);let h=new Set;if(u)g.info(t,"Reading tests from the following local file paths:"),t.forEach(b=>{if(!qn(b))throw new Error(`Path '${b}' does not exist.`);if(Ya(b).isDirectory())Ve(b).map(v=>v.fullFilePath).forEach(v=>h.add(v));else if(b.endsWith(".yaml"))h.add(b);else throw new Error(`Path '${b}' is not a directory or a .yaml file.`)});else if(g.warn("The paths you specified are not files or directories that exist locally."),g.warn(`Fetching tests from Momentic Cloud (${n.baseURL}) instead...`),s)for(let b of await n.getAllTestIds())h.add(b);else h=new Set(t);let m=Array.from(h);g.info(m,`Identified ${m.length} tests to run locally:`);let f=[];for(let b=0;b<m.length;b+=l){let M=await Promise.all(m.slice(b,b+l).map(async v=>{let C="FAILED";try{C=await Kn({useLocalFiles:u,path:v,apiClient:n,generator:d,newBaseUrl:o,environment:c,noReport:i})}catch(j){let R=j instanceof Error?j.message:`${j}`;g.error(`Test ${v} failed with error: ${R}`)}return{runStatus:C,path:v}}));f=f.concat(M)}let w=f.filter(b=>b.runStatus==="PASSED");w.length>0&&(g.info(`Passed ${w.length} out of ${f.length} tests:`),w.forEach(b=>{g.info(`- ${b.path}`)}));let y=f.filter(b=>b.runStatus==="CANCELLED");y.length>0&&(g.warn(`Cancelled ${y.length} out of ${f.length} tests:`),y.forEach(b=>{g.warn(`- ${b.path}`)}));let E=f.filter(b=>b.runStatus==="FAILED");E.length>0&&(g.error(`Failed ${E.length} out of ${f.length} tests:`),E.forEach(b=>{g.error(`- ${b.path}`)}),process.exit(1)),process.exit(0)}async function Xn({tests:t,client:e,env:o,all:r}){let{queuedTests:n}=await e.queueTests({testPaths:t,env:o,all:r});g.info(`Successfully queued ${n.length} tests!`)}var Z=new Za;Z.name("momentic").description("Momentic CLI").version(Rn);Z.command("install-browsers").action(async()=>{await Dn()});Z.addOption(new ie("--log-level <level>").choices(["debug","info","warn","error"]).default("info")).on("option:log-level",t=>{g.setMinLevel(Vo[t.toUpperCase()])});Z.command("init").addOption(he).addOption(ue).addOption(pe).action(async t=>{let{yes:e,apiKey:o,server:r}=t,n=new J({baseURL:r,apiKey:o});await me(n,e)});Z.command("app").addOption(ue).addOption(pe).addOption(he).addOption(new ie("--port <port>","Port to run the app on. Defaults to 58888.").default(58888)).addOption(new ie("--mode <mode>","Mode to run the app in. Defaults to iframe.").choices(["iframe","web"]).default("iframe")).action(async t=>{let{apiKey:e,port:o,yes:r,server:n,mode:s}=t,i=new J({baseURL:n,apiKey:e});await me(i,r);let a=rl(import.meta.url),l=ol(a),c=Jn.resolve(l,"..","static"),d=Jn.resolve(l,"..","assets");await Tn({momenticServerUrl:n,mode:s,apiKey:e,serverPort:o,appPort:o,staticDir:c,assetsDir:d})});Z.command("run").alias("run-tests").addOption(ue).addOption(pe).addOption(vo).addOption(he).addOption(new ie("-r, --remote","Run tests remotely. The production version of this test will be queued for execution.").default(!0).conflicts(["start, waitOn, waitOnTimeout, local"]).implies({local:!1})).addOption(_n).addOption(new ie("-l, --local","Run tests locally. Useful for accessing apps on localhost. This option does not control where tests are read from (see <tests> argument documentation).").implies({remote:!1,noReport:!0})).addOption(new ie("--start <command>","Arbitrary setup command that will run before Momentic steps begin.")).addOption(new ie("--wait-on <url>","URL to wait to become accessible before Momentic tests begin.")).addOption(kn).addOption(new ie("--wait-on-timeout <timeout>","Max time to wait for the --wait-on URL to become accessible.").default(60,"one minute")).addOption(new ie("-p, --parallel <parallelization>","When running with the --local flag, the number of tests to run in parallel. Defaults to 1.").default(1)).addArgument(zn).action(async(t,e)=>{let{apiKey:o,server:r,remote:n,local:s,all:i,env:a,yes:l}=e,c=new J({baseURL:r,apiKey:o});if(await me(c,l),s){try{await Yn({tests:t,client:c,...e})}catch(d){g.error(d),process.exit(1)}return}if(n){for(let d of t)(d.endsWith(".yaml")||tl(d))&&(g.error(el`'${d}' looks like a local file or directory, but the --local flag was not supplied.
42
42
  Please supply the --local flag to run tests locally, or specify the test path without its file extension to queue tests remotely (e.g. 'hello-world').`),process.exit(1));await Xn({tests:t,client:c,all:i,env:a});return}g.error("One of --remote or --local must be specified"),process.exit(1)});Z.command("pull").description("Fetch one or more tests from Momentic Cloud and save it in to local disk in YAML format.").addOption(ue).addOption(pe).addOption(vo).addOption(he).addArgument(Fn).action(async(t,e)=>{let{apiKey:o,server:r,all:n,yes:s}=e,i=new J({baseURL:r,apiKey:o});if(await me(i,s),!n&&!t?.length)throw new Error("At least one test name or --all must be provided");await jn({testsToFetch:t,client:i,all:n})});Z.command("push").description("Save one or more tests in YAML format to Momentic Cloud.").addOption(he).addOption(ue).addOption(pe).addArgument(Un).action(async(t,e)=>{let{apiKey:o,server:r,yes:n}=e;await me(o,n);let s=new J({baseURL:r,apiKey:o});await Wn({paths:t,client:s,yes:n})});var Qn=Z.command("env").description("Perform operations on Momentic environments");Qn.command("push").description("Push one or more environments and associated variables to Momentic cloud.").addOption(he).addOption(ue).addOption(pe).addOption(Eo).addArgument(Ao).action(async(t,e)=>{let{apiKey:o,server:r,yes:n,all:s}=e;!t?.length&&!s&&(g.error("At least one environment name or --all must be provided"),process.exit(1)),await me(o,n);let i=new J({baseURL:r,apiKey:o});await Pn({names:t,client:i,yes:n,all:s})});Qn.command("pull").description("Pull one or more environments and associated variables from Momentic cloud.").addOption(he).addOption(ue).addOption(pe).addOption(Eo).addArgument(Ao).action(async(t,e)=>{let{apiKey:o,server:r,yes:n,all:s}=e;!t?.length&&!s&&(g.error("At least one environment name or --all must be provided"),process.exit(1)),await me(o,n);let i=new J({baseURL:r,apiKey:o});await Rt({envNames:t,client:i,skipPrompts:n,all:s})});async function nl(){await Z.parseAsync(process.argv)}nl();
package/dist/index.js CHANGED
@@ -19,12 +19,12 @@ import Bt from"dedent";import*as a from"zod";import*as M from"zod";var pe=M.obje
19
19
  ${g}${l}</${s}>
20
20
  `}if(e.neighbors!==void 0&&e.neighbors>0&&this.parent){let g=this.parent.children.findIndex(E=>E.id===this.id),S=g>0?this.parent.children[g-1]?.serialize({...e,neighbors:0}):"",x=g<this.parent.children.length-1?this.parent.children[g+1]?.serialize({...e,neighbors:0}):"";return`${S||""}
21
21
  ${p}
22
- ${x||""}`}return p}},Be=class{constructor(e,t,o){this.root=e;this.a11yIdNodeMap=t;this.dataMomenticIdMap=o}serialize(){return this.root?this.root.serialize():""}};function $o(r){return r.name?.value?`"${r.name.value}"`:r.role?.value&&r.role.value!=="none"&&r.role.value!=="generic"?`"${r.role.value}"`:`"${r.nodeId}"`}function bt(r,e,t){if(!e&&r.parentId)throw new Error(`Got no parent for accessibility node ${r.nodeId}: ${JSON.stringify(r)}`);let o=new He({id:parseInt(r.nodeId),role:r.role?.value||"",name:r.name?.value||"",content:r.value?.value||"",properties:r.properties,children:[],pathFromRoot:(e?`${e.pathFromRoot} `:"")+$o(r),backendNodeID:r.backendDOMNodeId,ignoredByCDP:r.ignored}),i=r.childIds??[];for(let s of i){if(!s)continue;let c=t.get(parseInt(s));if(!c)continue;let m=bt(c,o,t);m.length&&(o.children=o.children.concat(m))}if(o.role==="StaticText"&&(o.children=[]),o.children.length===1&&o.children[0].role==="StaticText"){let s=o.name,c=o.children[0]?.name;(s===c||!c)&&(o.children=[])}let n=[];for(let s=o.children.length-1;s>=0;s--){let c=o.children[s];if(c.role!=="StaticText"){n.push(c);continue}if(s===0||o.children[s-1].role!=="StaticText"){n.push(c);continue}o.children[s-1].name+=` ${c.name}`}if(o.children=n.reverse(),o.role==="generic"&&o.children.length===1){let s=o.children[0];if(o.name&&!Ge.includes(s.role)&&o.name===s.name)return o.children}if(!o.isInteresting()&&r.parentId)return o.children;for(let s of o.children)s.parent=o;return[o]}function St(r,e,t,o,i=1){r.id=i,i+=1,e.set(r.id,r),r.dataMomenticId?t.set(r.dataMomenticId,r):Ge.includes(r.role)||o.debug({node:r.serialize({neighbors:1,maxLevel:1})},"Node has no data-momentic-id");for(let n of r.children)i=St(n,e,t,o,i);return i}function Ct(r,e){if(!r.root)throw new Error("a11y tree has null root");r.allNodes=r.allNodes.filter(l=>l.ignored?!l.ignoredReasons?.find(c=>Ho.includes(c.name)):!0);let t=new Map;for(let l of r.allNodes)t.set(parseInt(l.nodeId),l);let o=bt(r.root,null,t);if(o.length>1)throw new Error(`Something went horribly wrong processing the a11y tree, we got: ${JSON.stringify(o)}`);if(o.length===0)throw new fe;let i=new Map,n=new Map;return St(o[0],i,n,e),new Be(o[0],i,n)}var de=(r,e)=>{e.id=r.id,e.content=r.content,e.name=r.name,e.role=r.role,e.numChildren=r.children.length,e.serializedForm=r.serialize({noID:!0,maxLevel:1,neighbors:1}),e.nodeOnlySerializedForm=r.serialize({noID:!0,noChildren:!0,noProperties:!0})},$e=(r,e)=>{let t=1;r.role===e.role&&t++;let o=["name","content"];for(let i of o){if(!r[i]?.trim())continue;let n=Ue(r[i],e[i])/Math.min(r[i].length,e[i].length);n===0?t+=2:n<=.1&&t++}if(e.numChildren!==void 0&&(r.children.length===e.numChildren&&e.numChildren>0?t++:(e.numChildren>0&&r.children.length===0||Math.abs(r.children.length-e.numChildren)>2)&&t--),e.nodeOnlySerializedForm){let i=r.serialize({noID:!0,noChildren:!0,noProperties:!0});Ue(i,e.nodeOnlySerializedForm)/Math.min(i.length,e.nodeOnlySerializedForm.length)<=.1&&t++}if(e.serializedForm){let i=r.serialize({noID:!0,maxLevel:1,neighbors:1}),n=Ue(i,e.serializedForm)/Math.min(i.length,e.serializedForm.length);n===0?t+=2:n<=.1&&t++}return t};var X={r:147,g:196,b:125,a:.55},vt={showInfo:!1,showRulers:!1,showStyles:!1,showAccessibilityInfo:!1,showExtensionLines:!1,contrastAlgorithm:"aa",contentColor:X,paddingColor:X,borderColor:X,marginColor:X,eventTargetColor:X,shapeColor:X,shapeMarginColor:X};var _=(r=1e3)=>new Promise(e=>setTimeout(()=>e(),r));function Et(){window.cursor||(window.cursor=document.createElement("img"),window.cursor.setAttribute("src","data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjMyIiB2aWV3Qm94PSIwIDAgMzIgMzIiIHdpZHRoPSIzMiIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48ZyBmaWxsPSJub25lIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDEwIDcpIj48cGF0aCBkPSJtNi4xNDggMTguNDczIDEuODYzLTEuMDAzIDEuNjE1LS44MzktMi41NjgtNC44MTZoNC4zMzJsLTExLjM3OS0xMS40MDh2MTYuMDE1bDMuMzE2LTMuMjIxeiIgZmlsbD0iI2ZmZiIvPjxwYXRoIGQ9Im02LjQzMSAxNyAxLjc2NS0uOTQxLTIuNzc1LTUuMjAyaDMuNjA0bC04LjAyNS04LjA0M3YxMS4xODhsMi41My0yLjQ0MnoiIGZpbGw9IiMwMDAiLz48L2c+PC9zdmc+"),window.cursor.setAttribute("id","momentic_cursor"),window.cursor.setAttribute("style","position: absolute; z-index: 99999999999; pointer-events: none; left:0; top:0; height:4%;"),window.cursor.style.filter="invert(0%) sepia(6%) saturate(24%) hue-rotate(315deg) brightness(89%) contrast(110%)",document.body.appendChild(cursor),document.onmousemove=function(r){r=r||window.event,document.getElementById("momentic_cursor").style.left=r.pageX+"px",document.getElementById("momentic_cursor").style.top=r.pageY+"px"})}function At(){window.globalHintManager||(window.globalHintManager=new window.HintManager),window.globalHintManager.capture()}function Tt(){window.globalHintManager&&window.globalHintManager.reset()}function Rt(){let r=document.body.getElementsByTagName("*"),e=1;for(let t=0;t<r.length;t++){let o=e.toString();for(;[6].some(n=>o.includes(n.toString()));)e++,o=e.toString();let i=r[t];i?.setAttribute("data-momentic-id",`${e}`),i?.setAttribute("aria-keyshortcuts",`${e}`),e++}}var Wo=new Set(["document","script","XMLHttpRequest","fetch","xhr"]),jo=new Set(["script","document"]),Vo=["intercom.io","googletagmanager.com","google-analytics.com","www.gstatic.com","gstatic.com","apis.google.com","sentry.io","newrelic.com","p.retool.com","m.stripe.com","m.stripe.network","js.stripe.com","assets.trybento.co","udon.trybento.co","cdn.lr-in-prod.com","r.lr-in-prod.com","content.product-usage.assembledhq.com","data.product-usage.assembledhq.com","static.zdassets.com","o.clarity.ms/collect"],Ko=["api.stripe.com","supabase.co"],Yo=[];async function xt(r){for(let e of Yo)await r.route(e,t=>t.abort())}function We(r){return`${r.resourceType()} ${r.method()} ${r.url()}`}function It(r){return r=r.replace(/^www\./,""),r}function Lt(r){return Ko.some(e=>r.includes(e))}function Ot(r,e){if(!Wo.has(r.resourceType()))return!1;let t=new URL(e),o=new URL(r.url());return Vo.some(i=>o.hostname.includes(i))?!1:jo.has(r.resourceType())||r.method()!=="GET"?!0:It(o.hostname).includes(It(t.hostname))}var Nt=()=>{window.clickListener&&document.removeEventListener("click",window.clickListener,{capture:!0}),window.clickListener=async r=>{console.log("[Momentic] Window click listener fired",r.target),await window.captureClick(r.target)},document.addEventListener("click",window.clickListener,{capture:!0})};var Mt=()=>{window.pressListener&&document.removeEventListener("keydown",window.pressListener,{capture:!0}),window.pressListener=async r=>{console.log("[Momentic] Window press listener fired"),await window.captureKeystroke({key:r.key})},document.addEventListener("keydown",window.pressListener,{capture:!0})};var Ce=cr(lr);Ce.use(mr());Ce.use(dr({provider:{id:"2captcha",token:process.env.TWO_CAPTCHA_KEY},visualFeedback:!0}));var Ve=class r{browser;context;page;a11yIdToNodeMap=new Map;dataMomenticIdToNodeMap=new Map;cdpClient;logger;localMode;activeFrame;transformer;mostRecentBrowserState;baseURL;constructor({browser:e,context:t,page:o,baseUrl:i,logger:n,localMode:l,cdpClient:s}){this.browser=e,this.context=t,this.cdpClient=s,this.page=o,this.baseURL=i,this.logger=n,this.localMode=!!l}static USER_AGENT=Pt["Desktop Chrome"].userAgent;static VIEWPORT={width:1920,height:1080};static async init({baseUrl:e,logger:t,browserArgs:o,contextArgs:i,takeScreenshots:n,onScreenshot:l,onClose:s,localMode:c,localAppUrl:m,extensionPath:u,timeout:p=8e3}){let I={headless:!0,handleSIGTERM:!1,chromiumSandbox:!1,...o??{}},g={viewport:r.VIEWPORT,deviceScaleFactor:process.platform==="darwin"?2:1,userAgent:Pt["Desktop Chrome"].userAgent,geolocation:{latitude:37.7749,longitude:-122.4194},locale:"en-US",timezoneId:"America/Los_Angeles",...i??{}},S=null,x,E;if(c){let y=ar(sr(),"momentic","chromium");x=await Ce.launchPersistentContext(y,{...I,...g,ignoreDefaultArgs:["--enable-automation","--enable-strict-mixed-content-checking"],ignoreHTTPSErrors:!0,bypassCSP:!0,args:["--allow-insecure-localhost","--disable-site-isolation-for-policy","--disable-site-isolation-trials",`--unsafely-treat-insecure-origin-as-secure=${m}`,`--load-extension=${u}`,"--test-type=browser"],baseURL:e}),E=x.pages()[0],s&&E.on("close",()=>{s()})}else S=await Ce.launch(I),x=await S.newContext({...g,baseURL:e}),E=await x.newPage();await xt(x);let k=new r({browser:S,context:x,page:E,baseUrl:e,logger:t,localMode:c,cdpClient:await x.newCDPSession(E)}),N=!1;(async()=>{try{await k.navigate({url:e,wrapPossibleNavigation:!1,initialNavigation:!0})}catch(y){t.error({err:y},"Failed to initialize chrome browser")}finally{N=!0}})();let f=async()=>{if(n)try{l?.({viewport:await k.viewport(),buffer:await k.screenshot()})}catch(y){t.error({err:y},"Failed to take screenshot")}};f();let A=setInterval(()=>{f()},250),me=Date.now();for(;!N&&Date.now()-me<p;)await _(250);return clearInterval(A),N||t.warn("Timeout elapsed waiting for browser to initialize - are you sure this page is accessible?"),k}async getUserPageOrFrame(){if(this.localMode&&this.activeFrame){let e=null,t=0;for(;!e&&t<5;)e=this.page.frame(this.activeFrame),await _(100),t++;if(!e)throw new w("InternalWebAgentError",`Failed to find frame ${this.activeFrame} on page ${this.page.url()}`);return e}return this.page}async initCDPSession(){let e=!1,t=2,o=async()=>{try{this.cdpClient=await this.context.newCDPSession(this.page),await this.cdpClient.send("Accessibility.enable"),await this.cdpClient.send("DOM.enable"),await this.cdpClient.send("Overlay.enable"),e=!0}catch(i){if(t>0)return this.logger.error({err:i},"Failed to initialize CDP session, re-creating CDP client"),await _(250),t--,o();throw i}};if(await Promise.race([o(),_(3e3)]),!e)throw new w("InternalWebAgentError","Timeout elapsed waiting for CDP session to initialize")}setLogger(e){this.logger=e}ping(){if(this.closed)throw new Error("Page has been closed");if(this.browser&&!this.browser.isConnected())throw new Error("Browser is not connected")}setActiveFrame(e){this.activeFrame=e}async reset(e){this.a11yIdToNodeMap.clear(),this.dataMomenticIdToNodeMap.clear();let t=this.context.pages();this.page=t[0];for(let o=1;o<t.length;o++)await t[o].close();if(e.clearCookies&&await this.context.clearCookies(),!this.page.isClosed()){if(e.clearStorage)try{await(await this.getUserPageOrFrame()).evaluate(async()=>{window.localStorage.clear(),window.sessionStorage.clear(),await indexedDB.databases().then(o=>{o.forEach(i=>{i.name&&indexedDB.deleteDatabase(i.name)})})})}catch(o){this.logger.warn({err:o},"Failed clearing site data, continuing...")}await this.navigate({url:e.url??this.baseURL,wrapPossibleNavigation:!1,initialNavigation:!0})}}async pageSetup(){try{this.localMode||await this.page.evaluate(Et)}catch{}}async wait(e){await _(e)}async toggleHints(e){let t=await this.getUserPageOrFrame();e.state==="on"?(await t.addStyleTag({content:_e.css}),await t.addScriptTag({content:_e.js}),await t.evaluate(At)):await t.evaluate(Tt)}async showHints(){await this.toggleHints({state:"on"});let e=async()=>{try{await this.toggleHints({state:"off"})}catch(t){this.logger.debug({err:t},"Failed to remove vision hints")}};setTimeout(()=>{e()},3e3)}async cleanup(){await this.page.close(),await this.context.close(),this.browser&&await this.browser.close()}get closed(){return this.page.isClosed()||!!this.browser&&!this.browser.isConnected()}async html(){return(await this.getUserPageOrFrame()).content()}async url(){return(await this.getUserPageOrFrame()).url()}async screenshotWithHints(e=100,t="device",o="/tmp/screenshots/test.jpg"){let i=o?.split("."),n=i?.slice(0,-1).join("."),l=i?.slice(-1)[0],s=Buffer.from("");await this.showHints();let c=await this.screenshot(e,t,o?`${n}-after-hint.${l}`:void 0);return{before:s,after:c}}async screenshot(e=100,t="device",o){let i={fullPage:!1,quality:e,scale:t,type:"jpeg",caret:"initial",path:o};return!this.localMode||!this.activeFrame?this.page.screenshot(i):this.page.locator(`iframe[name="${this.activeFrame}"]`).screenshot(i)}async viewport(){if(this.localMode&&this.activeFrame){let t=await this.page.locator(`iframe[name="${this.activeFrame}"]`).boundingBox();if(!t)throw new Error(`Failed to get bounding box for frame: ${this.activeFrame}`);return t}let e=this.page.viewportSize();if(!e)throw new Error("failed to get viewport");return e}async navigate({url:e,wrapPossibleNavigation:t=!0,initialNavigation:o=!1}){this.logger.debug(`Navigating to ${e}`);let i=Date.now(),n=async()=>{try{await(await this.getUserPageOrFrame()).goto(e,{waitUntil:"load",timeout:8e3}),this.logger.debug({url:e},`Got load event in ${Math.floor(Date.now()-i)}ms`)}catch{this.logger.warn({url:e},"Timeout elapsed waiting for page to fire load event, continuing anyways...")}};t?await this.wrapPossibleNavigation(n):await n();let l=await this.url();if(gt.has(l)&&process.env.NODE_ENV==="production")throw new w("ActionFailureError",`${e} took too long to load \u{1F61E}. Please ensure the site and your internet are working.`,{},!0);if(o&&(await this.initCDPSession(),await this.pageSetup(),await this.attachPageLoadListeners(),this.localMode))try{await this.exposeRecordingBindings()}catch(s){s instanceof Error&&s.message.includes("already registered")||this.logger.error({err:s},"Failed to expose recording bindings during navigation")}this.logger.info({url:e},"Navigation complete")}async type(e,t={}){let{clearContent:o=!0,pressKeysSequentially:i=!1}=t;o&&(process.platform==="darwin"?await this.page.keyboard.press("Meta+A"):await this.page.keyboard.press("Control+A"),await this.page.keyboard.press("Backspace")),i?await this.page.keyboard.type(e):await this.page.keyboard.insertText(e)}async clickByA11yID(e,t={}){let o=this.a11yIdToNodeMap.get(e);if(!o)throw new Error(`Could not find DOM node during click: ${e}`);let i=await this.clickUsingCDP(o,t);return await this.highlightNode(i),o.serialize({noChildren:!0,noProperties:!0,noID:!0})}async selectOptionByA11yID(e,t){let o=this.a11yIdToNodeMap.get(e);if(!o)throw new Error(`Could not find DOM node while selecting option: ${e}`);if(!o.backendNodeID)throw new Error(`Select target missing backend node id: ${o.getLogForm()}`);return await(await this.getLocatorFromBackendID(o.backendNodeID)).selectOption(t,{timeout:8e3}),await this.highlightNode(o),o.serialize({noChildren:!0,noProperties:!0,noID:!0})}async scrollIntoView(e){let t=await this.resolveCachedTargetToID(e),o=this.a11yIdToNodeMap.get(t);if(!o)throw new Error(`Could not find node in DOM with a11y id: ${t}`);if(!o.backendNodeID)throw new Error(`Focus target missing backend node id: ${o.getLogForm()}`);await(await this.getLocatorFromBackendID(o.backendNodeID)).scrollIntoViewIfNeeded({timeout:8e3})}async highlight(e){try{let t=await this.resolveCachedTargetToID(e),o=this.a11yIdToNodeMap.get(t);if(!o)throw new Error(`Could not find DOM node during highlight: ${t}`);if(!o.backendNodeID)throw new Error(`Highlight target missing backend node id: ${o.getLogForm()}`);await this.highlightNode(o)}catch(t){this.logger.warn({err:t,target:e},"Failed to highlight target")}}async highlightNode(e){try{await this.cdpClient.send("Overlay.highlightNode",{highlightConfig:vt,backendNodeId:e.backendNodeID})}catch{this.logger.warn("Failed to add node highlight, a page navigation likely occurred. This is non-fatal for tests.")}let t=async()=>{try{await this.cdpClient.send("Overlay.hideHighlight",{backendNodeId:e.backendNodeID})}catch(o){this.logger.debug({err:o},"Failed to remove node highlight")}};setTimeout(()=>{t()},3e3)}async wrapPossibleNavigation(e,t=8e3,o=!0){let i=Date.now(),n=await this.url(),l=Date.now(),s=new Map,c=new Map,m=f=>{let A=We(f.request());c.set(A,(c.get(A)??0)+1);let me=f.status();me>=500&&this.logger.warn({request:A,status:me},"Received 500 level response")},u=f=>{if(!Ot(f,n))return;let A=We(f);s.set(A,(s.get(A)??0)+1),l=Date.now()};this.page.on("response",m),this.page.on("request",u);let p=[];o&&(p=this.context.pages().map(f=>f.url()));let I=!1,g=e().catch(f=>(I=!0,f instanceof Error?f:new Error(`${f}`)));await _(250);let S=async f=>{let A=await f;if(A instanceof Error)throw A;return A},x=new Set,E=!1,N=await(async()=>{for(;!I&&!(!E&&Date.now()-i>t);){if(await _(250),E=!1,x=new Set,Date.now()-l<=1250)continue;let f=!1;for(let A of s.keys())s.get(A)!==c.get(A)&&(Lt(A)&&(E=!0),f=!0,x.add(A));if(!f)return this.logger.debug({url:await this.url(),requests:JSON.stringify(Array.from(s.entries()))},`Network idle in ${Math.floor(Date.now()-i)}ms`),!0}return!I&&x.size>0&&this.logger.warn({url:await this.url(),unfinishedRequests:JSON.stringify(Array.from(x.entries()))},"Timeout elapsed waiting for network idle, continuing anyways..."),!1})();if(this.page.off("response",m),this.page.off("request",u),!N)return S(g);let O=await this.url();if(!I&&Y(O,n)){this.logger.debug({startUrl:n,newUrl:O},"Detected url change in wrapPossibleNavigation, waiting for load state");let f=Math.max(t-(Date.now()-i),0);if(f>0)try{await(await this.getUserPageOrFrame()).waitForLoadState("load",{timeout:f})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for load state, continuing anyways...")}}if(o){let f=this.context.pages().map(A=>A.url());if(f.length>p.length)for(let A of f)A!==O&&await this.switchToPage(A)}return S(g)}async resolveCachedTargetToID(e){if(!ue(e)){let s=this.a11yIdToNodeMap.get(e.id);if(!s)throw new Error(`Resolving target failed, fresh value did not exist in node map: ${e.id}`);return de(s,e),e.id}let t=(await this.getA11yTree()).serialize();this.logger.debug({tree:t},"Refreshed a11y tree before resolving target");let o=this.a11yIdToNodeMap.get(e.id);if(o){let s=$e(o,e);if(s>=5)return this.logger.debug({target:e,proposedNode:o.getLogForm(),comparisonScore:s},"Resolved cached a11y target to node with exact same id"),de(o,e),e.id}let i=1/0,n=1/0,l;for(let s of this.a11yIdToNodeMap.values()){let c=$e(s,e);if(c>=5)return this.logger.debug({newNode:s.getLogForm(),target:e,comparisonScore:c},"Resolved cached a11y target to new node with field comparison"),de(s,e),s.id;if(!e.serializedForm)continue;let m=s.serialize({noID:!0,maxLevel:1,neighbors:1});if(Math.abs(m.length-e.serializedForm.length)>15)continue;let u=nr(e.serializedForm,m),p=u/Math.min(e.serializedForm.length,m.length);u<i&&p<.2&&(i=u,n=p,l=s)}if(l&&i<15)return this.logger.debug({newNode:l.getLogForm(),target:e,distance:i,ratio:n},"Resolved cached a11y target to new node with pure levenshtein distance"),de(l,e),l.id;throw new Error(`Could not find any relevant node given cached target: ${JSON.stringify({target:e,closestLevenshteinDistance:i,closestNode:l})}`)}async click(e,t={}){let o=await this.resolveCachedTargetToID(e);return await this.wrapPossibleNavigation(()=>this.clickByA11yID(o,t))}async hover(e){let t=await this.resolveCachedTargetToID(e),o=this.a11yIdToNodeMap.get(t);if(!o)throw new Error(`Could not find DOM node for hover: ${t}`);if(!o.backendNodeID)throw new Error(`Hover target missing backend node id: ${o.getLogForm()}`);return await(await this.getLocatorFromBackendID(o.backendNodeID)).hover({timeout:8e3}),await this.highlightNode(o),o.serialize({noChildren:!0,noProperties:!0,noID:!0})}async selectOption(e,t){let o=await this.resolveCachedTargetToID(e);return this.selectOptionByA11yID(o,t)}async press(e){await this.wrapPossibleNavigation(()=>this.page.keyboard.press(e))}async refresh(){if(this.localMode&&this.activeFrame){let t=(await this.getUserPageOrFrame()).url();await this.navigate({url:t,wrapPossibleNavigation:!0})}else try{await this.page.reload({timeout:3e3})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for page on reload, continuing anyways...")}}async getA11yTree(e=!1){let t=null,o=async()=>{let i=0,n=await this.url();for(;!t&&i<3;)try{let l=await this.getRawA11yTree(e);if(!l.root||l.allNodes.length<=1)throw new Error("No a11y tree found on page");t=Ct(l,this.logger)}catch(l){if(this.logger.error({err:l,url:n},"Error fetching a11y tree"),i===0)await _(1e3),i++;else throw new Error(`Max retries exceeded fetching a11y tree: ${l}`)}if(!t||!t.root)throw new Error("Accessibility tree appears empty");this.a11yIdToNodeMap=t.a11yIdNodeMap,this.dataMomenticIdToNodeMap=t.dataMomenticIdMap};if(await Promise.race([o(),_(8e3)]),t)return t;throw new w("ActionFailureError",`Getting accessibility tree timed out after ${8e3}ms`)}getA11yIdFromDataMomenticId(e){return this.dataMomenticIdToNodeMap.get(e)?.id}async getRawA11yTree(e=!1){let t=await this.url(),o=Date.now(),i=()=>{o=Date.now()};this.cdpClient.addListener("Accessibility.nodesUpdated",i);let n=!1,l=()=>{this.logger.info({url:t},"Load event fired on page"),n=!0,o=Date.now()};this.cdpClient.addListener("Accessibility.loadComplete",l);let s=Date.now(),c=!e;for(;!e&&Date.now()-s<3e3;){if(await _(250),!n&&Date.now()-s<1e3){process.env.NODE_ENV!=="production"&&this.logger.debug({url:t},"A11y tree not loaded yet, waiting...");continue}if(Date.now()-o>=1250){c=!1;break}this.logger.debug({url:t},"A11y tree not stable yet, waiting...")}this.logger.debug({duration:Date.now()-s,eventReceived:n,timeoutTriggered:c,skipWait:e},"A11y wait phase completed"),await(await this.getUserPageOrFrame()).evaluate(Rt);let m;if(this.localMode&&this.activeFrame){let{result:{objectId:p}}=await this.cdpClient.send("Runtime.evaluate",{expression:`document.querySelector("#${this.activeFrame}")`}),I=await this.cdpClient.send("DOM.describeNode",{objectId:p});m=(await this.cdpClient.send("Accessibility.getRootAXNode",{frameId:I.node.frameId})).node.backendDOMNodeId}else{let{node:p}=await this.cdpClient.send("Accessibility.getRootAXNode");m=p.backendDOMNodeId}let{nodes:u}=await this.cdpClient.send("Accessibility.queryAXTree",{backendNodeId:m});return this.cdpClient.removeListener("Accessibility.loadComplete",l),this.cdpClient.removeListener("Accessibility.nodesUpdated",i),{root:u[0],allNodes:u}}async clickUsingVisualCoordinates(e){let t=await this.getElementLocation(e);if(!t)throw new Error(`Could not find element location with backend node id: ${e}`);this.logger.debug({location:t},"Executing mouse click"),await this.page.mouse.click(t.centerX,t.centerY)}async getIDAttributeUsingCDP(e){await this.cdpClient.send("DOM.getDocument",{depth:0});let t=await this.cdpClient.send("DOM.requestNode",{objectId:e}),i=(await this.cdpClient.send("DOM.getAttributes",{nodeId:t.nodeId})).attributes,n=i.findIndex(l=>l===ee);if(n===-1||!i[n+1])throw new Error(`Could not find ${ee} for object ${e}`);return i[n+1]}async getLocatorFromBackendID(e){let t=await this.cdpClient.send("DOM.resolveNode",{backendNodeId:e});if(!t||!t.object.objectId)throw new Error(`Could not resolve backend node ${e}`);let o;try{o=await this.getIDAttributeUsingCDP(t.object.objectId)}catch(i){throw this.logger.error({err:i,object:JSON.stringify(t.object)},"Failed to get ID attribute"),i}return(await this.getUserPageOrFrame()).locator(`[${ee}="${o}"]`)}async clickUsingCDP(e,t={}){let o=0,i=e;for(;o<ft;){if(!i||i.role==="RootWebArea")throw new w("ActionFailureError",`Attempted to click node with no clickable surrounding elements: ${e.getLogForm()}`);if(i.role==="StaticText"){i=i.parent;continue}let n=i.backendNodeID;if(!n){this.logger.warn({node:i.getLogForm()},"Click candidate had no backend node ID"),i=i.parent;continue}try{let l=await this.getLocatorFromBackendID(n);return t.doubleClick?await l.dblclick({timeout:8e3}):await l.click({timeout:8e3,button:t.rightClick?"right":"left",force:t.force}),i.id!==e.id&&this.logger.info({oldNode:e.getLogForm(),newNode:i.getLogForm()},"Redirected click successfully to new element"),i}catch(l){this.logger.error({err:l,node:i.getLogForm()},"Failed click or click timed out"),o++,i=i.parent}}throw new Error(`Max click redirection attempts exhausted on original element: ${e.getLogForm()}`)}async getElementLocation(e){let t=await this.cdpClient.send("DOMSnapshot.captureSnapshot",{computedStyles:[],includeDOMRects:!0,includePaintOrder:!0}),o=await this.page.evaluate(()=>window.devicePixelRatio);process.platform==="darwin"&&o===1&&(o=2);let i=t.documents[0],n=i.layout,l=i.nodes,s=l.nodeName||[],c=l.backendNodeId||[],m=n.nodeIndex,u=n.bounds,p=-1;for(let N=0;N<s.length;N++)if(c[N]===e){p=m.indexOf(N);break}if(p===-1)throw new Error(`Could not find any backend node with ID ${e}`);let[I=0,g=0,S=0,x=0]=u[p];I/=o,g/=o,S/=o,x/=o;let E=I+S/2,k=g+x/2;return{centerX:E,centerY:k}}async scroll(e,t,o,i){let n=t==="left"?-1:1,l=i==="up"?-1:1;this.activeFrame?await(await this.getUserPageOrFrame()).evaluate(([c,m,u,p])=>window.scrollTo(window.scrollX+(c??window.innerWidth)*u,window.scrollY+(m??window.innerHeight)*p),[e,o,n,l]):await this.page.mouse.wheel((e??r.VIEWPORT.width)*n,(o??r.VIEWPORT.height)*l)}async scrollUp(e){await this.scroll(0,null,e??null,"up")}async scrollDown(e){await this.scroll(0,null,e??null,"down")}async scrollLeft(e){await this.scroll(e??null,"left",0,null)}async scrollRight(e){await this.scroll(e??null,"right",0,null)}async goForward(){await this.wrapPossibleNavigation(async()=>this.localMode&&this.activeFrame?(await this.getUserPageOrFrame()).evaluate(e=>{let t=e().contentWindow;t?t.history.forward():console.error("Failed to get content window for frame")},()=>document.querySelector(`iframe[name="${this.activeFrame}"]`)):this.page.goForward({timeout:8e3}))}async goBack(){await this.wrapPossibleNavigation(async()=>this.localMode&&this.activeFrame?(await this.getUserPageOrFrame()).evaluate(e=>{let t=e().contentWindow;t?t.history.back():console.error("Failed to get content window for frame")},()=>document.querySelector(`iframe[name="${this.activeFrame}"]`)):this.page.goBack({timeout:8e3}))}async switchToPage(e){let t=this.context.pages();for(let o=0;o<t.length;o++){let i=t[o];if(i.url().includes(e)){this.logger.info(`Switching to tab ${o} with url ${i.url()}`),this.page=i,await this.pageSetup(),await this.attachPageLoadListeners();try{await i.waitForLoadState("load",{timeout:3e3})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for load state during tab switch, continuing anyways...")}await this.initCDPSession();return}}throw new Error(`Could not find page with url containing ${e}`)}async attachPageLoadListeners(){if(this.localMode)return;let e=async t=>{t.parentFrame()||await this.pageSetup()};this.page.off("framenavigated",e),this.page.on("framenavigated",e)}async setCookie(e){let t=rt(e);await this.context.addCookies([t])}async setLocalStorage(e,t){await(await this.getUserPageOrFrame()).evaluate(([i,n])=>{i&&localStorage.setItem(i,n||"")},[e,t])}async solveCaptcha(){await this.getA11yTree();let e;for(let s of this.a11yIdToNodeMap.values())if(s.role==="image"&&s.name.toLowerCase().includes("captcha")){if(!s.backendNodeID)continue;e=await this.getLocatorFromBackendID(s.backendNodeID);break}if(!e){let s=await(await this.getUserPageOrFrame()).solveRecaptchas();if(!s.captchas||!s.captchas.length)throw new Error("No captchas found on the page");return}let t=await e.screenshot({type:"jpeg",animations:"allow",quality:100}),o=await fetch("https://api.2captcha.com/createTask",{method:"POST",body:JSON.stringify({clientKey:process.env.TWO_CAPTCHA_KEY,task:{type:"ImageToTextTask",body:t.toString("base64"),case:!0},languagePool:"en"})});if(!o.ok){let s=`Captcha solver API returned error response: ${o.statusText}`;throw this.logger.error({text:await o.text()},s),new Error(s)}let{taskId:i}=await o.json(),n=Date.now(),l="";for(;Date.now()-n<6e4;){await _(2500);let s=await fetch("https://api.2captcha.com/getTaskResult",{method:"POST",body:JSON.stringify({clientKey:process.env.TWO_CAPTCHA_KEY,taskId:i})});if(!s.ok){let m=`Captcha solution API returned error response: ${s.statusText}`;throw this.logger.error({text:await s.text()},m),new Error(m)}let c=await s.json();if(c.errorId){let m=`Captcha solution API returned error ID ${c.errorId}`;throw this.logger.error(m),new Error(m)}if(c.status==="ready"){l=c.solution.text;break}}if(!l)throw new Error("Captcha solution timed out");return l}getActiveFrame(){return this.activeFrame}async exposeRecordingBindings(){await this.context.exposeBinding("captureClick",async(e,t)=>{if(!this.transformer){this.logger.warn("Click captured without transformer");return}let o,i;try{o=await t.getAttribute("id"),i=await t.getAttribute(ee)}catch(c){throw this.logger.error({err:c},"Failed to get element attributes for click processing"),c}if(this.logger.info({id:o,dataMomenticId:i},"Click captured on element"),!i){this.logger.error({url:await this.url(),element:await t.evaluate(c=>c.outerHTML)},`Could not find ${ee} for recorded click`);return}let n=this.getA11yIdFromDataMomenticId(parseInt(i));if(!n){this.logger.warn({url:await this.url(),element:await t.evaluate(c=>c.outerHTML)},"Could not find a11y id for recorded click");return}let l={id:n};await this.resolveCachedTargetToID(l),(async()=>{try{let c=this.mostRecentBrowserState??(await this.getA11yTree(!0)).serialize();await this.transformer?.recordClick(l,c)}catch(c){this.logger.error({err:c},"Failed transforming captured click")}})()},{handle:!0}),await this.context.exposeBinding("captureKeystroke",async({},{key:e})=>{this.transformer&&(this.logger.info({key:e},"Captured keypress"),this.transformer.recordKeystroke(e))})}async startRecording(e,t){this.transformer=t;let o=await this.getA11yTree(!0);this.mostRecentBrowserState=o.serialize();let i=null,n=[async()=>{this.transformer=void 0}],l=async c=>{i&&clearTimeout(i),i=setInterval(()=>{if(!this.transformer||e.aborted)return;(async()=>{let u=0;for(;u<2;)try{let p=await this.getA11yTree(!0);this.mostRecentBrowserState=p.serialize();break}catch(p){this.logger.error({err:p},"Failed to get a11y tree in frame navigation listener"),u++}})()},250),n.push(async()=>{i&&(clearInterval(i),i=null)}),await c.evaluate(Nt),n.push(async()=>{await c.evaluate(()=>{document.removeEventListener("click",window.clickListener,{capture:!0})})}),await c.evaluate(Mt),n.push(async()=>{await c.evaluate(()=>{document.removeEventListener("keydown",window.pressListener,{capture:!0})})})};await l(await this.getUserPageOrFrame());let s=async c=>{c.name().startsWith(Le)&&(e.aborted||!this.transformer||(this.logger.info("Re-adding window recording event listeners on navigation"),await l(c)))};this.page.on("framenavigated",s),n.push(async()=>{this.page.off("framenavigated",s)}),e.addEventListener("abort",async()=>{for(let c of n)try{await c()}catch(m){this.logger.warn({err:m},"Recording cleanup function failed, continuing...")}})}async getSelectOptions(e){let t=this.a11yIdToNodeMap.get(e);if(!t)throw new Error(`Could not find node with a11y id ${e}`);if(!t.backendNodeID)throw new Error(`Node with a11y id ${e} has no backend node ID`);return await(await this.getLocatorFromBackendID(t.backendNodeID)).evaluate(n=>Array.from(n.querySelectorAll("option")).map(s=>s.value))}};var pr={type:"a11y",version:"1.0.0",useHistory:"diff",useGoalSplitter:!0},ur=pr;import{existsSync as fr}from"fs";import hr from"p-timeout";async function kt({code,fragment,timeoutMs=3e3}){let evalCode=code;if(fragment){let r=`__momentic${Math.round(Math.random()*1e4)}`;evalCode=`
22
+ ${x||""}`}return p}},Be=class{constructor(e,t,o){this.root=e;this.a11yIdNodeMap=t;this.dataMomenticIdMap=o}serialize(){return this.root?this.root.serialize():""}};function $o(r){return r.name?.value?`"${r.name.value}"`:r.role?.value&&r.role.value!=="none"&&r.role.value!=="generic"?`"${r.role.value}"`:`"${r.nodeId}"`}function bt(r,e,t){if(!e&&r.parentId)throw new Error(`Got no parent for accessibility node ${r.nodeId}: ${JSON.stringify(r)}`);let o=new He({id:parseInt(r.nodeId),role:r.role?.value||"",name:r.name?.value||"",content:r.value?.value||"",properties:r.properties,children:[],pathFromRoot:(e?`${e.pathFromRoot} `:"")+$o(r),backendNodeID:r.backendDOMNodeId,ignoredByCDP:r.ignored}),i=r.childIds??[];for(let s of i){if(!s)continue;let c=t.get(parseInt(s));if(!c)continue;let m=bt(c,o,t);m.length&&(o.children=o.children.concat(m))}if(o.role==="StaticText"&&(o.children=[]),o.children.length===1&&o.children[0].role==="StaticText"){let s=o.name,c=o.children[0]?.name;(s===c||!c)&&(o.children=[])}let n=[];for(let s=o.children.length-1;s>=0;s--){let c=o.children[s];if(c.role!=="StaticText"){n.push(c);continue}if(s===0||o.children[s-1].role!=="StaticText"){n.push(c);continue}o.children[s-1].name+=` ${c.name}`}if(o.children=n.reverse(),o.role==="generic"&&o.children.length===1){let s=o.children[0];if(o.name&&!Ge.includes(s.role)&&o.name===s.name)return o.children}if(!o.isInteresting()&&r.parentId)return o.children;for(let s of o.children)s.parent=o;return[o]}function St(r,e,t,o,i=1){r.id=i,i+=1,e.set(r.id,r),r.dataMomenticId?t.set(r.dataMomenticId,r):Ge.includes(r.role)||o.debug({node:r.serialize({neighbors:1,maxLevel:1})},"Node has no data-momentic-id");for(let n of r.children)i=St(n,e,t,o,i);return i}function Ct(r,e){if(!r.root)throw new Error("a11y tree has null root");r.allNodes=r.allNodes.filter(l=>l.ignored?!l.ignoredReasons?.find(c=>Ho.includes(c.name)):!0);let t=new Map;for(let l of r.allNodes)t.set(parseInt(l.nodeId),l);let o=bt(r.root,null,t);if(o.length>1)throw new Error(`Something went horribly wrong processing the a11y tree, we got: ${JSON.stringify(o)}`);if(o.length===0)throw new fe;let i=new Map,n=new Map;return St(o[0],i,n,e),new Be(o[0],i,n)}var de=(r,e)=>{e.id=r.id,e.content=r.content,e.name=r.name,e.role=r.role,e.numChildren=r.children.length,e.serializedForm=r.serialize({noID:!0,maxLevel:1,neighbors:1}),e.nodeOnlySerializedForm=r.serialize({noID:!0,noChildren:!0,noProperties:!0})},$e=(r,e)=>{let t=1;r.role===e.role&&t++;let o=["name","content"];for(let i of o){if(!r[i]?.trim())continue;let n=Ue(r[i],e[i])/Math.min(r[i].length,e[i].length);n===0?t+=2:n<=.1&&t++}if(e.numChildren!==void 0&&(r.children.length===e.numChildren&&e.numChildren>0?t++:(e.numChildren>0&&r.children.length===0||Math.abs(r.children.length-e.numChildren)>2)&&t--),e.nodeOnlySerializedForm){let i=r.serialize({noID:!0,noChildren:!0,noProperties:!0});Ue(i,e.nodeOnlySerializedForm)/Math.min(i.length,e.nodeOnlySerializedForm.length)<=.1&&t++}if(e.serializedForm){let i=r.serialize({noID:!0,maxLevel:1,neighbors:1}),n=Ue(i,e.serializedForm)/Math.min(i.length,e.serializedForm.length);n===0?t+=2:n<=.1&&t++}return t};var X={r:147,g:196,b:125,a:.55},vt={showInfo:!1,showRulers:!1,showStyles:!1,showAccessibilityInfo:!1,showExtensionLines:!1,contrastAlgorithm:"aa",contentColor:X,paddingColor:X,borderColor:X,marginColor:X,eventTargetColor:X,shapeColor:X,shapeMarginColor:X};var _=(r=1e3)=>new Promise(e=>setTimeout(()=>e(),r));function Et(){window.cursor||(window.cursor=document.createElement("img"),window.cursor.setAttribute("src","data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjMyIiB2aWV3Qm94PSIwIDAgMzIgMzIiIHdpZHRoPSIzMiIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48ZyBmaWxsPSJub25lIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDEwIDcpIj48cGF0aCBkPSJtNi4xNDggMTguNDczIDEuODYzLTEuMDAzIDEuNjE1LS44MzktMi41NjgtNC44MTZoNC4zMzJsLTExLjM3OS0xMS40MDh2MTYuMDE1bDMuMzE2LTMuMjIxeiIgZmlsbD0iI2ZmZiIvPjxwYXRoIGQ9Im02LjQzMSAxNyAxLjc2NS0uOTQxLTIuNzc1LTUuMjAyaDMuNjA0bC04LjAyNS04LjA0M3YxMS4xODhsMi41My0yLjQ0MnoiIGZpbGw9IiMwMDAiLz48L2c+PC9zdmc+"),window.cursor.setAttribute("id","momentic_cursor"),window.cursor.setAttribute("style","position: absolute; z-index: 99999999999; pointer-events: none; left:0; top:0; height:4%;"),window.cursor.style.filter="invert(0%) sepia(6%) saturate(24%) hue-rotate(315deg) brightness(89%) contrast(110%)",document.body.appendChild(cursor),document.onmousemove=function(r){r=r||window.event,document.getElementById("momentic_cursor").style.left=r.pageX+"px",document.getElementById("momentic_cursor").style.top=r.pageY+"px"})}function At(){window.globalHintManager||(window.globalHintManager=new window.HintManager),window.globalHintManager.capture()}function Tt(){window.globalHintManager&&window.globalHintManager.reset()}function Rt(){let r=document.body.getElementsByTagName("*"),e=1;for(let t=0;t<r.length;t++){let o=e.toString();for(;[6].some(n=>o.includes(n.toString()));)e++,o=e.toString();let i=r[t];i?.setAttribute("data-momentic-id",`${e}`),i?.setAttribute("aria-keyshortcuts",`${e}`),e++}}var Wo=new Set(["document","script","XMLHttpRequest","fetch","xhr"]),jo=new Set(["script","document"]),Vo=["intercom.io","googletagmanager.com","google-analytics.com","www.gstatic.com","gstatic.com","apis.google.com","sentry.io","newrelic.com","p.retool.com","m.stripe.com","m.stripe.network","js.stripe.com","assets.trybento.co","udon.trybento.co","cdn.lr-in-prod.com","r.lr-in-prod.com","content.product-usage.assembledhq.com","data.product-usage.assembledhq.com","static.zdassets.com","o.clarity.ms/collect"],Ko=["api.stripe.com","supabase.co"],Yo=[];async function xt(r){for(let e of Yo)await r.route(e,t=>t.abort())}function We(r){return`${r.resourceType()} ${r.method()} ${r.url()}`}function It(r){return r=r.replace(/^www\./,""),r}function Lt(r){return Ko.some(e=>r.includes(e))}function Ot(r,e){if(!Wo.has(r.resourceType()))return!1;let t=new URL(e),o=new URL(r.url());return Vo.some(i=>o.hostname.includes(i))?!1:jo.has(r.resourceType())||r.method()!=="GET"?!0:It(o.hostname).includes(It(t.hostname))}var Nt=()=>{window.clickListener&&document.removeEventListener("click",window.clickListener,{capture:!0}),window.clickListener=async r=>{console.log("[Momentic] Window click listener fired",r.target),await window.captureClick(r.target)},document.addEventListener("click",window.clickListener,{capture:!0})};var Mt=()=>{window.pressListener&&document.removeEventListener("keydown",window.pressListener,{capture:!0}),window.pressListener=async r=>{console.log("[Momentic] Window press listener fired"),await window.captureKeystroke({key:r.key})},document.addEventListener("keydown",window.pressListener,{capture:!0})};var Ce=cr(lr);Ce.use(mr());Ce.use(dr({provider:{id:"2captcha",token:process.env.TWO_CAPTCHA_KEY},visualFeedback:!0}));var Ve=class r{browser;context;page;a11yIdToNodeMap=new Map;dataMomenticIdToNodeMap=new Map;cdpClient;logger;localMode;activeFrame;transformer;mostRecentBrowserState;baseURL;constructor({browser:e,context:t,page:o,baseUrl:i,logger:n,localMode:l,cdpClient:s}){this.browser=e,this.context=t,this.cdpClient=s,this.page=o,this.baseURL=i,this.logger=n,this.localMode=!!l}static USER_AGENT=Pt["Desktop Chrome"].userAgent;static VIEWPORT={width:1920,height:1080};static async init({baseUrl:e,logger:t,browserArgs:o,contextArgs:i,takeScreenshots:n,onScreenshot:l,onClose:s,localMode:c,localAppUrl:m,extensionPath:u,timeout:p=8e3}){let I={headless:!0,handleSIGTERM:!1,chromiumSandbox:!1,...o??{}},g={viewport:r.VIEWPORT,deviceScaleFactor:process.platform==="darwin"?2:1,userAgent:Pt["Desktop Chrome"].userAgent,geolocation:{latitude:37.7749,longitude:-122.4194},locale:"en-US",timezoneId:"America/Los_Angeles",...i??{}},S=null,x,E;if(c){let y=ar(sr(),"momentic","chromium");x=await Ce.launchPersistentContext(y,{...I,...g,ignoreDefaultArgs:["--enable-automation","--enable-strict-mixed-content-checking"],ignoreHTTPSErrors:!0,bypassCSP:!0,args:["--allow-insecure-localhost","--disable-site-isolation-for-policy","--disable-site-isolation-trials",`--unsafely-treat-insecure-origin-as-secure=${m}`,`--load-extension=${u}`,"--test-type=browser"],baseURL:e}),E=x.pages()[0],s&&E.on("close",()=>{s()})}else S=await Ce.launch(I),x=await S.newContext({...g,baseURL:e}),E=await x.newPage();await xt(x);let k=new r({browser:S,context:x,page:E,baseUrl:e,logger:t,localMode:c,cdpClient:await x.newCDPSession(E)}),N=!1;(async()=>{try{await k.navigate({url:e,wrapPossibleNavigation:!1,initialNavigation:!0})}catch(y){t.error({err:y},"Failed to initialize chrome browser")}finally{N=!0}})();let f=async()=>{if(n)try{l?.({viewport:await k.viewport(),buffer:await k.screenshot()})}catch(y){t.error({err:y},"Failed to take screenshot")}};f();let A=setInterval(()=>{f()},250),me=Date.now();for(;!N&&Date.now()-me<p;)await _(250);return clearInterval(A),N||t.warn("Timeout elapsed waiting for browser to initialize - are you sure this page is accessible?"),k}async getUserPageOrFrame(){if(this.localMode&&this.activeFrame){let e=null,t=0;for(;!e&&t<5;)e=this.page.frame(this.activeFrame),await _(100),t++;if(!e)throw new w("InternalWebAgentError",`Failed to find frame ${this.activeFrame} on page ${this.page.url()}`);return e}return this.page}async initCDPSession(){let e=!1,t=2,o=async()=>{try{this.cdpClient=await this.context.newCDPSession(this.page),await this.cdpClient.send("Accessibility.enable"),await this.cdpClient.send("DOM.enable"),await this.cdpClient.send("Overlay.enable"),e=!0}catch(i){if(t>0)return this.logger.error({err:i},"Failed to initialize CDP session, re-creating CDP client"),await _(250),t--,o();throw i}};if(await Promise.race([o(),_(3e3)]),!e)throw new w("InternalWebAgentError","Timeout elapsed waiting for CDP session to initialize")}setLogger(e){this.logger=e}ping(){if(this.closed)throw new Error("Page has been closed");if(this.browser&&!this.browser.isConnected())throw new Error("Browser is not connected")}setActiveFrame(e){this.activeFrame=e}async reset(e){this.a11yIdToNodeMap.clear(),this.dataMomenticIdToNodeMap.clear();let t=this.context.pages();this.page=t[0];for(let o=1;o<t.length;o++)await t[o].close();if(e.clearCookies&&await this.context.clearCookies(),!this.page.isClosed()){if(e.clearStorage)try{await(await this.getUserPageOrFrame()).evaluate(async()=>{window.localStorage.clear(),window.sessionStorage.clear(),await indexedDB.databases().then(o=>{o.forEach(i=>{i.name&&indexedDB.deleteDatabase(i.name)})})})}catch(o){this.logger.warn({err:o},"Failed clearing site data, continuing...")}await this.navigate({url:e.url??this.baseURL,wrapPossibleNavigation:!1,initialNavigation:!0})}}async pageSetup(){try{this.localMode||await this.page.evaluate(Et)}catch{}}async wait(e){await _(e)}async toggleHints(e){let t=await this.getUserPageOrFrame();e.state==="on"?(await t.addStyleTag({content:_e.css}),await t.addScriptTag({content:_e.js}),await t.evaluate(At)):await t.evaluate(Tt)}async showHints(){await this.toggleHints({state:"on"});let e=async()=>{try{await this.toggleHints({state:"off"})}catch(t){this.logger.debug({err:t},"Failed to remove vision hints")}};setTimeout(()=>{e()},3e3)}async cleanup(){await this.page.close(),await this.context.close(),this.browser&&await this.browser.close()}get closed(){return this.page.isClosed()||!!this.browser&&!this.browser.isConnected()}async html(){return(await this.getUserPageOrFrame()).content()}async url(){return(await this.getUserPageOrFrame()).url()}async screenshotWithHints(e=100,t="device",o="/tmp/screenshots/test.jpg"){let i=o?.split("."),n=i?.slice(0,-1).join("."),l=i?.slice(-1)[0],s=Buffer.from("");await this.showHints();let c=await this.screenshot(e,t,o?`${n}-after-hint.${l}`:void 0);return{before:s,after:c}}async screenshot(e=100,t="device",o){let i={fullPage:!1,quality:e,scale:t,type:"jpeg",caret:"initial",path:o};return!this.localMode||!this.activeFrame?this.page.screenshot(i):this.page.locator(`iframe[name="${this.activeFrame}"]`).screenshot(i)}async viewport(){if(this.localMode&&this.activeFrame){let t=await this.page.locator(`iframe[name="${this.activeFrame}"]`).boundingBox();if(!t)throw new Error(`Failed to get bounding box for frame: ${this.activeFrame}`);return t}let e=this.page.viewportSize();if(!e)throw new Error("failed to get viewport");return e}async navigate({url:e,wrapPossibleNavigation:t=!0,initialNavigation:o=!1}){this.logger.debug(`Navigating to ${e}`);let i=Date.now(),n=async()=>{try{await(await this.getUserPageOrFrame()).goto(e,{waitUntil:"load",timeout:8e3}),this.logger.debug({url:e},`Got load event in ${Math.floor(Date.now()-i)}ms`)}catch{this.logger.warn({url:e},"Timeout elapsed waiting for page to fire load event, continuing anyways...")}};t?await this.wrapPossibleNavigation(n):await n();let l=await this.url();if(gt.has(l)&&process.env.NODE_ENV==="production")throw new w("ActionFailureError",`${e} took too long to load \u{1F61E}. Please ensure the site and your internet are working.`,{},!0);if(o&&(await this.initCDPSession(),await this.pageSetup(),await this.attachPageLoadListeners(),this.localMode))try{await this.exposeRecordingBindings()}catch(s){s instanceof Error&&s.message.includes("already registered")||this.logger.error({err:s},"Failed to expose recording bindings during navigation")}this.logger.info({url:e},"Navigation complete")}async type(e,t={}){let{clearContent:o=!0,pressKeysSequentially:i=!1}=t;o&&(process.platform==="darwin"?await this.page.keyboard.press("Meta+A"):await this.page.keyboard.press("Control+A"),await this.page.keyboard.press("Backspace")),i?await this.page.keyboard.type(e):await this.page.keyboard.insertText(e)}async clickByA11yID(e,t={}){let o=this.a11yIdToNodeMap.get(e);if(!o)throw new Error(`Could not find DOM node during click: ${e}`);let i=await this.clickUsingCDP(o,t);return await this.highlightNode(i),o.serialize({noChildren:!0,noProperties:!0,noID:!0})}async selectOptionByA11yID(e,t){let o=this.a11yIdToNodeMap.get(e);if(!o)throw new Error(`Could not find DOM node while selecting option: ${e}`);if(!o.backendNodeID)throw new Error(`Select target missing backend node id: ${o.getLogForm()}`);return await(await this.getLocatorFromBackendID(o.backendNodeID)).selectOption(t,{timeout:8e3}),await this.highlightNode(o),o.serialize({noChildren:!0,noProperties:!0,noID:!0})}async scrollIntoView(e){let t=await this.resolveCachedTargetToID(e),o=this.a11yIdToNodeMap.get(t);if(!o)throw new Error(`Could not find node in DOM with a11y id: ${t}`);if(!o.backendNodeID)throw new Error(`Focus target missing backend node id: ${o.getLogForm()}`);await(await this.getLocatorFromBackendID(o.backendNodeID)).scrollIntoViewIfNeeded({timeout:8e3})}async highlight(e){try{let t=await this.resolveCachedTargetToID(e),o=this.a11yIdToNodeMap.get(t);if(!o)throw new Error(`Could not find DOM node during highlight: ${t}`);if(!o.backendNodeID)throw new Error(`Highlight target missing backend node id: ${o.getLogForm()}`);await this.highlightNode(o)}catch(t){this.logger.warn({err:t,target:e},"Failed to highlight target")}}async highlightNode(e){try{await this.cdpClient.send("Overlay.highlightNode",{highlightConfig:vt,backendNodeId:e.backendNodeID})}catch{this.logger.warn("Failed to add node highlight, a page navigation likely occurred. This is non-fatal for tests.")}let t=async()=>{try{await this.cdpClient.send("Overlay.hideHighlight",{backendNodeId:e.backendNodeID})}catch(o){this.logger.debug({err:o},"Failed to remove node highlight")}};setTimeout(()=>{t()},3e3)}async wrapPossibleNavigation(e,t=8e3,o=!0){let i=Date.now(),n=await this.url(),l=Date.now(),s=new Map,c=new Map,m=f=>{let A=We(f.request());c.set(A,(c.get(A)??0)+1);let me=f.status();me>=500&&this.logger.warn({request:A,status:me},"Received 500 level response")},u=f=>{if(!Ot(f,n))return;let A=We(f);s.set(A,(s.get(A)??0)+1),l=Date.now()};this.page.on("response",m),this.page.on("request",u);let p=[];o&&(p=this.context.pages().map(f=>f.url()));let I=!1,g=e().catch(f=>(I=!0,f instanceof Error?f:new Error(`${f}`)));await _(250);let S=async f=>{let A=await f;if(A instanceof Error)throw A;return A},x=new Set,E=!1,N=await(async()=>{for(;!I&&!(!E&&Date.now()-i>t);){if(await _(250),E=!1,x=new Set,Date.now()-l<=1250)continue;let f=!1;for(let A of s.keys())s.get(A)!==c.get(A)&&(Lt(A)&&(E=!0),f=!0,x.add(A));if(!f)return this.logger.debug({url:await this.url(),requests:JSON.stringify(Array.from(s.entries()))},`Network idle in ${Math.floor(Date.now()-i)}ms`),!0}return!I&&x.size>0&&this.logger.warn({url:await this.url(),unfinishedRequests:JSON.stringify(Array.from(x.entries()))},"Timeout elapsed waiting for network idle, continuing anyways..."),!1})();if(this.page.off("response",m),this.page.off("request",u),!N)return S(g);let O=await this.url();if(!I&&Y(O,n)){this.logger.debug({startUrl:n,newUrl:O},"Detected url change in wrapPossibleNavigation, waiting for load state");let f=Math.max(t-(Date.now()-i),0);if(f>0)try{await(await this.getUserPageOrFrame()).waitForLoadState("load",{timeout:f})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for load state, continuing anyways...")}}if(o){let f=this.context.pages().map(A=>A.url());if(f.length>p.length)for(let A of f)A!==O&&await this.switchToPage(A)}return S(g)}async resolveCachedTargetToID(e){if(!ue(e)){let s=this.a11yIdToNodeMap.get(e.id);if(!s)throw new Error(`Resolving target failed, fresh value did not exist in node map: ${e.id}`);return de(s,e),e.id}let t=(await this.getA11yTree()).serialize();this.logger.debug({tree:t},"Refreshed a11y tree before resolving target");let o=this.a11yIdToNodeMap.get(e.id);if(o){let s=$e(o,e);if(s>=5)return this.logger.debug({target:e,proposedNode:o.getLogForm(),comparisonScore:s},"Resolved cached a11y target to node with exact same id"),de(o,e),e.id}let i=1/0,n=1/0,l;for(let s of this.a11yIdToNodeMap.values()){let c=$e(s,e);if(c>=5)return this.logger.debug({newNode:s.getLogForm(),target:e,comparisonScore:c},"Resolved cached a11y target to new node with field comparison"),de(s,e),s.id;if(!e.serializedForm)continue;let m=s.serialize({noID:!0,maxLevel:1,neighbors:1});if(Math.abs(m.length-e.serializedForm.length)>15)continue;let u=nr(e.serializedForm,m),p=u/Math.min(e.serializedForm.length,m.length);u<i&&p<.2&&(i=u,n=p,l=s)}if(l&&i<15)return this.logger.debug({newNode:l.getLogForm(),target:e,distance:i,ratio:n},"Resolved cached a11y target to new node with pure levenshtein distance"),de(l,e),l.id;throw new Error(`Could not find any relevant node given cached target: ${JSON.stringify({target:e,closestLevenshteinDistance:i,closestNode:l})}`)}async click(e,t={}){let o=await this.resolveCachedTargetToID(e);return await this.wrapPossibleNavigation(()=>this.clickByA11yID(o,t))}async hover(e){let t=await this.resolveCachedTargetToID(e),o=this.a11yIdToNodeMap.get(t);if(!o)throw new Error(`Could not find DOM node for hover: ${t}`);if(!o.backendNodeID)throw new Error(`Hover target missing backend node id: ${o.getLogForm()}`);return await(await this.getLocatorFromBackendID(o.backendNodeID)).hover({timeout:8e3}),await this.highlightNode(o),o.serialize({noChildren:!0,noProperties:!0,noID:!0})}async selectOption(e,t){let o=await this.resolveCachedTargetToID(e);return this.selectOptionByA11yID(o,t)}async press(e){await this.wrapPossibleNavigation(()=>this.page.keyboard.press(e))}async refresh(){if(this.localMode&&this.activeFrame){let t=(await this.getUserPageOrFrame()).url();await this.navigate({url:t,wrapPossibleNavigation:!0})}else try{await this.page.reload({timeout:3e3})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for page on reload, continuing anyways...")}}async getA11yTree(e=!1){let t=null,o=async()=>{let i=0,n=await this.url();for(;!t&&i<3;)try{let l=await this.getRawA11yTree(e);if(!l.root||l.allNodes.length<=1)throw new Error("No a11y tree found on page");t=Ct(l,this.logger)}catch(l){if(this.logger.error({err:l,url:n},"Error fetching a11y tree"),i===0)await _(1e3),i++;else throw new Error(`Max retries exceeded fetching a11y tree: ${l}`)}if(!t||!t.root)throw new Error("Accessibility tree appears empty");this.a11yIdToNodeMap=t.a11yIdNodeMap,this.dataMomenticIdToNodeMap=t.dataMomenticIdMap};if(await Promise.race([o(),_(8e3)]),t)return t;throw new w("ActionFailureError",`Getting accessibility tree timed out after ${8e3}ms`)}getA11yIdFromDataMomenticId(e){return this.dataMomenticIdToNodeMap.get(e)?.id}async getRawA11yTree(e=!1){let t=await this.url(),o=Date.now(),i=()=>{o=Date.now()};this.cdpClient.addListener("Accessibility.nodesUpdated",i);let n=!1,l=()=>{this.logger.info({url:t},"Load event fired on page"),n=!0,o=Date.now()};this.cdpClient.addListener("Accessibility.loadComplete",l);let s=Date.now(),c=!e;for(;!e&&Date.now()-s<3e3;){if(await _(250),!n&&Date.now()-s<1e3){process.env.NODE_ENV!=="production"&&this.logger.debug({url:t},"A11y tree not loaded yet, waiting...");continue}if(Date.now()-o>=1250){c=!1;break}this.logger.debug({url:t},"A11y tree not stable yet, waiting...")}this.logger.debug({duration:Date.now()-s,eventReceived:n,timeoutTriggered:c,skipWait:e},"A11y wait phase completed"),await(await this.getUserPageOrFrame()).evaluate(Rt);let m;if(this.localMode&&this.activeFrame){let{result:{objectId:p}}=await this.cdpClient.send("Runtime.evaluate",{expression:`document.querySelector("#${this.activeFrame}")`}),I=await this.cdpClient.send("DOM.describeNode",{objectId:p});m=(await this.cdpClient.send("Accessibility.getRootAXNode",{frameId:I.node.frameId})).node.backendDOMNodeId}else{let{node:p}=await this.cdpClient.send("Accessibility.getRootAXNode");m=p.backendDOMNodeId}let{nodes:u}=await this.cdpClient.send("Accessibility.queryAXTree",{backendNodeId:m});return this.cdpClient.removeListener("Accessibility.loadComplete",l),this.cdpClient.removeListener("Accessibility.nodesUpdated",i),{root:u[0],allNodes:u}}async clickUsingVisualCoordinates(e){let t=await this.getElementLocation(e);if(!t)throw new Error(`Could not find element location with backend node id: ${e}`);this.logger.debug({location:t},"Executing mouse click"),await this.page.mouse.click(t.centerX,t.centerY)}async getIDAttributeUsingCDP(e){await this.cdpClient.send("DOM.getDocument",{depth:0});let t=await this.cdpClient.send("DOM.requestNode",{objectId:e}),i=(await this.cdpClient.send("DOM.getAttributes",{nodeId:t.nodeId})).attributes,n=i.findIndex(l=>l===ee);if(n===-1||!i[n+1])throw new Error(`Could not find ${ee} for object ${e}`);return i[n+1]}async getLocatorFromBackendID(e){let t=await this.cdpClient.send("DOM.resolveNode",{backendNodeId:e});if(!t||!t.object.objectId)throw new Error(`Could not resolve backend node ${e}`);let o;try{o=await this.getIDAttributeUsingCDP(t.object.objectId)}catch(i){throw this.logger.error({err:i,object:JSON.stringify(t.object)},"Failed to get ID attribute"),i}return(await this.getUserPageOrFrame()).locator(`[${ee}="${o}"]`)}async clickUsingCDP(e,t={}){let o=0,i=e;for(;o<ft;){if(!i||i.role==="RootWebArea")throw new w("ActionFailureError",`Attempted to click node with no clickable surrounding elements: ${e.getLogForm()}`);if(i.role==="StaticText"){i=i.parent;continue}let n=i.backendNodeID;if(!n){this.logger.warn({node:i.getLogForm()},"Click candidate had no backend node ID"),i=i.parent;continue}try{let l=await this.getLocatorFromBackendID(n);return t.doubleClick?await l.dblclick({timeout:8e3}):await l.click({timeout:8e3,button:t.rightClick?"right":"left",force:t.force}),i.id!==e.id&&this.logger.info({oldNode:e.getLogForm(),newNode:i.getLogForm()},"Redirected click successfully to new element"),i}catch(l){this.logger.error({err:l,node:i.getLogForm()},"Failed click or click timed out"),o++,i=i.parent}}throw new Error(`Max click redirection attempts exhausted on original element: ${e.getLogForm()}`)}async getElementLocation(e){let t=await this.cdpClient.send("DOMSnapshot.captureSnapshot",{computedStyles:[],includeDOMRects:!0,includePaintOrder:!0}),o=await this.page.evaluate(()=>window.devicePixelRatio);process.platform==="darwin"&&o===1&&(o=2);let i=t.documents[0],n=i.layout,l=i.nodes,s=l.nodeName||[],c=l.backendNodeId||[],m=n.nodeIndex,u=n.bounds,p=-1;for(let N=0;N<s.length;N++)if(c[N]===e){p=m.indexOf(N);break}if(p===-1)throw new Error(`Could not find any backend node with ID ${e}`);let[I=0,g=0,S=0,x=0]=u[p];I/=o,g/=o,S/=o,x/=o;let E=I+S/2,k=g+x/2;return{centerX:E,centerY:k}}async scroll(e,t,o,i){let n=t==="left"?-1:1,l=i==="up"?-1:1;this.activeFrame?await(await this.getUserPageOrFrame()).evaluate(([c,m,u,p])=>window.scrollTo(window.scrollX+(c??window.innerWidth)*u,window.scrollY+(m??window.innerHeight)*p),[e,o,n,l]):await this.page.mouse.wheel((e??r.VIEWPORT.width)*n,(o??r.VIEWPORT.height)*l)}async scrollUp(e){await this.scroll(0,null,e??null,"up")}async scrollDown(e){await this.scroll(0,null,e??null,"down")}async scrollLeft(e){await this.scroll(e??null,"left",0,null)}async scrollRight(e){await this.scroll(e??null,"right",0,null)}async goForward(){await this.wrapPossibleNavigation(async()=>this.localMode&&this.activeFrame?(await this.getUserPageOrFrame()).evaluate(e=>{let t=e().contentWindow;t?t.history.forward():console.error("Failed to get content window for frame")},()=>document.querySelector(`iframe[name="${this.activeFrame}"]`)):this.page.goForward({timeout:8e3}))}async goBack(){await this.wrapPossibleNavigation(async()=>this.localMode&&this.activeFrame?(await this.getUserPageOrFrame()).evaluate(e=>{let t=e().contentWindow;t?t.history.back():console.error("Failed to get content window for frame")},()=>document.querySelector(`iframe[name="${this.activeFrame}"]`)):this.page.goBack({timeout:8e3}))}async switchToPage(e){let t=this.context.pages();for(let o=0;o<t.length;o++){let i=t[o];if(i.url().includes(e)){this.logger.info(`Switching to tab ${o} with url ${i.url()}`),this.page=i,await this.pageSetup(),await this.attachPageLoadListeners();try{await i.waitForLoadState("load",{timeout:3e3})}catch{this.logger.warn({url:await this.url()},"Timeout elapsed waiting for load state during tab switch, continuing anyways...")}await this.initCDPSession();return}}throw new Error(`Could not find page with url containing ${e}`)}async attachPageLoadListeners(){if(this.localMode)return;let e=async t=>{t.parentFrame()||await this.pageSetup()};this.page.off("framenavigated",e),this.page.on("framenavigated",e)}async setCookie(e){let t=rt(e);await this.context.addCookies([t])}async setLocalStorage(e,t){await(await this.getUserPageOrFrame()).evaluate(([i,n])=>{i&&localStorage.setItem(i,n||"")},[e,t])}async solveCaptcha(){await this.getA11yTree();let e;for(let s of this.a11yIdToNodeMap.values())if(s.role==="image"&&s.name.toLowerCase().includes("captcha")){if(!s.backendNodeID)continue;e=await this.getLocatorFromBackendID(s.backendNodeID);break}if(!e){let s=await(await this.getUserPageOrFrame()).solveRecaptchas();if(!s.captchas||!s.captchas.length)throw new Error("No captchas found on the page");return}let t=await e.screenshot({type:"jpeg",animations:"allow",quality:100}),o=await fetch("https://api.2captcha.com/createTask",{method:"POST",body:JSON.stringify({clientKey:process.env.TWO_CAPTCHA_KEY,task:{type:"ImageToTextTask",body:t.toString("base64"),case:!0},languagePool:"en"})});if(!o.ok){let s=`Captcha solver API returned error response: ${o.statusText}`;throw this.logger.error({text:await o.text()},s),new Error(s)}let{taskId:i}=await o.json(),n=Date.now(),l="";for(;Date.now()-n<6e4;){await _(2500);let s=await fetch("https://api.2captcha.com/getTaskResult",{method:"POST",body:JSON.stringify({clientKey:process.env.TWO_CAPTCHA_KEY,taskId:i})});if(!s.ok){let m=`Captcha solution API returned error response: ${s.statusText}`;throw this.logger.error({text:await s.text()},m),new Error(m)}let c=await s.json();if(c.errorId){let m=`Captcha solution API returned error ID ${c.errorId}`;throw this.logger.error(m),new Error(m)}if(c.status==="ready"){l=c.solution.text;break}}if(!l)throw new Error("Captcha solution timed out");return l}getActiveFrame(){return this.activeFrame}async exposeRecordingBindings(){await this.context.exposeBinding("captureClick",async(e,t)=>{if(!this.transformer){this.logger.warn("Click captured without transformer");return}let o,i;try{o=await t.getAttribute("id"),i=await t.getAttribute(ee)}catch(c){throw this.logger.error({err:c},"Failed to get element attributes for click processing"),c}if(this.logger.info({id:o,dataMomenticId:i},"Click captured on element"),!i){this.logger.error({url:await this.url(),element:await t.evaluate(c=>c.outerHTML)},`Could not find ${ee} for recorded click`);return}let n=this.getA11yIdFromDataMomenticId(parseInt(i));if(!n){this.logger.warn({url:await this.url(),element:await t.evaluate(c=>c.outerHTML)},"Could not find a11y id for recorded click");return}let l={id:n};await this.resolveCachedTargetToID(l),(async()=>{try{let c=this.mostRecentBrowserState??(await this.getA11yTree(!0)).serialize();await this.transformer?.recordClick(l,c)}catch(c){this.logger.error({err:c},"Failed transforming captured click")}})()},{handle:!0}),await this.context.exposeBinding("captureKeystroke",async({},{key:e})=>{this.transformer&&(this.logger.info({key:e},"Captured keypress"),this.transformer.recordKeystroke(e))})}async startRecording(e,t){this.transformer=t;let o=await this.getA11yTree(!0);this.mostRecentBrowserState=o.serialize();let i=null,n=[async()=>{this.transformer=void 0}],l=async c=>{i&&clearTimeout(i),i=setInterval(()=>{if(!this.transformer||e.aborted)return;(async()=>{let u=0;for(;u<2;)try{let p=await this.getA11yTree(!0);this.mostRecentBrowserState=p.serialize();break}catch(p){this.logger.error({err:p},"Failed to get a11y tree in frame navigation listener"),u++}})()},250),n.push(async()=>{i&&(clearInterval(i),i=null)}),await c.evaluate(Nt),n.push(async()=>{await c.evaluate(()=>{document.removeEventListener("click",window.clickListener,{capture:!0})})}),await c.evaluate(Mt),n.push(async()=>{await c.evaluate(()=>{document.removeEventListener("keydown",window.pressListener,{capture:!0})})})};await l(await this.getUserPageOrFrame());let s=async c=>{c.name().startsWith(Le)&&(e.aborted||!this.transformer||(this.logger.info("Re-adding window recording event listeners on navigation"),await l(c)))};this.page.on("framenavigated",s),n.push(async()=>{this.page.off("framenavigated",s)}),e.addEventListener("abort",async()=>{for(let c of n)try{await c()}catch(m){this.logger.warn({err:m},"Recording cleanup function failed, continuing...")}})}async getSelectOptions(e){let t=this.a11yIdToNodeMap.get(e);if(!t)throw new Error(`Could not find node with a11y id ${e}`);if(!t.backendNodeID)throw new Error(`Node with a11y id ${e} has no backend node ID`);return await(await this.getLocatorFromBackendID(t.backendNodeID)).evaluate(n=>Array.from(n.querySelectorAll("option")).map(s=>s.value))}};var pr={type:"a11y",version:"1.0.0",useHistory:"diff",useGoalSplitter:!0},ur=pr;import{existsSync as fr}from"fs";import{faker as Pa}from"@faker-js/faker";import Fa from"axios";import Ha from"moment";import hr from"p-timeout";async function kt({code,fragment,timeoutMs=1e4}){let evalCode=code;if(fragment){let r=`__momentic${Math.round(Math.random()*1e4)}`;evalCode=`
23
23
  const ${r} = async () => {
24
24
  ${code}
25
25
  }
26
26
  ${r}()
27
- `}let result,runCode=async()=>{result=await Promise.resolve(eval(evalCode))};return await hr(runCode(),{milliseconds:timeoutMs,message:`Timeout of ${timeoutMs}ms exceeded for code execution`}),result}import{z as ve}from"zod";var Ft=process.env.GCP_JS_EVAL_FUNCTION_ENDPOINT,gr=ve.object({result:ve.unknown(),error:ve.string().optional(),success:ve.boolean()});async function Ut({code:r,fragment:e,timeoutMs:t=3e3}){let o=new AbortController;if(!Ft)throw new Error("GCP_JS_EVAL_FUNCTION_ENDPOINT environment variable not set");let i=setTimeout(()=>{o.abort()},t),n=await fetch(Ft,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({code:r,fragment:e}),signal:o.signal});if(!n.ok)throw new Error(`Code evaluation server returned error code ${n.status}`);clearTimeout(i);let l;try{l=gr.parse(await n.json())}catch(s){throw new Error(`Failed to parse response: ${s}`)}if(l.error)throw new Error(l.error);return l.result}var yr=wr();function wr(){if(process.env.NODE_ENV!=="production")return!1;if(process.env.NAMESPACE)return!0;try{if(fr("/var/run/secrets/kubernetes.io/serviceaccount/token"))return!0}catch{}return!1}async function Ht(r){let e;return yr?(r.logger.info({code:r.code,fragment:r.fragment},"Running code remotely"),e=Ut):(r.logger.info({code:r.code,fragment:r.fragment},"Running code locally"),e=kt),e(r)}import br from"dedent";import Sr from"diff-lines";var Ee=class r{static ACTION_DEBOUNCE_DELAY=1e3;onStepRecord;signal;generator;indices;keyBuffer=[];timer=null;keyBufferMode="normal";lastStep=null;constructor({signal:e,onStepRecord:t,generator:o,initialIndices:i}){this.signal=e,this.onStepRecord=(n,l)=>{this.lastStep={step:n,indices:l},t(n,l)},this.generator=o,this.indices=i}async recordClick(e,t){if(this.signal.aborted)return;let o=he("CLICK"),i={type:"PRESET_ACTION",command:{...o,target:{elementDescriptor:`Generating description for: ${e.nodeOnlySerializedForm?.trim()??`element with id ${e.id}`}`,a11yData:e}}},n=Array.from(this.indices);this.indices[this.indices.length-1]++,this.onStepRecord(i,n);let l=await this.generator.getReverseMappedDescription({goal:`${e.id}`,browserState:t},!1);i.command.target.elementDescriptor=l.phrase,this.onStepRecord(i,n)}flushKeyBufferToCommand(){let e;if(this.keyBufferMode==="normal"){let i=he("TYPE");e={type:"PRESET_ACTION",command:{...i,target:void 0,value:this.keyBuffer.join("")}}}else{let i=he("PRESS");e={type:"PRESET_ACTION",command:{...i,value:this.keyBuffer.join("+")}}}let t,o=this.lastStep?.step.command;if(o?.type===e.command.type){let i=o.value,n=e.command.value;e={type:"PRESET_ACTION",command:{...o,value:e.command.type==="PRESS"?`${i}+${n}`:`${i}${n}`}},t=this.lastStep.indices}else o?.type==="CLICK"&&e.command.type==="TYPE"?(this.lastStep.step.command={...e.command,target:o.target},e=this.lastStep.step,t=this.lastStep.indices):(t=Array.from(this.indices),this.indices[this.indices.length-1]++);this.onStepRecord(e,t),this.keyBuffer=[],this.timer=null}recordKeystroke(e){if(e==="Shift"||e==="CapsLock"||this.signal.aborted)return;this.timer!==null&&clearTimeout(this.timer);let t="normal";e.length>1&&(t="special"),this.keyBuffer.length!==0&&t!==this.keyBufferMode&&this.flushKeyBufferToCommand(),this.keyBufferMode=t,this.keyBuffer.push(e),this.timer=setTimeout(()=>{this.flushKeyBufferToCommand()},r.ACTION_DEBOUNCE_DELAY)}};var Cr=1e4,Ke=class{browser;pendingInstructions;generator;commandHistory;config;closed=!1;logger;recordAbortController=null;constructor({browser:e,config:t,generator:o,logger:i}){this.browser=e,this.generator=o,this.config=t,this.logger=i,this.pendingInstructions=[],this.commandHistory=[]}get history(){return this.commandHistory.filter(e=>e.state==="DONE")}get lastExecutedCommand(){let e=this.history;return e.length===0?null:e[e.length-1]}setOpen(){this.closed=!1}isClosed(){return this.closed}setLogger(e){this.logger=e}resetHistory(){this.commandHistory=[],this.pendingInstructions=[]}async resetState(e){this.resetHistory(),this.closed=!0,await this.browser.reset(e)}async getBrowserState(){let t=await(await this.browser.getA11yTree()).serialize();return this.logger.debug({tree:t,activeFrame:this.browser.getActiveFrame()},"Got a11y tree"),t}getSerializedHistory(e,t){let o;return this.config.useHistory==="diff"?o=this.getDiffHistory(e,t):o=this.getListHistory(),o}async splitUserGoal(e,t,o){if(e==="AI_ACTION"&&t.match(/[,!;.]|(?:and)|(?:then)/)&&this.config.useGoalSplitter){let i=await this.generator.getGranularGoals({goal:t,url:await this.browser.url()},o);this.pendingInstructions=i.reverse()}else this.pendingInstructions=[t]}async promptToCommand(e,t,o){try{return await this.promptToCommandHelper(e,t,o)}catch(i){throw i instanceof w?i:new w("InternalWebAgentError",i instanceof Error?i.message:`${i}`,{cause:i})}}async promptToCommandHelper(e,t,o){if(this.pendingInstructions.length===0){if(!t.trim())throw new Error("Cannot generate commands for empty goal");await this.splitUserGoal(e,t,o)}let i=this.pendingInstructions[this.pendingInstructions.length-1];this.logger.info({goal:i},"Starting prompt translation");let n=Date.now(),l=await this.browser.url(),s=await this.getBrowserState();this.logger.info({duration:Date.now()-n,url:l},"Got browser state");let c=this.commandHistory.length;this.commandHistory.push({state:"PENDING",browserStateBeforeCommand:s,urlBeforeCommand:l,type:e});let m=this.getSerializedHistory(l,s),u={url:l,numPrevious:c,browserState:s,history:m,goal:i,lastCommand:this.lastExecutedCommand},p=await this.generator.getProposedCommand(u,o);if(this.logger.info({type:p.type,thoughts:p.thoughts},"Got proposed command"),p.type==="SUCCESS"){let I=this.pendingInstructions.pop();if(this.logger.info({finishedInstruction:I,remainingInstructions:this.pendingInstructions},"Removing pending instruction due to SUCCESS"),this.pendingInstructions.length!==0)return this.commandHistory=[],this.promptToCommand(e,"",o)}else p.type==="FAILURE"&&(this.logger.info({remainingInstructions:this.pendingInstructions},"Removing pending instructions due to FAILURE"),this.pendingInstructions=[]);return{context:u,command:p}}async locateElement(e,t,o){if(!e)throw new w("ActionFailureError","Cannot locate element with empty description");let i=await this.getBrowserState(),n;if(t){let{before:l,after:s}=await this.browser.screenshotWithHints();if(n=await this.generator.getElementLocationWithVision({goal:e,screenshot:l,hintActivatedScreenshot:s},o),n.id>0){let c=this.browser.getA11yIdFromDataMomenticId(n.id);if(!c)throw new w("InternalWebAgentError",`Unable to find corresponding DOM node for id ${n.id}`);n.id=c}}else n=await this.generator.getElementLocation({browserState:i,goal:e},o);if(n.id<0)throw new w("ActionFailureError",`Unable to locate element: ${n.thoughts?n.thoughts:"please ensure the element is visible and conforms to Accessibility guidelines"}`);return n}getDiffHistory(e,t){let o=this.history.filter(n=>n.type==="AI_ACTION");if(o.length===0)return"<NONE/>";let i=[`
27
+ `}let result,runCode=async()=>{result=await Promise.resolve(eval(evalCode))};return await hr(runCode(),{milliseconds:timeoutMs,message:`Timeout of ${timeoutMs}ms exceeded for code execution`}),result}import{z as ve}from"zod";var Ft=process.env.GCP_JS_EVAL_FUNCTION_ENDPOINT,gr=ve.object({result:ve.unknown(),error:ve.string().optional(),success:ve.boolean()});async function Ut({code:r,fragment:e,timeoutMs:t=1e4}){let o=new AbortController;if(!Ft)throw new Error("GCP_JS_EVAL_FUNCTION_ENDPOINT environment variable not set");let i=setTimeout(()=>{o.abort()},t),n=await fetch(Ft,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({code:r,fragment:e}),signal:o.signal});if(!n.ok)throw new Error(`Code evaluation server returned error code ${n.status}`);clearTimeout(i);let l;try{l=gr.parse(await n.json())}catch(s){throw new Error(`Failed to parse response: ${s}`)}if(l.error)throw new Error(l.error);return l.result}var yr=wr();function wr(){if(process.env.NODE_ENV!=="production")return!1;if(process.env.NAMESPACE)return!0;try{if(fr("/var/run/secrets/kubernetes.io/serviceaccount/token"))return!0}catch{}return!1}async function Ht(r){let e;return yr?(r.logger.info({code:r.code,fragment:r.fragment},"Running code remotely"),e=Ut):(r.logger.info({code:r.code,fragment:r.fragment},"Running code locally"),e=kt),e(r)}import br from"dedent";import Sr from"diff-lines";var Ee=class r{static ACTION_DEBOUNCE_DELAY=1e3;onStepRecord;signal;generator;indices;keyBuffer=[];timer=null;keyBufferMode="normal";lastStep=null;constructor({signal:e,onStepRecord:t,generator:o,initialIndices:i}){this.signal=e,this.onStepRecord=(n,l)=>{this.lastStep={step:n,indices:l},t(n,l)},this.generator=o,this.indices=i}async recordClick(e,t){if(this.signal.aborted)return;let o=he("CLICK"),i={type:"PRESET_ACTION",command:{...o,target:{elementDescriptor:`Generating description for: ${e.nodeOnlySerializedForm?.trim()??`element with id ${e.id}`}`,a11yData:e}}},n=Array.from(this.indices);this.indices[this.indices.length-1]++,this.onStepRecord(i,n);let l=await this.generator.getReverseMappedDescription({goal:`${e.id}`,browserState:t},!1);i.command.target.elementDescriptor=l.phrase,this.onStepRecord(i,n)}flushKeyBufferToCommand(){let e;if(this.keyBufferMode==="normal"){let i=he("TYPE");e={type:"PRESET_ACTION",command:{...i,target:void 0,value:this.keyBuffer.join("")}}}else{let i=he("PRESS");e={type:"PRESET_ACTION",command:{...i,value:this.keyBuffer.join("+")}}}let t,o=this.lastStep?.step.command;if(o?.type===e.command.type){let i=o.value,n=e.command.value;e={type:"PRESET_ACTION",command:{...o,value:e.command.type==="PRESS"?`${i}+${n}`:`${i}${n}`}},t=this.lastStep.indices}else o?.type==="CLICK"&&e.command.type==="TYPE"?(this.lastStep.step.command={...e.command,target:o.target},e=this.lastStep.step,t=this.lastStep.indices):(t=Array.from(this.indices),this.indices[this.indices.length-1]++);this.onStepRecord(e,t),this.keyBuffer=[],this.timer=null}recordKeystroke(e){if(e==="Shift"||e==="CapsLock"||this.signal.aborted)return;this.timer!==null&&clearTimeout(this.timer);let t="normal";e.length>1&&(t="special"),this.keyBuffer.length!==0&&t!==this.keyBufferMode&&this.flushKeyBufferToCommand(),this.keyBufferMode=t,this.keyBuffer.push(e),this.timer=setTimeout(()=>{this.flushKeyBufferToCommand()},r.ACTION_DEBOUNCE_DELAY)}};var Cr=1e4,Ke=class{browser;pendingInstructions;generator;commandHistory;config;closed=!1;logger;recordAbortController=null;constructor({browser:e,config:t,generator:o,logger:i}){this.browser=e,this.generator=o,this.config=t,this.logger=i,this.pendingInstructions=[],this.commandHistory=[]}get history(){return this.commandHistory.filter(e=>e.state==="DONE")}get lastExecutedCommand(){let e=this.history;return e.length===0?null:e[e.length-1]}setOpen(){this.closed=!1}isClosed(){return this.closed}setLogger(e){this.logger=e}resetHistory(){this.commandHistory=[],this.pendingInstructions=[]}async resetState(e){this.resetHistory(),this.closed=!0,await this.browser.reset(e)}async getBrowserState(){let t=await(await this.browser.getA11yTree()).serialize();return this.logger.debug({tree:t,activeFrame:this.browser.getActiveFrame()},"Got a11y tree"),t}getSerializedHistory(e,t){let o;return this.config.useHistory==="diff"?o=this.getDiffHistory(e,t):o=this.getListHistory(),o}async splitUserGoal(e,t,o){if(e==="AI_ACTION"&&t.match(/[,!;.]|(?:and)|(?:then)/)&&this.config.useGoalSplitter){let i=await this.generator.getGranularGoals({goal:t,url:await this.browser.url()},o);this.pendingInstructions=i.reverse()}else this.pendingInstructions=[t]}async promptToCommand(e,t,o){try{return await this.promptToCommandHelper(e,t,o)}catch(i){throw i instanceof w?i:new w("InternalWebAgentError",i instanceof Error?i.message:`${i}`,{cause:i})}}async promptToCommandHelper(e,t,o){if(this.pendingInstructions.length===0){if(!t.trim())throw new Error("Cannot generate commands for empty goal");await this.splitUserGoal(e,t,o)}let i=this.pendingInstructions[this.pendingInstructions.length-1];this.logger.info({goal:i},"Starting prompt translation");let n=Date.now(),l=await this.browser.url(),s=await this.getBrowserState();this.logger.info({duration:Date.now()-n,url:l},"Got browser state");let c=this.commandHistory.length;this.commandHistory.push({state:"PENDING",browserStateBeforeCommand:s,urlBeforeCommand:l,type:e});let m=this.getSerializedHistory(l,s),u={url:l,numPrevious:c,browserState:s,history:m,goal:i,lastCommand:this.lastExecutedCommand},p=await this.generator.getProposedCommand(u,o);if(this.logger.info({type:p.type,thoughts:p.thoughts},"Got proposed command"),p.type==="SUCCESS"){let I=this.pendingInstructions.pop();if(this.logger.info({finishedInstruction:I,remainingInstructions:this.pendingInstructions},"Removing pending instruction due to SUCCESS"),this.pendingInstructions.length!==0)return this.commandHistory=[],this.promptToCommand(e,"",o)}else p.type==="FAILURE"&&(this.logger.info({remainingInstructions:this.pendingInstructions},"Removing pending instructions due to FAILURE"),this.pendingInstructions=[]);return{context:u,command:p}}async locateElement(e,t,o){if(!e)throw new w("ActionFailureError","Cannot locate element with empty description");let i=await this.getBrowserState(),n;if(t){let{before:l,after:s}=await this.browser.screenshotWithHints();if(n=await this.generator.getElementLocationWithVision({goal:e,screenshot:l,hintActivatedScreenshot:s},o),n.id>0){let c=this.browser.getA11yIdFromDataMomenticId(n.id);if(!c)throw new w("InternalWebAgentError",`Unable to find corresponding DOM node for id ${n.id}`);n.id=c}}else n=await this.generator.getElementLocation({browserState:i,goal:e},o);if(n.id<0)throw new w("ActionFailureError",`Unable to locate element: ${n.thoughts?n.thoughts:"please ensure the element is visible and conforms to Accessibility guidelines"}`);return n}getDiffHistory(e,t){let o=this.history.filter(n=>n.type==="AI_ACTION");if(o.length===0)return"<NONE/>";let i=[`
28
28
  You have already executed the following commands successfully (most recent listed first)`,"-".repeat(10)];return o.reverse().forEach((n,l)=>{if(i.push(`COMMAND ${o.length-l}${l===0?" (command just executed)":""}: ${n.serializedCommand}`),l===0)if(Y(n.urlBeforeCommand,e))i.push(` URL CHANGE: '${n.urlBeforeCommand}' -> '${e}'`);else{let s=Sr(n.browserStateBeforeCommand,t,{n_surrounding:1});s?s.length<Cr?(i.push("PAGE CONTENT CHANGE:"),s.split(`
29
29
  `).forEach(c=>i.push(` ${c}`))):i.push("PAGE CONTENT CHANGE: <TOO_LONG_TO_DISPLAY/>"):i.push("PAGE CONTENT CHANGE: <NONE/>")}i.push("-".repeat(10))}),i.push(`STARTING URL: ${this.browser.baseURL}`),i.join(`
30
30
  `)}getListHistory(){return br`Here are the commands that you have successfully executed:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "momentic",
3
- "version": "0.0.30",
3
+ "version": "0.0.31",
4
4
  "description": "The Momentic SDK for Node.js",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",