@agent-device/revyl 0.21.39 → 0.21.40-nightly.20261119.188
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/runtime.mjs +1 -1
- package/package.json +3 -3
package/dist/runtime.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as e,o as t,r as n,t as r}from"./device.mjs";import{a as i,i as a,o,t as s}from"./errors.mjs";import{i as c,n as l,r as u}from"./snapshot-visibility.mjs";import{r as d}from"./device2.mjs";import{a as f,i as p,n as m,o as h,r as g}from"./revyl-client.mjs";import{n as ee,r as te,t as ne}from"./open-target.mjs";import _ from"node:fs";import v from"node:path";import{createHash as re,randomUUID as y}from"node:crypto";import{setTimeout as b}from"node:timers/promises";function x(e){let t=`${re(`sha256`).update(`agent-device:revyl:session:v1:${e}`).digest(`hex`).slice(0,32)}`;return[t.slice(0,8),t.slice(8,12),`4${t.slice(13,16)}`,`${(Number.parseInt(t.slice(16,17),16)&3|8).toString(16)}${t.slice(17,20)}`,t.slice(20,32)].join(`-`)}const S=24e4,ie=m+S+1e4,ae=new Set([503,409]);async function oe(e,t,r){let i=x(t.leaseId);if(r.signal?.aborted)return{status:`failed`,error:C(r)};let a;try{a=await se(e,r,i)}catch(e){return{status:`failed`,error:e,...T(e)?{rented:{sessionId:i}}:{}}}let o=typeof a.workflow_run_id==`string`?a.workflow_run_id:void 0,c=typeof a.session_id==`string`?a.session_id:void 0;if(o&&c&&c!==i)return{status:`failed`,rented:{runId:o,sessionId:c},error:new s(`COMMAND_FAILED`,`Revyl started the device as session ${c} instead of ${i}.`,{reason:`provider-session-identity-mismatch`,runId:o,providerSessionId:c,hint:`Retry the command; agent-device ends a Revyl session by the identity it issued.`})};if(!o)return{status:`failed`,rented:{sessionId:c??i},error:new s(`COMMAND_FAILED`,`Revyl did not start a ${r.platform} device: ${a.error??a.message??`no workflow run was returned`}`,{reason:`provider-start-refused`,platform:r.platform,...a.trace_id===void 0?{}:{traceId:a.trace_id}})};if(r.onRented?.({runId:o,sessionId:i}),r.signal?.aborted)return{status:`failed`,rented:{runId:o,sessionId:i},error:C(r)};let l=le(r);try{let a=await ue(e,o,l,{...r,sessionId:i});return{status:`ready`,session:{platform:r.platform,runId:o,sessionId:i,screen:me(a),dragDuration:a.supports_explicit_drag_duration===!0,device:n(r.platform,t,o),client:e,lease:t,launchedApp:{}}}}catch(e){return{status:`failed`,error:e,rented:{runId:o,sessionId:i}}}}async function se(e,t,n){if(t.signal?.aborted)throw C(t);let r={platform:t.platform,is_simulation:!0,session_id:n,idle_timeout_seconds:1800,...t.appUrl===void 0?{}:{app_url:t.appUrl},...t.deviceModel===void 0?{}:{device_model:t.deviceModel},...t.osVersion===void 0?{}:{os_version:t.osVersion}};return await e.startDevice(r)}function C(e){return new s(`COMMAND_FAILED`,`The Revyl device start was canceled before it was ready.`,{reason:`request_canceled`,platform:e.platform},e.signal?.reason)}function w(e,t,n,r){let i=`agent-device stops Revyl session ${r} once its start answers; end it from Revyl if it stays listed.`;return t.signal?.aborted?new s(`COMMAND_FAILED`,`The Revyl device start was canceled before it was ready.`,{reason:`request_canceled`,platform:e,providerSessionId:r,hint:i},t.signal.reason):new s(`COMMAND_FAILED`,`Revyl did not report a ready device within ${n} ms.`,{reason:`provider-not-ready`,timeoutMs:n,providerSessionId:r,...t.deadline===void 0?{}:{deadline:t.deadline},hint:`Retry the command; Revyl may have had no device of that model available. ${i}`})}function T(e){return!h(e)}function ce(e,t={},n){let r=e.map(e=>e.sessionId).join(`, `),i=e.length===1?e[0]:void 0;return new s(`COMMAND_FAILED`,`Revyl did not release session${i?``:`s`} ${r}.`,{reason:`provider-cleanup-pending`,...t,providerSessionIds:e.map(e=>e.sessionId),causes:e.map(e=>e.failure),...i?{providerSessionId:i.sessionId,...i.runId===void 0?{}:{runId:i.runId},cause:i.failure}:{},hint:`End the Revyl session${i?``:`s`} ${r}; each device stays rented and billed until it is.`},n)}function le(e){let t=e.deadline===void 0?S:Math.min(S,e.deadline-Date.now());return{at:Date.now()+t,budgetMs:Math.max(0,t)}}async function ue(e,t,n,r){for(await fe(e,t,n,r);;){O(n,r);let i=await E(e,t,n,r);if(O(n,r),i.device_connected===!0)return i;await A(n,r.signal)}}async function E(e,t,n,r){try{return await D(()=>e.getWorkerHealth(t,k(n,r)),n,r)}catch(e){if(!de(e))throw e;return{}}}async function D(e,t,n){try{return await e()}catch(e){throw O(t,n),e}}function de(e){return e instanceof s&&typeof e.details?.providerStatus==`number`&&ae.has(e.details.providerStatus)}async function fe(e,t,n,r){for(;;){O(n,r);let i=await D(()=>e.getWorkerConnection(t,k(n,r)),n,r);if(O(n,r),i.status===`ready`&&i.worker_ws_url)return;if(i.status===`cancelled`||i.status===`stopped`||i.status===`failed`)throw new s(`COMMAND_FAILED`,`Revyl did not start the ${r.platform} device: ${i.status}${i.message===null||i.message===void 0?``:` (${i.message})`}`,{reason:`provider-start-failed`,runId:t,status:i.status});await A(n,r.signal)}}function O(e,t){if(!(Date.now()<e.at))throw w(t.platform,t,e.budgetMs,t.sessionId)}function k(e,t){let n=AbortSignal.timeout(Math.max(0,e.at-Date.now()));return t.signal===void 0?n:AbortSignal.any([t.signal,n])}async function A(e,t){await b(Math.min(2e3,Math.max(0,e.at-Date.now())),void 0,t===void 0?{}:{signal:t}).catch(()=>void 0)}async function pe(e,t){await e.stopDeviceSession(t)}function me(e){let{screen_width:t,screen_height:n}=e;if(!(typeof t!=`number`||typeof n!=`number`||!Number.isFinite(t)||!Number.isFinite(n)||t<=0||n<=0))return{width:t,height:n}}var j=class{lease;generation;phase=`starting`;request;answerBy;abandon=new AbortController;start=Promise.resolve({status:`failed`,error:void 0});answered=!1;answer;markAnswered=()=>void 0;session=void 0;rentals=new Map;latest=void 0;unnamed=!1;waiters=new Set;followUpQueued=!1;ending=void 0;constructor(e,t,n,r){this.lease=e,this.generation=t,this.request=n,this.answerBy=r,this.answer=new Promise(e=>{this.markAnswered=e})}};function he(e){let t=new ge(e);return Object.freeze({live:e=>t.live(e),allocate:async(e,n,r)=>await t.allocate(e,n,r),release:async e=>await t.release(e),recover:async e=>await t.recover(e),shutdown:async()=>await t.shutdown()})}var ge=class{entries=new Map;shuttingDown=!1;generations=0;driver;constructor(e){this.driver=e}live(e){let t=this.entries.get(e);return t?.phase===`ready`?t.session:void 0}isCurrent(e){return this.entries.get(e.lease.leaseId)?.generation===e.generation}refusal(e,t,n){return this.shuttingDown?this.driver.shuttingDown(e):t&&(t.phase===`ending`||!this.isCurrent(t))?this.driver.releasing(e):n()}async allocate(e,t,n={}){let r=this.entries.get(e.leaseId);if(this.shuttingDown||r?.phase===`ending`)throw this.refusal(e,r,()=>0);r?.request!==void 0&&n.requireJoinable?.(r.request);let i=N(n),a=Date.now()+this.driver.startTimeoutMs,o=Math.min(i.deadline??a,a);if(!r&&P(i,o))throw this.driver.gaveUp(e,i,0,void 0);let s=r??this.rent(e,t,i,a),c=Math.max(0,o-Date.now()),l=await this.join(s,i,o);return this.deliver(s,l,i,o,c)}deliver(e,t,n,r,i){if(t.status===`failed`)throw this.refusal(e.lease,void 0,()=>t.error);let a=this.refusal(e.lease,e,()=>P(n,r)?this.driver.gaveUp(e.lease,n,i,e.latest):void 0);if(a!==void 0)throw a;return t.session}async release(e){let t=this.entries.get(e);if(!t)return;let n=await this.teardown(t);if(n.status===`pending`)throw this.pending(t,n,{route:`release`,leaseId:e});return n.status===`stopped`?{handle:n.handle,session:n.session}:void 0}async recover(e){let t=e.leaseId,n=this.entries.get(t);if(n===void 0){if(this.shuttingDown)throw this.driver.shuttingDown(e);n=this.recovering(e)}let r=await this.teardown(n);if(r.status===`pending`)throw this.pending(n,r,{route:`recover`,leaseId:t})}async shutdown(){this.shuttingDown=!0;let e=[],t=[...this.entries.values()];if((await Promise.all(t.map(async e=>await this.teardown(e)))).forEach((n,r)=>{if(n.status!==`pending`)return;let i=t[r].lease.leaseId;for(let t of n.owed)e.push({leaseId:i,...t})}),e.length>0)throw this.driver.cleanupPending(e,{route:`shutdown`})}rent(e,t,n,r){let i=new j(e,this.generations+=1,t,r);return i.start=this.runStart(i,t,n),this.entries.set(e.leaseId,i),i}recovering(e){let t=new j(e,this.generations+=1,void 0,Date.now());return t.answered=!0,t.markAnswered(),t.unnamed=!0,t.phase=`ending`,this.entries.set(e.leaseId,t),t}async runStart(e,t,n){let r=Date.now(),i=await this.callStart(e,t);if(_e(e,i,this.driver.handleKey),i.status===`ready`&&(e.session=i.session,e.phase===`starting`&&this.isCurrent(e)&&F(e)))return e.phase=`ready`,{status:`ready`,session:i.session};let a;try{a=this.startFailure(e,i,n,r)}catch(e){a=e}return await this.endFailedStart(e,a)}async callStart(e,t){try{return await this.driver.start(e.lease,t,{signal:e.abandon.signal,deadline:e.answerBy,rented:t=>{e.answered||(M(e,t,this.driver.handleKey(t)),e.phase===`ending`&&this.followUp(e))}})}catch(e){return{status:`rejected`,error:e}}}startFailure(e,t,n,r){let i=Math.max(0,e.answerBy-r);return this.refusal(e.lease,e,()=>t.status===`ready`?this.driver.gaveUp(e.lease,n,i,t.handle):t.error)}async endFailedStart(e,t){if(e.phase===`ending`)return this.followUp(e),{status:`failed`,error:t};let n=await this.teardown(e);if(n.status!==`pending`)return{status:`failed`,error:t};let r=e.lease.leaseId;return{status:`failed`,error:this.driver.cleanupPending(n.owed.map(e=>({leaseId:r,...e})),{route:`start`,leaseId:r},t)}}async join(e,t,n){if(e.phase===`ready`)return{status:`ready`,session:e.session};let r=Math.max(0,n-Date.now()),i=()=>{try{return this.refusal(e.lease,e,()=>this.driver.gaveUp(e.lease,t,r,e.latest))}catch(e){return e}},a=new AbortController,o=setTimeout(()=>a.abort(),r),s=t.signal?[t.signal,a.signal]:[a.signal],c={deadline:n,signal:t.signal},l=()=>void 0;try{if(e.waiters.add(c),s.some(e=>e.aborted))throw i();let t=new Promise((t,n)=>{l=()=>{let t=i();e.waiters.delete(c),this.abandonIfUnwanted(e),n(t)};for(let e of s)e.addEventListener(`abort`,l,{once:!0})});return await Promise.race([e.start,t])}finally{e.waiters.delete(c),this.abandonIfUnwanted(e),clearTimeout(o);for(let e of s)e.removeEventListener(`abort`,l)}}abandonIfUnwanted(e){e.answered||e.phase===`ending`||F(e)||this.teardown(e).catch(()=>void 0)}teardown(e){if(e.phase=`ending`,e.answered||e.abandon.abort(),e.ending)return e.ending;let t=this.runTeardown(e).finally(()=>{e.ending===t&&(e.ending=void 0)});return e.ending=t,t}async runTeardown(e){let t=new Map;for(;;){await this.untilAnswered(e);let n=await this.nameRental(e);if(n!==void 0)return n;let r=await this.stopRentals(e,t);if(r!==`progress`){if(r.length>0)return{status:`pending`,owed:r};if(!(e.unnamed||[...e.rentals.values()].some(e=>!e.stopped)))return this.finishTeardown(e)}}}async stopRentals(e,t){for(let n of e.rentals.values()){if(n.stopped)continue;if(t.get(n)!==void 0){if(n.stopping!==void 0)break;continue}let e=await this.stop(n);if(e===void 0)return`progress`;if(t.set(n,{handle:n.handle,cause:e.cause}),n.stopping!==void 0)break}return[...e.rentals.values()].filter(e=>!e.stopped).map(e=>t.get(e)??{handle:e.handle,cause:Error(`The stop waits for an earlier stop of this lease that is still in flight.`)})}async nameRental(e){if(e.unnamed)try{let t=await this.driver.findByLease(e.lease);M(e,t,this.driver.handleKey(t)),e.unnamed=!1;return}catch(e){return{status:`pending`,owed:[{handle:void 0,cause:e}]}}}finishTeardown(e){let t=e.latest;return e.answered?(this.isCurrent(e)&&this.entries.delete(e.lease.leaseId),t===void 0?{status:`nothing-rented`}:{status:`stopped`,handle:t,session:e.session}):{status:`pending`,owed:[{handle:t,cause:Error(`The provider start did not answer within its start timeout.`)}]}}followUp(e){let t=e.ending;if(t===void 0){this.teardown(e).catch(()=>void 0);return}e.followUpQueued||(e.followUpQueued=!0,t.then(async()=>(e.followUpQueued=!1,await this.teardown(e))).catch(()=>void 0))}async untilAnswered(e){if(e.answered)return;let t,n=new Promise(n=>{t=setTimeout(n,Math.max(0,e.answerBy-Date.now()))});try{await Promise.race([e.answer,n])}finally{clearTimeout(t)}}async stop(e){e.stopping??=this.stopHandle(e.handle).then(t=>(t===void 0&&(e.stopped=!0),e.stopping=void 0,t));let t=e.stopping,n=this.driver.stopTimeoutMs;if(n===void 0)return await t;let r,i=new Promise(e=>{r=setTimeout(()=>e({cause:Error(`The provider stop did not answer within ${n} ms.`)}),n)});try{return await Promise.race([t,i])}finally{clearTimeout(r)}}async stopHandle(e){try{await this.driver.stop(e);return}catch(e){return{cause:e}}}pending(e,t,n){return this.driver.cleanupPending(t.owed.map(t=>({leaseId:e.lease.leaseId,...t})),n)}};function _e(e,t,n){switch(t.status){case`ready`:M(e,t.handle,n(t.handle));break;case`failed`:t.rented!==void 0&&M(e,t.rented,n(t.rented));break;case`rejected`:e.rentals.size===0&&(e.unnamed=!0)}e.answered=!0,e.markAnswered()}function M(e,t,n){e.rentals.has(n)||e.rentals.set(n,{handle:t,stopped:!1,stopping:void 0}),e.latest=t}function N(e){return{...e.signal===void 0?{}:{signal:e.signal},...e.deadline===void 0?{}:{deadline:e.deadline}}}function P(e,t){return e.signal?.aborted===!0||Date.now()>=t}function F(e){if(e.phase===`ending`)return!1;let t=Date.now();for(let n of e.waiters)if(!n.signal?.aborted&&t<n.deadline)return!0;return!1}new Map([...Object.entries({ios:`ios-instance`,android:`android-instance`,harmonyos:`harmonyos-instance`}).flatMap(([e,t])=>t===void 0?[]:[[t,e]]),[`macos-app`,`macos`]]);function I(e,t={message:e=>`Invalid value: ${e}`}){let n=new Set(e),r=t.normalize??(e=>e);return{is:e=>typeof e==`string`&&n.has(e),parse:e=>{let i=r(e??``);if(n.has(i))return i;throw new s(`INVALID_ARGS`,t.message(e??``))}}}I([`up`,`down`,`left`,`right`],{message:e=>`Unknown direction: ${e}`});function ve(e){let t=e.direction,n=t===`up`||t===`down`,r=n?e.referenceHeight:e.referenceWidth,i=n?e.referenceWidth:e.referenceHeight,a=xe(e.amount),o=e.pixels===void 0?Math.round(r*a):Se(e.pixels),s=L(r),c=Math.max(1,r-s*2),l=Math.max(1,Math.min(o,c)),u=Math.round(l/2),d=Math.round(r/2),f={axisStart:d-u,axisEnd:d+u,cross:Math.round(i/2)},p=L(i),m=e.container?be(f,e.container,n,{axis:{min:s,max:r-s},cross:{min:p,max:i-p},crossFrame:{min:0,max:i}}):f,h=m.axisEnd-m.axisStart,g=h<l;return{direction:t,...ye(t,m),referenceWidth:e.referenceWidth,referenceHeight:e.referenceHeight,amount:e.amount,pixels:g?h:l,...g?{requestedPixels:l}:{}}}function ye(e,t){let n=e===`up`||e===`down`,r=n?{x:t.cross,y:t.axisStart}:{x:t.axisStart,y:t.cross},i=n?{x:t.cross,y:t.axisEnd}:{x:t.axisEnd,y:t.cross},[a,o]=e===`up`||e===`left`?[r,i]:[i,r];return{x1:a.x,y1:a.y,x2:o.x,y2:o.y}}function L(e){return Math.max(1,Math.round(e*.1))}function be(e,t,n,r){let i=R(n?t.y:t.x,n?t.height:t.width,r.axis),a=n?t.x:t.y,o=n?t.width:t.height,s=R(a,o,r.cross)??R(a,o,r.crossFrame);if(!i||!s)return e;let c=e.cross>=s.min&&e.cross<=s.max?e.cross:Math.round((s.min+s.max)/2);if(e.axisEnd-e.axisStart>=i.max-i.min)return{axisStart:i.min,axisEnd:i.max,cross:c};let l=e.axisStart<i.min?i.min-e.axisStart:Math.min(0,i.max-e.axisEnd);return{axisStart:e.axisStart+l,axisEnd:e.axisEnd+l,cross:c}}function R(e,t,n){let r=Math.max(n.min,Math.ceil(e)+8),i=Math.min(n.max,Math.floor(e+t)-1-8);return i>r?{min:r,max:i}:void 0}function xe(e){if(e===void 0)return .6;if(!Number.isFinite(e)||e<=0)throw new s(`INVALID_ARGS`,`scroll amount must be a positive number`);return e}function Se(e){if(!Number.isFinite(e)||e<=0)throw new s(`INVALID_ARGS`,`scroll pixels must be a positive integer`);return Math.max(1,Math.round(e))}Object.freeze({reason:`scroll_keyboard_occludes_surface`,hint:"The on-screen keyboard covers the surface this scroll would swipe, so it cannot reach it. Run `keyboard dismiss` and retry, accepting that it drops focus (re-tap the field to keep typing), or scroll before focusing the field."});const z=new Set([`com.google.android.inputmethod.latin`,`com.samsung.android.honeyboard`,`com.touchtype.swiftkey`,`com.microsoft.swiftkey`]);function Ce(e){let t=B(e.packageName),n=(e.resourceId??``).toLowerCase(),r=B(e.activeInputMethodPackage);if(t&&r&&t===r)return{inputMethodOwned:!0,source:`active-input-method`};if(r&&n.startsWith(`${r}:id/`))return{inputMethodOwned:!0,source:`active-input-method-resource`};if(t&&z.has(t))return{inputMethodOwned:!0,source:`known-ime-package`};for(let e of z)if(n.startsWith(`${e}:id/`))return{inputMethodOwned:!0,source:`known-ime-resource`};return{inputMethodOwned:!1,source:`app`}}function we(e){return Ce(e).inputMethodOwned}function Te(e){return we({packageName:e.bundleId,resourceId:e.identifier})}function B(e){return(e??``).trim().toLowerCase()||void 0}const Ee=new Set([`UIKeyboardDockItemButton`,`UIAccessibilityElementKBKey`]),De=new Set([`TUIPredictionViewCell`]),Oe=new Set([`UIKeyboardLayoutStar`,`TUIPredictionView`]),V=new Set([`_UIRemoteKeyboardPlaceholderView`,`TUISystemInputAssistantView`,`TUISystemInputAssistantPageView`]);function H(e,t){return e.role!==void 0&&t.has(e.role)}Object.freeze({reason:`tap_keyboard_occludes_target`,hint:"The visible keyboard covers this target, so the tap would land on a key instead of on it. End editing first: tap the app's own Done/Cancel/close control, or run `keyboard enter` when submitting is what you want (`keyboard dismiss` works only when the keyboard exposes its own dismiss key). This reads the snapshot the command measured against, so if the keyboard closed since it was taken, run `snapshot -i` and retry."});const ke=new Set([`keyboard`,`key`]);function U(e){return ke.has(l(e.type??``))||Te(e)}function W(e){return U(e)||Ee.has(e.role??``)}function G(e){return U(e)||H(e,Oe)}function Ae(e,t){if(W(e)||H(e,De)||H(e,V)||G(e))return!0;let n=new Set,r=e.parentIndex;for(;typeof r==`number`&&!n.has(r);){n.add(r);let e=t.get(r);if(!e)return!1;if(H(e,V)||G(e))return!0;r=e.parentIndex}return!1}function K(e){return e!==null&&u(e)}function q(e){return e.flatMap(e=>u(e.rect)?[e.rect]:[])}function je(e,t){let n=new Map(e.map(e=>[e.index,e.parentIndex])),r=new Set(t.map(e=>e.index)),i=new Set;for(let e of t)for(let t of Me(n,r,e.index))i.add(t);return i}function Me(e,t,n){let r=[],i=new Set,a=e.get(n);for(;typeof a==`number`&&!i.has(a);)i.add(a),t.has(a)&&r.push(a),a=e.get(a);return r}function Ne(e){let t=e.map(e=>[e.x,e.x+e.width]).sort((e,t)=>e[0]-t[0]),n=t[0]?.[1]??0;for(let[e,r]of t){if(e-n>2)return!1;n=Math.max(n,r)}return!0}function Pe(e){let t=Math.min(...e.anchorRects.map(e=>e.y)),n=Math.min(...e.anchorRects.map(e=>e.x)),r=Math.max(...e.anchorRects.map(e=>e.x+e.width)),i=e.viewport.y+e.viewport.height;return e.reportedBottom<i-80||r-n<=e.reportedBottom-t?null:{x:n,y:t,width:r-n,height:i-t}}function Fe(e){let t=q(e.anchorNodes.filter(t=>!e.planeIndices.has(t.index)));return Ne(t.length>0?t:e.anchorRects)}function Ie(e,t){let n=e.filter(U),r=q(n);if(r.length===0||!K(t))return null;let i=e.filter(W),a=q(i);if(a.length===0)return null;let o=Pe({anchorRects:r,reportedBottom:Math.max(...a.map(e=>e.y+e.height)),viewport:t});return!o||!u(o)||!Fe({anchorNodes:n,anchorRects:r,planeIndices:je(e,i)})?null:o}function J(e){let t=new Map(e.nodes.map(e=>[e.index,e]));return{nodes:e.nodes.map(e=>({...e,keyboardOwned:Ae(e,t)})),keyboard:Le(e)}}function Le(e){let t=Ie(e.nodes,e.viewport);return t?{kind:`derived`,derivation:`docked-tree`,frame:t,treeBound:!0}:K(e.viewport)&&e.nodes.some(U)?{kind:`tree-rejected`,derivation:`docked-tree`,...typeof e.visibility==`boolean`?{visible:e.visibility}:{}}:typeof e.visibility==`boolean`?{kind:`visibility-only`,visible:e.visibility}:{kind:`unmeasurable`,reason:e.nodes.some(U)?`keyboard-viewport-unavailable`:e.visibility===`failed`?`keyboard-visibility-probe-failed`:e.visibility===`unsupported`?`keyboard-visibility-probe-unsupported`:`keyboard-not-observed`}}function Re(e){return{readHierarchy:t=>e.client.getWorkerHierarchy(e.runId,t),viewport:()=>e.screen===void 0?void 0:c({x:0,y:0,...e.screen})}}async function ze(e,t,n){let r=await t.readHierarchy(n);return n?.throwIfAborted(),e.platform===`ios`?await Be(r,e.runId,t.viewport()):await He(r,t.viewport())}async function Be(e,t,n){let[{flattenIosTree:r,resolveIosAxTreeViewport:i},{interpretIosNode:a},o]=await Promise.all([import(`./ios-ax-tree.mjs`),import(`./ios-node-interpretation.mjs`),import(`./ios-snapshot-acquisition.mjs`)]),s=Ve(e),c=i(s,n);return o.createIosSnapshotAcquisition({producer:`revyl-ios-tree`,...J({nodes:r(s).map(e=>a(e)),viewport:c.kind===`missing`?null:c.rect}),viewport:c,lineage:{targetId:t}})}function Ve(e){let t;try{t=JSON.parse(e)}catch(e){throw X(e)}let n=Array.isArray(t)?t:[t];if(!n.every(Y))throw X();return n}function Y(e){if(typeof e!=`object`||!e||Array.isArray(e))return!1;let t=e;return[t.children,t.nodes,t.elements].every(e=>e===void 0||Array.isArray(e)&&e.every(Y))}function X(e){return new s(`COMMAND_FAILED`,`Revyl returned an iOS hierarchy that is not an accessibility node tree.`,{reason:`provider-response-invalid`},e)}async function He(e,t){let[{parseWebDriverAndroidSnapshotNodes:n},{stampSnapshotIdentity:r}]=await Promise.all([import(`./webdriver-android-source.mjs`),import(`./snapshot-producers.mjs`).then(e=>e.n)]),{nodes:i,keyboard:a}=J({nodes:n(e),viewport:t===void 0?null:{x:0,y:0,...t}});return r({nodes:i,keyboard:a,...t===void 0?{}:{viewport:t}},{backend:`android`,producer:`revyl-android-tree`})}var Ue=class{session;signal;constructor(e,t){this.session=e,this.signal=t}async open(e,t){let n=e.trim();if(t?.url)return await this.launch(n,t.launchArgs),await this.workerAction(`open_url`,{url:t.url}),n?{launchedAppBundleId:n}:void 0;if(ne(n)){await this.workerAction(`open_url`,{url:n});return}return await this.launch(n,t?.launchArgs),n?{launchedAppBundleId:n}:void 0}async launch(e,t){if(t?.length)throw new s(`UNSUPPORTED_OPERATION`,`Revyl launches an app without arguments.`,{reason:`unsupported-provider-mode`,hint:`Open a deep link instead: open <url>.`});await this.workerAction(`launch`,e?{bundle_id:e}:{}),e&&(this.session.launchedApp.bundleId=e)}async openDevice(){}async close(e){let t=e.trim();t&&this.requireCloseableApp(t,this.session.launchedApp.bundleId),await this.workerAction(`kill_app`)}requireCloseableApp(e,t){if(t===void 0)throw new s(`UNSUPPORTED_OPERATION`,`This Revyl session has not launched ${e}, so it has no app to end.`,{reason:`provider-app-not-launched`,command:`close`,requestedApp:e,hint:`Close without an app name to end the app this session runs, or open the app first.`});if(e!==t)throw new s(`UNSUPPORTED_OPERATION`,`Revyl ends the app this session launched (${t}), not ${e}.`,{reason:`unsupported-provider-mode`,command:`close`,sessionApp:t,requestedApp:e,hint:"Revyl has no way to name an app to `/kill_app`; end the session to stop what it runs."})}async tap(e,t){await this.workerAction(`tap`,{x:e,y:t})}async doubleTap(e,t){await this.workerAction(`double_tap`,{x:e,y:t})}async longPress(e,t,n=1500){await this.workerAction(`longpress`,{x:e,y:t,duration_ms:n})}async focus(e,t){await this.tap(e,t)}type(){throw Z(`type`,`Revyl enters text at a point; run fill <field> "text" instead of type.`)}async fill(e,t,n,r,o){if(n===``){await this.workerAction(`clear_text`,{x:e,y:t});return}if(!r||r<=0){await this.workerAction(`input`,{x:e,y:t,text:n,clear_first:!0});return}let s=0,c=Array.from(n);try{for(let[n,i]of c.entries()){if(this.signal?.aborted)throw s===0?a(this.cancellation(),`no`):this.cancellation();if(await this.workerAction(`input`,{x:e,y:t,text:i,clear_first:s===0}),s+=1,n===c.length-1)return;await b(r,void 0,this.signal===void 0?{}:{signal:this.signal})}}catch(e){throw i(this.canceledAs(e),s)}}async scroll(e,t){let n=t?.durationMs;n!==void 0&&this.requireDragDuration(n);let r=this.session.screen;if(!r)throw new s(`COMMAND_FAILED`,`Revyl reported no screen size for this session, so the scroll distance cannot be planned.`,{reason:`provider-viewport-unavailable`,runId:this.session.runId});let i=ve({direction:e,...t?.amount===void 0?{}:{amount:t.amount},...t?.pixels===void 0?{}:{pixels:t.pixels},referenceWidth:r.width,referenceHeight:r.height,...t?.containerRect===void 0?{}:{container:Ke(t.containerRect,r)}});return await this.workerAction(`drag`,{start_x:i.x1,start_y:i.y1,end_x:i.x2,end_y:i.y2,...n===void 0?{}:{duration_ms:n}}),{...i,...n===void 0?{}:{durationMs:n},backend:`revyl-device-proxy`}}requireDragDuration(e){if(!this.session.dragDuration)throw new s(`UNSUPPORTED_OPERATION`,`This Revyl worker scrolls with one drag and cannot be told how fast the finger travels.`,{reason:`unsupported-provider-mode`,command:`scroll`,flag:`--duration-ms`,runId:this.session.runId,hint:`Scroll without --duration-ms; the drag runs at the speed Revyl gives it.`});if(e<1)throw new s(`INVALID_ARGS`,`Revyl drag --duration-ms must be at least 1.`,{flag:`--duration-ms`})}async screenshot(e,t){let n=await this.session.client.captureWorkerScreenshot(this.session.runId,this.signal),r=v.dirname(e);_.mkdirSync(r,{recursive:!0});let i=v.join(r,`.${v.basename(e)}.${process.pid}.${y()}.tmp`);try{if(await _.promises.writeFile(i,n,this.signal===void 0?{}:{signal:this.signal}),this.session.platform===`ios`&&await Ge(i,this.session.screen,t?.pixelDensity),this.signal?.aborted===!0)throw this.cancellation();await _.promises.rename(i,e)}catch(e){throw await _.promises.rm(i,{force:!0}).catch(()=>void 0),this.canceledAs(e)}}async snapshot(e){return await ze(this.session,Re(this.session),e?.signal??this.signal)}async back(){await this.workerAction(`back`)}async home(){await this.workerAction(`go_home`)}setOrientation(e){throw Z(`orientation`,`Revyl does not expose orientation control.`)}setSetting(){throw Z(`settings`,`Revyl does not expose settings control.`)}cancellation(e){return new s(`COMMAND_FAILED`,`The Revyl action was canceled before it finished.`,{reason:`request_canceled`,runId:this.session.runId},e??this.signal?.reason)}canceledAs(e){return!this.signal?.aborted||e instanceof s?e:this.cancellation(e)}workerAction(e,t){return this.session.client.workerAction(this.session.runId,e,t,this.signal)}};function We(e,t){return new Ue(e,t)}async function Ge(e,t,n){if(t===void 0)throw new s(`COMMAND_FAILED`,`Revyl reported no screen size for this session, so the screenshot density cannot be known.`,{reason:`provider-viewport-unavailable`});let[{readPngSize:r},{resizePngFile:i},{computeDensityScaledScreenshotSize:a}]=await Promise.all([import(`./png-size.mjs`),import(`./png-resize.mjs`),import(`./screenshot-density.mjs`)]),o=await r(e),c=a(o,Math.max(o.width,o.height)/Math.max(t.width,t.height),n);c&&await i(e,c.width,c.height)}function Ke(e,t){let n=Math.min(e.width,t.width),r=Math.min(e.height,t.height);return{x:Math.max(0,Math.min(e.x,t.width-n)),y:Math.max(0,Math.min(e.y,t.height-r)),width:n,height:r}}function Z(e,t){return new s(`UNSUPPORTED_OPERATION`,t,{reason:`unsupported-provider-mode`,command:e})}function qe(e){return ee(r,[[`backendUrl`,f(e.backendUrl??`https://backend.revyl.ai`)],[`apiKey`,e.apiKey]])}function Je(e){let t=new Ye(e),n=te(r,qe({apiKey:e.apiKey,...e.backendUrl===void 0?{}:{backendUrl:e.backendUrl}}));if(n.kind!==`provider-runtime`)throw TypeError(`Invalid Revyl runtime owner`);return Object.freeze({runtime:t,platformModule:Object.freeze({owner:n,loadRuntime:async e=>await tt(t,n.instance,e)})})}var Ye=class{options;leases;provider=r;leaseLifecycle={allocate:(e,t)=>this.allocate(e,t),release:async e=>await this.release(e)};recoverExpiredLease=async e=>{$e(e,`recover`),await this.leases.recover(e)};deviceInventoryProvider=async e=>{if(e.leaseProvider!==this.provider||!e.leaseId)return null;let t=this.leases.live(e.leaseId);return t?d(t.device,e.platform)?[t.device]:[]:null};constructor(e){if(!e.apiKey.trim())throw TypeError(`Revyl runtime requires an apiKey`);this.options=e;let t=g({apiKey:e.apiKey,...e.backendUrl===void 0?{}:{backendUrl:e.backendUrl},clientVersion:e.clientVersion});this.leases=he(Xe(t))}ownsDevice(t){return e(t.id)!==void 0}hasLiveSession(e){return this.sessionForDevice(e)!==void 0}getInteractor(e,t){let n=this.sessionForDevice(e);return n?We(n,t?.signal):void 0}getDeploymentSession(e){let t=this.sessionForDevice(e);return t?{runId:t.runId,sessionId:t.sessionId,platform:t.platform,client:t.client}:void 0}async shutdown(){await this.leases.shutdown()}async allocate(e,n){let r=e.leaseProvider===this.provider?t(e.backend):void 0;if(r===void 0)return;let{rejectRefusedRevylProfileFields:i,requireSameRevylStartRequest:a,resolveRevylStartRequest:o}=await import(`./session-allocation.mjs`);i(n);let s={...o(n,{...this.options.deviceModel===void 0?{}:{deviceModel:this.options.deviceModel},...this.options.osVersion===void 0?{}:{osVersion:this.options.osVersion}}),platform:r};return $(await this.leases.allocate(e,s,{...Qe(n),requireJoinable:t=>a(e.leaseId,t,s)}))}async release(e){if(e.leaseProvider!==this.provider||!t(e.backend))return;let n=await this.leases.release(e.leaseId);if(n!==void 0)return n.session?$(n.session):{revylRunId:n.handle.runId,providerSessionId:n.handle.sessionId}}sessionForDevice(t){let n=e(t.id);if(!n)return;let r=this.leases.live(n.leaseId);return r?.platform===n.platform?r:void 0}};function Xe(e){return{startTimeoutMs:ie,start:async(t,n,r)=>{let i=await oe(e,t,{platform:n.platform,appUrl:n.appUrl,deviceModel:n.deviceModel,osVersion:n.osVersion,signal:r.signal,onRented:r.rented});if(i.status===`failed`)return i;let{runId:a,sessionId:o}=i.session;return{status:`ready`,session:i.session,handle:{runId:a,sessionId:o}}},stop:async t=>{try{await pe(e,t.sessionId)}catch(e){if(!p(e))throw e}},handleKey:e=>e.sessionId,findByLease:async e=>({sessionId:x(e.leaseId)}),shuttingDown:e=>et(e.leaseId,`allocate`),releasing:e=>new s(`COMMAND_FAILED`,`Revyl lease ${e.leaseId} is being released.`,{reason:`provider-lease-releasing`,leaseId:e.leaseId,hint:`Run open again; it allocates a new lease and rents a fresh device.`}),gaveUp:(e,t,n,r)=>w(Q(e),t,n,r?.sessionId??x(e.leaseId)),cleanupPending:(e,t,n)=>ce(e.map(e=>({...e.handle??{sessionId:x(e.leaseId)},failure:o(e.cause)})),Ze(t),n)}}function Ze(e){switch(e.route){case`shutdown`:return{operation:`shutdown`};case`start`:return{};case`release`:case`recover`:return{leaseId:e.leaseId}}}function Q(e){let n=t(e.backend);if(n===void 0)throw new s(`UNSUPPORTED_OPERATION`,`Revyl cannot serve this lease.`,{leaseId:e.leaseId,leaseProvider:e.leaseProvider,leaseBackend:e.backend});return n}function Qe(e){return{...e?.signal===void 0?{}:{signal:e.signal},...e?.deadline===void 0?{}:{deadline:e.deadline}}}function $e(e,n){if(!(e.leaseProvider===`revyl`&&t(e.backend)))throw new s(`UNSUPPORTED_OPERATION`,`Revyl cannot ${n} this expired lease.`,{leaseId:e.leaseId,leaseProvider:e.leaseProvider,leaseBackend:e.backend})}function et(e,t){return new s(`COMMAND_FAILED`,`The Revyl runtime is shutting down.`,{reason:`runtime-shutting-down`,operation:t,leaseId:e,hint:`Retry the command; the daemon is ending the sessions it holds.`})}async function tt(e,t,n){let{createRevylPlatformRuntimeOwner:r}=await import(`./platform-runtime.mjs`);return r({host:n,runtimeInstance:t,ownsDevice:t=>e.ownsDevice(t),hasLiveSession:t=>e.hasLiveSession(t),getInteractor:(t,n)=>e.getInteractor(t,n),getDeploymentSession:t=>e.getDeploymentSession(t)})}function $(e){return{revylRunId:e.runId,providerSessionId:e.sessionId,device:e.device}}export{Je as createRevylRuntime};
|
|
1
|
+
import{a as e,o as t,r as n,t as r}from"./device.mjs";import{a as i,i as a,o,t as s}from"./errors.mjs";import{i as c,n as l,r as u}from"./snapshot-visibility.mjs";import{r as d}from"./device2.mjs";import{a as f,i as p,n as m,o as h,r as g}from"./revyl-client.mjs";import{n as ee,r as te,t as ne}from"./open-target.mjs";import _ from"node:fs";import v from"node:path";import{createHash as re,randomUUID as y}from"node:crypto";import{setTimeout as b}from"node:timers/promises";function x(e){let t=`${re(`sha256`).update(`agent-device:revyl:session:v1:${e}`).digest(`hex`).slice(0,32)}`;return[t.slice(0,8),t.slice(8,12),`4${t.slice(13,16)}`,`${(Number.parseInt(t.slice(16,17),16)&3|8).toString(16)}${t.slice(17,20)}`,t.slice(20,32)].join(`-`)}const S=24e4,ie=m+S+1e4,ae=new Set([503,409]);async function oe(e,t,r){let i=x(t.leaseId);if(r.signal?.aborted)return{status:`failed`,error:C(r)};let a;try{a=await se(e,r,i)}catch(e){return{status:`failed`,error:e,...T(e)?{rented:{sessionId:i}}:{}}}let o=typeof a.workflow_run_id==`string`?a.workflow_run_id:void 0,c=typeof a.session_id==`string`?a.session_id:void 0;if(o&&c&&c!==i)return{status:`failed`,rented:{runId:o,sessionId:c},error:new s(`COMMAND_FAILED`,`Revyl started the device as session ${c} instead of ${i}.`,{reason:`provider-session-identity-mismatch`,runId:o,providerSessionId:c,hint:`Retry the command; agent-device ends a Revyl session by the identity it issued.`})};if(!o)return{status:`failed`,rented:{sessionId:c??i},error:new s(`COMMAND_FAILED`,`Revyl did not start a ${r.platform} device: ${a.error??a.message??`no workflow run was returned`}`,{reason:`provider-start-refused`,platform:r.platform,...a.trace_id===void 0?{}:{traceId:a.trace_id}})};if(r.onRented?.({runId:o,sessionId:i}),r.signal?.aborted)return{status:`failed`,rented:{runId:o,sessionId:i},error:C(r)};let l=le(r);try{let a=await ue(e,o,l,{...r,sessionId:i});return{status:`ready`,session:{platform:r.platform,runId:o,sessionId:i,screen:me(a),dragDuration:a.supports_explicit_drag_duration===!0,device:n(r.platform,t,o),client:e,lease:t,launchedApp:{}}}}catch(e){return{status:`failed`,error:e,rented:{runId:o,sessionId:i}}}}async function se(e,t,n){if(t.signal?.aborted)throw C(t);let r={platform:t.platform,is_simulation:!0,session_id:n,idle_timeout_seconds:1800,...t.appUrl===void 0?{}:{app_url:t.appUrl},...t.deviceModel===void 0?{}:{device_model:t.deviceModel},...t.osVersion===void 0?{}:{os_version:t.osVersion}};return await e.startDevice(r)}function C(e){return new s(`COMMAND_FAILED`,`The Revyl device start was canceled before it was ready.`,{reason:`request_canceled`,platform:e.platform},e.signal?.reason)}function w(e,t,n,r){let i=`agent-device stops Revyl session ${r} once its start answers; end it from Revyl if it stays listed.`;return t.signal?.aborted?new s(`COMMAND_FAILED`,`The Revyl device start was canceled before it was ready.`,{reason:`request_canceled`,platform:e,providerSessionId:r,hint:i},t.signal.reason):new s(`COMMAND_FAILED`,`Revyl did not report a ready device within ${n} ms.`,{reason:`provider-not-ready`,timeoutMs:n,providerSessionId:r,...t.deadline===void 0?{}:{deadline:t.deadline},hint:`Retry the command; Revyl may have had no device of that model available. ${i}`})}function T(e){return!h(e)}function ce(e,t={},n){let r=e.map(e=>e.sessionId).join(`, `),i=e.length===1?e[0]:void 0;return new s(`COMMAND_FAILED`,`Revyl did not release session${i?``:`s`} ${r}.`,{reason:`provider-cleanup-pending`,...t,providerSessionIds:e.map(e=>e.sessionId),causes:e.map(e=>e.failure),...i?{providerSessionId:i.sessionId,...i.runId===void 0?{}:{runId:i.runId},cause:i.failure}:{},hint:`End the Revyl session${i?``:`s`} ${r}; each device stays rented and billed until it is.`},n)}function le(e){let t=e.deadline===void 0?S:Math.min(S,e.deadline-Date.now());return{at:Date.now()+t,budgetMs:Math.max(0,t)}}async function ue(e,t,n,r){for(await fe(e,t,n,r);;){O(n,r);let i=await E(e,t,n,r);if(O(n,r),i.device_connected===!0)return i;await A(n,r.signal)}}async function E(e,t,n,r){try{return await D(()=>e.getWorkerHealth(t,k(n,r)),n,r)}catch(e){if(!de(e))throw e;return{}}}async function D(e,t,n){try{return await e()}catch(e){throw O(t,n),e}}function de(e){return e instanceof s&&typeof e.details?.providerStatus==`number`&&ae.has(e.details.providerStatus)}async function fe(e,t,n,r){for(;;){O(n,r);let i=await D(()=>e.getWorkerConnection(t,k(n,r)),n,r);if(O(n,r),i.status===`ready`&&i.worker_ws_url)return;if(i.status===`cancelled`||i.status===`stopped`||i.status===`failed`)throw new s(`COMMAND_FAILED`,`Revyl did not start the ${r.platform} device: ${i.status}${i.message===null||i.message===void 0?``:` (${i.message})`}`,{reason:`provider-start-failed`,runId:t,status:i.status});await A(n,r.signal)}}function O(e,t){if(!(Date.now()<e.at))throw w(t.platform,t,e.budgetMs,t.sessionId)}function k(e,t){let n=AbortSignal.timeout(Math.max(0,e.at-Date.now()));return t.signal===void 0?n:AbortSignal.any([t.signal,n])}async function A(e,t){await b(Math.min(2e3,Math.max(0,e.at-Date.now())),void 0,t===void 0?{}:{signal:t}).catch(()=>void 0)}async function pe(e,t){await e.stopDeviceSession(t)}function me(e){let{screen_width:t,screen_height:n}=e;if(!(typeof t!=`number`||typeof n!=`number`||!Number.isFinite(t)||!Number.isFinite(n)||t<=0||n<=0))return{width:t,height:n}}var j=class{lease;generation;phase=`starting`;request;answerBy;abandon=new AbortController;start=Promise.resolve({status:`failed`,error:void 0});answered=!1;answer;markAnswered=()=>void 0;session=void 0;rentals=new Map;latest=void 0;unnamed=!1;waiters=new Set;followUpQueued=!1;ending=void 0;constructor(e,t,n,r){this.lease=e,this.generation=t,this.request=n,this.answerBy=r,this.answer=new Promise(e=>{this.markAnswered=e})}};function he(e){let t=new ge(e);return Object.freeze({live:e=>t.live(e),allocate:async(e,n,r)=>await t.allocate(e,n,r),release:async e=>await t.release(e),recover:async e=>await t.recover(e),shutdown:async()=>await t.shutdown()})}var ge=class{entries=new Map;shuttingDown=!1;generations=0;driver;constructor(e){this.driver=e}live(e){let t=this.entries.get(e);return t?.phase===`ready`?t.session:void 0}isCurrent(e){return this.entries.get(e.lease.leaseId)?.generation===e.generation}refusal(e,t,n){return this.shuttingDown?this.driver.shuttingDown(e):t&&(t.phase===`ending`||!this.isCurrent(t))?this.driver.releasing(e):n()}async allocate(e,t,n={}){let r=this.entries.get(e.leaseId);if(this.shuttingDown||r?.phase===`ending`)throw this.refusal(e,r,()=>0);r?.request!==void 0&&n.requireJoinable?.(r.request);let i=N(n),a=Date.now()+this.driver.startTimeoutMs,o=Math.min(i.deadline??a,a);if(!r&&P(i,o))throw this.driver.gaveUp(e,i,0,void 0);let s=r??this.rent(e,t,i,a),c=Math.max(0,o-Date.now()),l=await this.join(s,i,o);return this.deliver(s,l,i,o,c)}deliver(e,t,n,r,i){if(t.status===`failed`)throw this.refusal(e.lease,void 0,()=>t.error);let a=this.refusal(e.lease,e,()=>P(n,r)?this.driver.gaveUp(e.lease,n,i,e.latest):void 0);if(a!==void 0)throw a;return t.session}async release(e){let t=this.entries.get(e);if(!t)return;let n=await this.teardown(t);if(n.status===`pending`)throw this.pending(t,n,{route:`release`,leaseId:e});return n.status===`stopped`?{handle:n.handle,session:n.session}:void 0}async recover(e){let t=e.leaseId,n=this.entries.get(t);if(n===void 0){if(this.shuttingDown)throw this.driver.shuttingDown(e);n=this.recovering(e)}let r=await this.teardown(n);if(r.status===`pending`)throw this.pending(n,r,{route:`recover`,leaseId:t})}async shutdown(){this.shuttingDown=!0;let e=[],t=[...this.entries.values()];if((await Promise.all(t.map(async e=>await this.teardown(e)))).forEach((n,r)=>{if(n.status!==`pending`)return;let i=t[r].lease.leaseId;for(let t of n.owed)e.push({leaseId:i,...t})}),e.length>0)throw this.driver.cleanupPending(e,{route:`shutdown`})}rent(e,t,n,r){let i=new j(e,this.generations+=1,t,r);return i.start=this.runStart(i,t,n),this.entries.set(e.leaseId,i),i}recovering(e){let t=new j(e,this.generations+=1,void 0,Date.now());return t.answered=!0,t.markAnswered(),t.unnamed=!0,t.phase=`ending`,this.entries.set(e.leaseId,t),t}async runStart(e,t,n){let r=Date.now(),i=await this.callStart(e,t);if(_e(e,i,this.driver.handleKey),i.status===`ready`&&(e.session=i.session,e.phase===`starting`&&this.isCurrent(e)&&F(e)))return e.phase=`ready`,{status:`ready`,session:i.session};let a;try{a=this.startFailure(e,i,n,r)}catch(e){a=e}return await this.endFailedStart(e,a)}async callStart(e,t){try{return await this.driver.start(e.lease,t,{signal:e.abandon.signal,deadline:e.answerBy,rented:t=>{e.answered||(M(e,t,this.driver.handleKey(t)),e.phase===`ending`&&this.followUp(e))}})}catch(e){return{status:`rejected`,error:e}}}startFailure(e,t,n,r){let i=Math.max(0,e.answerBy-r);return this.refusal(e.lease,e,()=>t.status===`ready`?this.driver.gaveUp(e.lease,n,i,t.handle):t.error)}async endFailedStart(e,t){if(e.phase===`ending`)return this.followUp(e),{status:`failed`,error:t};let n=await this.teardown(e);if(n.status!==`pending`)return{status:`failed`,error:t};let r=e.lease.leaseId;return{status:`failed`,error:this.driver.cleanupPending(n.owed.map(e=>({leaseId:r,...e})),{route:`start`,leaseId:r},t)}}async join(e,t,n){if(e.phase===`ready`)return{status:`ready`,session:e.session};let r=Math.max(0,n-Date.now()),i=()=>{try{return this.refusal(e.lease,e,()=>this.driver.gaveUp(e.lease,t,r,e.latest))}catch(e){return e}},a=new AbortController,o=setTimeout(()=>a.abort(),r),s=t.signal?[t.signal,a.signal]:[a.signal],c={deadline:n,signal:t.signal},l=()=>void 0;try{if(e.waiters.add(c),s.some(e=>e.aborted))throw i();let t=new Promise((t,n)=>{l=()=>{let t=i();e.waiters.delete(c),this.abandonIfUnwanted(e),n(t)};for(let e of s)e.addEventListener(`abort`,l,{once:!0})});return await Promise.race([e.start,t])}finally{e.waiters.delete(c),this.abandonIfUnwanted(e),clearTimeout(o);for(let e of s)e.removeEventListener(`abort`,l)}}abandonIfUnwanted(e){e.answered||e.phase===`ending`||F(e)||this.teardown(e).catch(()=>void 0)}teardown(e){if(e.phase=`ending`,e.answered||e.abandon.abort(),e.ending)return e.ending;let t=this.runTeardown(e).finally(()=>{e.ending===t&&(e.ending=void 0)});return e.ending=t,t}async runTeardown(e){let t=new Map;for(;;){await this.untilAnswered(e);let n=await this.nameRental(e);if(n!==void 0)return n;let r=await this.stopRentals(e,t);if(r!==`progress`){if(r.length>0)return{status:`pending`,owed:r};if(!(e.unnamed||[...e.rentals.values()].some(e=>!e.stopped)))return this.finishTeardown(e)}}}async stopRentals(e,t){for(let n of e.rentals.values()){if(n.stopped)continue;if(t.get(n)!==void 0){if(n.stopping!==void 0)break;continue}let e=await this.stop(n);if(e===void 0)return`progress`;if(t.set(n,{handle:n.handle,cause:e.cause}),n.stopping!==void 0)break}return[...e.rentals.values()].filter(e=>!e.stopped).map(e=>t.get(e)??{handle:e.handle,cause:Error(`The stop waits for an earlier stop of this lease that is still in flight.`)})}async nameRental(e){if(e.unnamed)try{let t=await this.driver.findByLease(e.lease);M(e,t,this.driver.handleKey(t)),e.unnamed=!1;return}catch(e){return{status:`pending`,owed:[{handle:void 0,cause:e}]}}}finishTeardown(e){let t=e.latest;return e.answered?(this.isCurrent(e)&&this.entries.delete(e.lease.leaseId),t===void 0?{status:`nothing-rented`}:{status:`stopped`,handle:t,session:e.session}):{status:`pending`,owed:[{handle:t,cause:Error(`The provider start did not answer within its start timeout.`)}]}}followUp(e){let t=e.ending;if(t===void 0){this.teardown(e).catch(()=>void 0);return}e.followUpQueued||(e.followUpQueued=!0,t.then(async()=>(e.followUpQueued=!1,await this.teardown(e))).catch(()=>void 0))}async untilAnswered(e){if(e.answered)return;let t,n=new Promise(n=>{t=setTimeout(n,Math.max(0,e.answerBy-Date.now()))});try{await Promise.race([e.answer,n])}finally{clearTimeout(t)}}async stop(e){e.stopping??=this.stopHandle(e.handle).then(t=>(t===void 0&&(e.stopped=!0),e.stopping=void 0,t));let t=e.stopping,n=this.driver.stopTimeoutMs;if(n===void 0)return await t;let r,i=new Promise(e=>{r=setTimeout(()=>e({cause:Error(`The provider stop did not answer within ${n} ms.`)}),n)});try{return await Promise.race([t,i])}finally{clearTimeout(r)}}async stopHandle(e){try{await this.driver.stop(e);return}catch(e){return{cause:e}}}pending(e,t,n){return this.driver.cleanupPending(t.owed.map(t=>({leaseId:e.lease.leaseId,...t})),n)}};function _e(e,t,n){switch(t.status){case`ready`:M(e,t.handle,n(t.handle));break;case`failed`:t.rented!==void 0&&M(e,t.rented,n(t.rented));break;case`rejected`:e.rentals.size===0&&(e.unnamed=!0)}e.answered=!0,e.markAnswered()}function M(e,t,n){e.rentals.has(n)||e.rentals.set(n,{handle:t,stopped:!1,stopping:void 0}),e.latest=t}function N(e){return{...e.signal===void 0?{}:{signal:e.signal},...e.deadline===void 0?{}:{deadline:e.deadline}}}function P(e,t){return e.signal?.aborted===!0||Date.now()>=t}function F(e){if(e.phase===`ending`)return!1;let t=Date.now();for(let n of e.waiters)if(!n.signal?.aborted&&t<n.deadline)return!0;return!1}new Map([...Object.entries({ios:`ios-instance`,android:`android-instance`,harmonyos:`harmonyos-instance`}).flatMap(([e,t])=>t===void 0?[]:[[t,e]]),[`macos-app`,`macos`]]);function I(e,t={message:e=>`Invalid value: ${e}`}){let n=new Set(e),r=t.normalize??(e=>e);return{is:e=>typeof e==`string`&&n.has(e),parse:e=>{let i=r(e??``);if(n.has(i))return i;throw new s(`INVALID_ARGS`,t.message(e??``))}}}I([`up`,`down`,`left`,`right`],{message:e=>`Unknown direction: ${e}`});function ve(e){let t=e.direction,n=t===`up`||t===`down`,r=n?e.referenceHeight:e.referenceWidth,i=n?e.referenceWidth:e.referenceHeight,a=xe(e.amount),o=e.pixels===void 0?Math.round(r*a):Se(e.pixels),s=L(r),c=Math.max(1,r-s*2),l=Math.max(1,Math.min(o,c)),u=Math.round(l/2),d=Math.round(r/2),f={axisStart:d-u,axisEnd:d+u,cross:Math.round(i/2)},p=L(i),m=e.container?be(f,e.container,n,{axis:{min:s,max:r-s},cross:{min:p,max:i-p},crossFrame:{min:0,max:i}}):f,h=m.axisEnd-m.axisStart,g=h<l;return{direction:t,...ye(t,m),referenceWidth:e.referenceWidth,referenceHeight:e.referenceHeight,amount:e.amount,pixels:g?h:l,...g?{requestedPixels:l}:{}}}function ye(e,t){let n=e===`up`||e===`down`,r=n?{x:t.cross,y:t.axisStart}:{x:t.axisStart,y:t.cross},i=n?{x:t.cross,y:t.axisEnd}:{x:t.axisEnd,y:t.cross},[a,o]=e===`up`||e===`left`?[r,i]:[i,r];return{x1:a.x,y1:a.y,x2:o.x,y2:o.y}}function L(e){return Math.max(1,Math.round(e*.1))}function be(e,t,n,r){let i=R(n?t.y:t.x,n?t.height:t.width,r.axis),a=n?t.x:t.y,o=n?t.width:t.height,s=R(a,o,r.cross)??R(a,o,r.crossFrame);if(!i||!s)return e;let c=e.cross>=s.min&&e.cross<=s.max?e.cross:Math.round((s.min+s.max)/2);if(e.axisEnd-e.axisStart>=i.max-i.min)return{axisStart:i.min,axisEnd:i.max,cross:c};let l=e.axisStart<i.min?i.min-e.axisStart:Math.min(0,i.max-e.axisEnd);return{axisStart:e.axisStart+l,axisEnd:e.axisEnd+l,cross:c}}function R(e,t,n){let r=Math.max(n.min,Math.ceil(e)+8),i=Math.min(n.max,Math.floor(e+t)-1-8);return i>r?{min:r,max:i}:void 0}function xe(e){if(e===void 0)return .6;if(!Number.isFinite(e)||e<=0)throw new s(`INVALID_ARGS`,`scroll amount must be a positive number`);return e}function Se(e){if(!Number.isFinite(e)||e<=0)throw new s(`INVALID_ARGS`,`scroll pixels must be a positive integer`);return Math.max(1,Math.round(e))}Object.freeze({reason:`scroll_keyboard_occludes_surface`,hint:"The on-screen keyboard covers the surface this scroll would swipe, so it cannot reach it. End editing with a visible dismissal control, or use `keyboard dismiss` on Android. You can also scroll before focusing the field."});const z=new Set([`com.google.android.inputmethod.latin`,`com.samsung.android.honeyboard`,`com.touchtype.swiftkey`,`com.microsoft.swiftkey`]);function Ce(e){let t=B(e.packageName),n=(e.resourceId??``).toLowerCase(),r=B(e.activeInputMethodPackage);if(t&&r&&t===r)return{inputMethodOwned:!0,source:`active-input-method`};if(r&&n.startsWith(`${r}:id/`))return{inputMethodOwned:!0,source:`active-input-method-resource`};if(t&&z.has(t))return{inputMethodOwned:!0,source:`known-ime-package`};for(let e of z)if(n.startsWith(`${e}:id/`))return{inputMethodOwned:!0,source:`known-ime-resource`};return{inputMethodOwned:!1,source:`app`}}function we(e){return Ce(e).inputMethodOwned}function Te(e){return we({packageName:e.bundleId,resourceId:e.identifier})}function B(e){return(e??``).trim().toLowerCase()||void 0}const Ee=new Set([`UIKeyboardDockItemButton`,`UIAccessibilityElementKBKey`]),De=new Set([`TUIPredictionViewCell`]),Oe=new Set([`UIKeyboardLayoutStar`,`TUIPredictionView`]),V=new Set([`_UIRemoteKeyboardPlaceholderView`,`TUISystemInputAssistantView`,`TUISystemInputAssistantPageView`]);function H(e,t){return e.role!==void 0&&t.has(e.role)}Object.freeze({reason:`tap_keyboard_occludes_target`,hint:"The visible keyboard covers this target, so the tap would land on a key instead of on it. End editing first. Press a visible dismiss key such as Hide Keyboard, or the app control that ends editing. On Android, use `keyboard dismiss`. Use `keyboard enter` when you intend to send Return. This reads the snapshot the command measured against, so if the keyboard closed since it was taken, run `snapshot -i` and retry."});const ke=new Set([`keyboard`,`key`]);function U(e){return ke.has(l(e.type??``))||Te(e)}function W(e){return U(e)||Ee.has(e.role??``)}function G(e){return U(e)||H(e,Oe)}function Ae(e,t){if(W(e)||H(e,De)||H(e,V)||G(e))return!0;let n=new Set,r=e.parentIndex;for(;typeof r==`number`&&!n.has(r);){n.add(r);let e=t.get(r);if(!e)return!1;if(H(e,V)||G(e))return!0;r=e.parentIndex}return!1}function K(e){return e!==null&&u(e)}function q(e){return e.flatMap(e=>u(e.rect)?[e.rect]:[])}function je(e,t){let n=new Map(e.map(e=>[e.index,e.parentIndex])),r=new Set(t.map(e=>e.index)),i=new Set;for(let e of t)for(let t of Me(n,r,e.index))i.add(t);return i}function Me(e,t,n){let r=[],i=new Set,a=e.get(n);for(;typeof a==`number`&&!i.has(a);)i.add(a),t.has(a)&&r.push(a),a=e.get(a);return r}function Ne(e){let t=e.map(e=>[e.x,e.x+e.width]).sort((e,t)=>e[0]-t[0]),n=t[0]?.[1]??0;for(let[e,r]of t){if(e-n>2)return!1;n=Math.max(n,r)}return!0}function Pe(e){let t=Math.min(...e.anchorRects.map(e=>e.y)),n=Math.min(...e.anchorRects.map(e=>e.x)),r=Math.max(...e.anchorRects.map(e=>e.x+e.width)),i=e.viewport.y+e.viewport.height;return e.reportedBottom<i-80||r-n<=e.reportedBottom-t?null:{x:n,y:t,width:r-n,height:i-t}}function Fe(e){let t=q(e.anchorNodes.filter(t=>!e.planeIndices.has(t.index)));return Ne(t.length>0?t:e.anchorRects)}function Ie(e,t){let n=e.filter(U),r=q(n);if(r.length===0||!K(t))return null;let i=e.filter(W),a=q(i);if(a.length===0)return null;let o=Pe({anchorRects:r,reportedBottom:Math.max(...a.map(e=>e.y+e.height)),viewport:t});return!o||!u(o)||!Fe({anchorNodes:n,anchorRects:r,planeIndices:je(e,i)})?null:o}function J(e){let t=new Map(e.nodes.map(e=>[e.index,e]));return{nodes:e.nodes.map(e=>({...e,keyboardOwned:Ae(e,t)})),keyboard:Le(e)}}function Le(e){let t=Ie(e.nodes,e.viewport);return t?{kind:`derived`,derivation:`docked-tree`,frame:t,treeBound:!0}:K(e.viewport)&&e.nodes.some(U)?{kind:`tree-rejected`,derivation:`docked-tree`,...typeof e.visibility==`boolean`?{visible:e.visibility}:{}}:typeof e.visibility==`boolean`?{kind:`visibility-only`,visible:e.visibility}:{kind:`unmeasurable`,reason:e.nodes.some(U)?`keyboard-viewport-unavailable`:e.visibility===`failed`?`keyboard-visibility-probe-failed`:e.visibility===`unsupported`?`keyboard-visibility-probe-unsupported`:`keyboard-not-observed`}}function Re(e){return{readHierarchy:t=>e.client.getWorkerHierarchy(e.runId,t),viewport:()=>e.screen===void 0?void 0:c({x:0,y:0,...e.screen})}}async function ze(e,t,n){let r=await t.readHierarchy(n);return n?.throwIfAborted(),e.platform===`ios`?await Be(r,e.runId,t.viewport()):await He(r,t.viewport())}async function Be(e,t,n){let[{flattenIosTree:r,resolveIosAxTreeViewport:i},{interpretIosNode:a},o]=await Promise.all([import(`./ios-ax-tree.mjs`),import(`./ios-node-interpretation.mjs`),import(`./ios-snapshot-acquisition.mjs`)]),s=Ve(e),c=i(s,n);return o.createIosSnapshotAcquisition({producer:`revyl-ios-tree`,...J({nodes:r(s).map(e=>a(e)),viewport:c.kind===`missing`?null:c.rect}),viewport:c,lineage:{targetId:t}})}function Ve(e){let t;try{t=JSON.parse(e)}catch(e){throw X(e)}let n=Array.isArray(t)?t:[t];if(!n.every(Y))throw X();return n}function Y(e){if(typeof e!=`object`||!e||Array.isArray(e))return!1;let t=e;return[t.children,t.nodes,t.elements].every(e=>e===void 0||Array.isArray(e)&&e.every(Y))}function X(e){return new s(`COMMAND_FAILED`,`Revyl returned an iOS hierarchy that is not an accessibility node tree.`,{reason:`provider-response-invalid`},e)}async function He(e,t){let[{parseWebDriverAndroidSnapshotNodes:n},{stampSnapshotIdentity:r}]=await Promise.all([import(`./webdriver-android-source.mjs`),import(`./snapshot-producers.mjs`).then(e=>e.n)]),{nodes:i,keyboard:a}=J({nodes:n(e),viewport:t===void 0?null:{x:0,y:0,...t}});return r({nodes:i,keyboard:a,...t===void 0?{}:{viewport:t}},{backend:`android`,producer:`revyl-android-tree`})}var Ue=class{session;signal;constructor(e,t){this.session=e,this.signal=t}async open(e,t){let n=e.trim();if(t?.url)return await this.launch(n,t.launchArgs),await this.workerAction(`open_url`,{url:t.url}),n?{launchedAppBundleId:n}:void 0;if(ne(n)){await this.workerAction(`open_url`,{url:n});return}return await this.launch(n,t?.launchArgs),n?{launchedAppBundleId:n}:void 0}async launch(e,t){if(t?.length)throw new s(`UNSUPPORTED_OPERATION`,`Revyl launches an app without arguments.`,{reason:`unsupported-provider-mode`,hint:`Open a deep link instead: open <url>.`});await this.workerAction(`launch`,e?{bundle_id:e}:{}),e&&(this.session.launchedApp.bundleId=e)}async openDevice(){}async close(e){let t=e.trim();t&&this.requireCloseableApp(t,this.session.launchedApp.bundleId),await this.workerAction(`kill_app`)}requireCloseableApp(e,t){if(t===void 0)throw new s(`UNSUPPORTED_OPERATION`,`This Revyl session has not launched ${e}, so it has no app to end.`,{reason:`provider-app-not-launched`,command:`close`,requestedApp:e,hint:`Close without an app name to end the app this session runs, or open the app first.`});if(e!==t)throw new s(`UNSUPPORTED_OPERATION`,`Revyl ends the app this session launched (${t}), not ${e}.`,{reason:`unsupported-provider-mode`,command:`close`,sessionApp:t,requestedApp:e,hint:"Revyl has no way to name an app to `/kill_app`; end the session to stop what it runs."})}async tap(e,t){await this.workerAction(`tap`,{x:e,y:t})}async doubleTap(e,t){await this.workerAction(`double_tap`,{x:e,y:t})}async longPress(e,t,n=1500){await this.workerAction(`longpress`,{x:e,y:t,duration_ms:n})}async focus(e,t){await this.tap(e,t)}type(){throw Z(`type`,`Revyl enters text at a point; run fill <field> "text" instead of type.`)}async fill(e,t,n,r,o){if(n===``){await this.workerAction(`clear_text`,{x:e,y:t});return}if(!r||r<=0){await this.workerAction(`input`,{x:e,y:t,text:n,clear_first:!0});return}let s=0,c=Array.from(n);try{for(let[n,i]of c.entries()){if(this.signal?.aborted)throw s===0?a(this.cancellation(),`no`):this.cancellation();if(await this.workerAction(`input`,{x:e,y:t,text:i,clear_first:s===0}),s+=1,n===c.length-1)return;await b(r,void 0,this.signal===void 0?{}:{signal:this.signal})}}catch(e){throw i(this.canceledAs(e),s)}}async scroll(e,t){let n=t?.durationMs;n!==void 0&&this.requireDragDuration(n);let r=this.session.screen;if(!r)throw new s(`COMMAND_FAILED`,`Revyl reported no screen size for this session, so the scroll distance cannot be planned.`,{reason:`provider-viewport-unavailable`,runId:this.session.runId});let i=ve({direction:e,...t?.amount===void 0?{}:{amount:t.amount},...t?.pixels===void 0?{}:{pixels:t.pixels},referenceWidth:r.width,referenceHeight:r.height,...t?.containerRect===void 0?{}:{container:Ke(t.containerRect,r)}});return await this.workerAction(`drag`,{start_x:i.x1,start_y:i.y1,end_x:i.x2,end_y:i.y2,...n===void 0?{}:{duration_ms:n}}),{...i,...n===void 0?{}:{durationMs:n},backend:`revyl-device-proxy`}}requireDragDuration(e){if(!this.session.dragDuration)throw new s(`UNSUPPORTED_OPERATION`,`This Revyl worker scrolls with one drag and cannot be told how fast the finger travels.`,{reason:`unsupported-provider-mode`,command:`scroll`,flag:`--duration-ms`,runId:this.session.runId,hint:`Scroll without --duration-ms; the drag runs at the speed Revyl gives it.`});if(e<1)throw new s(`INVALID_ARGS`,`Revyl drag --duration-ms must be at least 1.`,{flag:`--duration-ms`})}async screenshot(e,t){let n=await this.session.client.captureWorkerScreenshot(this.session.runId,this.signal),r=v.dirname(e);_.mkdirSync(r,{recursive:!0});let i=v.join(r,`.${v.basename(e)}.${process.pid}.${y()}.tmp`);try{if(await _.promises.writeFile(i,n,this.signal===void 0?{}:{signal:this.signal}),this.session.platform===`ios`&&await Ge(i,this.session.screen,t?.pixelDensity),this.signal?.aborted===!0)throw this.cancellation();await _.promises.rename(i,e)}catch(e){throw await _.promises.rm(i,{force:!0}).catch(()=>void 0),this.canceledAs(e)}}async snapshot(e){return await ze(this.session,Re(this.session),e?.signal??this.signal)}async back(){await this.workerAction(`back`)}async home(){await this.workerAction(`go_home`)}setOrientation(e){throw Z(`orientation`,`Revyl does not expose orientation control.`)}setSetting(){throw Z(`settings`,`Revyl does not expose settings control.`)}cancellation(e){return new s(`COMMAND_FAILED`,`The Revyl action was canceled before it finished.`,{reason:`request_canceled`,runId:this.session.runId},e??this.signal?.reason)}canceledAs(e){return!this.signal?.aborted||e instanceof s?e:this.cancellation(e)}workerAction(e,t){return this.session.client.workerAction(this.session.runId,e,t,this.signal)}};function We(e,t){return new Ue(e,t)}async function Ge(e,t,n){if(t===void 0)throw new s(`COMMAND_FAILED`,`Revyl reported no screen size for this session, so the screenshot density cannot be known.`,{reason:`provider-viewport-unavailable`});let[{readPngSize:r},{resizePngFile:i},{computeDensityScaledScreenshotSize:a}]=await Promise.all([import(`./png-size.mjs`),import(`./png-resize.mjs`),import(`./screenshot-density.mjs`)]),o=await r(e),c=a(o,Math.max(o.width,o.height)/Math.max(t.width,t.height),n);c&&await i(e,c.width,c.height)}function Ke(e,t){let n=Math.min(e.width,t.width),r=Math.min(e.height,t.height);return{x:Math.max(0,Math.min(e.x,t.width-n)),y:Math.max(0,Math.min(e.y,t.height-r)),width:n,height:r}}function Z(e,t){return new s(`UNSUPPORTED_OPERATION`,t,{reason:`unsupported-provider-mode`,command:e})}function qe(e){return ee(r,[[`backendUrl`,f(e.backendUrl??`https://backend.revyl.ai`)],[`apiKey`,e.apiKey]])}function Je(e){let t=new Ye(e),n=te(r,qe({apiKey:e.apiKey,...e.backendUrl===void 0?{}:{backendUrl:e.backendUrl}}));if(n.kind!==`provider-runtime`)throw TypeError(`Invalid Revyl runtime owner`);return Object.freeze({runtime:t,platformModule:Object.freeze({owner:n,loadRuntime:async e=>await tt(t,n.instance,e)})})}var Ye=class{options;leases;provider=r;leaseLifecycle={allocate:(e,t)=>this.allocate(e,t),release:async e=>await this.release(e)};recoverExpiredLease=async e=>{$e(e,`recover`),await this.leases.recover(e)};deviceInventoryProvider=async e=>{if(e.leaseProvider!==this.provider||!e.leaseId)return null;let t=this.leases.live(e.leaseId);return t?d(t.device,e.platform)?[t.device]:[]:null};constructor(e){if(!e.apiKey.trim())throw TypeError(`Revyl runtime requires an apiKey`);this.options=e;let t=g({apiKey:e.apiKey,...e.backendUrl===void 0?{}:{backendUrl:e.backendUrl},clientVersion:e.clientVersion});this.leases=he(Xe(t))}ownsDevice(t){return e(t.id)!==void 0}hasLiveSession(e){return this.sessionForDevice(e)!==void 0}getInteractor(e,t){let n=this.sessionForDevice(e);return n?We(n,t?.signal):void 0}getDeploymentSession(e){let t=this.sessionForDevice(e);return t?{runId:t.runId,sessionId:t.sessionId,platform:t.platform,client:t.client}:void 0}async shutdown(){await this.leases.shutdown()}async allocate(e,n){let r=e.leaseProvider===this.provider?t(e.backend):void 0;if(r===void 0)return;let{rejectRefusedRevylProfileFields:i,requireSameRevylStartRequest:a,resolveRevylStartRequest:o}=await import(`./session-allocation.mjs`);i(n);let s={...o(n,{...this.options.deviceModel===void 0?{}:{deviceModel:this.options.deviceModel},...this.options.osVersion===void 0?{}:{osVersion:this.options.osVersion}}),platform:r};return $(await this.leases.allocate(e,s,{...Qe(n),requireJoinable:t=>a(e.leaseId,t,s)}))}async release(e){if(e.leaseProvider!==this.provider||!t(e.backend))return;let n=await this.leases.release(e.leaseId);if(n!==void 0)return n.session?$(n.session):{revylRunId:n.handle.runId,providerSessionId:n.handle.sessionId}}sessionForDevice(t){let n=e(t.id);if(!n)return;let r=this.leases.live(n.leaseId);return r?.platform===n.platform?r:void 0}};function Xe(e){return{startTimeoutMs:ie,start:async(t,n,r)=>{let i=await oe(e,t,{platform:n.platform,appUrl:n.appUrl,deviceModel:n.deviceModel,osVersion:n.osVersion,signal:r.signal,onRented:r.rented});if(i.status===`failed`)return i;let{runId:a,sessionId:o}=i.session;return{status:`ready`,session:i.session,handle:{runId:a,sessionId:o}}},stop:async t=>{try{await pe(e,t.sessionId)}catch(e){if(!p(e))throw e}},handleKey:e=>e.sessionId,findByLease:async e=>({sessionId:x(e.leaseId)}),shuttingDown:e=>et(e.leaseId,`allocate`),releasing:e=>new s(`COMMAND_FAILED`,`Revyl lease ${e.leaseId} is being released.`,{reason:`provider-lease-releasing`,leaseId:e.leaseId,hint:`Run open again; it allocates a new lease and rents a fresh device.`}),gaveUp:(e,t,n,r)=>w(Q(e),t,n,r?.sessionId??x(e.leaseId)),cleanupPending:(e,t,n)=>ce(e.map(e=>({...e.handle??{sessionId:x(e.leaseId)},failure:o(e.cause)})),Ze(t),n)}}function Ze(e){switch(e.route){case`shutdown`:return{operation:`shutdown`};case`start`:return{};case`release`:case`recover`:return{leaseId:e.leaseId}}}function Q(e){let n=t(e.backend);if(n===void 0)throw new s(`UNSUPPORTED_OPERATION`,`Revyl cannot serve this lease.`,{leaseId:e.leaseId,leaseProvider:e.leaseProvider,leaseBackend:e.backend});return n}function Qe(e){return{...e?.signal===void 0?{}:{signal:e.signal},...e?.deadline===void 0?{}:{deadline:e.deadline}}}function $e(e,n){if(!(e.leaseProvider===`revyl`&&t(e.backend)))throw new s(`UNSUPPORTED_OPERATION`,`Revyl cannot ${n} this expired lease.`,{leaseId:e.leaseId,leaseProvider:e.leaseProvider,leaseBackend:e.backend})}function et(e,t){return new s(`COMMAND_FAILED`,`The Revyl runtime is shutting down.`,{reason:`runtime-shutting-down`,operation:t,leaseId:e,hint:`Retry the command; the daemon is ending the sessions it holds.`})}async function tt(e,t,n){let{createRevylPlatformRuntimeOwner:r}=await import(`./platform-runtime.mjs`);return r({host:n,runtimeInstance:t,ownsDevice:t=>e.ownsDevice(t),hasLiveSession:t=>e.hasLiveSession(t),getInteractor:(t,n)=>e.getInteractor(t,n),getDeploymentSession:t=>e.getDeploymentSession(t)})}function $(e){return{revylRunId:e.runId,providerSessionId:e.sessionId,device:e.device}}export{Je as createRevylRuntime};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-device/revyl",
|
|
3
|
-
"version": "0.21.
|
|
3
|
+
"version": "0.21.40-nightly.20261119.188",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Revyl cloud iOS simulator and Android emulator plugin for agent-device.",
|
|
6
6
|
"files": [
|
|
@@ -16,13 +16,13 @@
|
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@agent-device/capture-kit": "0.0.0",
|
|
19
|
-
"@agent-device/contracts": "0.0.0",
|
|
20
19
|
"@agent-device/host-kit": "0.0.0",
|
|
20
|
+
"@agent-device/contracts": "0.0.0",
|
|
21
21
|
"@agent-device/kernel": "0.0.0",
|
|
22
22
|
"@agent-device/provider-webdriver": "0.0.0",
|
|
23
23
|
"@agent-device/provision-kit": "0.0.0",
|
|
24
24
|
"@agent-device/test-kit": "0.0.0",
|
|
25
|
-
"agent-device": "0.21.
|
|
25
|
+
"agent-device": "0.21.40-nightly.20261119.188"
|
|
26
26
|
},
|
|
27
27
|
"agentDevicePlugin": {
|
|
28
28
|
"apiVersion": 1,
|