agent-device 0.16.6 → 0.16.7

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.
@@ -0,0 +1 @@
1
+ 45f8780d4d500a67e110b8056cb5bcfe7a9589223e81f6cca5a005e34714c62c agent-device-android-multitouch-helper-0.16.7.apk
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "android-multitouch-helper",
3
- "version": "0.16.6",
4
- "assetName": "agent-device-android-multitouch-helper-0.16.6.apk",
5
- "sha256": "fc975087b6d0ce63e25cb5d9475f896e828222ec4d7ed12d73e4045cb7cc6c5a",
3
+ "version": "0.16.7",
4
+ "assetName": "agent-device-android-multitouch-helper-0.16.7.apk",
5
+ "sha256": "45f8780d4d500a67e110b8056cb5bcfe7a9589223e81f6cca5a005e34714c62c",
6
6
  "packageName": "com.callstack.agentdevice.multitouchhelper",
7
- "versionCode": 16006,
7
+ "versionCode": 16007,
8
8
  "instrumentationRunner": "com.callstack.agentdevice.multitouchhelper/.MultiTouchInstrumentation",
9
9
  "statusProtocol": "android-multitouch-helper-v1"
10
10
  }
@@ -0,0 +1 @@
1
+ 5e134ba87fbe184f76138ea59e4577d19a1ccbbb7d573b4d41c8997e29da60fa agent-device-android-snapshot-helper-0.16.7.apk
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "android-snapshot-helper",
3
- "version": "0.16.6",
4
- "releaseTag": "v0.16.6",
5
- "assetName": "agent-device-android-snapshot-helper-0.16.6.apk",
3
+ "version": "0.16.7",
4
+ "releaseTag": "v0.16.7",
5
+ "assetName": "agent-device-android-snapshot-helper-0.16.7.apk",
6
6
  "apkUrl": null,
7
- "sha256": "a6a8555dda194d1e882a930bc6ccf555ff48f1645faf92968a0832f864c91661",
8
- "checksumName": "agent-device-android-snapshot-helper-0.16.6.apk.sha256",
7
+ "sha256": "5e134ba87fbe184f76138ea59e4577d19a1ccbbb7d573b4d41c8997e29da60fa",
8
+ "checksumName": "agent-device-android-snapshot-helper-0.16.7.apk.sha256",
9
9
  "packageName": "com.callstack.agentdevice.snapshothelper",
10
- "versionCode": 16006,
10
+ "versionCode": 16007,
11
11
  "instrumentationRunner": "com.callstack.agentdevice.snapshothelper/.SnapshotInstrumentation",
12
12
  "minSdk": 23,
13
13
  "targetSdk": 36,
