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.
30 lines • 22.6 kB
JavaScript
import{f as e,ji as t}from"./sdk-batch.js";import{ct as n}from"./sdk-contracts.js";import{g as r}from"./sdk-batch-runner.js";import{t as i}from"./success-text.js";import{i as a}from"./src3.js";import{a as o,i as s,n as c,o as l,r as u,s as ee}from"./output-common.js";import{n as te}from"./snapshot-lines.js";import{t as ne}from"./snapshot.js";function re(e){let t=[`Batch completed: ${e.executed}/${e.total} steps in ${e.totalDurationMs}ms`];for(let n of e.results)t.push(d(n));return{data:e,text:t.join(`
`)}}const ie={batch:l(re)};function d(e){let t=i(e.data)??e.command;return`${e.step}. OK ${t} (${e.durationMs}ms)`}function ae(e){if(!Array.isArray(e)||e.length===0)return e;let n=t(e);return e.map(e=>{let t=f(e,n,e=>e.label),r=f(e,n,e=>e.identifier),i=typeof e.label==`string`&&e.label.length>=24&&e.label===t,a=typeof e.identifier==`string`&&e.identifier.length>=24&&e.identifier===r;if(!i&&!a)return e;let o={...e};return i&&(delete o.label,o.inheritsLabel=!0),a&&(delete o.identifier,o.inheritsIdentifier=!0),o})}function f(e,t,n){let r=typeof e.parentIndex==`number`?t.get(e.parentIndex):void 0;for(;r;){let e=n(r);if(typeof e==`string`&&e.length>0)return e;r=typeof r.parentIndex==`number`?t.get(r.parentIndex):void 0}}function oe(e){return{data:e,text:se(e)}}function se(e){let t=[`agent-device audit`,ce(e.findings),...le(e.findings,e.refsGeneration)],n=Object.entries(e.guidance);return n.length>0&&t.push(``,...n.map(([e,t])=>`${e}: ${t}`)),e.limitations.length>0&&t.push(``,...e.limitations.map(e=>`Limitation: ${e}`)),`${t.join(`
`)}\n`}function ce(e){let t=e.filter(e=>e.severity===`warning`).length,n=e.length-t,r=[p(t,`warning`),p(n,`observation`)].filter(e=>e!==void 0);return`${r.length>0?r.join(`, `):`No findings`} — current screen`}function p(e,t){if(e!==0)return`${e} ${t}${e===1?``:`s`}`}function le(e,t){let n=e.map(e=>s(e.ref,t)??e.ref),r=Math.max(0,...n.map(e=>e.length)),i=Math.max(0,...e.map(e=>e.kind.length));return e.map((e,t)=>`${n[t].padEnd(r)} ${e.kind.padEnd(i)} ${fe(e)} ${ue(e)}`)}function ue(e){switch(e.kind){case`small-target`:return de(e.detail);case`missing-name`:return`No accessible name`;case`covered-control`:return`Accessible action is unavailable to touch`}}function de({width:e,height:t,unit:n,recommendedSize:r}){return`${e}×${t}${n}, below ${r}×${r}${n}`}function fe(e){let t=e.kind===`missing-name`?void 0:e.label,n=t?JSON.stringify(t):e.identifier?`id=${JSON.stringify(e.identifier)}`:void 0;return n?`${e.role} ${n}`:e.role}async function m(e){let{serializeSnapshotResult:t}=await import(`./result-serialization.js`).then(e=>e.n),n=t(e.raw||!Array.isArray(e.result.nodes)?e.result:{...e.result,nodes:ae(e.result.nodes)});return{data:n,jsonData:me(n),stderr:e.result.snapshotDiagnostics?.warning?`${e.result.snapshotDiagnostics.warning}\n`:void 0,text:ne(n,{raw:e.raw,flatten:e.interactiveOnly,scoped:typeof e.scope==`string`&&e.scope.trim().length>0,depthLimited:typeof e.depth==`number`})}}const pe={snapshot:async({input:e,result:t})=>await m({result:t,raw:e.raw,interactiveOnly:e.interactiveOnly,scope:e.scope,depth:e.depth}),audit:async({result:e})=>oe(e),wait:u,alert:u,settings:u};function me(e){let{unchanged:t,...n}=e;return n}function he(e){let t=[e.outPath,e.message];t.push(..._e(e));for(let n of e.matchedImages)t.push(`Matched: ${n.name} ${n.uuid}${n.arch?` ${n.arch}`:``}`);for(let n of e.warnings??[])t.push(`Warning: ${n}`);return{data:e,text:t.join(`
`)}}const ge={debug:l(he)};function _e(e){let t=e.crash,n=[`Crash: ${t.appName??`unknown app`}${t.crashedThread===void 0?``:` thread ${t.crashedThread}`}`];t.bundleId&&n.push(`Bundle: ${t.bundleId}`),t.exceptionType&&n.push(`Exception: ${t.exceptionType}`),t.terminationReason&&n.push(`Termination: ${t.terminationReason}`);for(let e of t.topFrames)n.push(`Frame ${e.index}: ${e.image} ${e.symbol??e.address}`);for(let e of t.findings)n.push(`Finding: ${e}`);return n}function ve(e){return t=>{let n=e(t);return n instanceof Promise?n.then(h):h(n)}}function h(e){return{data:e.data,text:ye(e.text,e.data)}}function g(t){let n={...t};for(let[r,i]of Object.entries(t))e(r)&&(n[r]=ve(i));return n}function ye(e,t){return`${e??``}${be(t.settle)}`}function be(e){if(!e||typeof e!=`object`)return``;let t=e,n=[b(t),..._(t),...y(t)];return t.hint&&n.push(`hint: ${t.hint}`),`\n${n.join(`
`)}`}function _(e){let t=e.diff,n=(t?.lines??[]).map(t=>`${t.kind===`removed`?`-`:`+`} ${v(t,e.refsGeneration)}`);return t?.truncated&&n.push(`… changed lines truncated`),n}function v(e,t){let n=e.text??``,r=s(e.ref,t);if(!r||!e.ref)return n;let i=`@${e.ref}`;return n.startsWith(`${i} `)?`${r}${n.slice(i.length)}`:`${r} ${n}`}function y(e){let t=e.tail??[];if(t.length===0)return[];let n=[`unchanged interactive (${t.length}):`];for(let r of t){let t=r.label?` "${r.label}"`:``,i=s(r.ref,e.refsGeneration)??`@${r.ref??``}`;n.push(`= ${i} [${r.role??``}]${t}`)}return e.tailTruncated&&n.push(`… more interactive elements not shown, use snapshot -i`),n}function b(e){let t=e.settled===!0?`settled`:`not settled`,n=e.diff?.summary;return n?`${t} after ${e.waitedMs??0}ms: +${n.additions??0} -${n.removals??0} (~${n.unchanged??0} unchanged)`:`${t} after ${e.waitedMs??0}ms`}function x(e){let t=e.result;return e.format===`attrs`?{data:t,text:JSON.stringify(t.node??{},null,2)}:{data:t,text:typeof t.text==`string`?t.text:``}}function S(e){let t=e,n=i(t);if(n)return{data:t,text:n};if(typeof t.text==`string`)return{data:t,text:t.text};if(Array.isArray(t.matches))return{data:t,text:C(t.matches,t.refsGeneration)};let r=s(t.ref,t.refsGeneration);return r?{data:t,text:`Found: ${r}`}:typeof t.found==`boolean`?{data:t,text:`Found: ${t.found}`}:t.node?{data:t,text:JSON.stringify(t.node,null,2)}:c(t)}function C(e,t){return[`${e.length} match${e.length===1?``:`es`}:`,...e.map(e=>w(e,t))].join(`
`)}function w(e,t){let r=e.ref===void 0?`(no ref)`:s(e.ref,t)??e.ref,i=n(e.node?.type??`Element`),a=e.node?te(e.node,i):``;return`= ${r} [${i}]${a?` "${a}"`:``}`}function T(e){let t=e;return{data:t,text:`Passed: is ${t.predicate??`assertion`}`}}function E(e){let t=e,n=t.ref??``,r=t.x,i=t.y;return!n||typeof r!=`number`||typeof i!=`number`?c(t):{data:t,text:`Tapped @${n} (${r}, ${i})`}}const D=g({click:l(E),press:l(E),fill:u,longpress:u,hover:u,scroll:u,get:({input:e,result:t})=>x({result:t,format:e.format===`attrs`?`attrs`:`text`}),is:l(T),find:l(S)});async function O(e){let{serializeDevice:t}=await import(`./result-serialization.js`).then(e=>e.n);return{data:{devices:e.map(t)},text:e.map(F).join(`
`)}}async function k(e){let{serializeDevice:t}=await import(`./result-serialization.js`).then(e=>e.n);return{data:{device:t(e.device),availableCommands:e.availableCommands},text:[`${F(e.device)} supports ${e.availableCommands.length} commands:`,e.availableCommands.join(` `)].join(`
`)}}function A(e){return{data:{apps:e.result},stderr:e.appsFilter===`all`?`Showing all apps, including system apps.
`:`Showing user-installed apps or deferred provider app assets. Use --all to include system apps on a live device.
`,text:e.result.length>0?e.result.join(`
`):e.appsFilter===`all`?`No apps found.`:`No user apps or provider app assets found.`}}async function j(e){if(`savedScript`in e)return{data:e,text:`Published script: ${e.savedScript}\nSession remains active: ${e.session}\nActions: ${e.actionCount}`};if(`stateDir`in e)return{data:e,text:e.stateDir};let{serializeSessionListEntry:t}=await import(`./result-serialization.js`).then(e=>e.n),n={sessions:e.sessions.map(t)};return{data:n,text:JSON.stringify(n,null,2)}}async function M(e,t=`terminal`){let{serializeOpenResult:n}=await import(`./result-serialization.js`).then(e=>e.n),r=n(e),a=[i(r)].filter(e=>!!e);t===`terminal`&&typeof r.sessionStateDir==`string`&&a.push(`Session state: ${r.sessionStateDir}`);for(let t of e.warnings??[])a.push(`Warning: ${t}`);let o=await N(e.snapshot);return o&&(r.snapshot=o.jsonData??o.data,o.text&&a.push(o.text)),{data:r,...o?.stderr?{stderr:o.stderr}:{},text:a.join(`
`)||null}}async function N(e){return!e||!Array.isArray(e.nodes)?null:await m({result:e,interactiveOnly:!0})}async function xe(e){let{serializeCloseResult:t}=await import(`./result-serialization.js`).then(e=>e.n);return c(t(e))}function Se(e){if(Ce(e))return{data:e,text:e.artifacts.length>0?e.artifacts.map(je).join(`
`):e.message??`No daemon artifacts available.`};let t=[e.message??`No cloud artifacts available for ${e.provider}.`],n=Me(e);return n&&t.push(`Retry: ${n}`),{data:e,text:e.cloudArtifacts.length>0?[...e.cloudArtifacts.map(Ae),...e.message?[e.message]:[]].join(`
`):t.join(`
`)}}function Ce(e){return`source`in e&&e.source===`daemon`}async function P(e){let{serializeDeployResult:t}=await import(`./result-serialization.js`).then(e=>e.n);return c(t(e))}async function we(e){let{serializeInstallFromSourceResult:t}=await import(`./result-serialization.js`).then(e=>e.n);return c(t(e))}function Te(e){let t=e,n=t.platform??`unknown`;return{data:t,text:`Boot ready: ${t.device??t.id??`unknown`} (${n})${Ee(t.selection)}`}}function Ee(e){if(typeof e!=`object`||!e)return``;let{reason:t,candidateCount:n}=e;return t===`newest-bootable-local`?`, picked by --pick newest from ${n} matching devices`:``}function De(e){let t=e,n=t.platform??`unknown`,r=t.device??t.id??`unknown`,i=t.shutdown;return{data:t,text:`${i&&typeof i==`object`&&`success`in i&&i.success===!0?`Shutdown`:`Shutdown failed`}: ${r} (${n})`}}async function Oe(e,t,n,r){let{formatDoctorCheckDetailLines:i,formatDoctorCheckSummaryLine:a}=await import(`./doctor-lines.js`).then(e=>e.t),{shouldColorStatusMarkers:o}=await import(`./status-markers.js`).then(e=>e.r),s=o(t,r),c=e,l=[`Doctor: ${typeof c.status==`string`?c.status:`unknown`}`],u=Ne(c.checks);if(n?.renderedToStderr){let e=typeof c.summary==`string`?c.summary:void 0;e&&l.push(e)}else if(u.length===0){let e=typeof c.summary==`string`?c.summary:`No blockers found.`;l.push(e)}else for(let e of u)l.push(a(e,{useColor:s})),l.push(...i(e));return{data:c,text:l.join(`
`)}}const ke={boot:l(Te),shutdown:l(De),devices:l(O),capabilities:l(k),doctor:({result:e,env:t,progress:n,stderrIsTTY:r})=>Oe(e,t,n,r),apps:({input:e,result:t})=>A({result:t,appsFilter:e.appsFilter}),session:l(j),artifacts:l(Se),open:({result:e,reader:t})=>M(e,t),close:l(xe),install:l(P),reinstall:l(P),"install-from-source":l(we),prepare:u,viewport:u};function F(e){let t=e.kind?` ${e.kind}`:``,n=e.target?` target=${e.target}`:``,r=e.model?` model=${JSON.stringify(e.model)}`:``,i=e.osVersion?` os=${e.osVersion}`:``,a=typeof e.booted==`boolean`?` booted=${e.booted}`:``,o=e.claimedBy?` claimed by session "${e.claimedBy.session}" in ${e.claimedBy.workspace}`:``;return`${e.name} (${e.platform}${t}${n}${r}${i})${a}${o}`}function Ae(e){let t=e.url?` ${e.url}`:``,n=e.availability?` ${e.availability}`:``;return`${e.kind}: ${e.name}${n}${t}`}function je(e){let t=e.artifactType?` (${e.artifactType})`:``;return`${e.filename}${t}: ${e.mimeType} ${e.sizeBytes} bytes id=${e.id}`}function Me(e){if(e.providerSessionId)return`agent-device artifacts ${e.providerSessionId} --provider ${e.provider} --json`}function Ne(e){return Array.isArray(e)?e.filter(e=>!!e&&typeof e==`object`&&!Array.isArray(e)):[]}function Pe(e){return{data:e.result,text:e.action===`reload`?`Reloaded React Native apps via ${e.result.reloadUrl}`:JSON.stringify(e.result,null,2)}}const Fe={metro:({input:e,result:t})=>Pe({result:t,action:e.action})},Ie=[`started`,`stopped`,`marked`,`cleared`,`restarted`,`relaunched`,`removedRotatedFiles`];function Le(e){return{data:e,text:e.path,stderr:W([H(e,[`active`,`state`,`backend`,`sizeBytes`]),H(e,Ie),e.hint,U(e.notes)])}}function Re(e){let t=e.events??[];return{data:e,text:t.length>0?He(t):`No session events found.`,stderr:W([H(e,[`path`,`cursor`,`nextCursor`,`endCursor`,`limit`])])}}function ze(e){let t=[],n=e.entries??[];if(e.path&&t.push(e.path),n.length===0)t.push(`No recent HTTP(s) entries found.`);else for(let e of n)t.push(...et(e));return{data:e,text:t.join(`
`),stderr:W([H(e,[`active`,`state`,`backend`,`include`,`scannedLines`,`matchedLines`]),U(e.notes)])}}function Be(e){return{data:e,text:[`Audio probe: ${String(e.state??`stopped`)} heard=${String(e.heard===!0)}`,I(`rmsDbfs`,e.rmsDbfs),I(`peakDbfs`,e.peakDbfs)].filter(e=>!!e).join(`
`),stderr:W([H(e,[`active`,`backend`,`source`,`durationMs`,`elapsedMs`,`bucketMs`,`sampleCount`,`sourceCount`,`mediaElementCount`]),U(e.notes)])}}const Ve={logs:l(Le),events:l(Re),network:l(ze),audio:l(Be)};function He(e){let t=e.map(Ue),n=Math.min(Math.max(...t.map(e=>e.label.length),5),32);return t.map(e=>We(e,n)).join(`
`)}function Ue(e){return{timestamp:Ge(e.ts),label:Ke(e),summary:qe(e)}}function We(e,t){let n=e.label.padEnd(t),r=e.timestamp?`${e.timestamp} ${n}`:n.trimEnd();return e.summary?`${r} ${e.summary}`:r.trimEnd()}function Ge(e){return e?e.replace(`T`,` `):``}function Ke(e){let t=e.command??`command`;switch(e.kind){case`request.started`:return`start ${t}`;case`request.finished`:return L([e.status===`error`?`error`:`ok`,t,$e(z(e.details?.durationMs))]);case`action.recorded`:return`action ${t}`;default:return L([e.kind??`event`,e.command])}}function qe(e){let t=Je(e.summary,e);return`${t}${Ye(e,t)}`.trim()}function Je(e,t){let n=e?.trim()??``,r=t.command??``;return t.kind===`request.started`&&n===`Started ${r}`||t.kind===`request.finished`&&n===`Finished ${r}`?``:n}function Ye(e,t){if(e.kind!==`action.recorded`)return``;let n=e.details;if(!n)return``;let r=[Xe(n,t),Qe(z(n.textLength))].filter(e=>!!e);return r.length>0?` (${r.join(`, `)})`:``}function Xe(e,t){let n=R(e.ref)??R(e.selector)??Ze(e);if(!(!n||t.includes(n)))return`target=${n}`}function Ze(e){let t=z(e.x),n=z(e.y);return t===void 0||n===void 0?void 0:`(${t}, ${n})`}function Qe(e){return e===void 0?void 0:`text=${e} chars`}function $e(e){return e===void 0?void 0:`${Math.round(e)}ms`}function I(e,t){if(!Array.isArray(t))return;let n=t.filter(e=>typeof e==`number`&&Number.isFinite(e));return n.length>0?`${e}: [${n.join(`, `)}]`:void 0}function L(e){return e.filter(e=>!!e).join(` `)}function R(e){return typeof e==`string`&&e.trim().length>0?e:void 0}function z(e){return typeof e==`number`&&Number.isFinite(e)?e:void 0}function et(e){let t=e.method??`HTTP`,n=e.url??`<unknown-url>`,r=e.status===void 0?``:` status=${e.status}`,i=e.timestamp?`${e.timestamp} `:``,a=e.durationMs===void 0?``:` durationMs=${e.durationMs}`,o=[`${i}${t} ${n}${`pathUnavailable`in e&&e.pathUnavailable?` (request path not logged)`:``}${r}${a}`];return e.headers?V(o,`headers`,e.headers):(B(o,`request headers`,e.requestHeaders),B(o,`response headers`,e.responseHeaders)),V(o,`request`,e.requestBody),V(o,`response`,e.responseBody),o}function B(e,t,n){!n||Object.keys(n).length===0||e.push(` ${t}: ${JSON.stringify(n)}`)}function V(e,t,n){n!==void 0&&e.push(` ${t}: ${n}`)}function H(e,t){return t.map(t=>e[t]!==void 0&&e[t]!==null?`${t}=${e[t]}`:``).filter(Boolean).join(` `)||void 0}function U(e){let t=e?.filter(e=>e.length>0)??[];return t.length>0?t.join(`
`):void 0}function W(e){return e.filter(e=>!!e).join(`
`)||void 0}function tt(e){let t=e;return{data:t,text:rt(t)}}const nt={perf:l(tt)};function rt(e){return ut(e)??it(e)}function it(e){let t=r(e.artifact)?e.artifact:void 0;return t?lt(t):at(r(e.metrics)?e.metrics:void 0)}function at(e){return r(e?.fps)?st(e):ot(e)??`Memory: unavailable - missing memory metric`}function ot(e){let t=r(e?.memory)?e.memory:void 0;if(!t)return;if(t.available===!1)return`Memory: unavailable - ${J(t)}`;let n=wt(t);return n?`Performance: ${n}`:`Memory: unavailable - missing memory metric`}function st(e){let t=r(e?.fps)?e.fps:void 0;if(!t)return q(`missing frame metric`);if(t.available===!1)return q(J(t));let n=yt(t);return n?ct(t,n):q(`missing dropped-frame summary`)}function ct(e,t){let n=[`Frame health: ${t}`],r=St(e);return r.length>0&&n.push(`Worst windows:`,...r),n.join(`
`)}function lt(e){let t=typeof e.kind==`string`?e.kind:`memory`;if(e.available===!1)return`Memory artifact (${t}): unavailable - ${typeof e.reason==`string`&&e.reason.length>0?e.reason:`not available`}`;let n=typeof e.path==`string`?e.path:void 0,r=Y(e.sizeBytes),i=r===void 0?``:` (${Q(r)})`;return n?`Memory artifact (${t}): ${n}${i}`:`Memory artifact (${t}): captured${i}`}function ut(e){return e.kind===`xctrace`?dt(e):mt(e)}function dt(e){let t=typeof e.perf==`string`?e.perf:void 0,n=ft(e);if(!t||!n||e.kind!==`xctrace`)return;let r=pt(n,typeof e.mode==`string`?e.mode:`capture`,t,e.template),i=G(e);return i.length>0?`${r}\nTop CPU self time:\n${i.join(`
`)}`:r}function G(e){let t=r(e.summary)?e.summary:void 0;return Array.isArray(t?.topFunctions)?t.topFunctions.slice(0,5).flatMap(e=>{if(!r(e))return[];let t=K(e.symbol),n=Y(e.selfSamplePercent);if(!t||n===void 0)return[];let i=K(e.binary);return[`- ${X(n)} ${t}${i?` (${i})`:``}`]}):[]}function ft(e){return typeof e.outPath==`string`?e.outPath:typeof e.reportPath==`string`?e.reportPath:void 0}function pt(e,t,n,r){let i=[e,`Perf ${t}: ${n}`];return typeof r==`string`&&i.push(`Template: ${r}`),i.join(`
`)}function mt(e){let t=ht(e);if(!t)return;let n=`Perf ${t.action}: ${t.kind} ${t.type}${gt(e)}${_t(e)}${vt(e)}`,r=G(e);return r.length>0?`${n}\nTop CPU self time:\n${r.join(`
`)}`:n}function ht(e){let t=K(e.action),n=K(e.kind),r=K(e.type);return t&&n&&r?{action:t,kind:n,type:r}:void 0}function gt(e){let t=K(e.state);return t?` state=${t}`:``}function _t(e){let t=K(e.outPath);if(!t)return``;let n=Y(e.sizeBytes);return`\n${t}${n===void 0?``:` (${Q(n)})`}`}function K(e){return typeof e==`string`?e:void 0}function vt(e){let t=r(e.summary)?e.summary:void 0,n=r(t?.frameHealth)?t.frameHealth:void 0;if(!n||n.available!==!0)return``;let i=Y(n.droppedFramePercent),a=Y(n.droppedFrameCount),o=Y(n.totalFrameCount);return i===void 0||a===void 0||o===void 0?``:`\nTrace frame health: dropped ${X(i)} (${Math.round(a)}/${Math.round(o)} frames)`}function q(e){return`Frame health: unavailable - ${e}`}function J(e){return typeof e.reason==`string`&&e.reason.length>0?e.reason:`not available`}function yt(e){let t=Y(e.droppedFramePercent),n=Y(e.droppedFrameCount);if(t!==void 0&&n!==void 0)return[`dropped ${X(t)}`,bt(n,Y(e.totalFrameCount)),xt(Y(e.sampleWindowMs))].filter(Boolean).join(` `)}function bt(e,t){return t===void 0?`(${Math.round(e)} dropped frames)`:`(${Math.round(e)}/${Math.round(t)} frames)`}function xt(e){return e===void 0?``:`window ${Z(e)}`}function St(e){return Array.isArray(e.worstWindows)?e.worstWindows.flatMap(e=>{if(!r(e))return[];let t=Ct(e);return t?[t]:[]}):[]}function Ct(e){let t=Y(e.startOffsetMs),n=Y(e.endOffsetMs),r=Y(e.missedDeadlineFrameCount);if(t===void 0||n===void 0||r===void 0)return;let i=Y(e.worstFrameMs),a=i===void 0?``:`, worst ${Z(i)}`;return`- +${Z(t)}-+${Z(n)}: ${Math.round(r)} missed-deadline frames${a}`}function wt(e){if(e?.available!==!0)return;let t=Y(e.residentMemoryKb)??Y(e.totalPssKb)??Y(e.totalRssKb);return t===void 0?void 0:`memory ${Tt(t)}`}function Y(e){return typeof e==`number`&&Number.isFinite(e)?e:void 0}function X(e){return`${Number.isInteger(e)?e:e.toFixed(1)}%`}function Z(e){let t=Math.max(0,Math.round(e));if(t<1e3)return`${t}ms`;let n=Math.round(t/1e3);if(n<60)return`${n}s`;let r=Math.floor(n/60),i=n%60;return i>0?`${r}m ${i}s`:`${r}m`}function Tt(e){let t=e/1024;return`${t>=10?Math.round(t):t.toFixed(1)}MB`}function Q(e){if(e<1024)return`${Math.round(e)}B`;let t=e/1024;if(t<1024)return`${t>=10?Math.round(t):t.toFixed(1)}KB`;let n=t/1024;return`${n>=10?Math.round(n):n.toFixed(1)}MB`}function Et(e){let t=e,n=typeof t.outPath==`string`?t.outPath:``,r=Ot(t);if(r.length<=1)return{data:t,text:Dt(t,n)};let i=[`Recording chunks:`];for(let e of r)i.push(` ${e.index}: ${e.path}`);return typeof t.telemetryPath==`string`&&i.push(`Telemetry: ${t.telemetryPath}`),typeof t.warning==`string`&&i.push(`Warning: ${t.warning}`),typeof t.overlayWarning==`string`&&i.push(`Overlay warning: ${t.overlayWarning}`),{data:t,text:i.join(`
`)}}const $={record:l(Et)};function Dt(e,t){let n=[];return t&&n.push(t),typeof e.sessionStateDir==`string`&&n.push(`Session state: ${e.sessionStateDir}`),typeof e.warning==`string`&&n.push(`Warning: ${e.warning}`),typeof e.overlayWarning==`string`&&n.push(`Overlay warning: ${e.overlayWarning}`),n.join(`
`)}function Ot(e){let t=e.chunks;return Array.isArray(t)?t.flatMap(e=>{if(!e||typeof e!=`object`)return[];let t=e;return typeof t.index!=`number`||typeof t.path!=`string`?[]:[{index:t.index,path:t.path}]}):[]}const kt={replay:u};function At(e){return{data:e,text:Ft(e)}}function jt(e){if(e.action===`status`){let t=[`Keyboard visible: ${e.visible===!0?`yes`:`no`}`,...Mt(e)];return e.platform===`android`&&(t.push(`Input type: ${e.type??e.inputType??`unknown`}`,`Input owner: ${e.inputOwner??`unknown`}`),e.inputMethodPackage&&t.push(`Input method: ${e.inputMethodPackage}`),e.focusedPackage&&t.push(`Focused package: ${e.focusedPackage}`),e.focusedResourceId&&t.push(`Focused resource: ${e.focusedResourceId}`),t.push(`Next action: ${Lt(e.visible,e.inputOwner)}`)),{data:e,text:t.join(`
`)}}return c(e)}function Mt(e){return e.visible!==!0||e.returnKey===void 0?[]:[`Return key: ${e.returnKey}`,...e.returnKeyLabel?[`Return key label: ${e.returnKeyLabel}`]:[]]}function Nt(e){return e.action===`read`?{data:e,text:e.text}:c(e)}const Pt=g({appstate:l(At),back:u,home:u,orientation:u,fold:u,"app-switcher":u,"action-button":u,keyboard:l(jt),clipboard:l(Nt),"tv-remote":u,"hardware-button":u,telephony:u});function Ft(e){switch(e.platform){case`ios`:case`macos`:return It(e);case`android`:case`harmonyos`:{let t=[`Foreground app: ${e.package??`unknown`}`];return e.activity&&t.push(`Activity: ${e.activity}`),t.join(`
`)}}}function It(e){let t=[`Session app: ${e.appName??e.appBundleId??`unknown`}`];return e.appBundleId&&t.push(`Bundle: ${e.appBundleId}`),e.state&&t.push(`State: ${e.state}`),e.source&&t.push(`Source: ${e.source}`),t.join(`
`)}function Lt(e,t){return t===`ime`?`Focused input appears to be owned by the keyboard/IME; dismiss or change the IME before retrying text entry.`:e===!0?`Keyboard is visible and focused input appears app-owned; fill/type can proceed.`:`Keyboard is hidden; focus an app field before type, or use fill with a concrete target.`}const Rt=zt([ie,pe,ge,D,ke,Fe,Ve,nt,$,kt,Pt]);function zt(e){let t={};for(let n of e)for(let[e,r]of Object.entries(n)){if(Object.hasOwn(t,e))throw Error(`Duplicate CLI output formatter: ${e}`);t[e]=r}return t}async function Bt(e){let t=await Rt[e.name]?.({input:e.input??{},result:e.result,progress:e.progress,env:e.env,stderrIsTTY:e.stderrIsTTY,reader:e.reader});if(t!==void 0)return ee(t,e.result,o(a(e.name)))}export{Bt as dispatchCliOutput};