UNPKG

agent-device

Version:

Open source device layer for AI coding agents: a CLI, MCP server, and typed Node.js API to control mobile, web, TV, and desktop apps on devices and simulators, locally or in the cloud.

2 lines • 4.62 kB
import{S as e,v as t}from"./sdk-contracts.js";import{Z as n}from"./sdk-io.js";import{w as r}from"./sdk-device.js";import{n as i}from"./runtime-transport-hints.js";import{t as a}from"./platform-runtime-android-mechanics.js";import"./command.js";import{n as o}from"./device-shell.js";import{n as s}from"./entities.js";const c=`debug_http_host`,l=`dev_server_https`,u=`RCT_jsLocation`,d=`RCT_packager_scheme`,f=`React Native runtime hints require adb run-as access to the app sandbox. Verify the app is debuggable and the selected package/device are correct.`,p=[`<?xml version="1.0" encoding="utf-8" standalone="yes" ?>`,`<map>`,`</map>`,``].join(` `);async function m(e){let{device:t,appId:n,values:a}=e;if(!n)return;let o=i({metroHost:a.metroHost,metroPort:a.metroPort?Number(a.metroPort):void 0,bundleUrl:a.bundleUrl});if(o){if(t.platform===`android`){await _(t,n,o);return}r(t)&&t.kind===`simulator`&&await E(t,n,o)}}async function h(e){let{device:t,appId:n}=e;if(n){if(t.platform===`android`){await v(t,n);return}r(t)&&t.kind===`simulator`&&await D(t,n)}}function g(e){return[`shared_prefs/${e}_preferences.xml`,`shared_prefs/ReactNativeDevPrefs.xml`]}async function _(e,t,n){await P(t);let r=[];for(let i of g(t)){let a=A(await b(e,t,i),c,`${n.host}:${n.port}`);a=j(a,l,n.scheme===`https`),r.push({path:i,xml:a})}await x(e,t,r)}async function v(e,t){await P(t);let n=[];for(let r of g(t)){let i=await b(e,t,r),a=N(N(i,c),l);a!==i&&n.push({path:r,xml:a})}n.length!==0&&await x(e,t,n)}async function y(e,t,n){let{runAndroidAdb:r}=await a();return await r(e,t,n)}async function b(e,t,n){let r=await y(e,o(`adb`,`shell`,[`run-as`,t,`cat`,n]),{allowFailure:!0});return r.exitCode===0?k(r.stdout):p}async function x(e,t,n){await S(e,t);try{await w(e,t,n)}catch(e){throw T(e,t)}}async function S(e,t){let n=o(`adb`,`shell`,[`run-as`,t,`id`]),r=await y(e,n,{allowFailure:!0});if(r.exitCode!==0)throw C(r,t,n)}function C(e,r,i){let a=I(e.stdout,e.stderr);return new t(`COMMAND_FAILED`,a?`Failed to access Android app sandbox for ${r}`:`Failed to probe Android app sandbox for ${r}`,n(e,{package:r,cmd:`adb`,args:i,hint:a?f:`adb shell run-as probe failed. Check adb connectivity and that the device is reachable. Inspect stderr/details for more information.`}))}async function w(e,t,n){await y(e,o(`adb`,`shell`,[`run-as`,t,`mkdir`,`-p`,`shared_prefs`]));for(let r of n)await y(e,o(`adb`,`shell`,[`run-as`,t,`tee`,r.path]),{stdin:r.xml.trimEnd()})}function T(n,r){let i=e(n);if(i.code===`TOOL_MISSING`)return i;let a=I(typeof i.details?.stdout==`string`?i.details.stdout:``,typeof i.details?.stderr==`string`?i.details.stderr:``);return new t(`COMMAND_FAILED`,a?`Failed to access Android app sandbox for ${r}`:`Failed to write Android runtime hints for ${r}`,{...i.details??{},package:r,cmd:`adb`,phase:`write-runtime-hints`,hint:a?f:`adb run-as succeeded, but writing React Native dev-server preferences failed. Inspect stderr/details for the failing shell command.`},i)}async function E(e,t,n){await O(e,[`spawn`,e.id,`defaults`,`write`,t,u,`-string`,`${n.host}:${n.port}`]),await O(e,[`spawn`,e.id,`defaults`,`write`,t,d,`-string`,n.scheme])}async function D(e,t){await O(e,[`spawn`,e.id,`defaults`,`delete`,t,u],{allowFailure:!0}),await O(e,[`spawn`,e.id,`defaults`,`delete`,t,d],{allowFailure:!0})}async function O(e,t,n){let[{buildSimctlArgsForDevice:r},{runXcrun:i}]=await Promise.all([import(`./simctl-facade.js`),import(`./tool-provider-facade.js`).then(e=>e.t)]);await i(r(e,t),n)}function k(e){let t=e.trim();return!t.includes(`<map`)||!t.includes(`</map>`)?p:`${t}\n`}function A(e,t,n){let r=` <string name="${s(t)}">${s(n)}</string>`;return M(N(e,t),r)}function j(e,t,n){let r=` <boolean name="${s(t)}" value="${n?`true`:`false`}" />`;return M(N(e,t),r)}function M(e,t){return k(e).replace(`</map>`,`${t}\n</map>`)}function N(e,t){let n=F(t);return k(e).replace(RegExp(`^\\s*<string name="${n}">[\\s\\S]*?<\\/string>\\n?`,`m`),``).replace(RegExp(`^\\s*<boolean name="${n}" value="(?:true|false)"\\s*\\/?>\\n?`,`m`),``)}async function P(e){let{classifyAndroidAppTarget:n,formatAndroidInstalledPackageRequiredMessage:r}=await a();if(n(e)!==`binary`)return;let i=r(e);throw new t(`INVALID_ARGS`,i,{package:e,hint:i})}function F(e){return e.replaceAll(/[.*+?^${}()|[\]\\]/g,String.raw`\$&`)}function I(e,t){let n=`${e}\n${t}`.toLowerCase();return[`run-as: package not debuggable`,`run-as: permission denied`,`run-as: package is unknown`,`run-as: unknown package`,`is unknown`,`is not an application`,`could not set capabilities`].some(e=>n.includes(e))}export{m as applyRuntimeHintValues,h as clearRuntimeHintValues};