package/dist/src/9533.js CHANGED
@@ -1 +1 @@
1
- import{promises as e}from"node:fs";import{deflateSync as t,inflateSync as n}from"node:zlib";import r from"node:path";import{asAppError as i,AppError as a}from"./9152.js";import{normalizeRef as o,centerOfRect as s,buildSnapshotPresentationKey as l,findNodeByRef as d}from"./4057.js";import{isScrollableNodeLike as c}from"./2842.js";import{successText as u}from"./1998.js";import{whichCmd as h,runCmd as p}from"./9818.js";import{trimText as f,extractNodeText as w,findSnapshotAncestor as m,parseSelectorChain as b,isNodeVisible as g,buildSnapshotNodeByIndex as y,isNodeEditable as x,buildTextPreview as v,findNearestHittableAncestor as k,buildSelectorChainForNode as I,findSelectorChainMatch as M,resolveSelectorChain as A,findNodeByLabel as N,resolveRefLabel as R,describeTextSurface as P,extractReadableText as S,formatSelectorFailure as D,normalizeType as E,isFillableType as O}from"./940.js";import{findBestMatchesByLocator as _}from"./7556.js";import"./7847.js";import{resolveAppsFilter as T,assertResolvedAppsFilter as $}from"./1393.js";let C=["status","get","dismiss","enter","return"];function L(e){return C.includes(e)}function U(e){let t=e?.trim().split(/\s+/,1)[0];if(!t||!t.startsWith("@")||t.length<3)return null;let n=t.slice(1);return/^[A-Za-z_-]*\d[\w-]*$/i.test(n)||/^(?:ref|node|element|el)[\w-]*$/i.test(n)?t:null}function B(e){let t=e.direction,n="up"===t||"down"===t?e.referenceHeight:e.referenceWidth,r=function(e){if(void 0===e)return .6;if(!Number.isFinite(e)||e<=0)throw new a("INVALID_ARGS","scroll amount must be a positive number");return e}(e.amount),i=void 0!==e.pixels?function(e){if(!Number.isFinite(e)||e<=0)throw new a("INVALID_ARGS","scroll pixels must be a positive integer");return Math.max(1,Math.round(e))}(e.pixels):Math.round(n*r),o=Math.max(1,Math.round(.05*n)),s=Math.max(1,Math.min(i,Math.max(1,n-2*o))),l=Math.round(s/2),d=Math.round(e.referenceWidth/2),c=Math.round(e.referenceHeight/2),u=(n,r,i,a)=>({direction:t,x1:n,y1:r,x2:i,y2:a,referenceWidth:e.referenceWidth,referenceHeight:e.referenceHeight,amount:e.amount,pixels:s});switch(t){case"up":return u(d,c-l,d,c+l);case"down":return u(d,c+l,d,c-l);case"left":return u(d-l,c,d+l,c);case"right":return u(d+l,c,d-l,c)}}function G(e){return{...B({...e,direction:function(e){switch(e){case"up":return"down";case"down":return"up";case"left":return"right";case"right":return"left"}}(e.direction)}),direction:e.direction}}function F(e,t,n={}){let r=n.marginPx??8,i="android"===n.platform?65:50,[a,o,s]="left"===e?[90,10,i]:"right"===e?[10,90,i]:"left-edge"===e?[99,15,50]:[1,85,50],l=Y(t,a,s,{marginPx:r}),d=Y(t,o,s,{marginPx:r});return{preset:e,x1:l.x,y1:l.y,x2:d.x,y2:d.y,referenceWidth:t.referenceWidth,referenceHeight:t.referenceHeight}}function q(e){switch(e){case"left":case"right":case"left-edge":case"right-edge":return e;default:throw new a("INVALID_ARGS","gesture swipe requires left, right, left-edge, or right-edge")}}function X(e){let t=function(e){let t=e.filter(e=>(function(e){if(!e)return!1;let t=e.toLowerCase();return t.includes("application")||t.includes("window")})(e.type)&&z(e.rect)).map(e=>e.rect).sort((e,t)=>(t?.width??0)*(t?.height??0)-(e?.width??0)*(e?.height??0))[0];if(t)return t;let n=e.map(e=>e.rect).filter(z);if(0===n.length)return;let r=Math.max(...n.map(e=>e.x+e.width)),i=Math.max(...n.map(e=>e.y+e.height));if(!(r<=0)&&!(i<=0))return{x:0,y:0,width:r,height:i}}(e);if(t)return{referenceWidth:t.width,referenceHeight:t.height}}function Y(e,t,n,r={}){let i={x:Math.round(e.referenceWidth*t/100),y:Math.round(e.referenceHeight*n/100)};return void 0===r.marginPx?i:V(i,e,r.marginPx)}function V(e,t,n){return{x:K(e.x,n,t.referenceWidth),y:K(e.y,n,t.referenceHeight)}}function H(e){switch(e){case"up":case"down":case"left":case"right":return e;default:throw new a("INVALID_ARGS",`Unknown direction: ${e}`)}}function z(e){return!!e&&e.width>0&&e.height>0}function K(e,t,n){let r=Math.max(t,n-t);return j(e,t,r)}function j(e,t,n){return Math.min(Math.round(n),Math.max(Math.round(t),Math.round(e)))}function W(e,t,n){return t>=e.x&&t<=e.x+e.width&&n>=e.y&&n<=e.y+e.height}function J(e){let t=null,n=-1;for(let r of e){let e=r.width*r.height;e>n&&(t=r,n=e)}return t}function Z(e,t,n,r){return Math.max(e,n)<=Math.min(t,r)}function Q(e){let t=e.trim().toLowerCase();return!!t&&/^(vertical|horizontal)\s+scroll\s+bar(?:,?\s*\d+\s+pages?)?$/.test(t)}function ee(e,t){if(!(e?.trim().toLowerCase()??"").includes("vertical scroll bar"))return null;let n=function(e){if(!e)return null;let t=/^(\d{1,3})%$/.exec(e.trim());if(!t)return null;let n=Number(t[1]);return Number.isFinite(n)?Math.max(0,Math.min(100,n)):null}(t);return null===n?null:n<=1?{above:!1,below:!0}:n>=99?{above:!0,below:!1}:{above:!0,below:!0}}function et(e){let t=new Map;for(let[n,r]of e.entries())t.set(r.index,n);let n=[],r=[];for(let[i,a]of e.entries()){let e=Math.max(0,a.depth??0);for(;r.length>0&&e<=r[r.length-1].depth;)r.pop();let o="number"==typeof a.parentIndex?t.get(a.parentIndex):void 0,s="number"==typeof o&&o<i?o:r[r.length-1]?.index;n.push({...a,index:i,depth:e,parentIndex:s}),r.push({depth:e,index:i})}return n}function en(e){return new Map(e.map(e=>[e.index,e]))}function er(e){return e.label?.trim()||e.value?.trim()||e.identifier?.trim()||""}function ei(e){if(0===e.length)return{nodes:e,hiddenCount:0,summaryLines:[]};let{byIndex:t,visibleNodeIndexes:n,offscreenNodes:r,hintedContainers:i}=eo(e),a=0===n.size?e:e.filter(e=>n.has(e.index));return{nodes:a.map(e=>(function(e,t){let n=t.get(e.index);if(!n||0===n.size)return e;let r=!!(!0===e.hiddenContentAbove||n.has("above"))||void 0,i=!!(!0===e.hiddenContentBelow||n.has("below"))||void 0;return{...e,hiddenContentAbove:r,hiddenContentBelow:i}})(e,i.directionsByContainer)),hiddenCount:0===n.size?0:e.length-a.length,summaryLines:function(e,t,n){let r=new Map;for(let i of e){let e=function(e,t,n){if(!e.rect)return null;let r=el(e,t,n);return r?ed(e.rect,r):null}(i,t,n);if(!e)continue;let a=r.get(e)??[];a.push(i),r.set(e,a)}return["above","below"].flatMap(e=>{let t=r.get(e);if(!t||0===t.length)return[];let n=(function(e){let t=new Set,n=[];for(let r of e){let e=er(r);!e||t.has(e)||(t.add(e),n.push(e))}return n})(t).slice(0,3).map(e=>`"${e}"`),i=1===t.length?"interactive item":"interactive items",a=n.length>0?`: ${n.join(", ")}`:"";return[`[off-screen ${e}] ${t.length} ${i}${a}`]})}(r.filter(e=>!i.coveredNodeIndexes.has(e.index)&&function(e){if(!0===e.hittable)return!0;let t=(e.type??"").toLowerCase();return t.includes("button")||t.includes("link")||t.includes("textfield")||t.includes("edittext")||t.includes("searchfield")||t.includes("checkbox")||t.includes("radio")||t.includes("switch")||t.includes("menuitem")||!!er(e)}(e)),e,t)}}function ea(e){if(0===e.length)return new Map;let{hintedContainers:t}=eo(e);var n=t.directionsByContainer;let r=new Map;for(let[e,t]of n){let n={};t.has("above")&&(n.hiddenContentAbove=!0),t.has("below")&&(n.hiddenContentBelow=!0),(n.hiddenContentAbove||n.hiddenContentBelow)&&r.set(e,n)}return r}function eo(e){let t=en(e),n=new Set,r=[];for(let i of e){if(es(i,e,t)){!function(e,t,n){let r=e,i=new Set;for(;r&&!i.has(r.index);)i.add(r.index),t.add(r.index),r="number"==typeof r.parentIndex?n.get(r.parentIndex):void 0}(i,n,t);continue}r.push(i)}let i=function(e,t,n,r){let i=new Map,a=new Map,o=new Set;for(let e of t){if(!e.rect)continue;let t=ec(e,n,r);if(!t?.rect)continue;let s=ed(e.rect,t.rect);if(!s)continue;let l=i.get(t.index)??new Set;l.add(s),i.set(t.index,l);let d=a.get(t.index)??new Set;d.add(s),a.set(t.index,d),o.add(e.index)}return function(e,t,n,r,i){for(let a of e){let e=function(e){let t=ee(e.label,e.value);if(!t)return null;let n=new Set;return t.above&&n.add("above"),t.below&&n.add("below"),n.size>0?n:null}(a);if(!e||0===e.size)continue;let o=ec(a,t,n);if(!o)continue;let s=r.get(o.index)??new Set,l=i.get(o.index);for(let t of e)(!l||!(l.size>0)||l.has(t))&&s.add(t);r.set(o.index,s)}}(e,n,r,i,a),{directionsByContainer:i,coveredNodeIndexes:o}}(e,r,n,t);return{byIndex:t,visibleNodeIndexes:n,offscreenNodes:r,hintedContainers:i}}function es(e,t,n=en(t)){var r;if(!e.rect)return!0;let i=el(e,t,n);return!i||(r=e.rect,Z(r.x,r.x+r.width,i.x,i.x+i.width)&&Z(r.y,r.y+r.height,i.y,i.y+i.height))}function el(e,t,n=en(t)){var r,i;let a=(r=e,i=n,eu(r,i,e=>!!e.rect)?.rect??null);return a||function(e,t){let n=s(t),r=e.filter(e=>{var t;return!!(t=e.rect)&&Number.isFinite(t.x)&&Number.isFinite(t.y)&&Number.isFinite(t.width)&&Number.isFinite(t.height)}),i=r.filter(e=>{let t=(e.type??"").toLowerCase();return t.includes("application")||t.includes("window")}),a=J(i.map(e=>e.rect).filter(e=>W(e,n.x,n.y)));if(a)return a;let o=J(i.map(e=>e.rect));if(o)return o;let l=J(r.map(e=>e.rect).filter(e=>W(e,n.x,n.y)));return l||null}(t,e.rect??{x:0,y:0,width:0,height:0})}function ed(e,t){return e.y+e.height<=t.y?"above":e.y>=t.y+t.height?"below":null}function ec(e,t,n){return eu(e,n,e=>t.has(e.index))}function eu(e,t,n){let r="number"==typeof e.parentIndex?t.get(e.parentIndex):void 0,i=new Set;for(;r&&!i.has(r.index);){if(i.add(r.index),n(r)&&c(r))return r;r="number"==typeof r.parentIndex?t.get(r.parentIndex):void 0}return null}async function eh(e){let{edge:t,target:n={},scope:r,captureNodes:i}=e;try{let e=await i(r),a=function(e,t,n={}){var r,i,a;let o,s=(e??[]).map((e,t)=>({...e,ref:"ref"in e&&e.ref?e.ref:`e${t+1}`}));if(0===s.length)return{canScroll:!1,emptySnapshot:!0,signature:""};let l=ea(s),d=function(e,t,n,r){let i=new Map(e.map(e=>[e.index,e])),a=e.filter(e=>c(e)&&ex(e.rect));if(0===a.length)return null;let o=function(e,t){if(void 0===e)return null;let n=t.get(e);for(;n;){if(c(n)&&ex(n.rect))return n;n=void 0===n.parentIndex?void 0:t.get(n.parentIndex)}return null}(r.nodeIndex,i);if(o)return o;let s=r.point;if(s){let e=a.filter(e=>{var t,n;return e.rect&&(t=e.rect,(n=s).x>=t.x&&n.x<=t.x+t.width&&n.y>=t.y&&n.y<=t.y+t.height)}).sort(eb);if(e.length>0)return e.find(e=>ew(e,t.get(e.index),n))??e[0]??null}let l=a.filter(e=>ew(e,t.get(e.index),n)).sort(eg);return l.length>0?l[0]??null:a.filter(t=>es(t,e)).sort(eg)[0]??a.sort(eg)[0]??null}(s,l,t,n),u=(d?(r=s,i=d.index,o=new Map(r.map(e=>[e.index,e])),r.filter(e=>e.index===i||function(e,t,n){let r=void 0===e.parentIndex?void 0:n.get(e.parentIndex);for(;r;){if(r.index===t)return!0;r=void 0===r.parentIndex?void 0:n.get(r.parentIndex)}return!1}(e,i,o))):s).map(e=>{let t=e.rect?["x","y","width","height"].map(t=>{var n;return n=e.rect?.[t],"number"==typeof n&&Number.isFinite(n)?n.toFixed(1):""}).join(","):"";return[String(e.index??""),String(e.parentIndex??""),String(e.type??""),String(e.label??""),String(e.value??""),t].join("|")}).join("\n");return d?{canScroll:ew(d,l.get(d.index),t),emptySnapshot:!1,signature:u,scope:[(a=d).identifier,a.label,a.value].map(e=>"string"==typeof e?e.trim():"").find(em)}:{canScroll:!1,emptySnapshot:!1,signature:u}}(e,t,n);if(r&&a.emptySnapshot)return await eh({edge:t,target:n,captureNodes:i});return a}catch(e){var o,s,l;throw o=t,s=r,l=e,s?new a("COMMAND_FAILED",`Failed to verify scroll ${o} state for scoped container`,{scope:s,hint:`scroll ${o} could not verify the scoped scroll container. Run snapshot -i -c for the current screen and retry with a visible scroll target.`},l):new a("COMMAND_FAILED",`Failed to verify scroll ${o} state`,{hint:`scroll ${o} needs a snapshot showing hidden content ${"bottom"===o?"below":"above"} before it will move.`},l)}}async function ep(e){let t,{edge:n,captureState:r,scroll:i}=e,o=await r();o.scope&&(o=await r(o.scope));let s=0;for(;o.canScroll;){if(s>=40)throw new a("COMMAND_FAILED",`scroll ${n} reached the safety limit before the snapshot showed the edge`,{hint:"The scoped scroll container still reports hidden content. Use a smaller manual scroll + snapshot loop to inspect the current state."});t=await i(),s+=1,o=await r(o.scope)}return{passes:s,result:t}}function ef(e,t,n,r,i){return t&&0===n?`Already at ${t}; no hidden content ${"bottom"===t?"below":"above"} detected`:t?`Scrolled to ${t} with ${n} ${e} passes`:void 0!==i?`Scrolled ${e} by ${i}px`:void 0!==r?`Scrolled ${e} by ${r}`:`Scrolled ${e}`}function ew(e,t,n){return"bottom"===n?!0===e.hiddenContentBelow||t?.hiddenContentBelow===!0:!0===e.hiddenContentAbove||t?.hiddenContentAbove===!0}function em(e){return e.length>0&&e.length<=80&&!/^(true|false)$/i.test(e)&&!/^\d+$/.test(e)&&!/^\d+%$/.test(e)}function eb(e,t){return ey(e.rect)-ey(t.rect)}function eg(e,t){return ey(t.rect)-ey(e.rect)}function ey(e){return e?e.width*e.height:0}function ex(e){return!!(e&&e.width>0&&e.height>0)}function ev(e,t,n,r){if(!Number.isFinite(e)||!Number.isInteger(e)||e<n||e>r)throw new a("INVALID_ARGS",`${t} must be an integer between ${n} and ${r}`);return e}function ek(e){let t=e?.trim().toLowerCase();return!!t&&(t.includes("open debugger to view warnings")||/^!,\s+/.test(t)||/^(warn|warning|error):\s+/.test(t)||/\b(?:possible\s+)?unhandled (?:promise )?rejection\b/.test(t)||t.includes("getsnapshot should be cached to avoid an infinite loop")||t.includes('unique "key" prop')||t.includes("unique 'key' prop")||t.includes("virtualizedlists should never be nested")||t.includes("failed prop type"))}function eI(e){return e.includes("open debugger to view warnings")||/^!,\s+open debugger\b/.test(e)}let eM=Buffer.from([137,80,78,71,13,10,26,10]),eA=new Map([[0,1],[2,3],[3,1],[4,2],[6,4]]),eN=new Map([[0,new Set([1,2,4,8,16])],[2,new Set([8,16])],[3,new Set([1,2,4,8])],[4,new Set([8,16])],[6,new Set([8,16])]]),eR=[{x:0,y:0,dx:8,dy:8},{x:4,y:0,dx:8,dy:8},{x:0,y:4,dx:4,dy:8},{x:2,y:0,dx:4,dy:4},{x:0,y:2,dx:2,dy:4},{x:1,y:0,dx:2,dy:2},{x:0,y:1,dx:1,dy:2}];class eP{width;height;data;static sync={read:eS,write:eD};constructor(e){this.width=eB(e.width,"width"),this.height=eB(e.height,"height");let t=this.width*this.height*4;if(this.data=e.data?Buffer.from(e.data):Buffer.alloc(t),this.data.length!==t)throw Error(`PNG data length must be ${t} bytes`)}}function eS(e){var t;let{metadata:r,idatChunks:i}=function(e){let t,n=[];for(let r of function*(e){if(!e.subarray(0,eM.length).equals(eM))throw Error("Invalid PNG signature");let t=eM.length;for(;t<e.length;){if(t+12>e.length)throw Error("Truncated PNG chunk");let n=e.readUInt32BE(t),r=e.toString("ascii",t+4,t+8),i=t+8,a=i+n;if(a+4>e.length)throw Error(`Truncated PNG ${r} chunk`);let o=e.subarray(i,a),s=e.readUInt32BE(a);if(eU(e.subarray(t+4,a))!==s)throw Error(`Invalid PNG ${r} chunk CRC`);t=a+4,yield{type:r,data:o}}}(e))if("IHDR"===r.type?t=function(e){var t;if(13!==e.length)throw Error("Invalid PNG IHDR length");let n=e.readUInt32BE(0),r=e.readUInt32BE(4),i=e[8],a=e[9],o=e[10],s=e[11],l=e[12];if(t=a,!eA.has(t))throw Error(`Unsupported PNG color type ${a}`);let d=eN.get(a);if(!d?.has(i))throw Error(`Unsupported PNG color type ${a} with bit depth ${i}`);if(0!==o)throw Error(`Unsupported PNG compression method ${o}`);if(0!==s)throw Error(`Unsupported PNG filter method ${s}`);if(0!==l&&1!==l)throw Error(`Unsupported PNG interlace method ${l}`);return{width:eB(n,"width"),height:eB(r,"height"),bitDepth:i,colorType:a,interlace:l}}(r.data):"IDAT"===r.type?n.push(Buffer.from(r.data)):t=function(e,t){if("PLTE"===e.type){if(!t)throw Error("PNG PLTE appeared before IHDR");t.palette=Buffer.from(e.data)}else if("tRNS"===e.type){if(!t)throw Error("PNG tRNS appeared before IHDR");t.transparency=Buffer.from(e.data)}return t}(r,t),"IEND"===r.type)break;return{metadata:t,idatChunks:n}}(e);if(!r)throw Error("PNG is missing IHDR");if(0===i.length)throw Error("PNG is missing IDAT");let a=function(e,t){let r=function(e){if(0===e.interlace)return function(e,t){return(e$(e)+1)*t}(e,e.height);let t=0;for(let n of eR){let r=eO(e.width,n.x,n.dx),i=eO(e.height,n.y,n.dy);0!==r&&0!==i&&(t+=function(e,t){return(e$(e)+1)*t}({...e,width:r,height:i},i))}return t}(t);try{let t=n(Buffer.concat(e),{maxOutputLength:r});if(t.length!==r)throw Error("PNG pixel data is truncated");return t}catch(e){var i;if((i=e)instanceof Error&&"code"in i&&"ERR_BUFFER_TOO_LARGE"===i.code)throw Error(`PNG pixel data exceeds expected length ${r}`);throw e}}(i,r);return new eP({width:r.width,height:r.height,data:1===r.interlace?function(e,t){let n=Buffer.alloc(t.width*t.height*4),r=0;for(let i of eR){let a=eO(t.width,i.x,i.dx),o=eO(t.height,i.y,i.dy);if(0===a||0===o)continue;let s={...t,width:a,height:o,interlace:0},l=eE({inflated:e,offset:r,scanlineLength:e$(s),height:o,bytesPerPixel:eC(s)});r=l.offset;let d=e$(s);for(let e=0;e<o;e+=1){let r=l.raw.subarray(e*d,(e+1)*d);for(let o=0;o<a;o+=1){let a=i.x+o*i.dx,l=((i.y+e*i.dy)*t.width+a)*4,[d,c,u,h]=e_(r,o,s);n[l]=d,n[l+1]=c,n[l+2]=u,n[l+3]=h}}}return n}(a,r):function(e,t){let n=Buffer.alloc(t.width*t.height*4),r=e$(t);for(let i=0;i<t.height;i+=1){let a=e.subarray(i*r,(i+1)*r);for(let e=0;e<t.width;e+=1){let r=(i*t.width+e)*4,[o,s,l,d]=e_(a,e,t);n[r]=o,n[r+1]=s,n[r+2]=l,n[r+3]=d}}return n}(eE({inflated:a,offset:0,scanlineLength:e$(t=r),height:t.height,bytesPerPixel:eC(t)}).raw,r)})}function eD(e){let n=4*e.width,r=Buffer.alloc((n+1)*e.height);for(let t=0;t<e.height;t+=1){let i=t*(n+1);r[i]=0,e.data.copy(r,i+1,t*n,(t+1)*n)}let i=Buffer.alloc(13);return i.writeUInt32BE(e.width,0),i.writeUInt32BE(e.height,4),i[8]=8,i[9]=6,i[10]=0,i[11]=0,i[12]=0,Buffer.concat([eM,eL("IHDR",i),eL("IDAT",t(r)),eL("IEND",Buffer.alloc(0))])}function eE(e){let{inflated:t,offset:n,scanlineLength:r,height:i,bytesPerPixel:a}=e,o=n+(r+1)*i;if(t.length<o)throw Error("PNG pixel data is truncated");let s=Buffer.alloc(r*i);for(let e=0;e<i;e+=1){let i=n+e*(r+1),o=e*r,l=t[i];for(let n=0;n<r;n+=1){let d=t[i+1+n],c=n>=a?s[o+n-a]:0,u=e>0?s[o+n-r]:0,h=e>0&&n>=a?s[o+n-r-a]:0;s[o+n]=function(e,t,n,r,i){if(0===e)return t;if(1===e)return t+n&255;if(2===e)return t+r&255;if(3===e)return t+Math.floor((n+r)/2)&255;if(4===e)return t+function(e,t,n){let r=e+t-n,i=Math.abs(r-e),a=Math.abs(r-t),o=Math.abs(r-n);return i<=a&&i<=o?e:a<=o?t:n}(n,r,i)&255;throw Error(`Unsupported PNG filter type ${e}`)}(l,d,c,u,h)}}return{raw:s,offset:o}}function eO(e,t,n){return e<=t?0:Math.floor((e-t+n-1)/n)}function e_(e,t,n){if(3===n.colorType){var r,i,a,o,s,l,d,c=e,u=t,h=n;if(!h.palette)throw Error("Indexed PNG is missing PLTE");let p=eT(c,u,h.bitDepth),f=3*p;if(f+2>=h.palette.length)throw Error("Indexed PNG palette is invalid");return[h.palette[f],h.palette[f+1],h.palette[f+2],h.transparency?.[p]??255]}if(n.bitDepth<8){let i,a;return[a=Math.round((i=eT(e,t,(r=n).bitDepth))/((1<<r.bitDepth)-1)*255),a,a,255*!(r.transparency&&r.transparency.length>=2&&i===r.transparency.readUInt16BE(0))]}let p=16===n.bitDepth?2:1,f=t*eA.get(n.colorType)*p,w=t=>16===n.bitDepth?e.readUInt16BE(f+2*t):e[f+t*p],m=e=>{var t;return t=w(e),16===n.bitDepth?Math.round(t/65535*255):t};if(0===n.colorType){let e=m(0);return[e,e,e,255*(i=w(0),!(a=n).transparency||!!(a.transparency.length<2)||i!==a.transparency.readUInt16BE(0))]}if(2===n.colorType){return[m(0),m(1),m(2),255*(o=w(0),s=w(1),l=w(2),!(d=n).transparency||!!(d.transparency.length<6)||o!==d.transparency.readUInt16BE(0)||s!==d.transparency.readUInt16BE(2)||l!==d.transparency.readUInt16BE(4))]}if(4===n.colorType){let e=m(0);return[e,e,e,m(1)]}return[m(0),m(1),m(2),m(3)]}function eT(e,t,n){let r=t*n;return e[Math.floor(r/8)]>>8-n-r%8&(1<<n)-1}function e$(e){let t=eA.get(e.colorType);return Math.ceil(e.width*t*e.bitDepth/8)}function eC(e){return Math.max(1,Math.ceil(eA.get(e.colorType)*e.bitDepth/8))}function eL(e,t){let n=Buffer.from(e,"ascii"),r=Buffer.alloc(8+t.length+4);return r.writeUInt32BE(t.length,0),n.copy(r,4),t.copy(r,8),r.writeUInt32BE(eU(Buffer.concat([n,t])),8+t.length),r}function eU(e){let t=0xffffffff;for(let n of e){t^=n;for(let e=0;e<8;e+=1)t=1&t?0xedb88320^t>>>1:t>>>1}return(0xffffffff^t)>>>0}function eB(e,t){if(!Number.isInteger(e)||e<1)throw Error(`PNG ${t} must be positive`);return e}function eG(e,t){try{return eP.sync.read(e)}catch(e){throw new a("COMMAND_FAILED",`Failed to decode ${t} as PNG`,{label:t,reason:e instanceof Error?e.message:String(e)})}}async function eF(t,n){if(!Number.isInteger(n)||n<1)throw new a("INVALID_ARGS","Screenshot max size must be a positive integer");let r=eG(await e.readFile(t),"screenshot"),i=Math.max(r.width,r.height);if(i<=n)return;let o=n/i,s=Math.max(1,Math.round(r.width*o)),l=Math.max(1,Math.round(r.height*o)),d=function(e,t,n){let r=new eP({width:t,height:n});for(let i=0;i<n;i+=1){let a=i*e.height/n,o=(i+1)*e.height/n;for(let n=0;n<t;n+=1){let s=n*e.width/t,l=(n+1)*e.width/t,d=0,c=0,u=0,h=0,p=0;for(let t=Math.floor(a);t<Math.ceil(o);t+=1){let n=Math.min(t+1,o)-Math.max(t,a);for(let r=Math.floor(s);r<Math.ceil(l);r+=1){let i=n*(Math.min(r+1,l)-Math.max(r,s)),a=(t*e.width+r)*4;d+=(e.data[a]??0)*i,c+=(e.data[a+1]??0)*i,u+=(e.data[a+2]??0)*i,h+=(e.data[a+3]??0)*i,p+=i}}let f=(i*r.width+n)*4;r.data[f]=Math.round(d/p),r.data[f+1]=Math.round(c/p),r.data[f+2]=Math.round(u/p),r.data[f+3]=Math.round(h/p)}}return r}(r,s,l);await e.writeFile(t,eP.sync.write(d))}async function eq(e,t,n){if("path"===t.kind&&!e.policy.allowLocalInputPaths)throw new a("INVALID_ARGS",`Local ${n.field??"input"} paths are not allowed by command policy`);try{return await e.artifacts.resolveInput(t,n)}catch(e){throw i(e)}}async function eX(e,t,n){if(t?.kind==="path"&&!e.policy.allowLocalOutputPaths)throw new a("INVALID_ARGS","Local output paths are not allowed by command policy");try{return await e.artifacts.reserveOutput(t,{...n,visibility:n.visibility??"client-visible",requestedClientPath:t?.kind==="downloadableArtifact"?t.clientPath??n.requestedClientPath:n.requestedClientPath})}catch(e){throw i(e)}}async function eY(e,t){try{return await e.artifacts.createTempFile(t)}catch(e){throw i(e)}}let eV=async(e,t)=>{let n;if(!e.backend.captureScreenshot)throw new a("UNSUPPORTED_OPERATION","screenshot is not supported by this backend");let r=await eX(e,t.out,{field:"path",ext:".png"});try{await e.backend.captureScreenshot({session:t.session,requestId:t.requestId,appId:t.appId,appBundleId:t.appBundleId,signal:t.signal??e.signal,metadata:t.metadata},r.path,{fullscreen:t.fullscreen,overlayRefs:t.overlayRefs,stabilize:t.stabilize,surface:t.surface}),void 0!==t.maxSize&&await eF(r.path,t.maxSize),n=await r.publish()}catch(e){throw await r.cleanup?.(),e}return{path:r.path,...n?{artifacts:[n]}:{},...u(`Saved screenshot: ${r.path}`)}},eH=[0,187,255,255];function ez(e,t,n,r){if(t<0||t>=e.width||n<0||n>=e.height)return;let i=(n*e.width+t)*4;e.data[i]=r[0],e.data[i+1]=r[1],e.data[i+2]=r[2],e.data[i+3]=r[3]}function eK(e,t,n){return Math.min(Math.max(e,t),n)}let ej=[{x:-1,y:-1},{x:0,y:-1},{x:1,y:-1},{x:-1,y:0},{x:1,y:0},{x:-1,y:1},{x:0,y:1},{x:1,y:1}];function eW(e){let{mask:t,width:n,height:r,hooks:i}=e,a=new Uint8Array(t.length),o=new Int32Array(t.length),s=[];for(let e=0;e<t.length;e+=1){var l,d;if(!eJ(t,a,e))continue;let c=0,u=(l=o,a[d=e]=1,l[0]=d,1),h=i.create(e);for(;c<u;){let e=o[c];c+=1,i.visit(h,e),u=function(e){let{mask:t,visited:n,queue:r,width:i,height:a,pixelIndex:o}=e,s=o%i,l=Math.floor(o/i),d=e.queueEnd;for(let e of ej){var c,u,h,p;let o=s+e.x,f=l+e.y;if(c=o,u=f,h=i,p=a,!(c>=0)||!(c<h)||!(u>=0)||!(u<p))continue;let w=f*i+o;eJ(t,n,w)&&(d=function(e,t,n,r){return t[r]=1,e[n]=r,n+1}(r,n,d,w))}return d}({mask:t,visited:a,queue:o,queueEnd:u,width:n,height:r,pixelIndex:e})}s.push(h)}return s}function eJ(e,t,n){return 1===e[n]&&1!==t[n]}function eZ(e){let t=1/0,n=1/0,r=-1/0,i=-1/0;for(let a of e)t=Math.min(t,a.x),n=Math.min(n,a.y),r=Math.max(r,a.x+a.width),i=Math.max(i,a.y+a.height);return{x:t,y:n,width:r-t,height:i-n}}function eQ(e,t){let n=Math.max(e.x,t.x),r=Math.max(e.y,t.y),i=Math.min(e.x+e.width,t.x+t.width),a=Math.min(e.y+e.height,t.y+t.height);return i<=n||a<=r?0:(i-n)*(a-r)}function e0(e){return{x:e.x+e.width/2,y:e.y+e.height/2}}function e1(e,t){return(e.x-t.x)**2+(e.y-t.y)**2}function e2(e,t,n){return Math.min(Math.max(e,t),n)}let e5={icon:90,toggle:90,chevron:75,separator:45,visual:35,background:10},e4={leading:20,trailing:20,separator:10,unknown:0,background:-30};function e8(e){return"background"!==e.likelyKind}function e3(e,t){return e.width>=.25*t.width||e.height>=.06*t.height}function e6(e,t){let n,r=0;for(let i of t){let t=tn(e,i.rect);t<=r||(r=t,n=i)}return n}function e9(e){let t=[];for(let n of[...e].sort((e,t)=>e.rect.y-t.rect.y)){let e=t.find(e=>{var t,r;return t=e.rect,tn(t,r=n.rect)>0||Math.abs(e0(t).y-e0(r).y)<=.5*Math.max(t.height,r.height)});if(!e){t.push({rect:n.rect,blocks:[n]});continue}e.blocks.push(n),e.blocks.sort((e,t)=>e.rect.x-t.rect.x),e.rect=eZ([e.rect,n.rect])}return t}function e7(e,t){let n,r=e0(e);for(let e of t){let t=Math.sqrt(e1(r,e0(e.rect)));n&&t>=n.distance||(n={block:e,distance:t})}return n}function te(e){let t=tt(e);return e.differentPixels>=24&&t.width>=3&&t.height>=3}function tt(e){return{x:e.minX,y:e.minY,width:e.maxX-e.minX+1,height:e.maxY-e.minY+1}}function tn(e,t){return Math.max(0,Math.min(e.y+e.height,t.y+t.height)-Math.max(e.y,t.y))}async function tr(e){if(await h("tesseract"))try{let[t,n]=await Promise.all([ta(e.baselinePath),ta(e.currentPath)]);if(0!==t.exitCode||0!==n.exitCode)return;let r=ti(t.stdout,e.width,e.height),i=ti(n.stdout,e.width,e.height),a=function(e,t){let n=new Set,r=[];for(let a of e){var i;let e=tu(a.text),o=function(e,t,n,r){let i=null,a=1/0;for(let o=0;o<n.length;o+=1){if(r.has(o))continue;let s=n[o];if(tu(s.text)!==t)continue;let l=e1(e0(e.normalizedRect),e0(s.normalizedRect));l>=a||(i=o,a=l)}return i}(a,e,t,n);if(null===o)continue;n.add(o);let s=function(e,t){let n={x:t.rect.x-e.rect.x,y:t.rect.y-e.rect.y,width:t.rect.width-e.rect.width,height:t.rect.height-e.rect.height},r=tf(t.rect.width/e.rect.width),i=tf(t.rect.height/e.rect.height),a=Math.abs(r-1)>=.08||Math.abs(i-1)>=.12;return{text:e.text,baselineRect:e.rect,currentRect:t.rect,delta:n,confidence:Math.round(100*Math.min(e.confidence,t.confidence))/100,possibleTextMetricMismatch:a}}(a,t[o]);i=s,(Math.abs(i.delta.x)>=2||Math.abs(i.delta.y)>=2||Math.abs(i.delta.width)>=2||Math.abs(i.delta.height)>=2||i.possibleTextMetricMismatch)&&r.push(s)}return r.sort((e,t)=>to(t)-to(e)).slice(0,12)}(r,i),o=function(e){let t=[];for(let n of[...e].sort((e,t)=>e.currentRect.y-t.currentRect.y)){let e=t.find(e=>32>=Math.abs(n.delta.x-th(e.map(e=>e.delta.x))));e?e.push(n):t.push([n])}return t.filter(e=>e.length>=2).map(ts).filter(e=>e.yRange.max-e.yRange.min<=60).sort((e,t)=>tl(t)-tl(e)).slice(0,4)}(a);if(0===r.length&&0===i.length)return;return{provider:"tesseract",baselineBlocks:r.length,currentBlocks:i.length,baselineBlocksRaw:r,currentBlocksRaw:i,matches:a,...o.length>0?{movementClusters:o}:{}}}catch{return}}function ti(e,t,n){let[r,...i]=e.split(/\r?\n/);if(!r)return[];let a=new Map(r.split(" ").map((e,t)=>[e,t])),o=[];for(let e of i){var s;if(!e.trim())continue;let t=e.split(" "),n=tc(t,a,"level"),r=td(t,a,"text").trim(),i=tc(t,a,"conf");if(5!==n||(s=r,!/[\p{L}\p{N}]/u.test(s))||i<0)continue;let l=tc(t,a,"left"),d=tc(t,a,"top"),c=tc(t,a,"width"),u=tc(t,a,"height");c<=0||u<=0||o.push({key:[td(t,a,"page_num"),td(t,a,"block_num"),td(t,a,"par_num"),td(t,a,"line_num")].join(":"),text:r,confidence:i,rect:{x:l,y:d,width:c,height:u}})}let l=new Map;for(let e of o){let t=l.get(e.key);t?t.push(e):l.set(e.key,[e])}return Array.from(l.values()).flatMap(e=>(function(e){let t=[...e].sort((e,t)=>e.rect.x-t.rect.x),n=[],r=[];for(let e of t){let t=r.at(-1);if(!t){r.push(e);continue}if(e.rect.x-(t.rect.x+t.rect.width)>Math.max(48,2.5*Math.max(t.rect.height,e.rect.height))){n.push(r),r=[e];continue}r.push(e)}return r.length>0&&n.push(r),n})(e)).map(e=>(function(e,t,n){if(0===e.length)return null;let r=[...e].sort((e,t)=>e.rect.x-t.rect.x),i=eZ(r.map(e=>e.rect)),a=Math.round(100*th(r.map(e=>e.confidence)))/100;return{text:r.map(e=>e.text).join(" "),confidence:a,rect:i,normalizedRect:{x:tp(i.x/t),y:tp(i.y/n),width:tp(i.width/t),height:tp(i.height/n)}}})(e,t,n)).filter(e=>null!==e)}function ta(e){return p("tesseract",[e,"stdout","-l","eng","tsv"],{allowFailure:!0,timeoutMs:1e4})}function to(e){return Math.abs(e.delta.x)+Math.abs(e.delta.y)+Math.abs(e.delta.width)+Math.abs(e.delta.height)+25*!!e.possibleTextMetricMismatch}function ts(e){let t=e.map(e=>e.delta.x),n=e.map(e=>e.delta.y);return{texts:e.map(e=>e.text),xRange:{min:Math.min(...t),max:Math.max(...t)},yRange:{min:Math.min(...n),max:Math.max(...n)}}}function tl(e){return 2*Math.abs((e.xRange.min+e.xRange.max)/2)+Math.abs((e.yRange.min+e.yRange.max)/2)}function td(e,t,n){let r=t.get(n);return void 0===r?"":e[r]??""}function tc(e,t,n){let r=Number(td(e,t,n));return Number.isFinite(r)?r:0}function tu(e){return e.trim().replace(/\s+/g," ").toLowerCase()}function th(e){return e.reduce((e,t)=>e+t,0)/e.length}function tp(e){return Math.round(100*e*100)/100}function tf(e){return Math.round(1e3*e)/1e3}function tw(e,t,n,r){return{r:Math.round(e/r),g:Math.round(t/r),b:Math.round(n/r)}}function tm(e){return .2126*e.r+.7152*e.g+.0722*e.b}function tb(e){return`#${tg(e.r)}${tg(e.g)}${tg(e.b)}`}function tg(e){return e.toString(16).padStart(2,"0")}function ty(e){return Math.round(100*e*100)/100}let tx=255*Math.sqrt(3);async function tv(t,n,i={}){var a,o,s,l,d;let c,u,h,p;await tk(t,"Baseline image not found"),await tk(n,"Current screenshot not found");let f=i.outputPath,[w,m]=await Promise.all([e.readFile(t),e.readFile(n)]),b=eG(w,"baseline screenshot"),g=eG(m,"current screenshot");tI(b.width,b.height,"baseline screenshot",i.maxPixels),tI(g.width,g.height,"current screenshot",i.maxPixels);let y=i.threshold??.1;if(b.width!==g.width||b.height!==g.height){let e=b.width*b.height;return await tM(i.outputPath),{match:!1,mismatchPercentage:100,totalPixels:e,differentPixels:e,dimensionMismatch:{expected:{width:b.width,height:b.height},actual:{width:g.width,height:g.height}}}}let x=b.width*b.height,v=y*tx,k=new eP({width:b.width,height:b.height}),I=new Uint8Array(x),M=0;for(let e=0,t=0;e<b.data.length;e+=4,t+=1){if(Math.sqrt((b.data[e]-g.data[e])**2+(b.data[e+1]-g.data[e+1])**2+(b.data[e+2]-g.data[e+2])**2)>v){M+=1,I[t]=1;let n=tA(g,e);k.data[e]=tN(n,220,.78),k.data[e+1]=tN(n,0,.78),k.data[e+2]=tN(n,0,.78),k.data[e+3]=255;continue}let n=tA(g,e);k.data[e]=n,k.data[e+1]=n,k.data[e+2]=n,k.data[e+3]=255}let A=M>0?(o=(c=function(e){let{diffMask:t,baseline:n,current:r}=e,{width:i,height:a}=n;return eW({mask:t,width:i,height:a,hooks:{create:e=>{var t,n;let r,a;return r=(t=e)%(n=i),{minX:r,minY:a=Math.floor(t/n),maxX:r,maxY:a,differentPixels:0,baselineRed:0,baselineGreen:0,baselineBlue:0,currentRed:0,currentGreen:0,currentBlue:0}},visit:(e,t)=>{var a,o,s,l,d;let c,u,h;return a=e,o=t,s=i,l=n,d=r,c=o%s,u=Math.floor(o/s),h=4*o,void(a.minX=Math.min(a.minX,c),a.minY=Math.min(a.minY,u),a.maxX=Math.max(a.maxX,c),a.maxY=Math.max(a.maxY,u),a.differentPixels+=1,a.baselineRed+=l.data[h],a.baselineGreen+=l.data[h+1],a.baselineBlue+=l.data[h+2],a.currentRed+=d.data[h],a.currentGreen+=d.data[h+1],a.currentBlue+=d.data[h+2])}}})}(a={diffMask:I,baseline:b,current:g,totalPixels:x,differentPixels:M,maxRegions:i.maxRegions})).length<=2e3?function(e){let t=[];for(let i of e.sort((e,t)=>{let n=e.minY-t.minY;return 0!==n?n:e.minX-t.minX})){var n,r;let e=t.find(e=>{var t,n,r;return t=e,n=i,r=12,t.minX-r<=n.maxX&&n.minX-r<=t.maxX&&t.minY-r<=n.maxY&&n.minY-r<=t.maxY});if(!e){t.push({...i});continue}n=e,r=i,n.minX=Math.min(n.minX,r.minX),n.minY=Math.min(n.minY,r.minY),n.maxX=Math.max(n.maxX,r.maxX),n.maxY=Math.max(n.maxY,r.maxY),n.differentPixels+=r.differentPixels,n.baselineRed+=r.baselineRed,n.baselineGreen+=r.baselineGreen,n.baselineBlue+=r.baselineBlue,n.currentRed+=r.currentRed,n.currentGreen+=r.currentGreen,n.currentBlue+=r.currentBlue}return t}(c):c,o.flatMap(e=>{var t,n,r;let i;return(t=e,n=a.baseline.width,r=a.baseline.height,i=t.maxX-t.minX+1,t.maxY-t.minY+1>=Math.max(48,Math.round(.07*r))&&i>=.35*n)?function(e,t,n){var r;let i=function(e,t){let n=[],r=null;for(let i=0;i<e.length;i+=1){if(e[i]<=t){r??=i;continue}null!==r&&(i-r>=6&&n.push([r,i-1]),r=null)}return null!==r&&e.length-r>=6&&n.push([r,e.length-1]),n}((r=function(e,t,n){let r=[];for(let i=e.minY;i<=e.maxY;i+=1){let a=0;for(let r=e.minX;r<=e.maxX;r+=1)1===t[i*n+r]&&(a+=1);r.push(a)}return r}(e,t.diffMask,t.baseline.width)).map((e,t)=>{let n=0,i=0,a=Math.max(0,t-3),o=Math.min(r.length-1,t+3);for(let e=a;e<=o;e+=1)n+=r[e],i+=1;return Math.round(n/i)}),Math.max(1,Math.round((e.maxX-e.minX+1)*.08))),a=function(e,t,n){let r=[],i=e.minY;for(let[a,o]of t){let t=e.minY+Math.round((a+o)/2);t-i+1<n||e.maxY-t<n||(r.push([i,t]),i=t+1)}return r.push([i,e.maxY]),r}(e,i,n);if(a.length<=1)return[e];let o=a.map(([n,r])=>(function(e,t,n,r){let i=null;for(let s=t;s<=n;s+=1)for(let t=e.minX;t<=e.maxX;t+=1){var a,o;let e=s*r.baseline.width+t;1===r.diffMask[e]&&function(e,t,n,r,i,a){let o=4*t;e.minX=Math.min(e.minX,n),e.minY=Math.min(e.minY,r),e.maxX=Math.max(e.maxX,n),e.maxY=Math.max(e.maxY,r),e.differentPixels+=1,e.baselineRed+=i.data[o],e.baselineGreen+=i.data[o+1],e.baselineBlue+=i.data[o+2],e.currentRed+=a.data[o],e.currentGreen+=a.data[o+1],e.currentBlue+=a.data[o+2]}(i??={minX:a=t,minY:o=s,maxX:a,maxY:o,differentPixels:0,baselineRed:0,baselineGreen:0,baselineBlue:0,currentRed:0,currentGreen:0,currentBlue:0},e,t,s,r.baseline,r.current)}return i})(e,n,r,t)).filter(e=>null!==e);return o.length>1?o:[e]}(e,a,Math.max(24,Math.round(.03*a.baseline.height))):[e]})).sort((e,t)=>{let n=t.differentPixels-e.differentPixels;if(0!==n)return n;let r=e.minY-t.minY;return 0!==r?r:e.minX-t.minX}).slice(0,Math.max(0,a.maxRegions??8)).map((e,t)=>{var n,r,i,o,s,l,d,c,u,h,p;let f,w,m,b,g,y,x,v,k,I,M,A,N,R,P,S,D;return n=e,r=t+1,i={width:a.baseline.width,height:a.baseline.height,totalPixels:a.totalPixels,differentPixels:a.differentPixels},y={x:n.minX,y:n.minY,width:n.maxX-n.minX+1,height:n.maxY-n.minY+1},x={x:Math.round(n.minX+y.width/2),y:Math.round(n.minY+y.height/2)},v=tw(n.baselineRed,n.baselineGreen,n.baselineBlue,n.differentPixels),k=tw(n.currentRed,n.currentGreen,n.currentBlue,n.differentPixels),I=y.width*y.height,M=ty(n.differentPixels/I),A=Math.round(tm(v)),N=Math.round(tm(k)),R=(o=y,s=i.width,l=i.height,o.width>=.55*s&&o.height>=.12*l?"large-area":o.width>=2.5*o.height?"horizontal-band":o.height>=2.5*o.width?"vertical-band":"compact"),P=(f=I/i.totalPixels)>=.04?"large":f>=.01?"medium":"small",S=(d=v,c=k,w=tm(d),Math.abs(m=tm(c)-w)>=12?m>0?"brighter":"darker":Math.max(Math.abs(c.r-d.r),Math.abs(c.g-d.g),Math.abs(c.b-d.b))>=12?"color-shift":"mixed"),D=(u=x,h=i.width,p=i.height,b=u.x<h/3?"left":u.x>2*h/3?"right":"center",g=u.y<p/3?"top":u.y>2*p/3?"bottom":"middle","center"===b&&"middle"===g?"center":`${g}-${b}`),{index:r,rect:y,normalizedRect:{x:ty(y.x/i.width),y:ty(y.y/i.height),width:ty(y.width/i.width),height:ty(y.height/i.height)},differentPixels:n.differentPixels,shareOfDiffPercentage:ty(n.differentPixels/i.differentPixels),densityPercentage:M,shape:R,size:P,location:D,averageBaselineColorHex:tb(v),averageCurrentColorHex:tb(k),baselineLuminance:A,currentLuminance:N,dominantChange:S}}):[];if(M>0&&f){for(let e of A)e.rect.width<4||e.rect.height<4||function(e,t){let n=eK(t.x,0,e.width-1),r=eK(t.y,0,e.height-1),i=eK(t.x+t.width-1,0,e.width-1),a=eK(t.y+t.height-1,0,e.height-1);for(let t=0;t<2;t+=1){for(let o=n;o<=i;o+=1)ez(e,o,r+t,eH),ez(e,o,a-t,eH);for(let o=r;o<=a;o+=1)ez(e,n+t,o,eH),ez(e,i-t,o,eH)}}(k,e.rect);await e.mkdir(r.dirname(f),{recursive:!0}),await e.writeFile(f,eP.sync.write(k))}else await tM(i.outputPath);let N=M>0?await tr({baselinePath:t,currentPath:n,width:b.width,height:b.height}):void 0,R=N&&(N.matches.length>0||(N.movementClusters?.length??0)>0)?{provider:N.provider,baselineBlocks:N.baselineBlocks,currentBlocks:N.currentBlocks,matches:N.matches,...N.movementClusters?{movementClusters:N.movementClusters}:{}}:void 0,P=M>0&&N?(u=function(e){let t=[];for(let n of e.sort((e,t)=>e.minY-t.minY||e.minX-t.minX)){let e=t.find(e=>{var t,r,i;return t=e,r=n,i=10,t.minX-i<=r.maxX&&r.minX-i<=t.maxX&&t.minY-i<=r.maxY&&r.minY-i<=t.maxY});if(!e){t.push({...n});continue}e.minX=Math.min(e.minX,n.minX),e.minY=Math.min(e.minY,n.minY),e.maxX=Math.max(e.maxX,n.maxX),e.maxY=Math.max(e.maxY,n.maxY),e.differentPixels+=n.differentPixels}return t}((l=function(e,t,n,r){let i=new Uint8Array(e);if(!r)return i;for(let e of[...r.baselineBlocksRaw,...r.currentBlocksRaw]){var a;!function(e,t,n,r){let i=e2(Math.floor(r.x),0,t-1),a=e2(Math.floor(r.y),0,n-1),o=e2(Math.ceil(r.x+r.width),0,t),s=e2(Math.ceil(r.y+r.height),0,n);for(let n=a;n<s;n+=1)for(let r=i;r<o;r+=1)e[n*t+r]=0}(i,t,n,{x:(a=e.rect).x-8,y:a.y-8,width:a.width+16,height:a.height+16})}return i}((s={diffMask:I,width:b.width,height:b.height,regions:A,ocr:N}).diffMask,s.width,s.height,s.ocr),d=s.width,eW({mask:l,width:d,height:s.height,hooks:{create:e=>{var t,n;let r,i;return r=(t=e)%(n=d),{minX:r,minY:i=Math.floor(t/n),maxX:r,maxY:i,differentPixels:0}},visit:(e,t)=>{var n,r,i;let a,o;return n=e,a=(r=t)%(i=d),o=Math.floor(r/i),void(n.minX=Math.min(n.minX,a),n.minY=Math.min(n.minY,o),n.maxX=Math.max(n.maxX,a),n.maxY=Math.max(n.maxY,o),n.differentPixels+=1)}}}))),h=e9(s.ocr?.currentBlocksRaw??[]),p=e9(s.ocr?.baselineBlocksRaw??[]),u.filter(te).map(e=>{var t,n,r,i,a,o,l,d,c,u,f;let w,m,b,g,y,x,v,k,I,M;return t=e,n=s,r=h,i=p,g=function(e,t){let n,r=0;for(let i of t){let t=eQ(e,i.rect);t<=r||(r=t,n=i)}return n?.index}(b=tt(t),n.regions),y=function(e,t,n){let r=e6(e,t);if(r)return e7(e,r.blocks);let i=e6(e,n);return i?e7(e,i.blocks):void 0}(b,r,i),x=function(e,t,n){if(e.height<=3&&e.width>=.12*n)return"separator";if(!t)return e.width>=.4*n?"background":"unknown";if(e.width>=.4*n)return"background";let r=e.x+e.width/2,i=t.x+t.width/2;return r<i-t.width/2?"leading":r>i+t.width/2?"trailing":e.width>=.35*n?"background":"unknown"}(b,y?.block.rect,n.width),v=(a=b,o=x,l=t.differentPixels,d=n,w=a.width/a.height,m=l/(a.width*a.height),"separator"===o?"separator":"background"===o?"background":"trailing"===o&&w>=1.5&&w<=3.8&&m>=.35?"toggle":"trailing"===o&&a.width<=.06*d.width&&a.height<=.04*d.height?"chevron":"leading"===o&&w>=.55&&w<=1.8?"icon":e3(a,d)?"background":"visual"),k={...g?{regionIndex:g}:{},slot:x,likelyKind:v,rect:b},{...g?{regionIndex:g}:{},slot:x,likelyKind:v,rect:b,...y?{nearestText:y.block.text.trim().replace(/^[^\p{L}\p{N}]+/u,"").replace(/^\p{L}\s+/u,"")}:{},score:(c=k,u=t.differentPixels,f=n,I=e3(c.rect,f)?-35:0,M=20*!!c.regionIndex,e5[c.likelyKind]+e4[c.slot]+M+I+Math.min(20,u/200))}}).filter(e=>e.rect.y>=.08*s.height).filter(e8).sort((e,t)=>t.score-e.score).slice(0,Math.max(0,s.maxDeltas??12)).map((e,t)=>{var n;return n=e,{index:t+1,...n.regionIndex?{regionIndex:n.regionIndex}:{},slot:n.slot,likelyKind:n.likelyKind,rect:n.rect,...n.nearestText?{nearestText:n.nearestText}:{}}})):[],S=x>0?Math.round(M/x*1e4)/100:0;return{...M>0&&f?{diffPath:f}:{},...A.length>0?{regions:A}:{},...R?{ocr:R}:{},...P.length>0?{nonTextDeltas:P}:{},totalPixels:x,differentPixels:M,mismatchPercentage:S,match:0===M}}async function tk(t,n){try{await e.access(t)}catch{throw new a("INVALID_ARGS",`${n}: ${t}`)}}function tI(e,t,n,r){if(null==r||r<=0)return;let i=e*t;if(!(i<=r))throw new a("INVALID_ARGS",`${n} is ${i} pixels, which exceeds the configured maxImagePixels limit of ${r}`)}async function tM(t){if(t)try{await e.unlink(t)}catch(e){var n;if(!("object"==typeof(n=e)&&null!==n&&"code"in n&&"ENOENT"===n.code))throw e}}function tA(e,t){return tN(Math.round(.299*e.data[t]+.587*e.data[t+1]+.114*e.data[t+2]),255,.72)}function tN(e,t,n){return Math.round(e*(1-n)+t*n)}function tR(e){return e.width*e.height}function tP(e){return Math.round(100*e*100)/100}async function tS(e,t){let n=await eY(e,{prefix:"agent-device-diff-current",ext:".png"});try{await tE(e,t,n.path,tO(t))}catch(e){throw await n.cleanup(),e}return n}async function tD(e,t,n,r,i){var a,o,s,l;if(!t.overlayRefs)return r;if(r.match||r.dimensionMismatch)return n&&await tT(n),r;let d=(a=t,o=n,a.currentOverlayOut?a.currentOverlayOut:a.out?.kind==="path"?{kind:"path",path:t_(o??a.out.path)}:a.out?.kind==="downloadableArtifact"?{kind:"downloadableArtifact",...a.out.clientPath?{clientPath:t_(a.out.clientPath)}:{},...a.out.fileName?{fileName:t_(a.out.fileName)}:{}}:void 0),c=await eX(e,d,{field:"currentOverlayPath",ext:".png"});try{let n=await tE(e,t,c.path,{overlayRefs:!0,...tO(t)}),a=await c.publish();return a&&i.push(a),{...r,currentOverlayPath:n.path??c.path,...n.overlayRefs?{currentOverlayRefCount:n.overlayRefs.length}:{},...r.regions&&n.overlayRefs?{regions:(s=r.regions,l=n.overlayRefs,s.map(e=>{var t,n;let r,i=(t=e,n=l,r=tR(t.rect),n.map(e=>{let n=e.overlayRect,i=eQ(t.rect,n);return i<=0?null:{ref:e.ref,...e.label?{label:e.label}:{},rect:n,overlayCoveragePercentage:tP(i/tR(n)),regionCoveragePercentage:tP(i/r)}}).filter(e=>null!==e).sort((e,t)=>{let n=t.regionCoveragePercentage-e.regionCoveragePercentage;return 0!==n?n:t.overlayCoveragePercentage-e.overlayCoveragePercentage}).slice(0,3).map(e=>({ref:e.ref,...e.label?{label:e.label}:{},rect:e.rect,regionCoveragePercentage:e.regionCoveragePercentage})));return i.length>0?{...e,currentOverlayMatches:i}:e}))}:{}}}catch(e){throw await c.cleanup?.(),e}}async function tE(e,t,n,r={}){if(!e.backend.captureScreenshot)throw new a("UNSUPPORTED_OPERATION","screenshot is not supported by this backend");return await e.backend.captureScreenshot({session:t.session,requestId:t.requestId,signal:t.signal??e.signal,metadata:t.metadata},n,r)??{}}function tO(e){return e.surface?{surface:e.surface}:{}}function t_(e){let t=r.extname(e),n=t?e.slice(0,-t.length):e;return`${n}.current-overlay${t||".png"}`}async function tT(t){try{await e.unlink(t_(t))}catch(e){var n;if(!("object"==typeof(n=e)&&null!==n&&"code"in n&&"ENOENT"===n.code))throw e}}function t$(e){return"live"===e.kind}let tC={application:"application",navigationbar:"navigation-bar",tabbar:"tab-bar",button:"button",imagebutton:"button",link:"link",cell:"cell",statictext:"text",checkedtextview:"text",textfield:"text-field",edittext:"text-field",textarea:"text-view",switch:"switch",slider:"slider",image:"image",imageview:"image",webview:"webview",framelayout:"group",linearlayout:"group",relativelayout:"group",constraintlayout:"group",viewgroup:"group",view:"group",listview:"list",recyclerview:"list",collectionview:"collection",searchfield:"search",segmentedcontrol:"segmented-control",group:"group",window:"window",checkbox:"checkbox",radio:"radio",menuitem:"menu-item",toolbar:"toolbar",scrollarea:"scroll-area",scrollview:"scroll-area",nestedscrollview:"scroll-area",table:"table"};function tL(e,t={}){let n=[],r=[];for(let i of e){let e=i.depth??0,a=i.label?.trim()||i.value?.trim()||i.identifier?.trim()||"",o=tG(i.type??"Element");if("group"===o&&!a)continue;for(;n.length>0&&e<=n[n.length-1];)n.pop();let s=n.length;n.push(e),r.push({node:i,depth:s,type:o,text:tU(i,s,!1,o,t)})}return r}function tU(e,t,n,r,i={}){var a,o,s,l,d,c,u,h;let p,w=r??tG(e.type??"Element"),m=P(e,w),b=(a=e,o=w,s=i,l=m,s.summarizeTextSurfaces&&l.shouldSummarize&&function(e,t,n){let r=f(e.label);if(r&&r!==n)return r;let i=f(e.identifier);if(i&&!tq(i)&&i!==n)return i;switch(t){case"text":case"text-view":return"Text view";case"text-field":return"Text field";case"search":return"Search field";default:return""}}(a,o,l.text)||tB(a,o)),g=" ".repeat(t),y=e.ref?`@${e.ref}`:"",x=(d=e,c=w,u=i,h=m,p=[],(!1===d.enabled&&p.push("disabled"),u.summarizeTextSurfaces)?(!0===d.selected&&p.push("selected"),!0===d.focused&&p.push("focused"),tF(c)&&p.push("editable"),function(e,t){if("scroll-area"===t)return!0;let n=(e.type??"").toLowerCase(),r=`${e.role??""} ${e.subrole??""}`.toLowerCase();return n.includes("scroll")||r.includes("scroll")}(d,c)&&p.push("scrollable"),p.push(...d.presentationHints??[]),h.shouldSummarize&&(p.push(`preview:"${v(h.text).replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`),p.push("truncated")),tX(p)):p).map(e=>` [${e}]`).join(""),k=b?` "${b}"`:"";return n?`${g}${y} [${w}]${x}`.trimEnd():`${g}${y} [${w}]${k}${x}`.trimEnd()}function tB(e,t){var n,r;let i=e.label?.trim();if(i&&(n=t,r=i,("scroll-area"===n||"list"===n||"collection"===n||"table"===n)&&Q(r)))return"";let a=e.value?.trim();if(tF(t)){if(a)return a;if(i)return i}else if(i)return i;if(a)return a;let o=e.identifier?.trim();return!o||tq(o)&&("group"===t||"image"===t||"list"===t||"collection"===t)?"":o}function tG(e){var t;let n=e.replace(/XCUIElementType/gi,"").toLowerCase(),r=e.includes(".")&&(e.startsWith("android.")||e.startsWith("androidx.")||e.startsWith("com."));return(n.includes(".")&&(n=n.replace(/^android\.widget\./,"").replace(/^android\.view\./,"").replace(/^android\.webkit\./,"").replace(/^androidx\./,"").replace(/^com\.google\.android\./,"").replace(/^com\.android\./,""),r&&n.includes(".")&&(n=n.slice(n.lastIndexOf(".")+1))),"textview"===n)?r?"text":"text-view":(t=n,(Object.prototype.hasOwnProperty.call(tC,t)?tC[t]:void 0)||n||"element")}function tF(e){return"text-field"===e||"text-view"===e||"search"===e}function tq(e){return/^[\w.]+:id\/[\w.-]+$/i.test(e)}function tX(e){return[...new Set(e)]}function tY(e,t){let n=tG(e.type??"Element"),r=tB(e,n),i=!1===e.enabled?"disabled":"enabled",a=!0===e.selected?"selected":"unselected",o=!0===e.hittable?"hittable":"not-hittable";return[String(t??e.depth??0),n,r,i,a,o].join("|")}function tV(e,t){return t.flatten?e.map(e=>({text:tU(e,0,!1),comparable:tY(e,0)})):tL(e).map(e=>({text:e.text,comparable:tY(e.node,e.depth)}))}function tH(e,t){return e.get(t)??0}function tz(e){let t=e.map(e=>[e.label,e.value,e.identifier,e.type,e.role].filter(Boolean).join(" ")).join("\n").toLowerCase(),n=tZ(e,tj),r=tZ(e,tW),i=tZ(e,ek,tJ),a=tZ(e,eI),o=tQ(n),s=tQ(r),l=tQ(i),d=/\b[\w.$<>/-]+\.(?:tsx?|jsx?):\d+(?::\d+)?\b/.test(t)||/\b[\w.$<>/-]+\.(?:tsx?|jsx?)\s+\(\d+:\d+\)/.test(t),c=o.length>0||s.length>0||/\b(reload js|copy stack)\b/.test(t),u=/\b(redbox|runtime error|reload js|copy stack|component stack|call stack)\b/.test(t)||d&&c;return{detected:l.length>0||a.length>0||c&&(/\b(logbox|redbox|reload js|copy stack|component stack|call stack|runtime error|open debugger to view warnings)\b/.test(t)||d),redBox:u,dismissRefs:o,minimizeRefs:s,collapsedRefs:l,dismissNodes:n,minimizeNodes:r,collapsedNodes:i}}function tK(e){let t,n=tz(e);if(!n.detected)return null;if(n.redBox){let e=t0(n.minimizeNodes);if(e)return t1(e,"minimize");let t=t0(n.dismissNodes);return t?{...t1(t,t2(t)),warning:"RedBox Minimize control was not exposed; used Dismiss fallback"}:null}let r=t0(n.dismissNodes);if(r)return t1(r,t2(r));let i=0===(t=n.collapsedNodes.filter(e=>e.rect)).length?null:t.sort((e,t)=>{let n=+(!0===e.hittable),r=+(!0===t.hittable);if(n!==r)return r-n;let i=e.rect?.width??0,a=t.rect?.width??0;return i!==a?a-i:(t.rect?.y??0)-(e.rect?.y??0)})[0]??null;return i?.rect?{action:"close-collapsed-banner",point:function(e){var t,n;if(!e.rect)throw Error("Collapsed React Native warning node must have rect");let r=Math.min(e.rect.height,52),i=Math.min(36,Math.max(18,.45*r));return{x:Math.round((t=e.rect.x+e.rect.width-i,n=e.rect.x+1,Math.min(e.rect.x+e.rect.width-1,Math.max(n,t)))),y:Math.round(e.rect.y+r/2)}}(i),ref:i.ref,label:t5(i)}:null}function tj(e){return"dismiss"===e||"close"===e||["x","\xd7","✕","✖","⨯"].includes(e)}function tW(e){return/^minimi[sz]e$/.test(e)}function tJ(e){return!e.rect||e.rect.height<=180}function tZ(e,t,n=()=>!0){let r=[];for(let i of e)i.ref&&n(i)&&[i.label,i.value,i.identifier].map(e=>e?.trim().toLowerCase()).filter(e=>!!e).some(e=>t(e))&&r.push(i);return r}function tQ(e){return e.map(e=>e.ref)}function t0(e){return e.find(e=>e.rect)??null}function t1(e,t){if(!e.rect)throw Error("React Native overlay target node must have rect");return{action:t,point:s(e.rect),ref:e.ref,label:t5(e)}}function t2(e){return"dismiss"===t5(e)?.trim().toLowerCase()?"dismiss":"close"}function t5(e){return e.label??e.value??e.identifier}function t4(e){return e.map(e=>({index:e.index,depth:e.depth,parentIndex:e.parentIndex,type:e.type,role:e.role,subrole:e.subrole,label:e.label,value:e.value,identifier:e.identifier,enabled:e.enabled,selected:e.selected,focused:e.focused,hittable:e.hittable,rect:e.rect,bundleId:e.bundleId,appName:e.appName,windowTitle:e.windowTitle,surface:e.surface,hiddenContentAbove:e.hiddenContentAbove,hiddenContentBelow:e.hiddenContentBelow}))}function t8(e,t){return{session:t.session,requestId:t.requestId,signal:t.signal??e.signal,metadata:t.metadata}}function t3(e){return e.clock?.now()??Date.now()}async function t6(e,t){e.clock?await e.clock.sleep(t):await new Promise(e=>setTimeout(e,t))}async function t9(e,t){var n,r,i,o,s,d;let c,u,h,p;if(!e.backend.captureSnapshot)throw new a("UNSUPPORTED_OPERATION","snapshot is not supported by this backend");let f=t.session??"default",w=await e.sessions.get(f),m=await e.backend.captureSnapshot({session:f,requestId:t.requestId,appId:w?.appId,appBundleId:w?.appBundleId,signal:t.signal??e.signal,metadata:t.metadata},{interactiveOnly:t.interactiveOnly,compact:t.compact,depth:t.depth,scope:t.scope,raw:t.raw}),b=(r=m,i=e,n=r.snapshot?r.snapshot:{nodes:r.nodes??[],truncated:r.truncated,backend:r.backend,createdAt:t3(i)},n.presentationKey?n:{...n,presentationKey:l(t)}),g=t3(e);return{snapshot:b,result:m,session:w,warnings:((c=[...(o={result:m,snapshot:b,options:t,session:w,capturedAt:b.createdAt??g,runtimeNow:g}).result.warnings??[]]).push(...function(e){let t=e.result.analysis;if("android"!==e.snapshot.backend||!0!==e.options.interactiveOnly||e.snapshot.nodes.length>0||!t||(t.rawNodeCount??0)<12)return[];let n=[`Interactive snapshot is empty after filtering ${t.rawNodeCount} raw Android nodes. Likely causes: the app content is not accessibility-visible yet, a transient route change, or depth/filter options hid the target.`];return"number"==typeof e.options.depth&&"number"==typeof t.maxDepth&&t.maxDepth>=e.options.depth+2&&n.push(`Interactive output is empty at depth ${e.options.depth}; retry without -d.`),n}(o)),(u=function(e){if(e?.backend!=="uiautomator-dump")return;let t=e.fallbackReason?` Reason: ${e.fallbackReason}`:"";return`Android snapshot helper unavailable; using stock UIAutomator dump, which can time out on busy React Native UIs.${t}`}(o.result.androidSnapshot))&&c.push(u),(h=function(e){if(tz(e).detected)return"Hint: React Native warning/error overlay detected. It overlays part of the app and should be handled before interacting.\nRun: agent-device react-native dismiss-overlay\nThen run: agent-device snapshot -i -c\nUse refs from the new snapshot."}(o.snapshot.nodes))&&c.push(h),(p=function(e){var t,n;let r=e.session?.snapshot,i=!!r&&[e.capturedAt,e.runtimeNow].some(e=>{let t=e-r.createdAt;return t>=0&&t<=2e3});if(!e.result.freshness&&r&&i&&(t=r.nodes.length,n=e.snapshot.nodes.length,!(t<12)&&n<=Math.floor(.2*t)))return ne}(o))&&c.push(p),c.push(...(s=o.result.freshness,d=o.snapshot.backend,s?.staleAfterRetries&&"android"===d?"stuck-route"===s.reason?[`Recent ${s.action} was followed by a nearly identical snapshot after ${s.retryCount} automatic retr${1===s.retryCount?"y":"ies"}. If you expected navigation or submit, the tree may still be stale. Use screenshot as visual truth, wait briefly, then re-snapshot once.`]:"sharp-drop"===s.reason?[ne]:[]:[])),Array.from(new Set(c)))}}function t7(e,t){let n=t.session?.name??e??"default";return{...t.session??{name:n},name:n,snapshot:t.snapshot,appName:t.result.appName??t.session?.appName,appBundleId:t.result.appBundleId??t.session?.appBundleId}}let ne="Recent snapshots dropped sharply in node count, which suggests stale or mid-transition UI. Use screenshot as visual truth, wait briefly, then re-snapshot once.";function nt(e){return["visible","hidden","exists","editable","selected","text"].includes(e)}function nn(e){let{predicate:t,node:n,nodes:r,expectedText:i,platform:a}=e,o=w(n),s=x(n,a),l=!0===n.selected,d="text"===t?g(n):function(e,t,n){var r,i,a;let o;if("android"===n&&!1===e.visibleToUser)return!1;if(nr(e.rect))return function(e,t){return es(e,t)}(e,t);if(e.rect)return!1;if("android"!==n&&!0===e.hittable)return!0;let s=(r=e,i=t,a=n,o=y(i),m(i,r,o,e=>!function(e,t){if("android"===t&&!1===e.visibleToUser)return!1;let n=E(e.type??"");return!(n.includes("application")||n.includes("window")||n.includes("scrollview")||n.includes("tableview")||n.includes("collectionview"))&&"table"!==n&&"list"!==n&&"listview"!==n&&("android"===t?!0===e.hittable&&nr(e.rect):!0===e.hittable||nr(e.rect))}(e,a)?null:e));return!!s&&(nr(s.rect)?function(e,t){return es(e,t)}(s,t):"android"!==n&&!0===s.hittable)}(n,r,a),c=!1;switch(t){case"visible":c=d;break;case"hidden":c=!d;break;case"editable":c=s;break;case"selected":c=l;break;case"text":c=o===(i??"")}let u="text"===t?`expected="${i??""}" actual="${o}"`:`actual=${JSON.stringify({visible:d,editable:s,selected:l})}`;return{pass:c,actualText:o,details:u}}function nr(e){return!!(e&&Number.isFinite(e.x)&&Number.isFinite(e.y)&&Number.isFinite(e.width)&&Number.isFinite(e.height)&&e.width>0&&e.height>0)}async function ni(e,t){let n=t??"default",r=await e.sessions.get(n);if(!r)throw new a("SESSION_NOT_FOUND","No active session. Run open first.");if(!r.snapshot)throw new a("INVALID_ARGS","No snapshot in session. Run snapshot first.");return{sessionName:n,session:r,snapshot:r.snapshot}}async function na(e,t,n={updateSession:!0}){let r=e.backend.captureSnapshot;if(!r)throw new a("UNSUPPORTED_OPERATION","snapshot is not supported by this backend");let i=t.session??"default",o=await e.sessions.get(i),s=await r(t8(e,t),{interactiveOnly:!1,compact:!1,depth:t.depth,scope:n.scope??t.scope,raw:t.raw}),l=s.snapshot??{nodes:s.nodes??[],truncated:s.truncated,backend:s.backend,createdAt:t3(e)};return n.updateSession&&o&&await e.sessions.set({...o,snapshot:l}),{sessionName:i,session:o,snapshot:l}}async function no(e,t,n){if(e.backend.readText){let r=await e.backend.readText(t8(e,{session:t.sessionName}),n);if(r.text.trim())return r.text}return S(n)}let ns=async(e,t)=>{if("ref"===t.target.kind){let n=await ni(e,t.session),r=function(e,t,n){let r=o(t);if(!r)throw new a("INVALID_ARGS",n.invalidRefMessage);let i=d(e,r)??(n.fallbackLabel.length>0?N(e,n.fallbackLabel):null);if(!i)throw new a("COMMAND_FAILED",n.notFoundMessage);return{ref:r,node:i}}(n.snapshot.nodes,t.target.ref,{fallbackLabel:t.target.fallbackLabel??"",invalidRefMessage:"get text requires a ref like @e2",notFoundMessage:`Ref ${t.target.ref} not found`}),i=I(r.node,e.backend.platform,{action:"get"}),s={kind:"ref",ref:`@${r.ref}`};return"attrs"===t.property?{kind:"attrs",target:s,node:r.node,selectorChain:i}:{kind:"text",target:s,text:await no(e,n,r.node),node:r.node,selectorChain:i}}let n=await nx(e,t,t.session??"default",{selector:t.target.selector,disambiguateAmbiguous:"text"===t.property}),r=I(n.node,e.backend.platform,{action:"get"});if("attrs"===t.property)return{kind:"attrs",target:{kind:"selector",selector:n.selector},node:n.node,selectorChain:r};let i=await no(e,n.capture,n.node);return{kind:"text",target:{kind:"selector",selector:n.selector},text:i,node:n.node,selectorChain:r}},nl=async(e,t)=>{let n=await ns(e,{...t,property:"text",target:t.target});if("text"!==n.kind)throw new a("COMMAND_FAILED","getText returned non-text result");return n},nd=async(e,t)=>{let n=await ns(e,{...t,property:"attrs",target:t.target});if("attrs"!==n.kind)throw new a("COMMAND_FAILED","getAttrs returned non-attrs result");return n},nc=async(e,t)=>{if(!nt(t.predicate))throw new a("INVALID_ARGS","is requires predicate: visible|hidden|exists|editable|selected|text");if("text"===t.predicate&&!t.expectedText)throw new a("INVALID_ARGS","is text requires expected text value");let n=await na(e,t,{updateSession:!0}),r=b(t.selector);if("exists"===t.predicate){let i=M(n.snapshot.nodes,r,{platform:e.backend.platform});if(!i)throw new a("COMMAND_FAILED",D(r,[],{unique:!1}));return{predicate:t.predicate,pass:!0,selector:i.selector.raw,matches:i.matches,selectorChain:r.selectors.map(e=>e.raw)}}let i=A(n.snapshot.nodes,r,{platform:e.backend.platform,requireRect:!1,requireUnique:!0,disambiguateAmbiguous:!1});if(!i)throw new a("COMMAND_FAILED",D(r,[],{unique:!0}),{command:"is",reason:"selector_not_found",predicate:t.predicate,selector:r.raw});let o=nn({predicate:t.predicate,node:i.node,nodes:n.snapshot.nodes,expectedText:t.expectedText,platform:e.backend.platform});if(!o.pass)throw new a("COMMAND_FAILED",`is ${t.predicate} failed for selector ${i.selector.raw}: ${o.details}`,{command:"is",reason:"predicate_failed",predicate:t.predicate,selector:i.selector.raw,predicateDetails:o.details});return{predicate:t.predicate,pass:!0,selector:i.selector.raw,..."text"===t.predicate?{text:o.actualText}:{},selectorChain:r.selectors.map(e=>e.raw)}},nu=async(e,t)=>await nc(e,{...t,predicate:"visible",selector:t.target.selector}),nh=async(e,t)=>await nc(e,{...t,predicate:"hidden",selector:t.target.selector}),np=async(e,t)=>{if("sleep"===t.target.kind)return await t6(e,t.target.durationMs),{kind:"sleep",waitedMs:t.target.durationMs};if("ref"===t.target.kind){let n=await ni(e,t.session),r=o(t.target.ref);if(!r)throw new a("INVALID_ARGS",`Invalid ref: ${t.target.ref}`);let i=d(n.snapshot.nodes,r),s=i?R(i,n.snapshot.nodes):void 0;if(!s)throw new a("COMMAND_FAILED",`Ref ${t.target.ref} not found or has no label`);return await ng(e,t,s,t.target.timeoutMs)}if("selector"===t.target.kind)return await nb(e,t,t.target.selector,t.target.timeoutMs);if(!t.target.text)throw new a("INVALID_ARGS","wait requires text");return await ng(e,t,t.target.text,t.target.timeoutMs)},nf=async(e,t)=>{let n=await np(e,{...t,target:{kind:"text",text:t.text,timeoutMs:t.timeoutMs}});if("text"!==n.kind)throw new a("COMMAND_FAILED","waitForText returned non-text result");return n};async function nw(e,t,n){let r=t.timeoutMs??1e4,i=t3(e);for(;t3(e)-i<r;){let{match:r}=await nm(e,t,n);if(r)return{kind:"found",found:!0,waitedMs:t3(e)-i};await t6(e,300)}throw new a("COMMAND_FAILED","find wait timed out")}async function nm(e,t,n){let r=await na(e,t,{updateSession:!0,scope:"text"===n||"label"===n||"any"===n?t.query:void 0}),i=_(r.snapshot.nodes,n,t.query,{requireRect:!1}).matches[0];return{capture:r,match:i}}async function nb(e,t,n,r){let i=r??1e4,o=t3(e),s=b(n);for(;t3(e)-o<i;){let n=M((await na(e,t,{updateSession:!0})).snapshot.nodes,s,{platform:e.backend.platform});if(n)return{kind:"selector",selector:n.selector.raw,waitedMs:t3(e)-o};await t6(e,300)}throw new a("COMMAND_FAILED",`wait timed out for selector: ${n}`)}async function ng(e,t,n,r){let i=r??1e4,o=t3(e);for(;t3(e)-o<i;){if(e.backend.findText?(await e.backend.findText(t8(e,t),n)).found:await ny(e,t,n))return{kind:"text",text:n,waitedMs:t3(e)-o};await t6(e,300)}throw new a("COMMAND_FAILED",`wait timed out for text: ${n}`)}async function ny(e,t,n){return!!N((await na(e,t,{updateSession:!0})).snapshot.nodes,n)}async function nx(e,t,n,r){let i=await na(e,{...t,session:n},{updateSession:!0}),o=b(r.selector),s=A(i.snapshot.nodes,o,{platform:e.backend.platform,requireRect:!1,requireUnique:!0,disambiguateAmbiguous:r.disambiguateAmbiguous});if(!s)throw new a("COMMAND_FAILED",D(o,[],{unique:!0}));return{capture:i,node:s.node,selector:s.selector.raw,ref:`@${s.node.ref}`}}function nv(e){let t=nk(e);if(!t)return null;let n=s(t);return Number.isFinite(n.x)&&Number.isFinite(n.y)?n:null}function nk(e){if(!e)return null;let t=Number(e.x),n=Number(e.y),r=Number(e.width),i=Number(e.height);return Number.isFinite(t)&&Number.isFinite(n)&&Number.isFinite(r)&&Number.isFinite(i)&&!(r<0)&&!(i<0)?{x:t,y:n,width:r,height:i}:null}function nI(e){return{x:nM(e.x,e.width),y:nM(e.y,e.height)}}function nM(e,t){if(t<=1)return Math.floor(e);let n=Math.ceil(e);return Math.round(Math.min(Math.floor(e+t-1),Math.max(n,e+t/2)))}let nA=["button","link","menuitem","tabitem","textfield","searchfield","securetextfield","checkbox","radio","switch","cell"];function nN(e,t){return nR(e,t).node}function nR(e,t){var n;let r=function(e,t){let n=nk(t.rect);if(!n)return null;let r=t,i=new Set;for(;!i.has(r.ref);){i.add(r.ref);let t=e.filter(e=>{if(e.parentIndex!==r.index||!e.hittable)return!1;let t=nk(e.rect);return!!t&&nS(t,n)});if(1!==t.length)break;let a=t[0];if(void 0===a)break;r=a}return r===t?null:r}(e,t);if(r?.rect&&nv(r.rect))return{node:r,reason:"same-rect-descendant"};if([(n=t).type,n.role,n.subrole].map(e=>E(e??"")).some(nP)&&t.rect&&nv(t.rect))return{node:t,reason:"semantic-target"};let i=k(e,t);return i?.rect&&nv(i.rect)?!function(e,t,n){var r,i,a,o;let l,d,c,u=nk(e.rect),h=nk(t.rect);if(!u||!h)return!1;let p=function(e,t){let n=s(t),r=e.filter(e=>{let t=(e.type??"").toLowerCase();return t.includes("application")||t.includes("window")}).map(e=>nk(e.rect)).filter(e=>null!==e);if(0===r.length)return null;let i=r.filter(e=>W(e,n.x,n.y));return J(i.length>0?i:r)}(n,u);return!!p&&(r=h,i=p,l=(a=r,o=i,Math.max(0,Math.min(a.x+a.width,o.x+o.width)-Math.max(a.x,o.x))*Math.max(0,Math.min(a.y+a.height,o.y+o.height)-Math.max(a.y,o.y))),d=r.width*r.height,c=i.width*i.height,!(l<=0)&&!(d<=0)&&!(c<=0)&&!!(l/c>=.9)&&!!(l/d>=.8))&&!nS(u,h)}(t,i,e)?{node:i,reason:"hittable-ancestor"}:{node:t,reason:"overly-broad-ancestor"}:{node:t,reason:"original"}}function nP(e){return"tab"===e||nA.some(t=>e.includes(t))}function nS(e,t){return .5>=Math.abs(e.x-t.x)&&.5>=Math.abs(e.y-t.y)&&.5>=Math.abs(e.width-t.width)&&.5>=Math.abs(e.height-t.height)}async function nD(e,t,n){if(await nO(e,t,n.action),"point"===t.target.kind)return{kind:"point",point:{x:t.target.x,y:t.target.y}};if("ref"===t.target.kind){let r=await nT(e,t,t.target),i=r.resolved,s=n.promoteToHittableAncestor?nN(r.snapshot.nodes,i.node):i.node;return function(e,t,n,r){let i=e.rect?el(e,t):null;if(!(!e.rect||!i||es(e,t)))throw new a("COMMAND_FAILED",`Ref ${n} is off-screen and not safe to ${r}`,{reason:"offscreen_ref",ref:o(n),rect:e.rect,viewport:i,hint:`Use scroll with the direction from the off-screen summary, take a fresh snapshot, then retry ${r} with the new ref or a selector.`})}(s,r.snapshot.nodes,t.target.ref,n.action),{kind:"ref",point:nC(s,`Ref ${t.target.ref} not found or has invalid bounds`),target:{kind:"ref",ref:`@${i.ref}`},node:s,selectorChain:I(s,e.backend.platform,{action:"fill"===n.action?"fill":"click"}),refLabel:R(s,r.snapshot.nodes)}}let r=await nE(e,t,n.requireInteractive),i=b(t.target.selector),s=A(r.snapshot.nodes,i,{platform:e.backend.platform,requireRect:!0,requireUnique:!0,disambiguateAmbiguous:!0});if(!s||!s.node.rect)throw new a("COMMAND_FAILED",D(i,s?.diagnostics??[],{unique:!0}));let l=n.promoteToHittableAncestor?nN(r.snapshot.nodes,s.node):s.node;return{kind:"selector",point:nC(l,`Selector ${s.selector.raw} resolved to invalid bounds`),target:{kind:"selector",selector:s.selector.raw},node:l,selectorChain:I(l,e.backend.platform,{action:"fill"===n.action?"fill":"click"}),refLabel:R(l,r.snapshot.nodes)}}async function nE(e,t,n){if(!e.backend.captureSnapshot)throw new a("UNSUPPORTED_OPERATION","snapshot is not supported by this backend");let r=t.session??"default",i=await e.sessions.get(r);if(!i)throw new a("SESSION_NOT_FOUND","No active session. Run open first.");let o=await e.backend.captureSnapshot(t8(e,t),{interactiveOnly:n,compact:n}),s=o.snapshot??{nodes:o.nodes??[],truncated:o.truncated,backend:o.backend,createdAt:t3(e)};return await e.sessions.set({...i,snapshot:s}),{snapshot:s}}async function nO(e,t,n){if("macos"!==e.backend.platform)return;let r=await n_(e,t);if(("desktop"===r||"menubar"===r)&&("menubar"!==r||"click"!==n&&"press"!==n))throw new a("UNSUPPORTED_OPERATION",`${n} is not supported on macOS ${r} sessions yet. Open an app session to act, or use the ${r} surface to inspect.`)}async function n_(e,t){let n=await e.sessions.get(t.session??"default");return n?.metadata?.surface}async function nT(e,t,n){let r=t.session??"default",i=await e.sessions.get(r);if(!i)throw new a("SESSION_NOT_FOUND","No active session. Run open first.");if(!i.snapshot)throw new a("INVALID_ARGS","No snapshot in session. Run snapshot first.");let o=n.fallbackLabel??"",s=n$(i.snapshot.nodes,n.ref,{fallbackLabel:o,requireRect:!0});if(s)return{snapshot:i.snapshot,resolved:s};let l=await nE(e,t,!0),d=n$(l.snapshot.nodes,n.ref,{fallbackLabel:o,requireRect:!0});if(!d)throw new a("COMMAND_FAILED",`Ref ${n.ref} not found or has no bounds`);return{...l,resolved:d}}function n$(e,t,n){let r=o(t);if(!r)throw new a("INVALID_ARGS",`Invalid ref: ${t}`);let i=d(e,r);if(nL(i,n.requireRect))return{ref:r,node:i};let s=n.fallbackLabel.length>0?N(e,n.fallbackLabel):null;return nL(s,n.requireRect)?{ref:r,node:s}:null}function nC(e,t){if(!e.rect)throw new a("COMMAND_FAILED",t);let n=s(e.rect);if(!Number.isFinite(n.x)||!Number.isFinite(n.y))throw new a("COMMAND_FAILED",t);return n}function nL(e,t){if(!e)return!1;if(!t)return!0;if(!e.rect)return!1;let{x:n,y:r,width:i,height:a}=e.rect;if(!Number.isFinite(Number(n))||!Number.isFinite(Number(r))||!Number.isFinite(Number(i))||!Number.isFinite(Number(a))||0>Number(i)||0>Number(a))return!1;let o=s(e.rect);return Number.isFinite(o.x)&&Number.isFinite(o.y)}async function nU(e,t,n){let r;if(t.from||t.to||t.direction||void 0!==t.distance)throw new a("INVALID_ARGS","gesture swipe preset cannot be combined with from, to, direction, or distance");let i=q(t.preset);await nO(e,t,"swipe");let o=F(i,{referenceWidth:(r=nV((await nE(e,t,!1)).snapshot.nodes)).width,referenceHeight:r.height},{platform:e.backend.platform}),s=void 0===t.durationMs?void 0:ev(t.durationMs,"durationMs",16,1e4),l={x:o.x1,y:o.y1},d={x:o.x2,y:o.y2},c=nz(await n(t8(e,t),l,d,{durationMs:s}));return{kind:"swipe",from:l,to:d,preset:i,...void 0!==s?{durationMs:s}:{},fromTarget:{kind:"viewport"},...c?{backendResult:c}:{},...u(`Swiped ${i}`)}}async function nB(e,t){let n=t.target??{kind:"viewport"};return"viewport"===n.kind?(await nO(e,t,"scroll"),{kind:"viewport"}):await nD(e,{...t,target:n},{action:"scroll",requireInteractive:!1,promoteToHittableAncestor:!1})}async function nG(e,t){if(t.from){var n;if("x"in(n=t.from)&&"y"in n)return await nO(e,t,"swipe"),{point:nX(t.from,"from")};let r=await nD(e,{...t,target:t.from},{action:"swipe",requireInteractive:!1,promoteToHittableAncestor:!1});return{point:r.point,target:r}}if(!t.direction)throw new a("INVALID_ARGS","swipe requires from+to or a direction");return await nO(e,t,"swipe"),{point:s(nV((await nE(e,t,!1)).snapshot.nodes)),target:{kind:"viewport"}}}async function nF(e,t,n,r,i){if(!e.backend.captureSnapshot)throw new a("UNSUPPORTED_OPERATION",`scroll ${n} requires snapshot support to verify hidden content before scrolling`);let{captureSnapshot:o}=e.backend;return await eh({edge:n,target:r,scope:i,captureNodes:async n=>{let r=await o(t8(e,t),{compact:!0,scope:n});return r.snapshot?.nodes??r.nodes??[]}})}function nq(e,t){switch(e){case"up":case"down":case"left":case"right":return e;default:throw new a("INVALID_ARGS",`${t} must be up, down, left, or right`)}}function nX(e,t){let n=Number(e.x),r=Number(e.y);if(!Number.isFinite(n)||!Number.isFinite(r))throw new a("INVALID_ARGS",`${t} point requires finite x and y`);return{x:n,y:r}}function nY(e,t){if(!Number.isFinite(e)||e<=0)throw new a("INVALID_ARGS",`${t} must be a positive number`);return e}function nV(e){let t=e.filter(t=>es(t,e)).map(e=>e.rect).filter(nH),n=t.length>0?t:e.map(e=>e.rect).filter(nH);if(0===n.length)throw new a("COMMAND_FAILED","Cannot infer viewport for directional swipe");let r=Math.min(...n.map(e=>e.x)),i=Math.min(...n.map(e=>e.y));return{x:r,y:i,width:Math.max(...n.map(e=>e.x+e.width))-r,height:Math.max(...n.map(e=>e.y+e.height))-i}}function nH(e){return!!(e&&e.width>0&&e.height>0)}function nz(e){return e&&"object"==typeof e?e:void 0}async function nK(e,t,n){let r=await nD(e,t,{action:n,requireInteractive:!0,promoteToHittableAncestor:!0});if(!e.backend.tap)throw new a("UNSUPPORTED_OPERATION","tap is not supported by this backend");let i=nj(await e.backend.tap(t8(e,t),r.point,{button:t.button,count:t.count,intervalMs:t.intervalMs,holdMs:t.holdMs,jitterPx:t.jitterPx,doubleTap:t.doubleTap}));return{...r,...i?{backendResult:i}:{}}}function nj(e){return e&&"object"==typeof e?e:void 0}function nW(e,t){if(void 0!==e)return nJ(e,t)}function nJ(e,t){let n=e?.trim();if(!n)throw new a("INVALID_ARGS",`${t} must be a non-empty string`);return n}function nZ(e){return e&&"object"==typeof e?e:void 0}let nQ=/^[A-Za-z0-9_.:-]{1,64}$/;async function n0(e,t){if(!t||"object"!=typeof t)throw new a("INVALID_ARGS","apps.push requires an input");if("json"===t.kind)return n1(t.payload,"apps.push JSON payload",8192),{backendInput:{kind:"json",payload:t.payload},inputKind:"json"};let n=await eq(e,t,{usage:"apps.push",field:"input"});return{backendInput:{kind:"file",path:n.path},inputKind:"file",...n.cleanup?{cleanup:n.cleanup}:{}}}function n1(e,t,n){if(function(e,t){if(!e||"object"!=typeof e||Array.isArray(e))throw new a("INVALID_ARGS",`${t} must be a JSON object`)}(e,t),Buffer.byteLength(function(e,t){try{let n=JSON.stringify(e);if("string"!=typeof n)throw new a("INVALID_ARGS",`${t} must be JSON-serializable`);return n}catch{throw new a("INVALID_ARGS",`${t} must be JSON-serializable`)}}(e,t),"utf8")>n)throw new a("INVALID_ARGS",`${t} exceeds ${n} bytes`)}function n2(e,t){return{session:t.session,requestId:t.requestId,signal:t.signal??e.signal,metadata:t.metadata}}function n5(e){return e&&"object"==typeof e?e:void 0}async function n4(e,t,n){let r="reinstall"===n?"reinstallApp":"installApp",i=e.backend[r];if(!i)throw new a("UNSUPPORTED_OPERATION",`admin.${n} is not supported by this backend`);let o="app"in t&&void 0!==t.app?nJ(t.app,"app"):void 0;if("installFromSource"!==n&&!o)throw new a("INVALID_ARGS",`admin.${n} requires app`);let s=t8(e,t),l=await n8(e,s,t.source);try{var d,c,h,p,f;let t,r,a,w,m,b,g,y,x=await i.call(e.backend,s,{...o?{app:o}:{},source:l.source});return d=n,c=o,h=l.source,p=x,t=n7(p),r=n9(p,"appName"),a=n9(p,"appId"),w=n9(p,"bundleId"),m=n9(p,"packageName"),b=n9(p,"launchTarget"),g=n9(p,"installablePath"),y=n9(p,"archivePath"),{kind:"reinstall"===d?"appReinstalled":"installFromSource"===d?"appInstalledFromSource":"appInstalled",...c?{app:c}:{},source:h,...a?{appId:a}:{},...r?{appName:r}:{},...w?{bundleId:w}:{},...m?{packageName:m}:{},...b?{launchTarget:b}:{},...g?{installablePath:g}:{},...y?{archivePath:y}:{},...t?{backendResult:t}:{},...u(`${"reinstall"===d?"Reinstalled":"Installed"}: ${r??b??c??(f=h,"path"===f.kind?f.path:"uploadedArtifact"===f.kind?f.id:f.url)}`)}}finally{await l.cleanup?.()}}async function n8(e,t,n){let r=n6(n),i=await n3(e,r);try{let n=e.backend.resolveInstallSource?await e.backend.resolveInstallSource(t,i.source):i.source;return{source:n6(n),...i.cleanup?{cleanup:i.cleanup}:{}}}catch(e){if(i.cleanup)try{await i.cleanup()}catch{}throw e}}async function n3(e,t){if("url"===t.kind)return{source:t};let n=await eq(e,t,{usage:"admin.install",field:"source"});return{source:{kind:"path",path:n.path},...n.cleanup?{cleanup:n.cleanup}:{}}}function n6(e){if(!e||"object"!=typeof e)throw new a("INVALID_ARGS","install source is required");if("path"===e.kind)return{kind:"path",path:nJ(e.path,"source.path")};if("uploadedArtifact"===e.kind)return{kind:"uploadedArtifact",id:nJ(e.id,"source.id")};if("url"===e.kind){let t=nJ(e.url,"source.url");return function(e){let t;try{t=new URL(e)}catch{throw new a("INVALID_ARGS",`Invalid install source URL: ${e}`)}if("http:"!==t.protocol&&"https:"!==t.protocol)throw new a("INVALID_ARGS","Install source URL must use http or https")}(t),{kind:"url",url:t}}throw new a("INVALID_ARGS","install source kind must be path, uploadedArtifact, or url")}function n9(e,t){let n=e[t];return"string"==typeof n&&n.length>0?n:void 0}function n7(e){return e&&"object"==typeof e?e:void 0}function re(e,t){if("start"===e||"stop"===e)return e;throw new a("INVALID_ARGS",`${t} action must be start or stop`)}function rt(e,t,n,r){return{kind:"start"===e?r.startKind:r.stopKind,action:e,...n?{artifact:n}:{},backendResult:t,...u("start"===e?r.startMessage:r.stopMessage)}}let rn=/(?:authorization|cookie|token|secret|password|passwd|api[-_]?key)/i,rr=/\b[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}\b/g;function ri(e){var t,n;let r=!1,i=e;return{value:(t=i=(i=(i=(i=(i=i.replaceAll(/(authorization|token|secret|password|passwd|api[-_]?key)=([^&\s]+)/gi,(e,t)=>(r=!0,`${String(t)}=[REDACTED]`))).replaceAll(/("(?:authorization|cookie|token|secret|password|passwd|api[-_]?key)"\s*:\s*")([^"]*)(")/gi,(e,t,n,i)=>(r=!0,`${String(t)}[REDACTED]${String(i)}`))).replaceAll(/\b(Bearer\s+)([^\s",;]+)/gi,(e,t)=>(r=!0,`${String(t)}[REDACTED]`))).replaceAll(/((?:authorization|cookie|token|secret|password|passwd|api[-_]?key)\s*[:=]\s*)([^\s,;&]+)/gi,(e,t)=>(r=!0,`${String(t)}[REDACTED]`))).replaceAll(rr,()=>(r=!0,"[REDACTED]")),n=()=>{r=!0},i=/(https?:\/\/|token|secret|password|authorization|cookie|api[-_]?key)/i.test(t)?t.replaceAll(/https?:\/\/[^\s"'<>)]+/gi,e=>{let t=ra(e);return t?(t.redacted&&n(),t.value):e}):t),redacted:r}}function ra(e){try{let t=new URL(e),n=function(e){let t=!1;for(let n of Array.from(e.searchParams.keys()))rn.test(n)&&(e.searchParams.set(n,"[REDACTED]"),t=!0);return t}(t);return(t.username||t.password)&&(t.username="REDACTED",t.password="REDACTED",n=!0),{value:t.toString(),redacted:n}}catch{return}}let ro=/(?:authorization|cookie|token|secret|password|passwd|api[-_]?key)/i;function rs(e){if(!e)return{redacted:!1};let t=!1,n={};for(let[r,i]of Object.entries(e))if(ro.test(r))n[r]="[REDACTED]",t=!0;else{let e=ru(i,2048);n[r]=e.value??"",t||=e.redacted}return{value:n,redacted:t}}function rl(e){return void 0===e?{redacted:!1}:function(e){try{let t=JSON.parse(e),n=rc(t,ri);return rh(JSON.stringify(n.value),2048,n.redacted)}catch{return}}(e)??ru(e,2048)}function rd(e){return rc(e,e=>ru(e,2048))}function rc(e,t){if(void 0===e)return{redacted:!1};if("string"==typeof e)return t(e);if(!e||"object"!=typeof e)return{value:e,redacted:!1};if(Array.isArray(e)){let n=!1;return{value:e.map(e=>{let r=rc(e,t);return n||=r.redacted,r.value}),redacted:n}}let n=!1,r={};for(let[i,a]of Object.entries(e)){if(ro.test(i)){r[i]="[REDACTED]",n=!0;continue}let e=rc(a,t);r[i]=e.value,n||=e.redacted}return{value:r,redacted:n}}function ru(e,t){if(void 0===e)return{redacted:!1};let n=ri(e);return rh(n.value,t,n.redacted)}function rh(e,t,n){if(void 0===e)return{redacted:n};let r=e;return r.length>t&&(r=`${r.slice(0,t)}...[truncated]`,n=!0),{value:r,redacted:n}}async function rp(e,t){let n=t8(e,t),r=t.session?await e.sessions.get(t.session):void 0;return{...n,...t.appId??r?.appId?{appId:t.appId??r?.appId}:{},...t.appBundleId??r?.appBundleId?{appBundleId:t.appBundleId??r?.appBundleId}:{}}}function rf(e,t,n,r){return{...rw(e),...void 0!==e.cursor?{cursor:nJ(e.cursor,"cursor")}:{},limit:void 0===e.limit?t:ev(e.limit,r,1,n)}}function rw(e){return{...void 0!==e.since?{since:nJ(e.since,"since")}:{},...void 0!==e.until?{until:nJ(e.until,"until")}:{}}}function rm(e,t,n=50){if(!Array.isArray(e))throw new a("INVALID_ARGS",`${t} must be an array of strings`);if(e.length>n)throw new a("INVALID_ARGS",`${t} must contain at most ${n} entries`);return e.map((e,n)=>nJ(e,`${t}[${n}]`))}let rb=eV,rg=async(e,t)=>{let n,r,i;if(!t.baseline)throw new a("INVALID_ARGS","diff screenshot requires a baseline image");let o=function(e){if(null==e||""===e)return .1;let t=Number(e);if(Number.isNaN(t)||t<0||t>1)throw new a("INVALID_ARGS","--threshold must be a number between 0 and 1");return t}(t.threshold),s=t.current??{kind:"live"};if(t.overlayRefs&&!t$(s))throw new a("INVALID_ARGS","diff screenshot <current.png> cannot use --overlay-refs because saved-image comparisons have no live accessibility refs");let l=await eq(e,t.baseline,{usage:"diff screenshot baseline",field:"baseline"}),d=[];try{let a;a=t$(s)?(r=await tS(e,t)).path:(n=await eq(e,s,{usage:"diff screenshot current",field:"current"})).path,i=t.out?await eX(e,t.out,{field:"diffPath",ext:".png"}):void 0;let c=await tv(l.path,a,{threshold:o,outputPath:i?.path,maxPixels:e.policy.maxImagePixels});t$(s)&&(c=await tD(e,t,i?.path,c,d));let u=c.diffPath?await i?.publish():void 0;return u&&d.push(u),c.diffPath||await i?.cleanup?.(),{...c,...d.length>0?{artifacts:d}:{}}}catch(e){throw await i?.cleanup?.(),e}finally{await l.cleanup?.(),await n?.cleanup?.(),await r?.cleanup?.()}},ry=async(e,t)=>{var n;let r,i,a=await t9(e,t),o=function(e){var t,n,r,i,a;let{previous:o,current:s,options:l,identity:d}=e;if(!0===l.forceFull||!0===l.raw||!o||!1===o.comparisonSafe||!1===s.comparisonSafe||(t=o,n=s,r=d,t.backend&&n.backend&&t.backend!==n.backend||r?.previousAppBundleId&&r.currentAppBundleId&&r.previousAppBundleId!==r.currentAppBundleId)||!o.presentationKey||o.presentationKey!==s.presentationKey||(i=o,a=s,i.truncated!==a.truncated||JSON.stringify(t4(i.nodes))!==JSON.stringify(t4(a.nodes))))return;let c=l.scope?.trim();return{ageMs:Math.max(0,s.createdAt-o.createdAt),nodeCount:s.nodes.length,...!0===l.interactiveOnly?{interactiveOnly:!0}:{},...c?{scope:c}:{}}}({previous:a.session?.snapshot,current:a.snapshot,options:t,identity:{previousAppBundleId:a.session?.appBundleId,currentAppBundleId:a.result.appBundleId??a.session?.appBundleId}});return await e.sessions.set(t7(t.session,a)),{nodes:a.snapshot.nodes,truncated:a.snapshot.truncated??!1,visibility:function(e){var t;let{nodes:n,backend:r,snapshotRaw:i}=e;if(i||"macos-helper"===(t=r)||"linux-atspi"===t)return{partial:!1,visibleNodeCount:n.length,totalNodeCount:n.length,reasons:[]};let a=ei(n),o=new Set;return a.hiddenCount>0&&o.add("offscreen-nodes"),a.nodes.some(e=>e.hiddenContentAbove)&&o.add("scroll-hidden-above"),a.nodes.some(e=>e.hiddenContentBelow)&&o.add("scroll-hidden-below"),{partial:o.size>0,visibleNodeCount:a.nodes.length,totalNodeCount:n.length,reasons:[...o]}}({nodes:a.snapshot.nodes,backend:a.snapshot.backend,snapshotRaw:t.raw}),...a.result.androidSnapshot?{androidSnapshot:a.result.androidSnapshot}:{},...a.warnings.length>0?{warnings:a.warnings}:{},...o?{unchanged:o}:{},...(r=(n=a).result.appName??n.session?.appName,i=n.result.appBundleId??n.session?.appBundleId,{...r||i?{appName:r??i}:{},...i?{appBundleId:i}:{}})}},rx=async(e,t)=>{let n=await t9(e,t),r=!0===t.interactiveOnly,i=n.session?.snapshot,a=t7(t.session,n);if(!i){let t=function(e,t={}){return tV(e,t).length}(n.snapshot.nodes,{flatten:r});return await e.sessions.set(a),{mode:"snapshot",baselineInitialized:!0,summary:{additions:0,removals:0,unchanged:t},lines:[],...n.warnings.length>0?{warnings:n.warnings}:{}}}let o=function(e,t,n={}){let r=function(e,t){let n=e.length,r=t.length,i=n+r,a=new Map,o=[];a.set(1,0);for(let s=0;s<=i;s+=1){o.push(new Map(a));for(let i=-s;i<=s;i+=2){let l=i===-s||i!==s&&tH(a,i-1)<tH(a,i+1)?tH(a,i+1):tH(a,i-1)+1,d=l-i;for(;l<n&&d<r&&e[l].comparable===t[d].comparable;)l+=1,d+=1;if(a.set(i,l),l>=n&&d>=r)return function(e,t,n,r,i){let a=[],o=r,s=i;for(let r=e.length-1;r>=0;r-=1){let i=e[r],l=o-s,d=l===-r||l!==r&&tH(i,l-1)<tH(i,l+1)?l+1:l-1,c=tH(i,d),u=c-d;for(;o>c&&s>u;)a.push({kind:"unchanged",text:n[s-1].text}),o-=1,s-=1;if(0===r)break;o===c?(a.push({kind:"added",text:n[u].text}),s=u):(a.push({kind:"removed",text:t[c].text}),o=c)}return a.reverse(),a}(o,e,t,n,r)}}return[]}(tV(e,n),tV(t,n)),i={additions:0,removals:0,unchanged:0};for(let e of r)"added"===e.kind&&(i.additions+=1),"removed"===e.kind&&(i.removals+=1),"unchanged"===e.kind&&(i.unchanged+=1);return{summary:i,lines:r}}(i.nodes,n.snapshot.nodes,{flatten:r});return await e.sessions.set(a),{mode:"snapshot",baselineInitialized:!1,summary:o.summary,lines:o.lines,...n.warnings.length>0?{warnings:n.warnings}:{}}},rv=async(e,t)=>{let n=t.locator??"any";if(!t.query)throw new a("INVALID_ARGS","find requires a value");if("wait"===t.action)return await nw(e,t,n);let{capture:r,match:i}=await nm(e,t,n);if(!i)throw new a("COMMAND_FAILED","find did not match any element");if("exists"===t.action)return{kind:"found",found:!0};let o=`@${i.ref}`;return"get_attrs"===t.action?{kind:"attrs",ref:o,node:i}:{kind:"text",ref:o,text:await no(e,r,i),node:i}},rk=ns,rI=nl,rM=nd,rA=nc,rN=nu,rR=nh,rP=np,rS=nf,rD=async(e,t)=>await nK(e,t,"click"),rE=async(e,t)=>await nK(e,t,"press"),rO=async(e,t)=>{var n;if(!t.text)throw new a("INVALID_ARGS","fill requires text");let r=await nD(e,t,{action:"fill",requireInteractive:!0,promoteToHittableAncestor:!1});if(!e.backend.fill)throw new a("UNSUPPORTED_OPERATION","fill is not supported by this backend");let i=nj(await e.backend.fill(t8(e,t),r.point,t.text,{delayMs:t.delayMs})),o="node"in r?r.node.type??"":"",s=o&&!O(o,e.backend.platform)?`fill target ${n=r,n.target?.kind==="ref"?n.target.ref:n.target?.kind==="selector"?n.target.selector:"point"} resolved to "${o}", attempting fill anyway.`:void 0;return{...r,text:t.text,...s?{warning:s}:{},...i?{backendResult:i}:{}}},r_=async(e,t)=>{let n=t.text;if(!n)throw new a("INVALID_ARGS","type requires text");let r=U(n);if(r)throw new a("INVALID_ARGS",`type does not accept a target ref like "${r}"`,{hint:`Use fill ${r} "text" to target that field, or press ${r} then type "text" to append.`});if(!e.backend.typeText)throw new a("UNSUPPORTED_OPERATION","type is not supported by this backend");let i=ev(t.delayMs??0,"delay-ms",0,1e4),o=nj(await e.backend.typeText(t8(e,t),n,{delayMs:i}));return{kind:"text",text:n,delayMs:i,...o?{backendResult:o}:{},...u(`Typed ${Array.from(n).length} chars`)}},rT=async(e,t)=>{let n=await nD(e,t,{action:"focus",requireInteractive:!0,promoteToHittableAncestor:!1});if(!e.backend.focus)throw new a("UNSUPPORTED_OPERATION","focus is not supported by this backend");let r=nz(await e.backend.focus(t8(e,t),n.point));return{...n,...r?{backendResult:r}:{},...u(`Focused (${n.point.x}, ${n.point.y})`)}},r$=async(e,t)=>{let n=await nD(e,t,{action:"longPress",requireInteractive:!0,promoteToHittableAncestor:!0});if(!e.backend.longPress)throw new a("UNSUPPORTED_OPERATION","longPress is not supported by this backend");let r=void 0===t.durationMs?void 0:ev(t.durationMs,"durationMs",0,12e4),i=nz(await e.backend.longPress(t8(e,t),n.point,{durationMs:r}));return{...n,...void 0!==r?{durationMs:r}:{},...i?{backendResult:i}:{},...u(`Long pressed (${n.point.x}, ${n.point.y})`)}},rC=async(e,t)=>{if(!e.backend.swipe)throw new a("UNSUPPORTED_OPERATION","swipe is not supported by this backend");if(t.preset)return await nU(e,t,e.backend.swipe);let n=await nG(e,t),r=function(e,t){if(t.to)return{point:nX(t.to,"to")};let n=nq(t.direction,"swipe direction"),r=nY(t.distance??200,"swipe distance");switch(n){case"up":return{point:{x:e.x,y:e.y-r},direction:n,distance:r};case"down":return{point:{x:e.x,y:e.y+r},direction:n,distance:r};case"left":return{point:{x:e.x-r,y:e.y},direction:n,distance:r};case"right":return{point:{x:e.x+r,y:e.y},direction:n,distance:r}}}(n.point,t),i=void 0===t.durationMs?void 0:ev(t.durationMs,"durationMs",16,1e4),o=nz(await e.backend.swipe(t8(e,t),n.point,r.point,{durationMs:i}));return{kind:"swipe",from:n.point,to:r.point,...r.direction?{direction:r.direction}:{},...void 0!==r.distance?{distance:r.distance}:{},...void 0!==i?{durationMs:i}:{},...n.target?{fromTarget:n.target}:{},...o?{backendResult:o}:{},...u("Swiped")}},rL=async(e,t)=>{var n,r,i,o;let s;if(!e.backend.scroll)throw new a("UNSUPPORTED_OPERATION","scroll is not supported by this backend");let l="bottom"===(n=t.direction)?{direction:"down",edge:"bottom"}:"top"===n?{direction:"up",edge:"top"}:{direction:nq(n,"scroll direction")},d=(r=t.amount,i="scroll amount",void 0===r?void 0:nY(r,i)),c=function(e,t){if(void 0!==e){if(!Number.isFinite(e)||!Number.isInteger(e)||e<=0)throw new a("INVALID_ARGS",`${t} must be a positive integer`);return e}}(t.pixels,"scroll pixels");if(void 0!==d&&void 0!==c)throw new a("INVALID_ARGS","scroll accepts either amount or pixels, not both");let h=await nB(e,t),p="viewport"===h.kind?{kind:"viewport"}:{kind:"point",point:h.point},f=e.backend.scroll,w=async()=>await f(t8(e,t),p,{direction:l.direction,...void 0!==d?{amount:d}:{},...void 0!==c?{pixels:c}:{}}),m=0;if(l.edge){let n=l.edge,r="viewport"===(o=h).kind?{}:{point:o.point,nodeIndex:"node"in o?o.node.index:void 0},i=await ep({edge:n,captureState:async i=>await nF(e,t,n,r,i),scroll:w});s=i.result,m=i.passes}else s=await w(),m=1;let b=nz(s);return{...h,direction:l.direction,...l.edge?{edge:l.edge,passes:m}:{},...void 0!==d?{amount:d}:{},...void 0!==c?{pixels:c}:{},...b?{backendResult:b}:{},...u(ef(l.direction,l.edge,m,d,c))}},rU=async(e,t)=>{if(!e.backend.pinch)throw new a("UNSUPPORTED_OPERATION","pinch is not supported by this backend");await nO(e,t,"pinch");let n=nY(t.scale,"pinch scale"),r=t.center?await nD(e,{...t,target:t.center},{action:"pinch",requireInteractive:!1,promoteToHittableAncestor:!1}):void 0,i=nz(await e.backend.pinch(t8(e,t),{scale:n,...r?{center:r.point}:{}}));return{kind:"pinch",scale:n,...r?{center:r.point,centerTarget:r}:{},...i?{backendResult:i}:{},...u(`Pinched to scale ${n}`)}},rB=async(e,t={})=>{if(!e.backend.pressBack)throw new a("UNSUPPORTED_OPERATION","system.back is not supported by this backend");let n=t.mode??"in-app";if("in-app"!==n&&"system"!==n)throw new a("INVALID_ARGS","system.back mode must be in-app or system");let r=nZ(await e.backend.pressBack(t8(e,t),{mode:n}));return{kind:"systemBack",mode:n,...r?{backendResult:r}:{},...u("Back")}},rG=async(e,t={})=>{if(!e.backend.pressHome)throw new a("UNSUPPORTED_OPERATION","system.home is not supported by this backend");let n=nZ(await e.backend.pressHome(t8(e,t)));return{kind:"systemHome",...n?{backendResult:n}:{},...u("Home")}},rF=async(e,t)=>{if(!e.backend.rotate)throw new a("UNSUPPORTED_OPERATION","system.rotate is not supported by this backend");let n=function(e){switch(e){case"portrait":case"portrait-upside-down":case"landscape-left":case"landscape-right":return e;default:throw new a("INVALID_ARGS","system.rotate orientation must be portrait, portrait-upside-down, landscape-left, or landscape-right")}}(t.orientation),r=nZ(await e.backend.rotate(t8(e,t),n));return{kind:"systemRotated",orientation:n,...r?{backendResult:r}:{},...u(`Rotated to ${n}`)}},rq=async(e,t={})=>{var n,r,i,o,s,l,d;if(!e.backend.setKeyboard)throw new a("UNSUPPORTED_OPERATION","system.keyboard is not supported by this backend");let c=t.action??"status";if(!L(c))throw new a("INVALID_ARGS","system.keyboard action must be status, get, dismiss, enter, or return");let h=await e.backend.setKeyboard(t8(e,t),{action:c}),p=nZ(h),f=(n=h)&&"object"==typeof n?h:{};return"enter"===c||"return"===c?{kind:"keyboardEnterPressed",action:"enter",state:f,...(r=p)?{backendResult:r}:{},...u("Keyboard enter pressed")}:"dismiss"===c?(i=c,{kind:"keyboardDismissed",action:i,state:o=f,...(s=p)?{backendResult:s}:{},...u(!1===o.dismissed?"Keyboard already hidden":"Keyboard dismissed")}):(l=c,{kind:"keyboardState",action:l,state:f,...(d=p)?{backendResult:d}:{}})},rX=async(e,t)=>{if("read"===t.action){if(!e.backend.getClipboard)throw new a("UNSUPPORTED_OPERATION","system.clipboard read is not supported by this backend");let n=await e.backend.getClipboard(t8(e,t));return{kind:"clipboardText",action:"read",text:"string"==typeof n?n:n.text}}if("write"!==t.action)throw new a("INVALID_ARGS","system.clipboard action must be read or write");if(!e.backend.setClipboard)throw new a("UNSUPPORTED_OPERATION","system.clipboard write is not supported by this backend");if("string"!=typeof t.text)throw new a("INVALID_ARGS","system.clipboard write requires text");let n=nZ(await e.backend.setClipboard(t8(e,t),t.text));return{kind:"clipboardUpdated",action:"write",textLength:Array.from(t.text).length,...n?{backendResult:n}:{},...u("Clipboard updated")}},rY=async(e,t={})=>{if(!e.backend.openSettings)throw new a("UNSUPPORTED_OPERATION","system.settings is not supported by this backend");let n=nW(t.target,"target"),r=nZ(await e.backend.openSettings(t8(e,t),n));return{kind:"settingsOpened",...n?{target:n}:{},...r?{backendResult:r}:{},...u(n?`Opened settings: ${n}`:"Opened settings")}},rV=async(e,t={})=>{var n,r;if(!e.backend.handleAlert)throw new a("UNSUPPORTED_OPERATION","system.alert is not supported by this backend");let i=t.action??"get";if("get"!==i&&"accept"!==i&&"dismiss"!==i&&"wait"!==i)throw new a("INVALID_ARGS","system.alert action must be get, accept, dismiss, or wait");let o=void 0===t.timeoutMs?void 0:ev(t.timeoutMs,"timeoutMs",0,12e4),s=await e.backend.handleAlert(t8(e,t),i,{timeoutMs:o});return n=i,r=s,"get"===n?function(e){if("alertStatus"!==e.kind)throw new a("COMMAND_FAILED","system.alert get returned an invalid backend result");return{kind:"alertStatus",action:"get",alert:e.alert}}(r):"wait"===n?function(e){if("alertWait"!==e.kind)throw new a("COMMAND_FAILED","system.alert wait returned an invalid backend result");return{kind:"alertWait",action:"wait",alert:e.alert,...void 0!==e.waitedMs?{waitedMs:e.waitedMs}:{},...void 0!==e.timedOut?{timedOut:e.timedOut}:{},...u(e.alert?"Alert visible":"Alert wait timed out")}}(r):function(e,t){if("alertHandled"!==t.kind)throw new a("COMMAND_FAILED",`system.alert ${e} returned an invalid backend result`);return{kind:"alertHandled",action:e,handled:t.handled,...t.alert?{alert:t.alert}:{},...t.button?{button:t.button}:{},...u(t.handled?`Alert ${e}ed`:"No alert handled")}}(n,r)},rH=async(e,t={})=>{if(!e.backend.openAppSwitcher)throw new a("UNSUPPORTED_OPERATION","system.appSwitcher is not supported by this backend");let n=nZ(await e.backend.openAppSwitcher(t8(e,t)));return{kind:"appSwitcherOpened",...n?{backendResult:n}:{},...u("Opened app switcher")}},rz=async(e,t)=>{var n;if(!e.backend.openApp)throw new a("UNSUPPORTED_OPERATION","apps.open is not supported by this backend");let r=function(e){var t;let n=nW(e.app,"app"),r=nW(e.appId,"appId"),i=nW(e.bundleId,"bundleId"),o=nW(e.packageName,"packageName"),s=nW(e.url,"url"),l=nW(e.activity,"activity"),d={...n?{app:n}:{},...r?{appId:r}:{},...i?{bundleId:i}:{},...o?{packageName:o}:{},...s?{url:s}:{},...l?{activity:l}:{}};if(!((t=d).app??t.appId??t.bundleId??t.packageName??t.url??t.activity))throw new a("INVALID_ARGS","apps.open requires app, appId, bundleId, packageName, url, or activity");return d}(t),i=n5(await e.backend.openApp(n2(e,t),r,{launchArgs:t.launchArgs,relaunch:t.relaunch}));return{kind:"appOpened",target:r,relaunch:!0===t.relaunch,...i?{backendResult:i}:{},...u(`Opened: ${(n=r).app??n.appId??n.bundleId??n.packageName??n.url??n.activity??"app"}`)}},rK=async(e,t={})=>{if(!e.backend.closeApp)throw new a("UNSUPPORTED_OPERATION","apps.close is not supported by this backend");let n=nW(t.app,"app"),r=n5(await e.backend.closeApp(n2(e,t),n));return{kind:"appClosed",...n?{app:n}:{},...r?{backendResult:r}:{},...u(n?`Closed: ${n}`:"Closed app")}},rj=async(e,t={})=>{if(!e.backend.listApps)throw new a("UNSUPPORTED_OPERATION","apps.list is not supported by this backend");return{kind:"appsList",apps:await e.backend.listApps(n2(e,t),$(t.filter))}},rW=async(e,t)=>{if(!e.backend.getAppState)throw new a("UNSUPPORTED_OPERATION","apps.state is not supported by this backend");let n=nJ(t.app,"app"),r=await e.backend.getAppState(n2(e,t),n);return{kind:"appState",app:n,state:r}},rJ=async(e,t)=>{if(!e.backend.pushFile)throw new a("UNSUPPORTED_OPERATION","apps.push is not supported by this backend");let n=nJ(t.app,"app"),r=await n0(e,t.input);try{let i=await e.backend.pushFile(n2(e,t),r.backendInput,n),a=n5(i);return{kind:"appPushed",app:n,inputKind:r.inputKind,...a?{backendResult:a}:{},...u(`Pushed to ${n}`)}}finally{await r.cleanup?.()}},rZ=async(e,t)=>{var n,r;if(!e.backend.triggerAppEvent)throw new a("UNSUPPORTED_OPERATION","apps.triggerEvent is not supported by this backend");let i=function(e){let t=nJ(e,"name");if(!nQ.test(t))throw new a("INVALID_ARGS",`Invalid apps.triggerEvent name: ${t}`,{hint:"Use 1-64 chars: letters, numbers, underscore, dot, colon, or dash."});return t}(t.name);n=t.payload,r=`apps.triggerEvent payload for "${i}"`,void 0!==n&&n1(n,r,8192);let o=n5(await e.backend.triggerAppEvent(n2(e,t),{name:i,...t.payload?{payload:t.payload}:{}}));return{kind:"appEventTriggered",name:i,...t.payload?{payload:t.payload}:{},...o?{backendResult:o}:{},...u(`Triggered app event: ${i}`)}},rQ=async(e,t={})=>{if(!e.backend.listDevices)throw new a("UNSUPPORTED_OPERATION","admin.devices is not supported by this backend");return{kind:"adminDevices",devices:await e.backend.listDevices(t8(e,t),t.filter)}},r0=async(e,t={})=>{if(!e.backend.bootDevice)throw new a("UNSUPPORTED_OPERATION","admin.boot is not supported by this backend");let n=function(e){if(!e)return;let t=nW(e.id,"target.id"),n=nW(e.name,"target.name"),r={...t?{id:t}:{},...n?{name:n}:{},...e.platform?{platform:e.platform}:{},...e.target?{target:e.target}:{},...void 0!==e.headless?{headless:e.headless}:{}};return Object.keys(r).length>0?r:void 0}(t.target),r=n7(await e.backend.bootDevice(t8(e,t),n));return{kind:"deviceBooted",...n?{target:n}:{},...r?{backendResult:r}:{},...u("Booted device")}},r1=async(e,t)=>await n4(e,t,"install"),r2=async(e,t)=>await n4(e,t,"reinstall"),r5=async(e,t)=>await n4(e,t,"installFromSource"),r4=async(e,t)=>{let n=re(t.action,"record"),r="start"===n?e.backend.startRecording:e.backend.stopRecording;if(!r)throw new a("UNSUPPORTED_OPERATION",`record ${n} is not supported by this backend`);let i=t.out?await eX(e,t.out,{field:"path",ext:".mp4"}):void 0;try{var o,s,l,d,c;let a,u,h=(o=t,s=i?.path,a=void 0===o.fps?void 0:ev(o.fps,"fps",1,60),u=void 0===o.quality?void 0:ev(o.quality,"quality",5,10),{...s?{outPath:s}:{},...void 0!==a?{fps:a}:{},...void 0!==u?{quality:u}:{},...void 0!==o.hideTouches?{showTouches:!0!==o.hideTouches}:{}}),p=await r.call(e.backend,t8(e,t),h),f=await i?.publish();return l=n,d=p,c=f,{..."string"==typeof d.path?{path:d.path}:{},..."string"==typeof d.telemetryPath?{telemetryPath:d.telemetryPath}:{},..."string"==typeof d.warning?{warning:d.warning}:{},...rt(l,d,c,{startKind:"recordingStarted",stopKind:"recordingStopped",startMessage:"Recording started",stopMessage:"Recording stopped"})}}catch(e){throw await i?.cleanup?.(),e}},r8=async(e,t)=>{let n=re(t.action,"trace"),r="start"===n?e.backend.startTrace:e.backend.stopTrace;if(!r)throw new a("UNSUPPORTED_OPERATION",`trace ${n} is not supported by this backend`);let i=t.out?await eX(e,t.out,{field:"outPath",ext:".trace"}):void 0;try{var o,s,l;let a={...i?.path?{outPath:i.path}:{}},d=await r.call(e.backend,t8(e,t),a),c=await i?.publish();return o=n,s=d,l=c,{..."string"==typeof s.outPath?{outPath:s.outPath}:{},...rt(o,s,l,{startKind:"traceStarted",stopKind:"traceStopped",startMessage:"Trace started",stopMessage:"Trace stopped"})}}catch(e){throw await i?.cleanup?.(),e}},r3=async(e,t={})=>{var n,r;let i;if(!e.backend.readLogs)throw new a("UNSUPPORTED_OPERATION","diagnostics.logs is not supported by this backend");return i=!0===(n=await e.backend.readLogs(await rp(e,t),{...rf(r=t,100,500,"logs limit"),...void 0!==r.levels?{levels:rm(r.levels,"levels")}:{},...void 0!==r.search?{search:nJ(r.search,"search")}:{},...void 0!==r.source?{source:nJ(r.source,"source")}:{}})).redacted,{kind:"diagnosticsLogs",entries:n.entries.map(e=>{let t=ru(e.message,4096),n=rd(e.metadata);return i||=t.redacted||n.redacted,{...e.timestamp?{timestamp:e.timestamp}:{},...e.level?{level:e.level}:{},message:t.value??"",...e.source?{source:e.source}:{},...n.value?{metadata:n.value}:{}}}),...n.nextCursor?{nextCursor:n.nextCursor}:{},...n.timeWindow?{timeWindow:n.timeWindow}:{},...n.backend?{backend:n.backend}:{},redacted:i,...n.notes?{notes:n.notes}:{}}},r6=async(e,t={})=>{var n,r,i;let o;if(!e.backend.dumpNetwork)throw new a("UNSUPPORTED_OPERATION","diagnostics.network is not supported by this backend");let s={...rf(i=t,25,200,"network limit"),include:function(e){if(void 0===e)return"summary";if("summary"===e||"headers"===e||"body"===e||"all"===e)return e;throw new a("INVALID_ARGS","network include must be summary, headers, body, or all")}(i.include)};return n=await e.backend.dumpNetwork(await rp(e,t),s),r=s.include??"summary",o=!0===n.redacted,{kind:"diagnosticsNetwork",entries:n.entries.map(e=>{var t;let n=e.url?ra(t=e.url)??ru(t,2048):void 0,i="headers"===r||"all"===r?rs(e.requestHeaders):void 0,a="headers"===r||"all"===r?rs(e.responseHeaders):void 0,s="body"===r||"all"===r?rl(e.requestBody):void 0,l="body"===r||"all"===r?rl(e.responseBody):void 0,d=rd(e.metadata);return o||=(n?.redacted??!1)||(i?.redacted??!1)||(a?.redacted??!1)||(s?.redacted??!1)||(l?.redacted??!1)||d.redacted,{...e.timestamp?{timestamp:e.timestamp}:{},...e.method?{method:e.method}:{},...n?{url:n.value}:{},...void 0!==e.status?{status:e.status}:{},...void 0!==e.durationMs?{durationMs:e.durationMs}:{},...i?.value?{requestHeaders:i.value}:{},...a?.value?{responseHeaders:a.value}:{},...s?.value!==void 0?{requestBody:s.value}:{},...l?.value!==void 0?{responseBody:l.value}:{},...d.value?{metadata:d.value}:{}}}),...n.nextCursor?{nextCursor:n.nextCursor}:{},...n.timeWindow?{timeWindow:n.timeWindow}:{},...n.backend?{backend:n.backend}:{},redacted:o,...n.notes?{notes:n.notes}:{}}},r9=async(e,t={})=>{var n,r;let i;if(!e.backend.measurePerf)throw new a("UNSUPPORTED_OPERATION","diagnostics.perf is not supported by this backend");return i=!0===(n=await e.backend.measurePerf(await rp(e,t),{...rw(r=t),...void 0!==r.sampleMs?{sampleMs:ev(r.sampleMs,"sampleMs",100,6e4)}:{},...void 0!==r.metrics?{metrics:rm(r.metrics,"metrics",20)}:{}})).redacted,{kind:"diagnosticsPerf",metrics:n.metrics.map(e=>{let t=ru(e.message,4096),n=rd(e.metadata);return i||=t.redacted||n.redacted,{name:e.name,...void 0!==e.value?{value:e.value}:{},...e.unit?{unit:e.unit}:{},...e.status?{status:e.status}:{},...void 0!==t.value?{message:t.value}:{},...n.value?{metadata:n.value}:{}}}),...n.startedAt?{startedAt:n.startedAt}:{},...n.endedAt?{endedAt:n.endedAt}:{},...n.backend?{backend:n.backend}:{},redacted:i,...n.notes?{notes:n.notes}:{}}};function r7(e){let t={backend:e.backend,artifacts:e.artifacts,sessions:e.sessions??function(e=[]){let t=new Map(e.map(e=>[e.name,ie(e)]));return{get:e=>ie(t.get(e)),set:e=>{t.set(e.name,ie(e))},delete:e=>{t.delete(e)},list:()=>Array.from(t.values(),e=>ie(e))}}(),policy:e.policy??function(e={}){return{allowLocalInputPaths:!1,allowLocalOutputPaths:!1,maxImagePixels:2e7,allowNamedBackendCapabilities:[],...e}}(),diagnostics:e.diagnostics,clock:e.clock,signal:e.signal};return{...t,capture:{screenshot:e=>rb(t,e),diffScreenshot:e=>rg(t,e),snapshot:e=>ry(t,e),diffSnapshot:e=>rx(t,e)},selectors:{find:e=>rv(t,e),get:e=>rk(t,e),getText:(e,n={})=>rI(t,{...n,target:e}),getAttrs:(e,n={})=>rM(t,{...n,target:e}),is:e=>rA(t,e),isVisible:(e,n={})=>rN(t,{...n,target:e}),isHidden:(e,n={})=>rR(t,{...n,target:e}),wait:e=>rP(t,e),waitForText:(e,n={})=>rS(t,{...n,text:e})},interactions:{click:(e,n={})=>rD(t,{...n,target:e}),press:(e,n={})=>rE(t,{...n,target:e}),fill:(e,n,r={})=>rO(t,{...r,target:e,text:n}),typeText:(e,n={})=>r_(t,{...n,text:e}),focus:(e,n={})=>rT(t,{...n,target:e}),longPress:(e,n={})=>r$(t,{...n,target:e}),swipe:e=>rC(t,e),scroll:e=>rL(t,e),pinch:e=>rU(t,e)},system:{back:e=>rB(t,e),home:e=>rG(t,e),rotate:e=>rF(t,e),keyboard:e=>rq(t,e),clipboard:e=>rX(t,e),settings:e=>rY(t,e),alert:e=>rV(t,e),appSwitcher:e=>rH(t,e)},apps:{open:e=>rz(t,e),close:e=>rK(t,e),list:(e={})=>rj(t,{...e,filter:T(e.filter)}),state:e=>rW(t,e),push:e=>rJ(t,e),triggerEvent:e=>rZ(t,e)},admin:{devices:e=>rQ(t,e),boot:e=>r0(t,e),install:e=>r1(t,e),reinstall:e=>r2(t,e),installFromSource:e=>r5(t,e)},recording:{record:e=>r4(t,e),trace:e=>r8(t,e)},observability:{logs:e=>r3(t,e),network:e=>r6(t,e),perf:e=>r9(t,e)}}}function ie(e){if(e)return{...e,...e.snapshot?{snapshot:structuredClone(e.snapshot)}:{},...e.metadata?{metadata:function(e){try{return structuredClone(e)}catch{return{...e}}}(e.metadata)}:{}}}function it(e={}){return{allowLocalInputPaths:!0,allowLocalOutputPaths:!0,maxImagePixels:2e7,allowNamedBackendCapabilities:[],...e}}export{eP as PNG,ei as buildMobileSnapshotPresentation,B as buildScrollGesturePlan,tL as buildSnapshotDisplayLines,G as buildSwipeGesturePlan,F as buildSwipePresetGesturePlan,eh as captureScrollEdgeState,V as clampGesturePoint,j as clampToRange,r7 as createAgentDevice,eG as decodePng,ea as deriveMobileSnapshotHiddenContentHints,tz as detectReactNativeOverlay,er as displayNodeLabel,nn as evaluateIsPredicate,U as findMistargetedTypeRefToken,ef as formatScrollEdgeMessage,tU as formatSnapshotLine,X as inferGestureReferenceFrame,ee as inferVerticalScrollIndicatorDirections,nM as interiorCoordinate,L as isKeyboardAction,ek as isReactNativeCollapsedWarningLabel,nt as isSupportedPredicate,Q as isSystemScrollIndicatorLabel,it as localCommandPolicy,et as normalizeSnapshotTree,H as parseScrollDirection,q as parseSwipePreset,Y as pointFromPercent,nI as pointInsideRect,ev as requireIntInRange,nN as resolveActionableTouchNode,nR as resolveActionableTouchResolution,tK as resolveReactNativeOverlayDismissTarget,nv as resolveRectCenter,ep as runScrollEdgePasses};
1
+ import{promises as e}from"node:fs";import{deflateSync as t,inflateSync as n}from"node:zlib";import r from"node:path";import{asAppError as i,AppError as a}from"./9152.js";import{normalizeRef as o,centerOfRect as s,buildSnapshotPresentationKey as l,findNodeByRef as d}from"./4057.js";import{isScrollableNodeLike as c}from"./2842.js";import{successText as u}from"./1998.js";import{whichCmd as h,runCmd as p}from"./9818.js";import{trimText as f,extractNodeText as w,findSnapshotAncestor as m,parseSelectorChain as b,isNodeVisible as g,buildSnapshotNodeByIndex as y,isNodeEditable as x,buildTextPreview as v,findNearestHittableAncestor as k,buildSelectorChainForNode as I,findSelectorChainMatch as M,resolveSelectorChain as A,findNodeByLabel as N,resolveRefLabel as R,describeTextSurface as P,extractReadableText as S,formatSelectorFailure as D,normalizeType as E,isFillableType as O}from"./940.js";import{findBestMatchesByLocator as _}from"./7556.js";import"./7847.js";import{resolveAppsFilter as T,assertResolvedAppsFilter as $}from"./1393.js";let C=["status","get","dismiss","enter","return"];function L(e){return C.includes(e)}function U(e){let t=e?.trim().split(/\s+/,1)[0];if(!t||!t.startsWith("@")||t.length<3)return null;let n=t.slice(1);return/^[A-Za-z_-]*\d[\w-]*$/i.test(n)||/^(?:ref|node|element|el)[\w-]*$/i.test(n)?t:null}function B(e){let t=e.direction,n="up"===t||"down"===t?e.referenceHeight:e.referenceWidth,r=function(e){if(void 0===e)return .6;if(!Number.isFinite(e)||e<=0)throw new a("INVALID_ARGS","scroll amount must be a positive number");return e}(e.amount),i=void 0!==e.pixels?function(e){if(!Number.isFinite(e)||e<=0)throw new a("INVALID_ARGS","scroll pixels must be a positive integer");return Math.max(1,Math.round(e))}(e.pixels):Math.round(n*r),o=Math.max(1,Math.round(.05*n)),s=Math.max(1,Math.min(i,Math.max(1,n-2*o))),l=Math.round(s/2),d=Math.round(e.referenceWidth/2),c=Math.round(e.referenceHeight/2),u=(n,r,i,a)=>({direction:t,x1:n,y1:r,x2:i,y2:a,referenceWidth:e.referenceWidth,referenceHeight:e.referenceHeight,amount:e.amount,pixels:s});switch(t){case"up":return u(d,c-l,d,c+l);case"down":return u(d,c+l,d,c-l);case"left":return u(d-l,c,d+l,c);case"right":return u(d+l,c,d-l,c)}}function G(e){return{...B({...e,direction:function(e){switch(e){case"up":return"down";case"down":return"up";case"left":return"right";case"right":return"left"}}(e.direction)}),direction:e.direction}}function F(e,t,n={}){let r=n.marginPx??8,[i,a,o]="left"===e?[85,15,65]:"right"===e?[15,85,65]:"left-edge"===e?[99,15,50]:[1,85,50],s=Y(t,i,o,{marginPx:r}),l=Y(t,a,o,{marginPx:r});return{preset:e,x1:s.x,y1:s.y,x2:l.x,y2:l.y,referenceWidth:t.referenceWidth,referenceHeight:t.referenceHeight}}function q(e){switch(e){case"left":case"right":case"left-edge":case"right-edge":return e;default:throw new a("INVALID_ARGS","gesture swipe requires left, right, left-edge, or right-edge")}}function X(e){let t=function(e){let t=e.filter(e=>(function(e){if(!e)return!1;let t=e.toLowerCase();return t.includes("application")||t.includes("window")})(e.type)&&z(e.rect)).map(e=>e.rect).sort((e,t)=>(t?.width??0)*(t?.height??0)-(e?.width??0)*(e?.height??0))[0];if(t)return t;let n=e.map(e=>e.rect).filter(z);if(0===n.length)return;let r=Math.max(...n.map(e=>e.x+e.width)),i=Math.max(...n.map(e=>e.y+e.height));if(!(r<=0)&&!(i<=0))return{x:0,y:0,width:r,height:i}}(e);if(t)return{referenceWidth:t.width,referenceHeight:t.height}}function Y(e,t,n,r={}){let i={x:Math.round(e.referenceWidth*t/100),y:Math.round(e.referenceHeight*n/100)};return void 0===r.marginPx?i:V(i,e,r.marginPx)}function V(e,t,n){return{x:K(e.x,n,t.referenceWidth),y:K(e.y,n,t.referenceHeight)}}function H(e){switch(e){case"up":case"down":case"left":case"right":return e;default:throw new a("INVALID_ARGS",`Unknown direction: ${e}`)}}function z(e){return!!e&&e.width>0&&e.height>0}function K(e,t,n){let r=Math.max(t,n-t);return j(e,t,r)}function j(e,t,n){return Math.min(Math.round(n),Math.max(Math.round(t),Math.round(e)))}function W(e,t,n){return t>=e.x&&t<=e.x+e.width&&n>=e.y&&n<=e.y+e.height}function J(e){let t=null,n=-1;for(let r of e){let e=r.width*r.height;e>n&&(t=r,n=e)}return t}function Z(e,t,n,r){return Math.max(e,n)<=Math.min(t,r)}function Q(e){let t=e.trim().toLowerCase();return!!t&&/^(vertical|horizontal)\s+scroll\s+bar(?:,?\s*\d+\s+pages?)?$/.test(t)}function ee(e,t){if(!(e?.trim().toLowerCase()??"").includes("vertical scroll bar"))return null;let n=function(e){if(!e)return null;let t=/^(\d{1,3})%$/.exec(e.trim());if(!t)return null;let n=Number(t[1]);return Number.isFinite(n)?Math.max(0,Math.min(100,n)):null}(t);return null===n?null:n<=1?{above:!1,below:!0}:n>=99?{above:!0,below:!1}:{above:!0,below:!0}}function et(e){let t=new Map;for(let[n,r]of e.entries())t.set(r.index,n);let n=[],r=[];for(let[i,a]of e.entries()){let e=Math.max(0,a.depth??0);for(;r.length>0&&e<=r[r.length-1].depth;)r.pop();let o="number"==typeof a.parentIndex?t.get(a.parentIndex):void 0,s="number"==typeof o&&o<i?o:r[r.length-1]?.index;n.push({...a,index:i,depth:e,parentIndex:s}),r.push({depth:e,index:i})}return n}function en(e){return new Map(e.map(e=>[e.index,e]))}function er(e){return e.label?.trim()||e.value?.trim()||e.identifier?.trim()||""}function ei(e){if(0===e.length)return{nodes:e,hiddenCount:0,summaryLines:[]};let{byIndex:t,visibleNodeIndexes:n,offscreenNodes:r,hintedContainers:i}=eo(e),a=0===n.size?e:e.filter(e=>n.has(e.index));return{nodes:a.map(e=>(function(e,t){let n=t.get(e.index);if(!n||0===n.size)return e;let r=!!(!0===e.hiddenContentAbove||n.has("above"))||void 0,i=!!(!0===e.hiddenContentBelow||n.has("below"))||void 0;return{...e,hiddenContentAbove:r,hiddenContentBelow:i}})(e,i.directionsByContainer)),hiddenCount:0===n.size?0:e.length-a.length,summaryLines:function(e,t,n){let r=new Map;for(let i of e){let e=function(e,t,n){if(!e.rect)return null;let r=el(e,t,n);return r?ed(e.rect,r):null}(i,t,n);if(!e)continue;let a=r.get(e)??[];a.push(i),r.set(e,a)}return["above","below"].flatMap(e=>{let t=r.get(e);if(!t||0===t.length)return[];let n=(function(e){let t=new Set,n=[];for(let r of e){let e=er(r);!e||t.has(e)||(t.add(e),n.push(e))}return n})(t).slice(0,3).map(e=>`"${e}"`),i=1===t.length?"interactive item":"interactive items",a=n.length>0?`: ${n.join(", ")}`:"";return[`[off-screen ${e}] ${t.length} ${i}${a}`]})}(r.filter(e=>!i.coveredNodeIndexes.has(e.index)&&function(e){if(!0===e.hittable)return!0;let t=(e.type??"").toLowerCase();return t.includes("button")||t.includes("link")||t.includes("textfield")||t.includes("edittext")||t.includes("searchfield")||t.includes("checkbox")||t.includes("radio")||t.includes("switch")||t.includes("menuitem")||!!er(e)}(e)),e,t)}}function ea(e){if(0===e.length)return new Map;let{hintedContainers:t}=eo(e);var n=t.directionsByContainer;let r=new Map;for(let[e,t]of n){let n={};t.has("above")&&(n.hiddenContentAbove=!0),t.has("below")&&(n.hiddenContentBelow=!0),(n.hiddenContentAbove||n.hiddenContentBelow)&&r.set(e,n)}return r}function eo(e){let t=en(e),n=new Set,r=[];for(let i of e){if(es(i,e,t)){!function(e,t,n){let r=e,i=new Set;for(;r&&!i.has(r.index);)i.add(r.index),t.add(r.index),r="number"==typeof r.parentIndex?n.get(r.parentIndex):void 0}(i,n,t);continue}r.push(i)}let i=function(e,t,n,r){let i=new Map,a=new Map,o=new Set;for(let e of t){if(!e.rect)continue;let t=ec(e,n,r);if(!t?.rect)continue;let s=ed(e.rect,t.rect);if(!s)continue;let l=i.get(t.index)??new Set;l.add(s),i.set(t.index,l);let d=a.get(t.index)??new Set;d.add(s),a.set(t.index,d),o.add(e.index)}return function(e,t,n,r,i){for(let a of e){let e=function(e){let t=ee(e.label,e.value);if(!t)return null;let n=new Set;return t.above&&n.add("above"),t.below&&n.add("below"),n.size>0?n:null}(a);if(!e||0===e.size)continue;let o=ec(a,t,n);if(!o)continue;let s=r.get(o.index)??new Set,l=i.get(o.index);for(let t of e)(!l||!(l.size>0)||l.has(t))&&s.add(t);r.set(o.index,s)}}(e,n,r,i,a),{directionsByContainer:i,coveredNodeIndexes:o}}(e,r,n,t);return{byIndex:t,visibleNodeIndexes:n,offscreenNodes:r,hintedContainers:i}}function es(e,t,n=en(t)){var r;if(!e.rect)return!0;let i=el(e,t,n);return!i||(r=e.rect,Z(r.x,r.x+r.width,i.x,i.x+i.width)&&Z(r.y,r.y+r.height,i.y,i.y+i.height))}function el(e,t,n=en(t)){var r,i;let a=(r=e,i=n,eu(r,i,e=>!!e.rect)?.rect??null);return a||function(e,t){let n=s(t),r=e.filter(e=>{var t;return!!(t=e.rect)&&Number.isFinite(t.x)&&Number.isFinite(t.y)&&Number.isFinite(t.width)&&Number.isFinite(t.height)}),i=r.filter(e=>{let t=(e.type??"").toLowerCase();return t.includes("application")||t.includes("window")}),a=J(i.map(e=>e.rect).filter(e=>W(e,n.x,n.y)));if(a)return a;let o=J(i.map(e=>e.rect));if(o)return o;let l=J(r.map(e=>e.rect).filter(e=>W(e,n.x,n.y)));return l||null}(t,e.rect??{x:0,y:0,width:0,height:0})}function ed(e,t){return e.y+e.height<=t.y?"above":e.y>=t.y+t.height?"below":null}function ec(e,t,n){return eu(e,n,e=>t.has(e.index))}function eu(e,t,n){let r="number"==typeof e.parentIndex?t.get(e.parentIndex):void 0,i=new Set;for(;r&&!i.has(r.index);){if(i.add(r.index),n(r)&&c(r))return r;r="number"==typeof r.parentIndex?t.get(r.parentIndex):void 0}return null}async function eh(e){let{edge:t,target:n={},scope:r,captureNodes:i}=e;try{let e=await i(r),a=function(e,t,n={}){var r,i,a;let o,s=(e??[]).map((e,t)=>({...e,ref:"ref"in e&&e.ref?e.ref:`e${t+1}`}));if(0===s.length)return{canScroll:!1,emptySnapshot:!0,signature:""};let l=ea(s),d=function(e,t,n,r){let i=new Map(e.map(e=>[e.index,e])),a=e.filter(e=>c(e)&&ex(e.rect));if(0===a.length)return null;let o=function(e,t){if(void 0===e)return null;let n=t.get(e);for(;n;){if(c(n)&&ex(n.rect))return n;n=void 0===n.parentIndex?void 0:t.get(n.parentIndex)}return null}(r.nodeIndex,i);if(o)return o;let s=r.point;if(s){let e=a.filter(e=>{var t,n;return e.rect&&(t=e.rect,(n=s).x>=t.x&&n.x<=t.x+t.width&&n.y>=t.y&&n.y<=t.y+t.height)}).sort(eb);if(e.length>0)return e.find(e=>ew(e,t.get(e.index),n))??e[0]??null}let l=a.filter(e=>ew(e,t.get(e.index),n)).sort(eg);return l.length>0?l[0]??null:a.filter(t=>es(t,e)).sort(eg)[0]??a.sort(eg)[0]??null}(s,l,t,n),u=(d?(r=s,i=d.index,o=new Map(r.map(e=>[e.index,e])),r.filter(e=>e.index===i||function(e,t,n){let r=void 0===e.parentIndex?void 0:n.get(e.parentIndex);for(;r;){if(r.index===t)return!0;r=void 0===r.parentIndex?void 0:n.get(r.parentIndex)}return!1}(e,i,o))):s).map(e=>{let t=e.rect?["x","y","width","height"].map(t=>{var n;return n=e.rect?.[t],"number"==typeof n&&Number.isFinite(n)?n.toFixed(1):""}).join(","):"";return[String(e.index??""),String(e.parentIndex??""),String(e.type??""),String(e.label??""),String(e.value??""),t].join("|")}).join("\n");return d?{canScroll:ew(d,l.get(d.index),t),emptySnapshot:!1,signature:u,scope:[(a=d).identifier,a.label,a.value].map(e=>"string"==typeof e?e.trim():"").find(em)}:{canScroll:!1,emptySnapshot:!1,signature:u}}(e,t,n);if(r&&a.emptySnapshot)return await eh({edge:t,target:n,captureNodes:i});return a}catch(e){var o,s,l;throw o=t,s=r,l=e,s?new a("COMMAND_FAILED",`Failed to verify scroll ${o} state for scoped container`,{scope:s,hint:`scroll ${o} could not verify the scoped scroll container. Run snapshot -i -c for the current screen and retry with a visible scroll target.`},l):new a("COMMAND_FAILED",`Failed to verify scroll ${o} state`,{hint:`scroll ${o} needs a snapshot showing hidden content ${"bottom"===o?"below":"above"} before it will move.`},l)}}async function ep(e){let t,{edge:n,captureState:r,scroll:i}=e,o=await r();o.scope&&(o=await r(o.scope));let s=0;for(;o.canScroll;){if(s>=40)throw new a("COMMAND_FAILED",`scroll ${n} reached the safety limit before the snapshot showed the edge`,{hint:"The scoped scroll container still reports hidden content. Use a smaller manual scroll + snapshot loop to inspect the current state."});t=await i(),s+=1,o=await r(o.scope)}return{passes:s,result:t}}function ef(e,t,n,r,i){return t&&0===n?`Already at ${t}; no hidden content ${"bottom"===t?"below":"above"} detected`:t?`Scrolled to ${t} with ${n} ${e} passes`:void 0!==i?`Scrolled ${e} by ${i}px`:void 0!==r?`Scrolled ${e} by ${r}`:`Scrolled ${e}`}function ew(e,t,n){return"bottom"===n?!0===e.hiddenContentBelow||t?.hiddenContentBelow===!0:!0===e.hiddenContentAbove||t?.hiddenContentAbove===!0}function em(e){return e.length>0&&e.length<=80&&!/^(true|false)$/i.test(e)&&!/^\d+$/.test(e)&&!/^\d+%$/.test(e)}function eb(e,t){return ey(e.rect)-ey(t.rect)}function eg(e,t){return ey(t.rect)-ey(e.rect)}function ey(e){return e?e.width*e.height:0}function ex(e){return!!(e&&e.width>0&&e.height>0)}function ev(e,t,n,r){if(!Number.isFinite(e)||!Number.isInteger(e)||e<n||e>r)throw new a("INVALID_ARGS",`${t} must be an integer between ${n} and ${r}`);return e}function ek(e){let t=e?.trim().toLowerCase();return!!t&&(t.includes("open debugger to view warnings")||/^!,\s+/.test(t)||/^(warn|warning|error):\s+/.test(t)||/\b(?:possible\s+)?unhandled (?:promise )?rejection\b/.test(t)||t.includes("getsnapshot should be cached to avoid an infinite loop")||t.includes('unique "key" prop')||t.includes("unique 'key' prop")||t.includes("virtualizedlists should never be nested")||t.includes("failed prop type"))}function eI(e){return e.includes("open debugger to view warnings")||/^!,\s+open debugger\b/.test(e)}let eM=Buffer.from([137,80,78,71,13,10,26,10]),eA=new Map([[0,1],[2,3],[3,1],[4,2],[6,4]]),eN=new Map([[0,new Set([1,2,4,8,16])],[2,new Set([8,16])],[3,new Set([1,2,4,8])],[4,new Set([8,16])],[6,new Set([8,16])]]),eR=[{x:0,y:0,dx:8,dy:8},{x:4,y:0,dx:8,dy:8},{x:0,y:4,dx:4,dy:8},{x:2,y:0,dx:4,dy:4},{x:0,y:2,dx:2,dy:4},{x:1,y:0,dx:2,dy:2},{x:0,y:1,dx:1,dy:2}];class eP{width;height;data;static sync={read:eS,write:eD};constructor(e){this.width=eB(e.width,"width"),this.height=eB(e.height,"height");let t=this.width*this.height*4;if(this.data=e.data?Buffer.from(e.data):Buffer.alloc(t),this.data.length!==t)throw Error(`PNG data length must be ${t} bytes`)}}function eS(e){var t;let{metadata:r,idatChunks:i}=function(e){let t,n=[];for(let r of function*(e){if(!e.subarray(0,eM.length).equals(eM))throw Error("Invalid PNG signature");let t=eM.length;for(;t<e.length;){if(t+12>e.length)throw Error("Truncated PNG chunk");let n=e.readUInt32BE(t),r=e.toString("ascii",t+4,t+8),i=t+8,a=i+n;if(a+4>e.length)throw Error(`Truncated PNG ${r} chunk`);let o=e.subarray(i,a),s=e.readUInt32BE(a);if(eU(e.subarray(t+4,a))!==s)throw Error(`Invalid PNG ${r} chunk CRC`);t=a+4,yield{type:r,data:o}}}(e))if("IHDR"===r.type?t=function(e){var t;if(13!==e.length)throw Error("Invalid PNG IHDR length");let n=e.readUInt32BE(0),r=e.readUInt32BE(4),i=e[8],a=e[9],o=e[10],s=e[11],l=e[12];if(t=a,!eA.has(t))throw Error(`Unsupported PNG color type ${a}`);let d=eN.get(a);if(!d?.has(i))throw Error(`Unsupported PNG color type ${a} with bit depth ${i}`);if(0!==o)throw Error(`Unsupported PNG compression method ${o}`);if(0!==s)throw Error(`Unsupported PNG filter method ${s}`);if(0!==l&&1!==l)throw Error(`Unsupported PNG interlace method ${l}`);return{width:eB(n,"width"),height:eB(r,"height"),bitDepth:i,colorType:a,interlace:l}}(r.data):"IDAT"===r.type?n.push(Buffer.from(r.data)):t=function(e,t){if("PLTE"===e.type){if(!t)throw Error("PNG PLTE appeared before IHDR");t.palette=Buffer.from(e.data)}else if("tRNS"===e.type){if(!t)throw Error("PNG tRNS appeared before IHDR");t.transparency=Buffer.from(e.data)}return t}(r,t),"IEND"===r.type)break;return{metadata:t,idatChunks:n}}(e);if(!r)throw Error("PNG is missing IHDR");if(0===i.length)throw Error("PNG is missing IDAT");let a=function(e,t){let r=function(e){if(0===e.interlace)return function(e,t){return(e$(e)+1)*t}(e,e.height);let t=0;for(let n of eR){let r=eO(e.width,n.x,n.dx),i=eO(e.height,n.y,n.dy);0!==r&&0!==i&&(t+=function(e,t){return(e$(e)+1)*t}({...e,width:r,height:i},i))}return t}(t);try{let t=n(Buffer.concat(e),{maxOutputLength:r});if(t.length!==r)throw Error("PNG pixel data is truncated");return t}catch(e){var i;if((i=e)instanceof Error&&"code"in i&&"ERR_BUFFER_TOO_LARGE"===i.code)throw Error(`PNG pixel data exceeds expected length ${r}`);throw e}}(i,r);return new eP({width:r.width,height:r.height,data:1===r.interlace?function(e,t){let n=Buffer.alloc(t.width*t.height*4),r=0;for(let i of eR){let a=eO(t.width,i.x,i.dx),o=eO(t.height,i.y,i.dy);if(0===a||0===o)continue;let s={...t,width:a,height:o,interlace:0},l=eE({inflated:e,offset:r,scanlineLength:e$(s),height:o,bytesPerPixel:eC(s)});r=l.offset;let d=e$(s);for(let e=0;e<o;e+=1){let r=l.raw.subarray(e*d,(e+1)*d);for(let o=0;o<a;o+=1){let a=i.x+o*i.dx,l=((i.y+e*i.dy)*t.width+a)*4,[d,c,u,h]=e_(r,o,s);n[l]=d,n[l+1]=c,n[l+2]=u,n[l+3]=h}}}return n}(a,r):function(e,t){let n=Buffer.alloc(t.width*t.height*4),r=e$(t);for(let i=0;i<t.height;i+=1){let a=e.subarray(i*r,(i+1)*r);for(let e=0;e<t.width;e+=1){let r=(i*t.width+e)*4,[o,s,l,d]=e_(a,e,t);n[r]=o,n[r+1]=s,n[r+2]=l,n[r+3]=d}}return n}(eE({inflated:a,offset:0,scanlineLength:e$(t=r),height:t.height,bytesPerPixel:eC(t)}).raw,r)})}function eD(e){let n=4*e.width,r=Buffer.alloc((n+1)*e.height);for(let t=0;t<e.height;t+=1){let i=t*(n+1);r[i]=0,e.data.copy(r,i+1,t*n,(t+1)*n)}let i=Buffer.alloc(13);return i.writeUInt32BE(e.width,0),i.writeUInt32BE(e.height,4),i[8]=8,i[9]=6,i[10]=0,i[11]=0,i[12]=0,Buffer.concat([eM,eL("IHDR",i),eL("IDAT",t(r)),eL("IEND",Buffer.alloc(0))])}function eE(e){let{inflated:t,offset:n,scanlineLength:r,height:i,bytesPerPixel:a}=e,o=n+(r+1)*i;if(t.length<o)throw Error("PNG pixel data is truncated");let s=Buffer.alloc(r*i);for(let e=0;e<i;e+=1){let i=n+e*(r+1),o=e*r,l=t[i];for(let n=0;n<r;n+=1){let d=t[i+1+n],c=n>=a?s[o+n-a]:0,u=e>0?s[o+n-r]:0,h=e>0&&n>=a?s[o+n-r-a]:0;s[o+n]=function(e,t,n,r,i){if(0===e)return t;if(1===e)return t+n&255;if(2===e)return t+r&255;if(3===e)return t+Math.floor((n+r)/2)&255;if(4===e)return t+function(e,t,n){let r=e+t-n,i=Math.abs(r-e),a=Math.abs(r-t),o=Math.abs(r-n);return i<=a&&i<=o?e:a<=o?t:n}(n,r,i)&255;throw Error(`Unsupported PNG filter type ${e}`)}(l,d,c,u,h)}}return{raw:s,offset:o}}function eO(e,t,n){return e<=t?0:Math.floor((e-t+n-1)/n)}function e_(e,t,n){if(3===n.colorType){var r,i,a,o,s,l,d,c=e,u=t,h=n;if(!h.palette)throw Error("Indexed PNG is missing PLTE");let p=eT(c,u,h.bitDepth),f=3*p;if(f+2>=h.palette.length)throw Error("Indexed PNG palette is invalid");return[h.palette[f],h.palette[f+1],h.palette[f+2],h.transparency?.[p]??255]}if(n.bitDepth<8){let i,a;return[a=Math.round((i=eT(e,t,(r=n).bitDepth))/((1<<r.bitDepth)-1)*255),a,a,255*!(r.transparency&&r.transparency.length>=2&&i===r.transparency.readUInt16BE(0))]}let p=16===n.bitDepth?2:1,f=t*eA.get(n.colorType)*p,w=t=>16===n.bitDepth?e.readUInt16BE(f+2*t):e[f+t*p],m=e=>{var t;return t=w(e),16===n.bitDepth?Math.round(t/65535*255):t};if(0===n.colorType){let e=m(0);return[e,e,e,255*(i=w(0),!(a=n).transparency||!!(a.transparency.length<2)||i!==a.transparency.readUInt16BE(0))]}if(2===n.colorType){return[m(0),m(1),m(2),255*(o=w(0),s=w(1),l=w(2),!(d=n).transparency||!!(d.transparency.length<6)||o!==d.transparency.readUInt16BE(0)||s!==d.transparency.readUInt16BE(2)||l!==d.transparency.readUInt16BE(4))]}if(4===n.colorType){let e=m(0);return[e,e,e,m(1)]}return[m(0),m(1),m(2),m(3)]}function eT(e,t,n){let r=t*n;return e[Math.floor(r/8)]>>8-n-r%8&(1<<n)-1}function e$(e){let t=eA.get(e.colorType);return Math.ceil(e.width*t*e.bitDepth/8)}function eC(e){return Math.max(1,Math.ceil(eA.get(e.colorType)*e.bitDepth/8))}function eL(e,t){let n=Buffer.from(e,"ascii"),r=Buffer.alloc(8+t.length+4);return r.writeUInt32BE(t.length,0),n.copy(r,4),t.copy(r,8),r.writeUInt32BE(eU(Buffer.concat([n,t])),8+t.length),r}function eU(e){let t=0xffffffff;for(let n of e){t^=n;for(let e=0;e<8;e+=1)t=1&t?0xedb88320^t>>>1:t>>>1}return(0xffffffff^t)>>>0}function eB(e,t){if(!Number.isInteger(e)||e<1)throw Error(`PNG ${t} must be positive`);return e}function eG(e,t){try{return eP.sync.read(e)}catch(e){throw new a("COMMAND_FAILED",`Failed to decode ${t} as PNG`,{label:t,reason:e instanceof Error?e.message:String(e)})}}async function eF(t,n){if(!Number.isInteger(n)||n<1)throw new a("INVALID_ARGS","Screenshot max size must be a positive integer");let r=eG(await e.readFile(t),"screenshot"),i=Math.max(r.width,r.height);if(i<=n)return;let o=n/i,s=Math.max(1,Math.round(r.width*o)),l=Math.max(1,Math.round(r.height*o)),d=function(e,t,n){let r=new eP({width:t,height:n});for(let i=0;i<n;i+=1){let a=i*e.height/n,o=(i+1)*e.height/n;for(let n=0;n<t;n+=1){let s=n*e.width/t,l=(n+1)*e.width/t,d=0,c=0,u=0,h=0,p=0;for(let t=Math.floor(a);t<Math.ceil(o);t+=1){let n=Math.min(t+1,o)-Math.max(t,a);for(let r=Math.floor(s);r<Math.ceil(l);r+=1){let i=n*(Math.min(r+1,l)-Math.max(r,s)),a=(t*e.width+r)*4;d+=(e.data[a]??0)*i,c+=(e.data[a+1]??0)*i,u+=(e.data[a+2]??0)*i,h+=(e.data[a+3]??0)*i,p+=i}}let f=(i*r.width+n)*4;r.data[f]=Math.round(d/p),r.data[f+1]=Math.round(c/p),r.data[f+2]=Math.round(u/p),r.data[f+3]=Math.round(h/p)}}return r}(r,s,l);await e.writeFile(t,eP.sync.write(d))}async function eq(e,t,n){if("path"===t.kind&&!e.policy.allowLocalInputPaths)throw new a("INVALID_ARGS",`Local ${n.field??"input"} paths are not allowed by command policy`);try{return await e.artifacts.resolveInput(t,n)}catch(e){throw i(e)}}async function eX(e,t,n){if(t?.kind==="path"&&!e.policy.allowLocalOutputPaths)throw new a("INVALID_ARGS","Local output paths are not allowed by command policy");try{return await e.artifacts.reserveOutput(t,{...n,visibility:n.visibility??"client-visible",requestedClientPath:t?.kind==="downloadableArtifact"?t.clientPath??n.requestedClientPath:n.requestedClientPath})}catch(e){throw i(e)}}async function eY(e,t){try{return await e.artifacts.createTempFile(t)}catch(e){throw i(e)}}let eV=async(e,t)=>{let n;if(!e.backend.captureScreenshot)throw new a("UNSUPPORTED_OPERATION","screenshot is not supported by this backend");let r=await eX(e,t.out,{field:"path",ext:".png"});try{await e.backend.captureScreenshot({session:t.session,requestId:t.requestId,appId:t.appId,appBundleId:t.appBundleId,signal:t.signal??e.signal,metadata:t.metadata},r.path,{fullscreen:t.fullscreen,overlayRefs:t.overlayRefs,stabilize:t.stabilize,surface:t.surface}),void 0!==t.maxSize&&await eF(r.path,t.maxSize),n=await r.publish()}catch(e){throw await r.cleanup?.(),e}return{path:r.path,...n?{artifacts:[n]}:{},...u(`Saved screenshot: ${r.path}`)}},eH=[0,187,255,255];function ez(e,t,n,r){if(t<0||t>=e.width||n<0||n>=e.height)return;let i=(n*e.width+t)*4;e.data[i]=r[0],e.data[i+1]=r[1],e.data[i+2]=r[2],e.data[i+3]=r[3]}function eK(e,t,n){return Math.min(Math.max(e,t),n)}let ej=[{x:-1,y:-1},{x:0,y:-1},{x:1,y:-1},{x:-1,y:0},{x:1,y:0},{x:-1,y:1},{x:0,y:1},{x:1,y:1}];function eW(e){let{mask:t,width:n,height:r,hooks:i}=e,a=new Uint8Array(t.length),o=new Int32Array(t.length),s=[];for(let e=0;e<t.length;e+=1){var l,d;if(!eJ(t,a,e))continue;let c=0,u=(l=o,a[d=e]=1,l[0]=d,1),h=i.create(e);for(;c<u;){let e=o[c];c+=1,i.visit(h,e),u=function(e){let{mask:t,visited:n,queue:r,width:i,height:a,pixelIndex:o}=e,s=o%i,l=Math.floor(o/i),d=e.queueEnd;for(let e of ej){var c,u,h,p;let o=s+e.x,f=l+e.y;if(c=o,u=f,h=i,p=a,!(c>=0)||!(c<h)||!(u>=0)||!(u<p))continue;let w=f*i+o;eJ(t,n,w)&&(d=function(e,t,n,r){return t[r]=1,e[n]=r,n+1}(r,n,d,w))}return d}({mask:t,visited:a,queue:o,queueEnd:u,width:n,height:r,pixelIndex:e})}s.push(h)}return s}function eJ(e,t,n){return 1===e[n]&&1!==t[n]}function eZ(e){let t=1/0,n=1/0,r=-1/0,i=-1/0;for(let a of e)t=Math.min(t,a.x),n=Math.min(n,a.y),r=Math.max(r,a.x+a.width),i=Math.max(i,a.y+a.height);return{x:t,y:n,width:r-t,height:i-n}}function eQ(e,t){let n=Math.max(e.x,t.x),r=Math.max(e.y,t.y),i=Math.min(e.x+e.width,t.x+t.width),a=Math.min(e.y+e.height,t.y+t.height);return i<=n||a<=r?0:(i-n)*(a-r)}function e0(e){return{x:e.x+e.width/2,y:e.y+e.height/2}}function e1(e,t){return(e.x-t.x)**2+(e.y-t.y)**2}function e2(e,t,n){return Math.min(Math.max(e,t),n)}let e5={icon:90,toggle:90,chevron:75,separator:45,visual:35,background:10},e4={leading:20,trailing:20,separator:10,unknown:0,background:-30};function e8(e){return"background"!==e.likelyKind}function e3(e,t){return e.width>=.25*t.width||e.height>=.06*t.height}function e6(e,t){let n,r=0;for(let i of t){let t=tn(e,i.rect);t<=r||(r=t,n=i)}return n}function e9(e){let t=[];for(let n of[...e].sort((e,t)=>e.rect.y-t.rect.y)){let e=t.find(e=>{var t,r;return t=e.rect,tn(t,r=n.rect)>0||Math.abs(e0(t).y-e0(r).y)<=.5*Math.max(t.height,r.height)});if(!e){t.push({rect:n.rect,blocks:[n]});continue}e.blocks.push(n),e.blocks.sort((e,t)=>e.rect.x-t.rect.x),e.rect=eZ([e.rect,n.rect])}return t}function e7(e,t){let n,r=e0(e);for(let e of t){let t=Math.sqrt(e1(r,e0(e.rect)));n&&t>=n.distance||(n={block:e,distance:t})}return n}function te(e){let t=tt(e);return e.differentPixels>=24&&t.width>=3&&t.height>=3}function tt(e){return{x:e.minX,y:e.minY,width:e.maxX-e.minX+1,height:e.maxY-e.minY+1}}function tn(e,t){return Math.max(0,Math.min(e.y+e.height,t.y+t.height)-Math.max(e.y,t.y))}async function tr(e){if(await h("tesseract"))try{let[t,n]=await Promise.all([ta(e.baselinePath),ta(e.currentPath)]);if(0!==t.exitCode||0!==n.exitCode)return;let r=ti(t.stdout,e.width,e.height),i=ti(n.stdout,e.width,e.height),a=function(e,t){let n=new Set,r=[];for(let a of e){var i;let e=tu(a.text),o=function(e,t,n,r){let i=null,a=1/0;for(let o=0;o<n.length;o+=1){if(r.has(o))continue;let s=n[o];if(tu(s.text)!==t)continue;let l=e1(e0(e.normalizedRect),e0(s.normalizedRect));l>=a||(i=o,a=l)}return i}(a,e,t,n);if(null===o)continue;n.add(o);let s=function(e,t){let n={x:t.rect.x-e.rect.x,y:t.rect.y-e.rect.y,width:t.rect.width-e.rect.width,height:t.rect.height-e.rect.height},r=tf(t.rect.width/e.rect.width),i=tf(t.rect.height/e.rect.height),a=Math.abs(r-1)>=.08||Math.abs(i-1)>=.12;return{text:e.text,baselineRect:e.rect,currentRect:t.rect,delta:n,confidence:Math.round(100*Math.min(e.confidence,t.confidence))/100,possibleTextMetricMismatch:a}}(a,t[o]);i=s,(Math.abs(i.delta.x)>=2||Math.abs(i.delta.y)>=2||Math.abs(i.delta.width)>=2||Math.abs(i.delta.height)>=2||i.possibleTextMetricMismatch)&&r.push(s)}return r.sort((e,t)=>to(t)-to(e)).slice(0,12)}(r,i),o=function(e){let t=[];for(let n of[...e].sort((e,t)=>e.currentRect.y-t.currentRect.y)){let e=t.find(e=>32>=Math.abs(n.delta.x-th(e.map(e=>e.delta.x))));e?e.push(n):t.push([n])}return t.filter(e=>e.length>=2).map(ts).filter(e=>e.yRange.max-e.yRange.min<=60).sort((e,t)=>tl(t)-tl(e)).slice(0,4)}(a);if(0===r.length&&0===i.length)return;return{provider:"tesseract",baselineBlocks:r.length,currentBlocks:i.length,baselineBlocksRaw:r,currentBlocksRaw:i,matches:a,...o.length>0?{movementClusters:o}:{}}}catch{return}}function ti(e,t,n){let[r,...i]=e.split(/\r?\n/);if(!r)return[];let a=new Map(r.split(" ").map((e,t)=>[e,t])),o=[];for(let e of i){var s;if(!e.trim())continue;let t=e.split(" "),n=tc(t,a,"level"),r=td(t,a,"text").trim(),i=tc(t,a,"conf");if(5!==n||(s=r,!/[\p{L}\p{N}]/u.test(s))||i<0)continue;let l=tc(t,a,"left"),d=tc(t,a,"top"),c=tc(t,a,"width"),u=tc(t,a,"height");c<=0||u<=0||o.push({key:[td(t,a,"page_num"),td(t,a,"block_num"),td(t,a,"par_num"),td(t,a,"line_num")].join(":"),text:r,confidence:i,rect:{x:l,y:d,width:c,height:u}})}let l=new Map;for(let e of o){let t=l.get(e.key);t?t.push(e):l.set(e.key,[e])}return Array.from(l.values()).flatMap(e=>(function(e){let t=[...e].sort((e,t)=>e.rect.x-t.rect.x),n=[],r=[];for(let e of t){let t=r.at(-1);if(!t){r.push(e);continue}if(e.rect.x-(t.rect.x+t.rect.width)>Math.max(48,2.5*Math.max(t.rect.height,e.rect.height))){n.push(r),r=[e];continue}r.push(e)}return r.length>0&&n.push(r),n})(e)).map(e=>(function(e,t,n){if(0===e.length)return null;let r=[...e].sort((e,t)=>e.rect.x-t.rect.x),i=eZ(r.map(e=>e.rect)),a=Math.round(100*th(r.map(e=>e.confidence)))/100;return{text:r.map(e=>e.text).join(" "),confidence:a,rect:i,normalizedRect:{x:tp(i.x/t),y:tp(i.y/n),width:tp(i.width/t),height:tp(i.height/n)}}})(e,t,n)).filter(e=>null!==e)}function ta(e){return p("tesseract",[e,"stdout","-l","eng","tsv"],{allowFailure:!0,timeoutMs:1e4})}function to(e){return Math.abs(e.delta.x)+Math.abs(e.delta.y)+Math.abs(e.delta.width)+Math.abs(e.delta.height)+25*!!e.possibleTextMetricMismatch}function ts(e){let t=e.map(e=>e.delta.x),n=e.map(e=>e.delta.y);return{texts:e.map(e=>e.text),xRange:{min:Math.min(...t),max:Math.max(...t)},yRange:{min:Math.min(...n),max:Math.max(...n)}}}function tl(e){return 2*Math.abs((e.xRange.min+e.xRange.max)/2)+Math.abs((e.yRange.min+e.yRange.max)/2)}function td(e,t,n){let r=t.get(n);return void 0===r?"":e[r]??""}function tc(e,t,n){let r=Number(td(e,t,n));return Number.isFinite(r)?r:0}function tu(e){return e.trim().replace(/\s+/g," ").toLowerCase()}function th(e){return e.reduce((e,t)=>e+t,0)/e.length}function tp(e){return Math.round(100*e*100)/100}function tf(e){return Math.round(1e3*e)/1e3}function tw(e,t,n,r){return{r:Math.round(e/r),g:Math.round(t/r),b:Math.round(n/r)}}function tm(e){return .2126*e.r+.7152*e.g+.0722*e.b}function tb(e){return`#${tg(e.r)}${tg(e.g)}${tg(e.b)}`}function tg(e){return e.toString(16).padStart(2,"0")}function ty(e){return Math.round(100*e*100)/100}let tx=255*Math.sqrt(3);async function tv(t,n,i={}){var a,o,s,l,d;let c,u,h,p;await tk(t,"Baseline image not found"),await tk(n,"Current screenshot not found");let f=i.outputPath,[w,m]=await Promise.all([e.readFile(t),e.readFile(n)]),b=eG(w,"baseline screenshot"),g=eG(m,"current screenshot");tI(b.width,b.height,"baseline screenshot",i.maxPixels),tI(g.width,g.height,"current screenshot",i.maxPixels);let y=i.threshold??.1;if(b.width!==g.width||b.height!==g.height){let e=b.width*b.height;return await tM(i.outputPath),{match:!1,mismatchPercentage:100,totalPixels:e,differentPixels:e,dimensionMismatch:{expected:{width:b.width,height:b.height},actual:{width:g.width,height:g.height}}}}let x=b.width*b.height,v=y*tx,k=new eP({width:b.width,height:b.height}),I=new Uint8Array(x),M=0;for(let e=0,t=0;e<b.data.length;e+=4,t+=1){if(Math.sqrt((b.data[e]-g.data[e])**2+(b.data[e+1]-g.data[e+1])**2+(b.data[e+2]-g.data[e+2])**2)>v){M+=1,I[t]=1;let n=tA(g,e);k.data[e]=tN(n,220,.78),k.data[e+1]=tN(n,0,.78),k.data[e+2]=tN(n,0,.78),k.data[e+3]=255;continue}let n=tA(g,e);k.data[e]=n,k.data[e+1]=n,k.data[e+2]=n,k.data[e+3]=255}let A=M>0?(o=(c=function(e){let{diffMask:t,baseline:n,current:r}=e,{width:i,height:a}=n;return eW({mask:t,width:i,height:a,hooks:{create:e=>{var t,n;let r,a;return r=(t=e)%(n=i),{minX:r,minY:a=Math.floor(t/n),maxX:r,maxY:a,differentPixels:0,baselineRed:0,baselineGreen:0,baselineBlue:0,currentRed:0,currentGreen:0,currentBlue:0}},visit:(e,t)=>{var a,o,s,l,d;let c,u,h;return a=e,o=t,s=i,l=n,d=r,c=o%s,u=Math.floor(o/s),h=4*o,void(a.minX=Math.min(a.minX,c),a.minY=Math.min(a.minY,u),a.maxX=Math.max(a.maxX,c),a.maxY=Math.max(a.maxY,u),a.differentPixels+=1,a.baselineRed+=l.data[h],a.baselineGreen+=l.data[h+1],a.baselineBlue+=l.data[h+2],a.currentRed+=d.data[h],a.currentGreen+=d.data[h+1],a.currentBlue+=d.data[h+2])}}})}(a={diffMask:I,baseline:b,current:g,totalPixels:x,differentPixels:M,maxRegions:i.maxRegions})).length<=2e3?function(e){let t=[];for(let i of e.sort((e,t)=>{let n=e.minY-t.minY;return 0!==n?n:e.minX-t.minX})){var n,r;let e=t.find(e=>{var t,n,r;return t=e,n=i,r=12,t.minX-r<=n.maxX&&n.minX-r<=t.maxX&&t.minY-r<=n.maxY&&n.minY-r<=t.maxY});if(!e){t.push({...i});continue}n=e,r=i,n.minX=Math.min(n.minX,r.minX),n.minY=Math.min(n.minY,r.minY),n.maxX=Math.max(n.maxX,r.maxX),n.maxY=Math.max(n.maxY,r.maxY),n.differentPixels+=r.differentPixels,n.baselineRed+=r.baselineRed,n.baselineGreen+=r.baselineGreen,n.baselineBlue+=r.baselineBlue,n.currentRed+=r.currentRed,n.currentGreen+=r.currentGreen,n.currentBlue+=r.currentBlue}return t}(c):c,o.flatMap(e=>{var t,n,r;let i;return(t=e,n=a.baseline.width,r=a.baseline.height,i=t.maxX-t.minX+1,t.maxY-t.minY+1>=Math.max(48,Math.round(.07*r))&&i>=.35*n)?function(e,t,n){var r;let i=function(e,t){let n=[],r=null;for(let i=0;i<e.length;i+=1){if(e[i]<=t){r??=i;continue}null!==r&&(i-r>=6&&n.push([r,i-1]),r=null)}return null!==r&&e.length-r>=6&&n.push([r,e.length-1]),n}((r=function(e,t,n){let r=[];for(let i=e.minY;i<=e.maxY;i+=1){let a=0;for(let r=e.minX;r<=e.maxX;r+=1)1===t[i*n+r]&&(a+=1);r.push(a)}return r}(e,t.diffMask,t.baseline.width)).map((e,t)=>{let n=0,i=0,a=Math.max(0,t-3),o=Math.min(r.length-1,t+3);for(let e=a;e<=o;e+=1)n+=r[e],i+=1;return Math.round(n/i)}),Math.max(1,Math.round((e.maxX-e.minX+1)*.08))),a=function(e,t,n){let r=[],i=e.minY;for(let[a,o]of t){let t=e.minY+Math.round((a+o)/2);t-i+1<n||e.maxY-t<n||(r.push([i,t]),i=t+1)}return r.push([i,e.maxY]),r}(e,i,n);if(a.length<=1)return[e];let o=a.map(([n,r])=>(function(e,t,n,r){let i=null;for(let s=t;s<=n;s+=1)for(let t=e.minX;t<=e.maxX;t+=1){var a,o;let e=s*r.baseline.width+t;1===r.diffMask[e]&&function(e,t,n,r,i,a){let o=4*t;e.minX=Math.min(e.minX,n),e.minY=Math.min(e.minY,r),e.maxX=Math.max(e.maxX,n),e.maxY=Math.max(e.maxY,r),e.differentPixels+=1,e.baselineRed+=i.data[o],e.baselineGreen+=i.data[o+1],e.baselineBlue+=i.data[o+2],e.currentRed+=a.data[o],e.currentGreen+=a.data[o+1],e.currentBlue+=a.data[o+2]}(i??={minX:a=t,minY:o=s,maxX:a,maxY:o,differentPixels:0,baselineRed:0,baselineGreen:0,baselineBlue:0,currentRed:0,currentGreen:0,currentBlue:0},e,t,s,r.baseline,r.current)}return i})(e,n,r,t)).filter(e=>null!==e);return o.length>1?o:[e]}(e,a,Math.max(24,Math.round(.03*a.baseline.height))):[e]})).sort((e,t)=>{let n=t.differentPixels-e.differentPixels;if(0!==n)return n;let r=e.minY-t.minY;return 0!==r?r:e.minX-t.minX}).slice(0,Math.max(0,a.maxRegions??8)).map((e,t)=>{var n,r,i,o,s,l,d,c,u,h,p;let f,w,m,b,g,y,x,v,k,I,M,A,N,R,P,S,D;return n=e,r=t+1,i={width:a.baseline.width,height:a.baseline.height,totalPixels:a.totalPixels,differentPixels:a.differentPixels},y={x:n.minX,y:n.minY,width:n.maxX-n.minX+1,height:n.maxY-n.minY+1},x={x:Math.round(n.minX+y.width/2),y:Math.round(n.minY+y.height/2)},v=tw(n.baselineRed,n.baselineGreen,n.baselineBlue,n.differentPixels),k=tw(n.currentRed,n.currentGreen,n.currentBlue,n.differentPixels),I=y.width*y.height,M=ty(n.differentPixels/I),A=Math.round(tm(v)),N=Math.round(tm(k)),R=(o=y,s=i.width,l=i.height,o.width>=.55*s&&o.height>=.12*l?"large-area":o.width>=2.5*o.height?"horizontal-band":o.height>=2.5*o.width?"vertical-band":"compact"),P=(f=I/i.totalPixels)>=.04?"large":f>=.01?"medium":"small",S=(d=v,c=k,w=tm(d),Math.abs(m=tm(c)-w)>=12?m>0?"brighter":"darker":Math.max(Math.abs(c.r-d.r),Math.abs(c.g-d.g),Math.abs(c.b-d.b))>=12?"color-shift":"mixed"),D=(u=x,h=i.width,p=i.height,b=u.x<h/3?"left":u.x>2*h/3?"right":"center",g=u.y<p/3?"top":u.y>2*p/3?"bottom":"middle","center"===b&&"middle"===g?"center":`${g}-${b}`),{index:r,rect:y,normalizedRect:{x:ty(y.x/i.width),y:ty(y.y/i.height),width:ty(y.width/i.width),height:ty(y.height/i.height)},differentPixels:n.differentPixels,shareOfDiffPercentage:ty(n.differentPixels/i.differentPixels),densityPercentage:M,shape:R,size:P,location:D,averageBaselineColorHex:tb(v),averageCurrentColorHex:tb(k),baselineLuminance:A,currentLuminance:N,dominantChange:S}}):[];if(M>0&&f){for(let e of A)e.rect.width<4||e.rect.height<4||function(e,t){let n=eK(t.x,0,e.width-1),r=eK(t.y,0,e.height-1),i=eK(t.x+t.width-1,0,e.width-1),a=eK(t.y+t.height-1,0,e.height-1);for(let t=0;t<2;t+=1){for(let o=n;o<=i;o+=1)ez(e,o,r+t,eH),ez(e,o,a-t,eH);for(let o=r;o<=a;o+=1)ez(e,n+t,o,eH),ez(e,i-t,o,eH)}}(k,e.rect);await e.mkdir(r.dirname(f),{recursive:!0}),await e.writeFile(f,eP.sync.write(k))}else await tM(i.outputPath);let N=M>0?await tr({baselinePath:t,currentPath:n,width:b.width,height:b.height}):void 0,R=N&&(N.matches.length>0||(N.movementClusters?.length??0)>0)?{provider:N.provider,baselineBlocks:N.baselineBlocks,currentBlocks:N.currentBlocks,matches:N.matches,...N.movementClusters?{movementClusters:N.movementClusters}:{}}:void 0,P=M>0&&N?(u=function(e){let t=[];for(let n of e.sort((e,t)=>e.minY-t.minY||e.minX-t.minX)){let e=t.find(e=>{var t,r,i;return t=e,r=n,i=10,t.minX-i<=r.maxX&&r.minX-i<=t.maxX&&t.minY-i<=r.maxY&&r.minY-i<=t.maxY});if(!e){t.push({...n});continue}e.minX=Math.min(e.minX,n.minX),e.minY=Math.min(e.minY,n.minY),e.maxX=Math.max(e.maxX,n.maxX),e.maxY=Math.max(e.maxY,n.maxY),e.differentPixels+=n.differentPixels}return t}((l=function(e,t,n,r){let i=new Uint8Array(e);if(!r)return i;for(let e of[...r.baselineBlocksRaw,...r.currentBlocksRaw]){var a;!function(e,t,n,r){let i=e2(Math.floor(r.x),0,t-1),a=e2(Math.floor(r.y),0,n-1),o=e2(Math.ceil(r.x+r.width),0,t),s=e2(Math.ceil(r.y+r.height),0,n);for(let n=a;n<s;n+=1)for(let r=i;r<o;r+=1)e[n*t+r]=0}(i,t,n,{x:(a=e.rect).x-8,y:a.y-8,width:a.width+16,height:a.height+16})}return i}((s={diffMask:I,width:b.width,height:b.height,regions:A,ocr:N}).diffMask,s.width,s.height,s.ocr),d=s.width,eW({mask:l,width:d,height:s.height,hooks:{create:e=>{var t,n;let r,i;return r=(t=e)%(n=d),{minX:r,minY:i=Math.floor(t/n),maxX:r,maxY:i,differentPixels:0}},visit:(e,t)=>{var n,r,i;let a,o;return n=e,a=(r=t)%(i=d),o=Math.floor(r/i),void(n.minX=Math.min(n.minX,a),n.minY=Math.min(n.minY,o),n.maxX=Math.max(n.maxX,a),n.maxY=Math.max(n.maxY,o),n.differentPixels+=1)}}}))),h=e9(s.ocr?.currentBlocksRaw??[]),p=e9(s.ocr?.baselineBlocksRaw??[]),u.filter(te).map(e=>{var t,n,r,i,a,o,l,d,c,u,f;let w,m,b,g,y,x,v,k,I,M;return t=e,n=s,r=h,i=p,g=function(e,t){let n,r=0;for(let i of t){let t=eQ(e,i.rect);t<=r||(r=t,n=i)}return n?.index}(b=tt(t),n.regions),y=function(e,t,n){let r=e6(e,t);if(r)return e7(e,r.blocks);let i=e6(e,n);return i?e7(e,i.blocks):void 0}(b,r,i),x=function(e,t,n){if(e.height<=3&&e.width>=.12*n)return"separator";if(!t)return e.width>=.4*n?"background":"unknown";if(e.width>=.4*n)return"background";let r=e.x+e.width/2,i=t.x+t.width/2;return r<i-t.width/2?"leading":r>i+t.width/2?"trailing":e.width>=.35*n?"background":"unknown"}(b,y?.block.rect,n.width),v=(a=b,o=x,l=t.differentPixels,d=n,w=a.width/a.height,m=l/(a.width*a.height),"separator"===o?"separator":"background"===o?"background":"trailing"===o&&w>=1.5&&w<=3.8&&m>=.35?"toggle":"trailing"===o&&a.width<=.06*d.width&&a.height<=.04*d.height?"chevron":"leading"===o&&w>=.55&&w<=1.8?"icon":e3(a,d)?"background":"visual"),k={...g?{regionIndex:g}:{},slot:x,likelyKind:v,rect:b},{...g?{regionIndex:g}:{},slot:x,likelyKind:v,rect:b,...y?{nearestText:y.block.text.trim().replace(/^[^\p{L}\p{N}]+/u,"").replace(/^\p{L}\s+/u,"")}:{},score:(c=k,u=t.differentPixels,f=n,I=e3(c.rect,f)?-35:0,M=20*!!c.regionIndex,e5[c.likelyKind]+e4[c.slot]+M+I+Math.min(20,u/200))}}).filter(e=>e.rect.y>=.08*s.height).filter(e8).sort((e,t)=>t.score-e.score).slice(0,Math.max(0,s.maxDeltas??12)).map((e,t)=>{var n;return n=e,{index:t+1,...n.regionIndex?{regionIndex:n.regionIndex}:{},slot:n.slot,likelyKind:n.likelyKind,rect:n.rect,...n.nearestText?{nearestText:n.nearestText}:{}}})):[],S=x>0?Math.round(M/x*1e4)/100:0;return{...M>0&&f?{diffPath:f}:{},...A.length>0?{regions:A}:{},...R?{ocr:R}:{},...P.length>0?{nonTextDeltas:P}:{},totalPixels:x,differentPixels:M,mismatchPercentage:S,match:0===M}}async function tk(t,n){try{await e.access(t)}catch{throw new a("INVALID_ARGS",`${n}: ${t}`)}}function tI(e,t,n,r){if(null==r||r<=0)return;let i=e*t;if(!(i<=r))throw new a("INVALID_ARGS",`${n} is ${i} pixels, which exceeds the configured maxImagePixels limit of ${r}`)}async function tM(t){if(t)try{await e.unlink(t)}catch(e){var n;if(!("object"==typeof(n=e)&&null!==n&&"code"in n&&"ENOENT"===n.code))throw e}}function tA(e,t){return tN(Math.round(.299*e.data[t]+.587*e.data[t+1]+.114*e.data[t+2]),255,.72)}function tN(e,t,n){return Math.round(e*(1-n)+t*n)}function tR(e){return e.width*e.height}function tP(e){return Math.round(100*e*100)/100}async function tS(e,t){let n=await eY(e,{prefix:"agent-device-diff-current",ext:".png"});try{await tE(e,t,n.path,tO(t))}catch(e){throw await n.cleanup(),e}return n}async function tD(e,t,n,r,i){var a,o,s,l;if(!t.overlayRefs)return r;if(r.match||r.dimensionMismatch)return n&&await tT(n),r;let d=(a=t,o=n,a.currentOverlayOut?a.currentOverlayOut:a.out?.kind==="path"?{kind:"path",path:t_(o??a.out.path)}:a.out?.kind==="downloadableArtifact"?{kind:"downloadableArtifact",...a.out.clientPath?{clientPath:t_(a.out.clientPath)}:{},...a.out.fileName?{fileName:t_(a.out.fileName)}:{}}:void 0),c=await eX(e,d,{field:"currentOverlayPath",ext:".png"});try{let n=await tE(e,t,c.path,{overlayRefs:!0,...tO(t)}),a=await c.publish();return a&&i.push(a),{...r,currentOverlayPath:n.path??c.path,...n.overlayRefs?{currentOverlayRefCount:n.overlayRefs.length}:{},...r.regions&&n.overlayRefs?{regions:(s=r.regions,l=n.overlayRefs,s.map(e=>{var t,n;let r,i=(t=e,n=l,r=tR(t.rect),n.map(e=>{let n=e.overlayRect,i=eQ(t.rect,n);return i<=0?null:{ref:e.ref,...e.label?{label:e.label}:{},rect:n,overlayCoveragePercentage:tP(i/tR(n)),regionCoveragePercentage:tP(i/r)}}).filter(e=>null!==e).sort((e,t)=>{let n=t.regionCoveragePercentage-e.regionCoveragePercentage;return 0!==n?n:t.overlayCoveragePercentage-e.overlayCoveragePercentage}).slice(0,3).map(e=>({ref:e.ref,...e.label?{label:e.label}:{},rect:e.rect,regionCoveragePercentage:e.regionCoveragePercentage})));return i.length>0?{...e,currentOverlayMatches:i}:e}))}:{}}}catch(e){throw await c.cleanup?.(),e}}async function tE(e,t,n,r={}){if(!e.backend.captureScreenshot)throw new a("UNSUPPORTED_OPERATION","screenshot is not supported by this backend");return await e.backend.captureScreenshot({session:t.session,requestId:t.requestId,signal:t.signal??e.signal,metadata:t.metadata},n,r)??{}}function tO(e){return e.surface?{surface:e.surface}:{}}function t_(e){let t=r.extname(e),n=t?e.slice(0,-t.length):e;return`${n}.current-overlay${t||".png"}`}async function tT(t){try{await e.unlink(t_(t))}catch(e){var n;if(!("object"==typeof(n=e)&&null!==n&&"code"in n&&"ENOENT"===n.code))throw e}}function t$(e){return"live"===e.kind}let tC={application:"application",navigationbar:"navigation-bar",tabbar:"tab-bar",button:"button",imagebutton:"button",link:"link",cell:"cell",statictext:"text",checkedtextview:"text",textfield:"text-field",edittext:"text-field",textarea:"text-view",switch:"switch",slider:"slider",image:"image",imageview:"image",webview:"webview",framelayout:"group",linearlayout:"group",relativelayout:"group",constraintlayout:"group",viewgroup:"group",view:"group",listview:"list",recyclerview:"list",collectionview:"collection",searchfield:"search",segmentedcontrol:"segmented-control",group:"group",window:"window",checkbox:"checkbox",radio:"radio",menuitem:"menu-item",toolbar:"toolbar",scrollarea:"scroll-area",scrollview:"scroll-area",nestedscrollview:"scroll-area",table:"table"};function tL(e,t={}){let n=[],r=[];for(let i of e){let e=i.depth??0,a=i.label?.trim()||i.value?.trim()||i.identifier?.trim()||"",o=tG(i.type??"Element");if("group"===o&&!a)continue;for(;n.length>0&&e<=n[n.length-1];)n.pop();let s=n.length;n.push(e),r.push({node:i,depth:s,type:o,text:tU(i,s,!1,o,t)})}return r}function tU(e,t,n,r,i={}){var a,o,s,l,d,c,u,h;let p,w=r??tG(e.type??"Element"),m=P(e,w),b=(a=e,o=w,s=i,l=m,s.summarizeTextSurfaces&&l.shouldSummarize&&function(e,t,n){let r=f(e.label);if(r&&r!==n)return r;let i=f(e.identifier);if(i&&!tq(i)&&i!==n)return i;switch(t){case"text":case"text-view":return"Text view";case"text-field":return"Text field";case"search":return"Search field";default:return""}}(a,o,l.text)||tB(a,o)),g=" ".repeat(t),y=e.ref?`@${e.ref}`:"",x=(d=e,c=w,u=i,h=m,p=[],(!1===d.enabled&&p.push("disabled"),u.summarizeTextSurfaces)?(!0===d.selected&&p.push("selected"),!0===d.focused&&p.push("focused"),tF(c)&&p.push("editable"),function(e,t){if("scroll-area"===t)return!0;let n=(e.type??"").toLowerCase(),r=`${e.role??""} ${e.subrole??""}`.toLowerCase();return n.includes("scroll")||r.includes("scroll")}(d,c)&&p.push("scrollable"),p.push(...d.presentationHints??[]),h.shouldSummarize&&(p.push(`preview:"${v(h.text).replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`),p.push("truncated")),tX(p)):p).map(e=>` [${e}]`).join(""),k=b?` "${b}"`:"";return n?`${g}${y} [${w}]${x}`.trimEnd():`${g}${y} [${w}]${k}${x}`.trimEnd()}function tB(e,t){var n,r;let i=e.label?.trim();if(i&&(n=t,r=i,("scroll-area"===n||"list"===n||"collection"===n||"table"===n)&&Q(r)))return"";let a=e.value?.trim();if(tF(t)){if(a)return a;if(i)return i}else if(i)return i;if(a)return a;let o=e.identifier?.trim();return!o||tq(o)&&("group"===t||"image"===t||"list"===t||"collection"===t)?"":o}function tG(e){var t;let n=e.replace(/XCUIElementType/gi,"").toLowerCase(),r=e.includes(".")&&(e.startsWith("android.")||e.startsWith("androidx.")||e.startsWith("com."));return(n.includes(".")&&(n=n.replace(/^android\.widget\./,"").replace(/^android\.view\./,"").replace(/^android\.webkit\./,"").replace(/^androidx\./,"").replace(/^com\.google\.android\./,"").replace(/^com\.android\./,""),r&&n.includes(".")&&(n=n.slice(n.lastIndexOf(".")+1))),"textview"===n)?r?"text":"text-view":(t=n,(Object.prototype.hasOwnProperty.call(tC,t)?tC[t]:void 0)||n||"element")}function tF(e){return"text-field"===e||"text-view"===e||"search"===e}function tq(e){return/^[\w.]+:id\/[\w.-]+$/i.test(e)}function tX(e){return[...new Set(e)]}function tY(e,t){let n=tG(e.type??"Element"),r=tB(e,n),i=!1===e.enabled?"disabled":"enabled",a=!0===e.selected?"selected":"unselected",o=!0===e.hittable?"hittable":"not-hittable";return[String(t??e.depth??0),n,r,i,a,o].join("|")}function tV(e,t){return t.flatten?e.map(e=>({text:tU(e,0,!1),comparable:tY(e,0)})):tL(e).map(e=>({text:e.text,comparable:tY(e.node,e.depth)}))}function tH(e,t){return e.get(t)??0}function tz(e){let t=e.map(e=>[e.label,e.value,e.identifier,e.type,e.role].filter(Boolean).join(" ")).join("\n").toLowerCase(),n=tZ(e,tj),r=tZ(e,tW),i=tZ(e,ek,tJ),a=tZ(e,eI),o=tQ(n),s=tQ(r),l=tQ(i),d=/\b[\w.$<>/-]+\.(?:tsx?|jsx?):\d+(?::\d+)?\b/.test(t)||/\b[\w.$<>/-]+\.(?:tsx?|jsx?)\s+\(\d+:\d+\)/.test(t),c=o.length>0||s.length>0||/\b(reload js|copy stack)\b/.test(t),u=/\b(redbox|runtime error|reload js|copy stack|component stack|call stack)\b/.test(t)||d&&c;return{detected:l.length>0||a.length>0||c&&(/\b(logbox|redbox|reload js|copy stack|component stack|call stack|runtime error|open debugger to view warnings)\b/.test(t)||d),redBox:u,dismissRefs:o,minimizeRefs:s,collapsedRefs:l,dismissNodes:n,minimizeNodes:r,collapsedNodes:i}}function tK(e){let t,n=tz(e);if(!n.detected)return null;if(n.redBox){let e=t0(n.minimizeNodes);if(e)return t1(e,"minimize");let t=t0(n.dismissNodes);return t?{...t1(t,t2(t)),warning:"RedBox Minimize control was not exposed; used Dismiss fallback"}:null}let r=t0(n.dismissNodes);if(r)return t1(r,t2(r));let i=0===(t=n.collapsedNodes.filter(e=>e.rect)).length?null:t.sort((e,t)=>{let n=+(!0===e.hittable),r=+(!0===t.hittable);if(n!==r)return r-n;let i=e.rect?.width??0,a=t.rect?.width??0;return i!==a?a-i:(t.rect?.y??0)-(e.rect?.y??0)})[0]??null;return i?.rect?{action:"close-collapsed-banner",point:function(e){var t,n;if(!e.rect)throw Error("Collapsed React Native warning node must have rect");let r=Math.min(e.rect.height,52),i=Math.min(36,Math.max(18,.45*r));return{x:Math.round((t=e.rect.x+e.rect.width-i,n=e.rect.x+1,Math.min(e.rect.x+e.rect.width-1,Math.max(n,t)))),y:Math.round(e.rect.y+r/2)}}(i),ref:i.ref,label:t5(i)}:null}function tj(e){return"dismiss"===e||"close"===e||["x","\xd7","✕","✖","⨯"].includes(e)}function tW(e){return/^minimi[sz]e$/.test(e)}function tJ(e){return!e.rect||e.rect.height<=180}function tZ(e,t,n=()=>!0){let r=[];for(let i of e)i.ref&&n(i)&&[i.label,i.value,i.identifier].map(e=>e?.trim().toLowerCase()).filter(e=>!!e).some(e=>t(e))&&r.push(i);return r}function tQ(e){return e.map(e=>e.ref)}function t0(e){return e.find(e=>e.rect)??null}function t1(e,t){if(!e.rect)throw Error("React Native overlay target node must have rect");return{action:t,point:s(e.rect),ref:e.ref,label:t5(e)}}function t2(e){return"dismiss"===t5(e)?.trim().toLowerCase()?"dismiss":"close"}function t5(e){return e.label??e.value??e.identifier}function t4(e){return e.map(e=>({index:e.index,depth:e.depth,parentIndex:e.parentIndex,type:e.type,role:e.role,subrole:e.subrole,label:e.label,value:e.value,identifier:e.identifier,enabled:e.enabled,selected:e.selected,focused:e.focused,hittable:e.hittable,rect:e.rect,bundleId:e.bundleId,appName:e.appName,windowTitle:e.windowTitle,surface:e.surface,hiddenContentAbove:e.hiddenContentAbove,hiddenContentBelow:e.hiddenContentBelow}))}function t8(e,t){return{session:t.session,requestId:t.requestId,signal:t.signal??e.signal,metadata:t.metadata}}function t3(e){return e.clock?.now()??Date.now()}async function t6(e,t){e.clock?await e.clock.sleep(t):await new Promise(e=>setTimeout(e,t))}async function t9(e,t){var n,r,i,o,s,d;let c,u,h,p;if(!e.backend.captureSnapshot)throw new a("UNSUPPORTED_OPERATION","snapshot is not supported by this backend");let f=t.session??"default",w=await e.sessions.get(f),m=await e.backend.captureSnapshot({session:f,requestId:t.requestId,appId:w?.appId,appBundleId:w?.appBundleId,signal:t.signal??e.signal,metadata:t.metadata},{interactiveOnly:t.interactiveOnly,compact:t.compact,depth:t.depth,scope:t.scope,raw:t.raw}),b=(r=m,i=e,n=r.snapshot?r.snapshot:{nodes:r.nodes??[],truncated:r.truncated,backend:r.backend,createdAt:t3(i)},n.presentationKey?n:{...n,presentationKey:l(t)}),g=t3(e);return{snapshot:b,result:m,session:w,warnings:((c=[...(o={result:m,snapshot:b,options:t,session:w,capturedAt:b.createdAt??g,runtimeNow:g}).result.warnings??[]]).push(...function(e){let t=e.result.analysis;if("android"!==e.snapshot.backend||!0!==e.options.interactiveOnly||e.snapshot.nodes.length>0||!t||(t.rawNodeCount??0)<12)return[];let n=[`Interactive snapshot is empty after filtering ${t.rawNodeCount} raw Android nodes. Likely causes: the app content is not accessibility-visible yet, a transient route change, or depth/filter options hid the target.`];return"number"==typeof e.options.depth&&"number"==typeof t.maxDepth&&t.maxDepth>=e.options.depth+2&&n.push(`Interactive output is empty at depth ${e.options.depth}; retry without -d.`),n}(o)),(u=function(e){if(e?.backend!=="uiautomator-dump")return;let t=e.fallbackReason?` Reason: ${e.fallbackReason}`:"";return`Android snapshot helper unavailable; using stock UIAutomator dump, which can time out on busy React Native UIs.${t}`}(o.result.androidSnapshot))&&c.push(u),(h=function(e){if(tz(e).detected)return"Hint: React Native warning/error overlay detected. It overlays part of the app and should be handled before interacting.\nRun: agent-device react-native dismiss-overlay\nThen run: agent-device snapshot -i -c\nUse refs from the new snapshot."}(o.snapshot.nodes))&&c.push(h),(p=function(e){var t,n;let r=e.session?.snapshot,i=!!r&&[e.capturedAt,e.runtimeNow].some(e=>{let t=e-r.createdAt;return t>=0&&t<=2e3});if(!e.result.freshness&&r&&i&&(t=r.nodes.length,n=e.snapshot.nodes.length,!(t<12)&&n<=Math.floor(.2*t)))return ne}(o))&&c.push(p),c.push(...(s=o.result.freshness,d=o.snapshot.backend,s?.staleAfterRetries&&"android"===d?"stuck-route"===s.reason?[`Recent ${s.action} was followed by a nearly identical snapshot after ${s.retryCount} automatic retr${1===s.retryCount?"y":"ies"}. If you expected navigation or submit, the tree may still be stale. Use screenshot as visual truth, wait briefly, then re-snapshot once.`]:"sharp-drop"===s.reason?[ne]:[]:[])),Array.from(new Set(c)))}}function t7(e,t){let n=t.session?.name??e??"default";return{...t.session??{name:n},name:n,snapshot:t.snapshot,appName:t.result.appName??t.session?.appName,appBundleId:t.result.appBundleId??t.session?.appBundleId}}let ne="Recent snapshots dropped sharply in node count, which suggests stale or mid-transition UI. Use screenshot as visual truth, wait briefly, then re-snapshot once.";function nt(e){return["visible","hidden","exists","editable","selected","text"].includes(e)}function nn(e){let{predicate:t,node:n,nodes:r,expectedText:i,platform:a}=e,o=w(n),s=x(n,a),l=!0===n.selected,d="text"===t?g(n):function(e,t,n){var r,i,a;let o;if("android"===n&&!1===e.visibleToUser)return!1;if(nr(e.rect))return function(e,t){return es(e,t)}(e,t);if(e.rect)return!1;if("android"!==n&&!0===e.hittable)return!0;let s=(r=e,i=t,a=n,o=y(i),m(i,r,o,e=>!function(e,t){if("android"===t&&!1===e.visibleToUser)return!1;let n=E(e.type??"");return!(n.includes("application")||n.includes("window")||n.includes("scrollview")||n.includes("tableview")||n.includes("collectionview"))&&"table"!==n&&"list"!==n&&"listview"!==n&&("android"===t?!0===e.hittable&&nr(e.rect):!0===e.hittable||nr(e.rect))}(e,a)?null:e));return!!s&&(nr(s.rect)?function(e,t){return es(e,t)}(s,t):"android"!==n&&!0===s.hittable)}(n,r,a),c=!1;switch(t){case"visible":c=d;break;case"hidden":c=!d;break;case"editable":c=s;break;case"selected":c=l;break;case"text":c=o===(i??"")}let u="text"===t?`expected="${i??""}" actual="${o}"`:`actual=${JSON.stringify({visible:d,editable:s,selected:l})}`;return{pass:c,actualText:o,details:u}}function nr(e){return!!(e&&Number.isFinite(e.x)&&Number.isFinite(e.y)&&Number.isFinite(e.width)&&Number.isFinite(e.height)&&e.width>0&&e.height>0)}async function ni(e,t){let n=t??"default",r=await e.sessions.get(n);if(!r)throw new a("SESSION_NOT_FOUND","No active session. Run open first.");if(!r.snapshot)throw new a("INVALID_ARGS","No snapshot in session. Run snapshot first.");return{sessionName:n,session:r,snapshot:r.snapshot}}async function na(e,t,n={updateSession:!0}){let r=e.backend.captureSnapshot;if(!r)throw new a("UNSUPPORTED_OPERATION","snapshot is not supported by this backend");let i=t.session??"default",o=await e.sessions.get(i),s=await r(t8(e,t),{interactiveOnly:!1,compact:!1,depth:t.depth,scope:n.scope??t.scope,raw:t.raw}),l=s.snapshot??{nodes:s.nodes??[],truncated:s.truncated,backend:s.backend,createdAt:t3(e)};return n.updateSession&&o&&await e.sessions.set({...o,snapshot:l}),{sessionName:i,session:o,snapshot:l}}async function no(e,t,n){if(e.backend.readText){let r=await e.backend.readText(t8(e,{session:t.sessionName}),n);if(r.text.trim())return r.text}return S(n)}let ns=async(e,t)=>{if("ref"===t.target.kind){let n=await ni(e,t.session),r=function(e,t,n){let r=o(t);if(!r)throw new a("INVALID_ARGS",n.invalidRefMessage);let i=d(e,r)??(n.fallbackLabel.length>0?N(e,n.fallbackLabel):null);if(!i)throw new a("COMMAND_FAILED",n.notFoundMessage);return{ref:r,node:i}}(n.snapshot.nodes,t.target.ref,{fallbackLabel:t.target.fallbackLabel??"",invalidRefMessage:"get text requires a ref like @e2",notFoundMessage:`Ref ${t.target.ref} not found`}),i=I(r.node,e.backend.platform,{action:"get"}),s={kind:"ref",ref:`@${r.ref}`};return"attrs"===t.property?{kind:"attrs",target:s,node:r.node,selectorChain:i}:{kind:"text",target:s,text:await no(e,n,r.node),node:r.node,selectorChain:i}}let n=await nx(e,t,t.session??"default",{selector:t.target.selector,disambiguateAmbiguous:"text"===t.property}),r=I(n.node,e.backend.platform,{action:"get"});if("attrs"===t.property)return{kind:"attrs",target:{kind:"selector",selector:n.selector},node:n.node,selectorChain:r};let i=await no(e,n.capture,n.node);return{kind:"text",target:{kind:"selector",selector:n.selector},text:i,node:n.node,selectorChain:r}},nl=async(e,t)=>{let n=await ns(e,{...t,property:"text",target:t.target});if("text"!==n.kind)throw new a("COMMAND_FAILED","getText returned non-text result");return n},nd=async(e,t)=>{let n=await ns(e,{...t,property:"attrs",target:t.target});if("attrs"!==n.kind)throw new a("COMMAND_FAILED","getAttrs returned non-attrs result");return n},nc=async(e,t)=>{if(!nt(t.predicate))throw new a("INVALID_ARGS","is requires predicate: visible|hidden|exists|editable|selected|text");if("text"===t.predicate&&!t.expectedText)throw new a("INVALID_ARGS","is text requires expected text value");let n=await na(e,t,{updateSession:!0}),r=b(t.selector);if("exists"===t.predicate){let i=M(n.snapshot.nodes,r,{platform:e.backend.platform});if(!i)throw new a("COMMAND_FAILED",D(r,[],{unique:!1}));return{predicate:t.predicate,pass:!0,selector:i.selector.raw,matches:i.matches,selectorChain:r.selectors.map(e=>e.raw)}}let i=A(n.snapshot.nodes,r,{platform:e.backend.platform,requireRect:!1,requireUnique:!0,disambiguateAmbiguous:!1});if(!i)throw new a("COMMAND_FAILED",D(r,[],{unique:!0}),{command:"is",reason:"selector_not_found",predicate:t.predicate,selector:r.raw});let o=nn({predicate:t.predicate,node:i.node,nodes:n.snapshot.nodes,expectedText:t.expectedText,platform:e.backend.platform});if(!o.pass)throw new a("COMMAND_FAILED",`is ${t.predicate} failed for selector ${i.selector.raw}: ${o.details}`,{command:"is",reason:"predicate_failed",predicate:t.predicate,selector:i.selector.raw,predicateDetails:o.details});return{predicate:t.predicate,pass:!0,selector:i.selector.raw,..."text"===t.predicate?{text:o.actualText}:{},selectorChain:r.selectors.map(e=>e.raw)}},nu=async(e,t)=>await nc(e,{...t,predicate:"visible",selector:t.target.selector}),nh=async(e,t)=>await nc(e,{...t,predicate:"hidden",selector:t.target.selector}),np=async(e,t)=>{if("sleep"===t.target.kind)return await t6(e,t.target.durationMs),{kind:"sleep",waitedMs:t.target.durationMs};if("ref"===t.target.kind){let n=await ni(e,t.session),r=o(t.target.ref);if(!r)throw new a("INVALID_ARGS",`Invalid ref: ${t.target.ref}`);let i=d(n.snapshot.nodes,r),s=i?R(i,n.snapshot.nodes):void 0;if(!s)throw new a("COMMAND_FAILED",`Ref ${t.target.ref} not found or has no label`);return await ng(e,t,s,t.target.timeoutMs)}if("selector"===t.target.kind)return await nb(e,t,t.target.selector,t.target.timeoutMs);if(!t.target.text)throw new a("INVALID_ARGS","wait requires text");return await ng(e,t,t.target.text,t.target.timeoutMs)},nf=async(e,t)=>{let n=await np(e,{...t,target:{kind:"text",text:t.text,timeoutMs:t.timeoutMs}});if("text"!==n.kind)throw new a("COMMAND_FAILED","waitForText returned non-text result");return n};async function nw(e,t,n){let r=t.timeoutMs??1e4,i=t3(e);for(;t3(e)-i<r;){let{match:r}=await nm(e,t,n);if(r)return{kind:"found",found:!0,waitedMs:t3(e)-i};await t6(e,300)}throw new a("COMMAND_FAILED","find wait timed out")}async function nm(e,t,n){let r=await na(e,t,{updateSession:!0,scope:"text"===n||"label"===n||"any"===n?t.query:void 0}),i=_(r.snapshot.nodes,n,t.query,{requireRect:!1}).matches[0];return{capture:r,match:i}}async function nb(e,t,n,r){let i=r??1e4,o=t3(e),s=b(n);for(;t3(e)-o<i;){let n=M((await na(e,t,{updateSession:!0})).snapshot.nodes,s,{platform:e.backend.platform});if(n)return{kind:"selector",selector:n.selector.raw,waitedMs:t3(e)-o};await t6(e,300)}throw new a("COMMAND_FAILED",`wait timed out for selector: ${n}`)}async function ng(e,t,n,r){let i=r??1e4,o=t3(e);for(;t3(e)-o<i;){if(e.backend.findText?(await e.backend.findText(t8(e,t),n)).found:await ny(e,t,n))return{kind:"text",text:n,waitedMs:t3(e)-o};await t6(e,300)}throw new a("COMMAND_FAILED",`wait timed out for text: ${n}`)}async function ny(e,t,n){return!!N((await na(e,t,{updateSession:!0})).snapshot.nodes,n)}async function nx(e,t,n,r){let i=await na(e,{...t,session:n},{updateSession:!0}),o=b(r.selector),s=A(i.snapshot.nodes,o,{platform:e.backend.platform,requireRect:!1,requireUnique:!0,disambiguateAmbiguous:r.disambiguateAmbiguous});if(!s)throw new a("COMMAND_FAILED",D(o,[],{unique:!0}));return{capture:i,node:s.node,selector:s.selector.raw,ref:`@${s.node.ref}`}}function nv(e){let t=nk(e);if(!t)return null;let n=s(t);return Number.isFinite(n.x)&&Number.isFinite(n.y)?n:null}function nk(e){if(!e)return null;let t=Number(e.x),n=Number(e.y),r=Number(e.width),i=Number(e.height);return Number.isFinite(t)&&Number.isFinite(n)&&Number.isFinite(r)&&Number.isFinite(i)&&!(r<0)&&!(i<0)?{x:t,y:n,width:r,height:i}:null}function nI(e){return{x:nM(e.x,e.width),y:nM(e.y,e.height)}}function nM(e,t){if(t<=1)return Math.floor(e);let n=Math.ceil(e);return Math.round(Math.min(Math.floor(e+t-1),Math.max(n,e+t/2)))}let nA=["button","link","menuitem","tabitem","textfield","searchfield","securetextfield","checkbox","radio","switch","cell"];function nN(e,t){return nR(e,t).node}function nR(e,t){var n;let r=function(e,t){let n=nk(t.rect);if(!n)return null;let r=t,i=new Set;for(;!i.has(r.ref);){i.add(r.ref);let t=e.filter(e=>{if(e.parentIndex!==r.index||!e.hittable)return!1;let t=nk(e.rect);return!!t&&nS(t,n)});if(1!==t.length)break;let a=t[0];if(void 0===a)break;r=a}return r===t?null:r}(e,t);if(r?.rect&&nv(r.rect))return{node:r,reason:"same-rect-descendant"};if([(n=t).type,n.role,n.subrole].map(e=>E(e??"")).some(nP)&&t.rect&&nv(t.rect))return{node:t,reason:"semantic-target"};let i=k(e,t);return i?.rect&&nv(i.rect)?!function(e,t,n){var r,i,a,o;let l,d,c,u=nk(e.rect),h=nk(t.rect);if(!u||!h)return!1;let p=function(e,t){let n=s(t),r=e.filter(e=>{let t=(e.type??"").toLowerCase();return t.includes("application")||t.includes("window")}).map(e=>nk(e.rect)).filter(e=>null!==e);if(0===r.length)return null;let i=r.filter(e=>W(e,n.x,n.y));return J(i.length>0?i:r)}(n,u);return!!p&&(r=h,i=p,l=(a=r,o=i,Math.max(0,Math.min(a.x+a.width,o.x+o.width)-Math.max(a.x,o.x))*Math.max(0,Math.min(a.y+a.height,o.y+o.height)-Math.max(a.y,o.y))),d=r.width*r.height,c=i.width*i.height,!(l<=0)&&!(d<=0)&&!(c<=0)&&!!(l/c>=.9)&&!!(l/d>=.8))&&!nS(u,h)}(t,i,e)?{node:i,reason:"hittable-ancestor"}:{node:t,reason:"overly-broad-ancestor"}:{node:t,reason:"original"}}function nP(e){return"tab"===e||nA.some(t=>e.includes(t))}function nS(e,t){return .5>=Math.abs(e.x-t.x)&&.5>=Math.abs(e.y-t.y)&&.5>=Math.abs(e.width-t.width)&&.5>=Math.abs(e.height-t.height)}async function nD(e,t,n){if(await nO(e,t,n.action),"point"===t.target.kind)return{kind:"point",point:{x:t.target.x,y:t.target.y}};if("ref"===t.target.kind){let r=await nT(e,t,t.target),i=r.resolved,s=n.promoteToHittableAncestor?nN(r.snapshot.nodes,i.node):i.node;return function(e,t,n,r){let i=e.rect?el(e,t):null;if(!(!e.rect||!i||es(e,t)))throw new a("COMMAND_FAILED",`Ref ${n} is off-screen and not safe to ${r}`,{reason:"offscreen_ref",ref:o(n),rect:e.rect,viewport:i,hint:`Use scroll with the direction from the off-screen summary, take a fresh snapshot, then retry ${r} with the new ref or a selector.`})}(s,r.snapshot.nodes,t.target.ref,n.action),{kind:"ref",point:nC(s,`Ref ${t.target.ref} not found or has invalid bounds`),target:{kind:"ref",ref:`@${i.ref}`},node:s,selectorChain:I(s,e.backend.platform,{action:"fill"===n.action?"fill":"click"}),refLabel:R(s,r.snapshot.nodes)}}let r=await nE(e,t,n.requireInteractive),i=b(t.target.selector),s=A(r.snapshot.nodes,i,{platform:e.backend.platform,requireRect:!0,requireUnique:!0,disambiguateAmbiguous:!0});if(!s||!s.node.rect)throw new a("COMMAND_FAILED",D(i,s?.diagnostics??[],{unique:!0}));let l=n.promoteToHittableAncestor?nN(r.snapshot.nodes,s.node):s.node;return{kind:"selector",point:nC(l,`Selector ${s.selector.raw} resolved to invalid bounds`),target:{kind:"selector",selector:s.selector.raw},node:l,selectorChain:I(l,e.backend.platform,{action:"fill"===n.action?"fill":"click"}),refLabel:R(l,r.snapshot.nodes)}}async function nE(e,t,n){if(!e.backend.captureSnapshot)throw new a("UNSUPPORTED_OPERATION","snapshot is not supported by this backend");let r=t.session??"default",i=await e.sessions.get(r);if(!i)throw new a("SESSION_NOT_FOUND","No active session. Run open first.");let o=await e.backend.captureSnapshot(t8(e,t),{interactiveOnly:n,compact:n}),s=o.snapshot??{nodes:o.nodes??[],truncated:o.truncated,backend:o.backend,createdAt:t3(e)};return await e.sessions.set({...i,snapshot:s}),{snapshot:s}}async function nO(e,t,n){if("macos"!==e.backend.platform)return;let r=await n_(e,t);if(("desktop"===r||"menubar"===r)&&("menubar"!==r||"click"!==n&&"press"!==n))throw new a("UNSUPPORTED_OPERATION",`${n} is not supported on macOS ${r} sessions yet. Open an app session to act, or use the ${r} surface to inspect.`)}async function n_(e,t){let n=await e.sessions.get(t.session??"default");return n?.metadata?.surface}async function nT(e,t,n){let r=t.session??"default",i=await e.sessions.get(r);if(!i)throw new a("SESSION_NOT_FOUND","No active session. Run open first.");if(!i.snapshot)throw new a("INVALID_ARGS","No snapshot in session. Run snapshot first.");let o=n.fallbackLabel??"",s=n$(i.snapshot.nodes,n.ref,{fallbackLabel:o,requireRect:!0});if(s)return{snapshot:i.snapshot,resolved:s};let l=await nE(e,t,!0),d=n$(l.snapshot.nodes,n.ref,{fallbackLabel:o,requireRect:!0});if(!d)throw new a("COMMAND_FAILED",`Ref ${n.ref} not found or has no bounds`);return{...l,resolved:d}}function n$(e,t,n){let r=o(t);if(!r)throw new a("INVALID_ARGS",`Invalid ref: ${t}`);let i=d(e,r);if(nL(i,n.requireRect))return{ref:r,node:i};let s=n.fallbackLabel.length>0?N(e,n.fallbackLabel):null;return nL(s,n.requireRect)?{ref:r,node:s}:null}function nC(e,t){if(!e.rect)throw new a("COMMAND_FAILED",t);let n=s(e.rect);if(!Number.isFinite(n.x)||!Number.isFinite(n.y))throw new a("COMMAND_FAILED",t);return n}function nL(e,t){if(!e)return!1;if(!t)return!0;if(!e.rect)return!1;let{x:n,y:r,width:i,height:a}=e.rect;if(!Number.isFinite(Number(n))||!Number.isFinite(Number(r))||!Number.isFinite(Number(i))||!Number.isFinite(Number(a))||0>Number(i)||0>Number(a))return!1;let o=s(e.rect);return Number.isFinite(o.x)&&Number.isFinite(o.y)}async function nU(e,t,n){let r;if(t.from||t.to||t.direction||void 0!==t.distance)throw new a("INVALID_ARGS","gesture swipe preset cannot be combined with from, to, direction, or distance");let i=q(t.preset);await nO(e,t,"swipe");let o=F(i,{referenceWidth:(r=nV((await nE(e,t,!1)).snapshot.nodes)).width,referenceHeight:r.height},{platform:e.backend.platform}),s=void 0===t.durationMs?void 0:ev(t.durationMs,"durationMs",16,1e4),l={x:o.x1,y:o.y1},d={x:o.x2,y:o.y2},c=nz(await n(t8(e,t),l,d,{durationMs:s}));return{kind:"swipe",from:l,to:d,preset:i,...void 0!==s?{durationMs:s}:{},fromTarget:{kind:"viewport"},...c?{backendResult:c}:{},...u(`Swiped ${i}`)}}async function nB(e,t){let n=t.target??{kind:"viewport"};return"viewport"===n.kind?(await nO(e,t,"scroll"),{kind:"viewport"}):await nD(e,{...t,target:n},{action:"scroll",requireInteractive:!1,promoteToHittableAncestor:!1})}async function nG(e,t){if(t.from){var n;if("x"in(n=t.from)&&"y"in n)return await nO(e,t,"swipe"),{point:nX(t.from,"from")};let r=await nD(e,{...t,target:t.from},{action:"swipe",requireInteractive:!1,promoteToHittableAncestor:!1});return{point:r.point,target:r}}if(!t.direction)throw new a("INVALID_ARGS","swipe requires from+to or a direction");return await nO(e,t,"swipe"),{point:s(nV((await nE(e,t,!1)).snapshot.nodes)),target:{kind:"viewport"}}}async function nF(e,t,n,r,i){if(!e.backend.captureSnapshot)throw new a("UNSUPPORTED_OPERATION",`scroll ${n} requires snapshot support to verify hidden content before scrolling`);let{captureSnapshot:o}=e.backend;return await eh({edge:n,target:r,scope:i,captureNodes:async n=>{let r=await o(t8(e,t),{compact:!0,scope:n});return r.snapshot?.nodes??r.nodes??[]}})}function nq(e,t){switch(e){case"up":case"down":case"left":case"right":return e;default:throw new a("INVALID_ARGS",`${t} must be up, down, left, or right`)}}function nX(e,t){let n=Number(e.x),r=Number(e.y);if(!Number.isFinite(n)||!Number.isFinite(r))throw new a("INVALID_ARGS",`${t} point requires finite x and y`);return{x:n,y:r}}function nY(e,t){if(!Number.isFinite(e)||e<=0)throw new a("INVALID_ARGS",`${t} must be a positive number`);return e}function nV(e){let t=e.filter(t=>es(t,e)).map(e=>e.rect).filter(nH),n=t.length>0?t:e.map(e=>e.rect).filter(nH);if(0===n.length)throw new a("COMMAND_FAILED","Cannot infer viewport for directional swipe");let r=Math.min(...n.map(e=>e.x)),i=Math.min(...n.map(e=>e.y));return{x:r,y:i,width:Math.max(...n.map(e=>e.x+e.width))-r,height:Math.max(...n.map(e=>e.y+e.height))-i}}function nH(e){return!!(e&&e.width>0&&e.height>0)}function nz(e){return e&&"object"==typeof e?e:void 0}async function nK(e,t,n){let r=await nD(e,t,{action:n,requireInteractive:!0,promoteToHittableAncestor:!0});if(!e.backend.tap)throw new a("UNSUPPORTED_OPERATION","tap is not supported by this backend");let i=nj(await e.backend.tap(t8(e,t),r.point,{button:t.button,count:t.count,intervalMs:t.intervalMs,holdMs:t.holdMs,jitterPx:t.jitterPx,doubleTap:t.doubleTap}));return{...r,...i?{backendResult:i}:{}}}function nj(e){return e&&"object"==typeof e?e:void 0}function nW(e,t){if(void 0!==e)return nJ(e,t)}function nJ(e,t){let n=e?.trim();if(!n)throw new a("INVALID_ARGS",`${t} must be a non-empty string`);return n}function nZ(e){return e&&"object"==typeof e?e:void 0}let nQ=/^[A-Za-z0-9_.:-]{1,64}$/;async function n0(e,t){if(!t||"object"!=typeof t)throw new a("INVALID_ARGS","apps.push requires an input");if("json"===t.kind)return n1(t.payload,"apps.push JSON payload",8192),{backendInput:{kind:"json",payload:t.payload},inputKind:"json"};let n=await eq(e,t,{usage:"apps.push",field:"input"});return{backendInput:{kind:"file",path:n.path},inputKind:"file",...n.cleanup?{cleanup:n.cleanup}:{}}}function n1(e,t,n){if(function(e,t){if(!e||"object"!=typeof e||Array.isArray(e))throw new a("INVALID_ARGS",`${t} must be a JSON object`)}(e,t),Buffer.byteLength(function(e,t){try{let n=JSON.stringify(e);if("string"!=typeof n)throw new a("INVALID_ARGS",`${t} must be JSON-serializable`);return n}catch{throw new a("INVALID_ARGS",`${t} must be JSON-serializable`)}}(e,t),"utf8")>n)throw new a("INVALID_ARGS",`${t} exceeds ${n} bytes`)}function n2(e,t){return{session:t.session,requestId:t.requestId,signal:t.signal??e.signal,metadata:t.metadata}}function n5(e){return e&&"object"==typeof e?e:void 0}async function n4(e,t,n){let r="reinstall"===n?"reinstallApp":"installApp",i=e.backend[r];if(!i)throw new a("UNSUPPORTED_OPERATION",`admin.${n} is not supported by this backend`);let o="app"in t&&void 0!==t.app?nJ(t.app,"app"):void 0;if("installFromSource"!==n&&!o)throw new a("INVALID_ARGS",`admin.${n} requires app`);let s=t8(e,t),l=await n8(e,s,t.source);try{var d,c,h,p,f;let t,r,a,w,m,b,g,y,x=await i.call(e.backend,s,{...o?{app:o}:{},source:l.source});return d=n,c=o,h=l.source,p=x,t=n7(p),r=n9(p,"appName"),a=n9(p,"appId"),w=n9(p,"bundleId"),m=n9(p,"packageName"),b=n9(p,"launchTarget"),g=n9(p,"installablePath"),y=n9(p,"archivePath"),{kind:"reinstall"===d?"appReinstalled":"installFromSource"===d?"appInstalledFromSource":"appInstalled",...c?{app:c}:{},source:h,...a?{appId:a}:{},...r?{appName:r}:{},...w?{bundleId:w}:{},...m?{packageName:m}:{},...b?{launchTarget:b}:{},...g?{installablePath:g}:{},...y?{archivePath:y}:{},...t?{backendResult:t}:{},...u(`${"reinstall"===d?"Reinstalled":"Installed"}: ${r??b??c??(f=h,"path"===f.kind?f.path:"uploadedArtifact"===f.kind?f.id:f.url)}`)}}finally{await l.cleanup?.()}}async function n8(e,t,n){let r=n6(n),i=await n3(e,r);try{let n=e.backend.resolveInstallSource?await e.backend.resolveInstallSource(t,i.source):i.source;return{source:n6(n),...i.cleanup?{cleanup:i.cleanup}:{}}}catch(e){if(i.cleanup)try{await i.cleanup()}catch{}throw e}}async function n3(e,t){if("url"===t.kind)return{source:t};let n=await eq(e,t,{usage:"admin.install",field:"source"});return{source:{kind:"path",path:n.path},...n.cleanup?{cleanup:n.cleanup}:{}}}function n6(e){if(!e||"object"!=typeof e)throw new a("INVALID_ARGS","install source is required");if("path"===e.kind)return{kind:"path",path:nJ(e.path,"source.path")};if("uploadedArtifact"===e.kind)return{kind:"uploadedArtifact",id:nJ(e.id,"source.id")};if("url"===e.kind){let t=nJ(e.url,"source.url");return function(e){let t;try{t=new URL(e)}catch{throw new a("INVALID_ARGS",`Invalid install source URL: ${e}`)}if("http:"!==t.protocol&&"https:"!==t.protocol)throw new a("INVALID_ARGS","Install source URL must use http or https")}(t),{kind:"url",url:t}}throw new a("INVALID_ARGS","install source kind must be path, uploadedArtifact, or url")}function n9(e,t){let n=e[t];return"string"==typeof n&&n.length>0?n:void 0}function n7(e){return e&&"object"==typeof e?e:void 0}function re(e,t){if("start"===e||"stop"===e)return e;throw new a("INVALID_ARGS",`${t} action must be start or stop`)}function rt(e,t,n,r){return{kind:"start"===e?r.startKind:r.stopKind,action:e,...n?{artifact:n}:{},backendResult:t,...u("start"===e?r.startMessage:r.stopMessage)}}let rn=/(?:authorization|cookie|token|secret|password|passwd|api[-_]?key)/i,rr=/\b[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}\b/g;function ri(e){var t,n;let r=!1,i=e;return{value:(t=i=(i=(i=(i=(i=i.replaceAll(/(authorization|token|secret|password|passwd|api[-_]?key)=([^&\s]+)/gi,(e,t)=>(r=!0,`${String(t)}=[REDACTED]`))).replaceAll(/("(?:authorization|cookie|token|secret|password|passwd|api[-_]?key)"\s*:\s*")([^"]*)(")/gi,(e,t,n,i)=>(r=!0,`${String(t)}[REDACTED]${String(i)}`))).replaceAll(/\b(Bearer\s+)([^\s",;]+)/gi,(e,t)=>(r=!0,`${String(t)}[REDACTED]`))).replaceAll(/((?:authorization|cookie|token|secret|password|passwd|api[-_]?key)\s*[:=]\s*)([^\s,;&]+)/gi,(e,t)=>(r=!0,`${String(t)}[REDACTED]`))).replaceAll(rr,()=>(r=!0,"[REDACTED]")),n=()=>{r=!0},i=/(https?:\/\/|token|secret|password|authorization|cookie|api[-_]?key)/i.test(t)?t.replaceAll(/https?:\/\/[^\s"'<>)]+/gi,e=>{let t=ra(e);return t?(t.redacted&&n(),t.value):e}):t),redacted:r}}function ra(e){try{let t=new URL(e),n=function(e){let t=!1;for(let n of Array.from(e.searchParams.keys()))rn.test(n)&&(e.searchParams.set(n,"[REDACTED]"),t=!0);return t}(t);return(t.username||t.password)&&(t.username="REDACTED",t.password="REDACTED",n=!0),{value:t.toString(),redacted:n}}catch{return}}let ro=/(?:authorization|cookie|token|secret|password|passwd|api[-_]?key)/i;function rs(e){if(!e)return{redacted:!1};let t=!1,n={};for(let[r,i]of Object.entries(e))if(ro.test(r))n[r]="[REDACTED]",t=!0;else{let e=ru(i,2048);n[r]=e.value??"",t||=e.redacted}return{value:n,redacted:t}}function rl(e){return void 0===e?{redacted:!1}:function(e){try{let t=JSON.parse(e),n=rc(t,ri);return rh(JSON.stringify(n.value),2048,n.redacted)}catch{return}}(e)??ru(e,2048)}function rd(e){return rc(e,e=>ru(e,2048))}function rc(e,t){if(void 0===e)return{redacted:!1};if("string"==typeof e)return t(e);if(!e||"object"!=typeof e)return{value:e,redacted:!1};if(Array.isArray(e)){let n=!1;return{value:e.map(e=>{let r=rc(e,t);return n||=r.redacted,r.value}),redacted:n}}let n=!1,r={};for(let[i,a]of Object.entries(e)){if(ro.test(i)){r[i]="[REDACTED]",n=!0;continue}let e=rc(a,t);r[i]=e.value,n||=e.redacted}return{value:r,redacted:n}}function ru(e,t){if(void 0===e)return{redacted:!1};let n=ri(e);return rh(n.value,t,n.redacted)}function rh(e,t,n){if(void 0===e)return{redacted:n};let r=e;return r.length>t&&(r=`${r.slice(0,t)}...[truncated]`,n=!0),{value:r,redacted:n}}async function rp(e,t){let n=t8(e,t),r=t.session?await e.sessions.get(t.session):void 0;return{...n,...t.appId??r?.appId?{appId:t.appId??r?.appId}:{},...t.appBundleId??r?.appBundleId?{appBundleId:t.appBundleId??r?.appBundleId}:{}}}function rf(e,t,n,r){return{...rw(e),...void 0!==e.cursor?{cursor:nJ(e.cursor,"cursor")}:{},limit:void 0===e.limit?t:ev(e.limit,r,1,n)}}function rw(e){return{...void 0!==e.since?{since:nJ(e.since,"since")}:{},...void 0!==e.until?{until:nJ(e.until,"until")}:{}}}function rm(e,t,n=50){if(!Array.isArray(e))throw new a("INVALID_ARGS",`${t} must be an array of strings`);if(e.length>n)throw new a("INVALID_ARGS",`${t} must contain at most ${n} entries`);return e.map((e,n)=>nJ(e,`${t}[${n}]`))}let rb=eV,rg=async(e,t)=>{let n,r,i;if(!t.baseline)throw new a("INVALID_ARGS","diff screenshot requires a baseline image");let o=function(e){if(null==e||""===e)return .1;let t=Number(e);if(Number.isNaN(t)||t<0||t>1)throw new a("INVALID_ARGS","--threshold must be a number between 0 and 1");return t}(t.threshold),s=t.current??{kind:"live"};if(t.overlayRefs&&!t$(s))throw new a("INVALID_ARGS","diff screenshot <current.png> cannot use --overlay-refs because saved-image comparisons have no live accessibility refs");let l=await eq(e,t.baseline,{usage:"diff screenshot baseline",field:"baseline"}),d=[];try{let a;a=t$(s)?(r=await tS(e,t)).path:(n=await eq(e,s,{usage:"diff screenshot current",field:"current"})).path,i=t.out?await eX(e,t.out,{field:"diffPath",ext:".png"}):void 0;let c=await tv(l.path,a,{threshold:o,outputPath:i?.path,maxPixels:e.policy.maxImagePixels});t$(s)&&(c=await tD(e,t,i?.path,c,d));let u=c.diffPath?await i?.publish():void 0;return u&&d.push(u),c.diffPath||await i?.cleanup?.(),{...c,...d.length>0?{artifacts:d}:{}}}catch(e){throw await i?.cleanup?.(),e}finally{await l.cleanup?.(),await n?.cleanup?.(),await r?.cleanup?.()}},ry=async(e,t)=>{var n;let r,i,a=await t9(e,t),o=function(e){var t,n,r,i,a;let{previous:o,current:s,options:l,identity:d}=e;if(!0===l.forceFull||!0===l.raw||!o||!1===o.comparisonSafe||!1===s.comparisonSafe||(t=o,n=s,r=d,t.backend&&n.backend&&t.backend!==n.backend||r?.previousAppBundleId&&r.currentAppBundleId&&r.previousAppBundleId!==r.currentAppBundleId)||!o.presentationKey||o.presentationKey!==s.presentationKey||(i=o,a=s,i.truncated!==a.truncated||JSON.stringify(t4(i.nodes))!==JSON.stringify(t4(a.nodes))))return;let c=l.scope?.trim();return{ageMs:Math.max(0,s.createdAt-o.createdAt),nodeCount:s.nodes.length,...!0===l.interactiveOnly?{interactiveOnly:!0}:{},...c?{scope:c}:{}}}({previous:a.session?.snapshot,current:a.snapshot,options:t,identity:{previousAppBundleId:a.session?.appBundleId,currentAppBundleId:a.result.appBundleId??a.session?.appBundleId}});return await e.sessions.set(t7(t.session,a)),{nodes:a.snapshot.nodes,truncated:a.snapshot.truncated??!1,visibility:function(e){var t;let{nodes:n,backend:r,snapshotRaw:i}=e;if(i||"macos-helper"===(t=r)||"linux-atspi"===t)return{partial:!1,visibleNodeCount:n.length,totalNodeCount:n.length,reasons:[]};let a=ei(n),o=new Set;return a.hiddenCount>0&&o.add("offscreen-nodes"),a.nodes.some(e=>e.hiddenContentAbove)&&o.add("scroll-hidden-above"),a.nodes.some(e=>e.hiddenContentBelow)&&o.add("scroll-hidden-below"),{partial:o.size>0,visibleNodeCount:a.nodes.length,totalNodeCount:n.length,reasons:[...o]}}({nodes:a.snapshot.nodes,backend:a.snapshot.backend,snapshotRaw:t.raw}),...a.result.androidSnapshot?{androidSnapshot:a.result.androidSnapshot}:{},...a.warnings.length>0?{warnings:a.warnings}:{},...o?{unchanged:o}:{},...(r=(n=a).result.appName??n.session?.appName,i=n.result.appBundleId??n.session?.appBundleId,{...r||i?{appName:r??i}:{},...i?{appBundleId:i}:{}})}},rx=async(e,t)=>{let n=await t9(e,t),r=!0===t.interactiveOnly,i=n.session?.snapshot,a=t7(t.session,n);if(!i){let t=function(e,t={}){return tV(e,t).length}(n.snapshot.nodes,{flatten:r});return await e.sessions.set(a),{mode:"snapshot",baselineInitialized:!0,summary:{additions:0,removals:0,unchanged:t},lines:[],...n.warnings.length>0?{warnings:n.warnings}:{}}}let o=function(e,t,n={}){let r=function(e,t){let n=e.length,r=t.length,i=n+r,a=new Map,o=[];a.set(1,0);for(let s=0;s<=i;s+=1){o.push(new Map(a));for(let i=-s;i<=s;i+=2){let l=i===-s||i!==s&&tH(a,i-1)<tH(a,i+1)?tH(a,i+1):tH(a,i-1)+1,d=l-i;for(;l<n&&d<r&&e[l].comparable===t[d].comparable;)l+=1,d+=1;if(a.set(i,l),l>=n&&d>=r)return function(e,t,n,r,i){let a=[],o=r,s=i;for(let r=e.length-1;r>=0;r-=1){let i=e[r],l=o-s,d=l===-r||l!==r&&tH(i,l-1)<tH(i,l+1)?l+1:l-1,c=tH(i,d),u=c-d;for(;o>c&&s>u;)a.push({kind:"unchanged",text:n[s-1].text}),o-=1,s-=1;if(0===r)break;o===c?(a.push({kind:"added",text:n[u].text}),s=u):(a.push({kind:"removed",text:t[c].text}),o=c)}return a.reverse(),a}(o,e,t,n,r)}}return[]}(tV(e,n),tV(t,n)),i={additions:0,removals:0,unchanged:0};for(let e of r)"added"===e.kind&&(i.additions+=1),"removed"===e.kind&&(i.removals+=1),"unchanged"===e.kind&&(i.unchanged+=1);return{summary:i,lines:r}}(i.nodes,n.snapshot.nodes,{flatten:r});return await e.sessions.set(a),{mode:"snapshot",baselineInitialized:!1,summary:o.summary,lines:o.lines,...n.warnings.length>0?{warnings:n.warnings}:{}}},rv=async(e,t)=>{let n=t.locator??"any";if(!t.query)throw new a("INVALID_ARGS","find requires a value");if("wait"===t.action)return await nw(e,t,n);let{capture:r,match:i}=await nm(e,t,n);if(!i)throw new a("COMMAND_FAILED","find did not match any element");if("exists"===t.action)return{kind:"found",found:!0};let o=`@${i.ref}`;return"get_attrs"===t.action?{kind:"attrs",ref:o,node:i}:{kind:"text",ref:o,text:await no(e,r,i),node:i}},rk=ns,rI=nl,rM=nd,rA=nc,rN=nu,rR=nh,rP=np,rS=nf,rD=async(e,t)=>await nK(e,t,"click"),rE=async(e,t)=>await nK(e,t,"press"),rO=async(e,t)=>{var n;if(!t.text)throw new a("INVALID_ARGS","fill requires text");let r=await nD(e,t,{action:"fill",requireInteractive:!0,promoteToHittableAncestor:!1});if(!e.backend.fill)throw new a("UNSUPPORTED_OPERATION","fill is not supported by this backend");let i=nj(await e.backend.fill(t8(e,t),r.point,t.text,{delayMs:t.delayMs})),o="node"in r?r.node.type??"":"",s=o&&!O(o,e.backend.platform)?`fill target ${n=r,n.target?.kind==="ref"?n.target.ref:n.target?.kind==="selector"?n.target.selector:"point"} resolved to "${o}", attempting fill anyway.`:void 0;return{...r,text:t.text,...s?{warning:s}:{},...i?{backendResult:i}:{}}},r_=async(e,t)=>{let n=t.text;if(!n)throw new a("INVALID_ARGS","type requires text");let r=U(n);if(r)throw new a("INVALID_ARGS",`type does not accept a target ref like "${r}"`,{hint:`Use fill ${r} "text" to target that field, or press ${r} then type "text" to append.`});if(!e.backend.typeText)throw new a("UNSUPPORTED_OPERATION","type is not supported by this backend");let i=ev(t.delayMs??0,"delay-ms",0,1e4),o=nj(await e.backend.typeText(t8(e,t),n,{delayMs:i}));return{kind:"text",text:n,delayMs:i,...o?{backendResult:o}:{},...u(`Typed ${Array.from(n).length} chars`)}},rT=async(e,t)=>{let n=await nD(e,t,{action:"focus",requireInteractive:!0,promoteToHittableAncestor:!1});if(!e.backend.focus)throw new a("UNSUPPORTED_OPERATION","focus is not supported by this backend");let r=nz(await e.backend.focus(t8(e,t),n.point));return{...n,...r?{backendResult:r}:{},...u(`Focused (${n.point.x}, ${n.point.y})`)}},r$=async(e,t)=>{let n=await nD(e,t,{action:"longPress",requireInteractive:!0,promoteToHittableAncestor:!0});if(!e.backend.longPress)throw new a("UNSUPPORTED_OPERATION","longPress is not supported by this backend");let r=void 0===t.durationMs?void 0:ev(t.durationMs,"durationMs",0,12e4),i=nz(await e.backend.longPress(t8(e,t),n.point,{durationMs:r}));return{...n,...void 0!==r?{durationMs:r}:{},...i?{backendResult:i}:{},...u(`Long pressed (${n.point.x}, ${n.point.y})`)}},rC=async(e,t)=>{if(!e.backend.swipe)throw new a("UNSUPPORTED_OPERATION","swipe is not supported by this backend");if(t.preset)return await nU(e,t,e.backend.swipe);let n=await nG(e,t),r=function(e,t){if(t.to)return{point:nX(t.to,"to")};let n=nq(t.direction,"swipe direction"),r=nY(t.distance??200,"swipe distance");switch(n){case"up":return{point:{x:e.x,y:e.y-r},direction:n,distance:r};case"down":return{point:{x:e.x,y:e.y+r},direction:n,distance:r};case"left":return{point:{x:e.x-r,y:e.y},direction:n,distance:r};case"right":return{point:{x:e.x+r,y:e.y},direction:n,distance:r}}}(n.point,t),i=void 0===t.durationMs?void 0:ev(t.durationMs,"durationMs",16,1e4),o=nz(await e.backend.swipe(t8(e,t),n.point,r.point,{durationMs:i}));return{kind:"swipe",from:n.point,to:r.point,...r.direction?{direction:r.direction}:{},...void 0!==r.distance?{distance:r.distance}:{},...void 0!==i?{durationMs:i}:{},...n.target?{fromTarget:n.target}:{},...o?{backendResult:o}:{},...u("Swiped")}},rL=async(e,t)=>{var n,r,i,o;let s;if(!e.backend.scroll)throw new a("UNSUPPORTED_OPERATION","scroll is not supported by this backend");let l="bottom"===(n=t.direction)?{direction:"down",edge:"bottom"}:"top"===n?{direction:"up",edge:"top"}:{direction:nq(n,"scroll direction")},d=(r=t.amount,i="scroll amount",void 0===r?void 0:nY(r,i)),c=function(e,t){if(void 0!==e){if(!Number.isFinite(e)||!Number.isInteger(e)||e<=0)throw new a("INVALID_ARGS",`${t} must be a positive integer`);return e}}(t.pixels,"scroll pixels");if(void 0!==d&&void 0!==c)throw new a("INVALID_ARGS","scroll accepts either amount or pixels, not both");let h=await nB(e,t),p="viewport"===h.kind?{kind:"viewport"}:{kind:"point",point:h.point},f=e.backend.scroll,w=async()=>await f(t8(e,t),p,{direction:l.direction,...void 0!==d?{amount:d}:{},...void 0!==c?{pixels:c}:{}}),m=0;if(l.edge){let n=l.edge,r="viewport"===(o=h).kind?{}:{point:o.point,nodeIndex:"node"in o?o.node.index:void 0},i=await ep({edge:n,captureState:async i=>await nF(e,t,n,r,i),scroll:w});s=i.result,m=i.passes}else s=await w(),m=1;let b=nz(s);return{...h,direction:l.direction,...l.edge?{edge:l.edge,passes:m}:{},...void 0!==d?{amount:d}:{},...void 0!==c?{pixels:c}:{},...b?{backendResult:b}:{},...u(ef(l.direction,l.edge,m,d,c))}},rU=async(e,t)=>{if(!e.backend.pinch)throw new a("UNSUPPORTED_OPERATION","pinch is not supported by this backend");await nO(e,t,"pinch");let n=nY(t.scale,"pinch scale"),r=t.center?await nD(e,{...t,target:t.center},{action:"pinch",requireInteractive:!1,promoteToHittableAncestor:!1}):void 0,i=nz(await e.backend.pinch(t8(e,t),{scale:n,...r?{center:r.point}:{}}));return{kind:"pinch",scale:n,...r?{center:r.point,centerTarget:r}:{},...i?{backendResult:i}:{},...u(`Pinched to scale ${n}`)}},rB=async(e,t={})=>{if(!e.backend.pressBack)throw new a("UNSUPPORTED_OPERATION","system.back is not supported by this backend");let n=t.mode??"in-app";if("in-app"!==n&&"system"!==n)throw new a("INVALID_ARGS","system.back mode must be in-app or system");let r=nZ(await e.backend.pressBack(t8(e,t),{mode:n}));return{kind:"systemBack",mode:n,...r?{backendResult:r}:{},...u("Back")}},rG=async(e,t={})=>{if(!e.backend.pressHome)throw new a("UNSUPPORTED_OPERATION","system.home is not supported by this backend");let n=nZ(await e.backend.pressHome(t8(e,t)));return{kind:"systemHome",...n?{backendResult:n}:{},...u("Home")}},rF=async(e,t)=>{if(!e.backend.rotate)throw new a("UNSUPPORTED_OPERATION","system.rotate is not supported by this backend");let n=function(e){switch(e){case"portrait":case"portrait-upside-down":case"landscape-left":case"landscape-right":return e;default:throw new a("INVALID_ARGS","system.rotate orientation must be portrait, portrait-upside-down, landscape-left, or landscape-right")}}(t.orientation),r=nZ(await e.backend.rotate(t8(e,t),n));return{kind:"systemRotated",orientation:n,...r?{backendResult:r}:{},...u(`Rotated to ${n}`)}},rq=async(e,t={})=>{var n,r,i,o,s,l,d;if(!e.backend.setKeyboard)throw new a("UNSUPPORTED_OPERATION","system.keyboard is not supported by this backend");let c=t.action??"status";if(!L(c))throw new a("INVALID_ARGS","system.keyboard action must be status, get, dismiss, enter, or return");let h=await e.backend.setKeyboard(t8(e,t),{action:c}),p=nZ(h),f=(n=h)&&"object"==typeof n?h:{};return"enter"===c||"return"===c?{kind:"keyboardEnterPressed",action:"enter",state:f,...(r=p)?{backendResult:r}:{},...u("Keyboard enter pressed")}:"dismiss"===c?(i=c,{kind:"keyboardDismissed",action:i,state:o=f,...(s=p)?{backendResult:s}:{},...u(!1===o.dismissed?"Keyboard already hidden":"Keyboard dismissed")}):(l=c,{kind:"keyboardState",action:l,state:f,...(d=p)?{backendResult:d}:{}})},rX=async(e,t)=>{if("read"===t.action){if(!e.backend.getClipboard)throw new a("UNSUPPORTED_OPERATION","system.clipboard read is not supported by this backend");let n=await e.backend.getClipboard(t8(e,t));return{kind:"clipboardText",action:"read",text:"string"==typeof n?n:n.text}}if("write"!==t.action)throw new a("INVALID_ARGS","system.clipboard action must be read or write");if(!e.backend.setClipboard)throw new a("UNSUPPORTED_OPERATION","system.clipboard write is not supported by this backend");if("string"!=typeof t.text)throw new a("INVALID_ARGS","system.clipboard write requires text");let n=nZ(await e.backend.setClipboard(t8(e,t),t.text));return{kind:"clipboardUpdated",action:"write",textLength:Array.from(t.text).length,...n?{backendResult:n}:{},...u("Clipboard updated")}},rY=async(e,t={})=>{if(!e.backend.openSettings)throw new a("UNSUPPORTED_OPERATION","system.settings is not supported by this backend");let n=nW(t.target,"target"),r=nZ(await e.backend.openSettings(t8(e,t),n));return{kind:"settingsOpened",...n?{target:n}:{},...r?{backendResult:r}:{},...u(n?`Opened settings: ${n}`:"Opened settings")}},rV=async(e,t={})=>{var n,r;if(!e.backend.handleAlert)throw new a("UNSUPPORTED_OPERATION","system.alert is not supported by this backend");let i=t.action??"get";if("get"!==i&&"accept"!==i&&"dismiss"!==i&&"wait"!==i)throw new a("INVALID_ARGS","system.alert action must be get, accept, dismiss, or wait");let o=void 0===t.timeoutMs?void 0:ev(t.timeoutMs,"timeoutMs",0,12e4),s=await e.backend.handleAlert(t8(e,t),i,{timeoutMs:o});return n=i,r=s,"get"===n?function(e){if("alertStatus"!==e.kind)throw new a("COMMAND_FAILED","system.alert get returned an invalid backend result");return{kind:"alertStatus",action:"get",alert:e.alert}}(r):"wait"===n?function(e){if("alertWait"!==e.kind)throw new a("COMMAND_FAILED","system.alert wait returned an invalid backend result");return{kind:"alertWait",action:"wait",alert:e.alert,...void 0!==e.waitedMs?{waitedMs:e.waitedMs}:{},...void 0!==e.timedOut?{timedOut:e.timedOut}:{},...u(e.alert?"Alert visible":"Alert wait timed out")}}(r):function(e,t){if("alertHandled"!==t.kind)throw new a("COMMAND_FAILED",`system.alert ${e} returned an invalid backend result`);return{kind:"alertHandled",action:e,handled:t.handled,...t.alert?{alert:t.alert}:{},...t.button?{button:t.button}:{},...u(t.handled?`Alert ${e}ed`:"No alert handled")}}(n,r)},rH=async(e,t={})=>{if(!e.backend.openAppSwitcher)throw new a("UNSUPPORTED_OPERATION","system.appSwitcher is not supported by this backend");let n=nZ(await e.backend.openAppSwitcher(t8(e,t)));return{kind:"appSwitcherOpened",...n?{backendResult:n}:{},...u("Opened app switcher")}},rz=async(e,t)=>{var n;if(!e.backend.openApp)throw new a("UNSUPPORTED_OPERATION","apps.open is not supported by this backend");let r=function(e){var t;let n=nW(e.app,"app"),r=nW(e.appId,"appId"),i=nW(e.bundleId,"bundleId"),o=nW(e.packageName,"packageName"),s=nW(e.url,"url"),l=nW(e.activity,"activity"),d={...n?{app:n}:{},...r?{appId:r}:{},...i?{bundleId:i}:{},...o?{packageName:o}:{},...s?{url:s}:{},...l?{activity:l}:{}};if(!((t=d).app??t.appId??t.bundleId??t.packageName??t.url??t.activity))throw new a("INVALID_ARGS","apps.open requires app, appId, bundleId, packageName, url, or activity");return d}(t),i=n5(await e.backend.openApp(n2(e,t),r,{launchArgs:t.launchArgs,relaunch:t.relaunch}));return{kind:"appOpened",target:r,relaunch:!0===t.relaunch,...i?{backendResult:i}:{},...u(`Opened: ${(n=r).app??n.appId??n.bundleId??n.packageName??n.url??n.activity??"app"}`)}},rK=async(e,t={})=>{if(!e.backend.closeApp)throw new a("UNSUPPORTED_OPERATION","apps.close is not supported by this backend");let n=nW(t.app,"app"),r=n5(await e.backend.closeApp(n2(e,t),n));return{kind:"appClosed",...n?{app:n}:{},...r?{backendResult:r}:{},...u(n?`Closed: ${n}`:"Closed app")}},rj=async(e,t={})=>{if(!e.backend.listApps)throw new a("UNSUPPORTED_OPERATION","apps.list is not supported by this backend");return{kind:"appsList",apps:await e.backend.listApps(n2(e,t),$(t.filter))}},rW=async(e,t)=>{if(!e.backend.getAppState)throw new a("UNSUPPORTED_OPERATION","apps.state is not supported by this backend");let n=nJ(t.app,"app"),r=await e.backend.getAppState(n2(e,t),n);return{kind:"appState",app:n,state:r}},rJ=async(e,t)=>{if(!e.backend.pushFile)throw new a("UNSUPPORTED_OPERATION","apps.push is not supported by this backend");let n=nJ(t.app,"app"),r=await n0(e,t.input);try{let i=await e.backend.pushFile(n2(e,t),r.backendInput,n),a=n5(i);return{kind:"appPushed",app:n,inputKind:r.inputKind,...a?{backendResult:a}:{},...u(`Pushed to ${n}`)}}finally{await r.cleanup?.()}},rZ=async(e,t)=>{var n,r;if(!e.backend.triggerAppEvent)throw new a("UNSUPPORTED_OPERATION","apps.triggerEvent is not supported by this backend");let i=function(e){let t=nJ(e,"name");if(!nQ.test(t))throw new a("INVALID_ARGS",`Invalid apps.triggerEvent name: ${t}`,{hint:"Use 1-64 chars: letters, numbers, underscore, dot, colon, or dash."});return t}(t.name);n=t.payload,r=`apps.triggerEvent payload for "${i}"`,void 0!==n&&n1(n,r,8192);let o=n5(await e.backend.triggerAppEvent(n2(e,t),{name:i,...t.payload?{payload:t.payload}:{}}));return{kind:"appEventTriggered",name:i,...t.payload?{payload:t.payload}:{},...o?{backendResult:o}:{},...u(`Triggered app event: ${i}`)}},rQ=async(e,t={})=>{if(!e.backend.listDevices)throw new a("UNSUPPORTED_OPERATION","admin.devices is not supported by this backend");return{kind:"adminDevices",devices:await e.backend.listDevices(t8(e,t),t.filter)}},r0=async(e,t={})=>{if(!e.backend.bootDevice)throw new a("UNSUPPORTED_OPERATION","admin.boot is not supported by this backend");let n=function(e){if(!e)return;let t=nW(e.id,"target.id"),n=nW(e.name,"target.name"),r={...t?{id:t}:{},...n?{name:n}:{},...e.platform?{platform:e.platform}:{},...e.target?{target:e.target}:{},...void 0!==e.headless?{headless:e.headless}:{}};return Object.keys(r).length>0?r:void 0}(t.target),r=n7(await e.backend.bootDevice(t8(e,t),n));return{kind:"deviceBooted",...n?{target:n}:{},...r?{backendResult:r}:{},...u("Booted device")}},r1=async(e,t)=>await n4(e,t,"install"),r2=async(e,t)=>await n4(e,t,"reinstall"),r5=async(e,t)=>await n4(e,t,"installFromSource"),r4=async(e,t)=>{let n=re(t.action,"record"),r="start"===n?e.backend.startRecording:e.backend.stopRecording;if(!r)throw new a("UNSUPPORTED_OPERATION",`record ${n} is not supported by this backend`);let i=t.out?await eX(e,t.out,{field:"path",ext:".mp4"}):void 0;try{var o,s,l,d,c;let a,u,h=(o=t,s=i?.path,a=void 0===o.fps?void 0:ev(o.fps,"fps",1,60),u=void 0===o.quality?void 0:ev(o.quality,"quality",5,10),{...s?{outPath:s}:{},...void 0!==a?{fps:a}:{},...void 0!==u?{quality:u}:{},...void 0!==o.hideTouches?{showTouches:!0!==o.hideTouches}:{}}),p=await r.call(e.backend,t8(e,t),h),f=await i?.publish();return l=n,d=p,c=f,{..."string"==typeof d.path?{path:d.path}:{},..."string"==typeof d.telemetryPath?{telemetryPath:d.telemetryPath}:{},..."string"==typeof d.warning?{warning:d.warning}:{},...rt(l,d,c,{startKind:"recordingStarted",stopKind:"recordingStopped",startMessage:"Recording started",stopMessage:"Recording stopped"})}}catch(e){throw await i?.cleanup?.(),e}},r8=async(e,t)=>{let n=re(t.action,"trace"),r="start"===n?e.backend.startTrace:e.backend.stopTrace;if(!r)throw new a("UNSUPPORTED_OPERATION",`trace ${n} is not supported by this backend`);let i=t.out?await eX(e,t.out,{field:"outPath",ext:".trace"}):void 0;try{var o,s,l;let a={...i?.path?{outPath:i.path}:{}},d=await r.call(e.backend,t8(e,t),a),c=await i?.publish();return o=n,s=d,l=c,{..."string"==typeof s.outPath?{outPath:s.outPath}:{},...rt(o,s,l,{startKind:"traceStarted",stopKind:"traceStopped",startMessage:"Trace started",stopMessage:"Trace stopped"})}}catch(e){throw await i?.cleanup?.(),e}},r3=async(e,t={})=>{var n,r;let i;if(!e.backend.readLogs)throw new a("UNSUPPORTED_OPERATION","diagnostics.logs is not supported by this backend");return i=!0===(n=await e.backend.readLogs(await rp(e,t),{...rf(r=t,100,500,"logs limit"),...void 0!==r.levels?{levels:rm(r.levels,"levels")}:{},...void 0!==r.search?{search:nJ(r.search,"search")}:{},...void 0!==r.source?{source:nJ(r.source,"source")}:{}})).redacted,{kind:"diagnosticsLogs",entries:n.entries.map(e=>{let t=ru(e.message,4096),n=rd(e.metadata);return i||=t.redacted||n.redacted,{...e.timestamp?{timestamp:e.timestamp}:{},...e.level?{level:e.level}:{},message:t.value??"",...e.source?{source:e.source}:{},...n.value?{metadata:n.value}:{}}}),...n.nextCursor?{nextCursor:n.nextCursor}:{},...n.timeWindow?{timeWindow:n.timeWindow}:{},...n.backend?{backend:n.backend}:{},redacted:i,...n.notes?{notes:n.notes}:{}}},r6=async(e,t={})=>{var n,r,i;let o;if(!e.backend.dumpNetwork)throw new a("UNSUPPORTED_OPERATION","diagnostics.network is not supported by this backend");let s={...rf(i=t,25,200,"network limit"),include:function(e){if(void 0===e)return"summary";if("summary"===e||"headers"===e||"body"===e||"all"===e)return e;throw new a("INVALID_ARGS","network include must be summary, headers, body, or all")}(i.include)};return n=await e.backend.dumpNetwork(await rp(e,t),s),r=s.include??"summary",o=!0===n.redacted,{kind:"diagnosticsNetwork",entries:n.entries.map(e=>{var t;let n=e.url?ra(t=e.url)??ru(t,2048):void 0,i="headers"===r||"all"===r?rs(e.requestHeaders):void 0,a="headers"===r||"all"===r?rs(e.responseHeaders):void 0,s="body"===r||"all"===r?rl(e.requestBody):void 0,l="body"===r||"all"===r?rl(e.responseBody):void 0,d=rd(e.metadata);return o||=(n?.redacted??!1)||(i?.redacted??!1)||(a?.redacted??!1)||(s?.redacted??!1)||(l?.redacted??!1)||d.redacted,{...e.timestamp?{timestamp:e.timestamp}:{},...e.method?{method:e.method}:{},...n?{url:n.value}:{},...void 0!==e.status?{status:e.status}:{},...void 0!==e.durationMs?{durationMs:e.durationMs}:{},...i?.value?{requestHeaders:i.value}:{},...a?.value?{responseHeaders:a.value}:{},...s?.value!==void 0?{requestBody:s.value}:{},...l?.value!==void 0?{responseBody:l.value}:{},...d.value?{metadata:d.value}:{}}}),...n.nextCursor?{nextCursor:n.nextCursor}:{},...n.timeWindow?{timeWindow:n.timeWindow}:{},...n.backend?{backend:n.backend}:{},redacted:o,...n.notes?{notes:n.notes}:{}}},r9=async(e,t={})=>{var n,r;let i;if(!e.backend.measurePerf)throw new a("UNSUPPORTED_OPERATION","diagnostics.perf is not supported by this backend");return i=!0===(n=await e.backend.measurePerf(await rp(e,t),{...rw(r=t),...void 0!==r.sampleMs?{sampleMs:ev(r.sampleMs,"sampleMs",100,6e4)}:{},...void 0!==r.metrics?{metrics:rm(r.metrics,"metrics",20)}:{}})).redacted,{kind:"diagnosticsPerf",metrics:n.metrics.map(e=>{let t=ru(e.message,4096),n=rd(e.metadata);return i||=t.redacted||n.redacted,{name:e.name,...void 0!==e.value?{value:e.value}:{},...e.unit?{unit:e.unit}:{},...e.status?{status:e.status}:{},...void 0!==t.value?{message:t.value}:{},...n.value?{metadata:n.value}:{}}}),...n.startedAt?{startedAt:n.startedAt}:{},...n.endedAt?{endedAt:n.endedAt}:{},...n.backend?{backend:n.backend}:{},redacted:i,...n.notes?{notes:n.notes}:{}}};function r7(e){let t={backend:e.backend,artifacts:e.artifacts,sessions:e.sessions??function(e=[]){let t=new Map(e.map(e=>[e.name,ie(e)]));return{get:e=>ie(t.get(e)),set:e=>{t.set(e.name,ie(e))},delete:e=>{t.delete(e)},list:()=>Array.from(t.values(),e=>ie(e))}}(),policy:e.policy??function(e={}){return{allowLocalInputPaths:!1,allowLocalOutputPaths:!1,maxImagePixels:2e7,allowNamedBackendCapabilities:[],...e}}(),diagnostics:e.diagnostics,clock:e.clock,signal:e.signal};return{...t,capture:{screenshot:e=>rb(t,e),diffScreenshot:e=>rg(t,e),snapshot:e=>ry(t,e),diffSnapshot:e=>rx(t,e)},selectors:{find:e=>rv(t,e),get:e=>rk(t,e),getText:(e,n={})=>rI(t,{...n,target:e}),getAttrs:(e,n={})=>rM(t,{...n,target:e}),is:e=>rA(t,e),isVisible:(e,n={})=>rN(t,{...n,target:e}),isHidden:(e,n={})=>rR(t,{...n,target:e}),wait:e=>rP(t,e),waitForText:(e,n={})=>rS(t,{...n,text:e})},interactions:{click:(e,n={})=>rD(t,{...n,target:e}),press:(e,n={})=>rE(t,{...n,target:e}),fill:(e,n,r={})=>rO(t,{...r,target:e,text:n}),typeText:(e,n={})=>r_(t,{...n,text:e}),focus:(e,n={})=>rT(t,{...n,target:e}),longPress:(e,n={})=>r$(t,{...n,target:e}),swipe:e=>rC(t,e),scroll:e=>rL(t,e),pinch:e=>rU(t,e)},system:{back:e=>rB(t,e),home:e=>rG(t,e),rotate:e=>rF(t,e),keyboard:e=>rq(t,e),clipboard:e=>rX(t,e),settings:e=>rY(t,e),alert:e=>rV(t,e),appSwitcher:e=>rH(t,e)},apps:{open:e=>rz(t,e),close:e=>rK(t,e),list:(e={})=>rj(t,{...e,filter:T(e.filter)}),state:e=>rW(t,e),push:e=>rJ(t,e),triggerEvent:e=>rZ(t,e)},admin:{devices:e=>rQ(t,e),boot:e=>r0(t,e),install:e=>r1(t,e),reinstall:e=>r2(t,e),installFromSource:e=>r5(t,e)},recording:{record:e=>r4(t,e),trace:e=>r8(t,e)},observability:{logs:e=>r3(t,e),network:e=>r6(t,e),perf:e=>r9(t,e)}}}function ie(e){if(e)return{...e,...e.snapshot?{snapshot:structuredClone(e.snapshot)}:{},...e.metadata?{metadata:function(e){try{return structuredClone(e)}catch{return{...e}}}(e.metadata)}:{}}}function it(e={}){return{allowLocalInputPaths:!0,allowLocalOutputPaths:!0,maxImagePixels:2e7,allowNamedBackendCapabilities:[],...e}}export{eP as PNG,ei as buildMobileSnapshotPresentation,B as buildScrollGesturePlan,tL as buildSnapshotDisplayLines,G as buildSwipeGesturePlan,F as buildSwipePresetGesturePlan,eh as captureScrollEdgeState,V as clampGesturePoint,j as clampToRange,r7 as createAgentDevice,eG as decodePng,ea as deriveMobileSnapshotHiddenContentHints,tz as detectReactNativeOverlay,er as displayNodeLabel,nn as evaluateIsPredicate,U as findMistargetedTypeRefToken,ef as formatScrollEdgeMessage,tU as formatSnapshotLine,X as inferGestureReferenceFrame,ee as inferVerticalScrollIndicatorDirections,nM as interiorCoordinate,L as isKeyboardAction,ek as isReactNativeCollapsedWarningLabel,nt as isSupportedPredicate,Q as isSystemScrollIndicatorLabel,it as localCommandPolicy,et as normalizeSnapshotTree,H as parseScrollDirection,q as parseSwipePreset,Y as pointFromPercent,nI as pointInsideRect,ev as requireIntInRange,nN as resolveActionableTouchNode,nR as resolveActionableTouchResolution,tK as resolveReactNativeOverlayDismissTarget,nv as resolveRectCenter,ep as runScrollEdgePasses};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-device",
3
- "version": "0.16.6",
3
+ "version": "0.16.7",
4
4
  "description": "Agent-native CLI for AI mobile testing and app automation across iOS, Android, tvOS, Android TV, macOS, and Linux.",
5
5
  "mcpName": "io.github.callstackincubator/agent-device",
6
6
  "license": "MIT",
package/server.json CHANGED
@@ -7,12 +7,12 @@
7
7
  "url": "https://github.com/callstackincubator/agent-device",
8
8
  "source": "github"
9
9
  },
10
- "version": "0.16.6",
10
+ "version": "0.16.7",
11
11
  "packages": [
12
12
  {
13
13
  "registryType": "npm",
14
14
  "identifier": "agent-device",
15
- "version": "0.16.6",
15
+ "version": "0.16.7",
16
16
  "transport": {
17
17
  "type": "stdio"
18
18
  }
@@ -1 +0,0 @@
1
- fc975087b6d0ce63e25cb5d9475f896e828222ec4d7ed12d73e4045cb7cc6c5a agent-device-android-multitouch-helper-0.16.6.apk
@@ -1 +0,0 @@
1
- a6a8555dda194d1e882a930bc6ccf555ff48f1645faf92968a0832f864c91661 agent-device-android-snapshot-helper-0.16.6.apk