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 • 6.34 kB
import{Z as e}from"./sdk-contracts.js";function t(e){let t=e?.resume;if(typeof t?.allowed==`boolean`&&!(!Number.isInteger(t.from)||typeof t.planDigest!=`string`)&&!(!t.allowed&&typeof t.reason!=`string`)&&(t.repairSessionHeld===void 0||t.repairSessionHeld===!0)&&!(t.alternateFrom!==void 0&&!Number.isInteger(t.alternateFrom)))return t}const n={digest:8192,default:24576,full:65536};function r(e){return e===`digest`||e===`full`?e:`default`}function i(e,t=256){let n=Buffer.from(e,`utf8`);if(n.length<=t)return e;let r=`…<truncated>`,i=Buffer.byteLength(r,`utf8`),a=Math.max(0,t-i);for(;a>0&&(n[a]&192)==128;)--a;return`${n.subarray(0,a).toString(`utf8`)}${r}`}function a(t,n=256){return i(e(t),n)}function o(e,t){let n=e;for(let e of t)e.value&&(n=n.split(e.value).join(`<var:${e.name}>`));return n}function s(e){return(t,n)=>a(o(t,e),n)}function c(e,t){return e.state!==`available`||e.refs.length<=t?e:{...e,refs:e.refs.slice(0,t),truncated:!0}}function l(e,t){let n=r(t),i=n===`digest`?8:20,a=c(e.screen,i),o=n===`digest`?[]:e.suggestions.slice(0,5);return{...e,screen:a,suggestions:o}}function u(e){return Buffer.byteLength(JSON.stringify(e),`utf8`)}function d(e){let{divergence:t,level:i,writeOverflowArtifact:a}=e,o=r(i),s=n[o],c=l(t,i),d=u(c);if(d<=s)return c;let p=d-s,m=a(l(t,`full`)),h=f(c);return`artifactPath`in m?{...h,overflow:{omittedBytes:p,artifactPath:m.artifactPath}}:{...h,artifactUnavailable:!0}}function f(e){return{version:e.version,kind:e.kind,step:{index:e.step.index,source:{path:a(e.step.source.path),line:e.step.source.line}},action:a(e.action),cause:{code:e.cause.code,message:a(e.cause.message),...e.cause.hint?{hint:a(e.cause.hint)}:{}},screen:{state:`unavailable`,reason:`omitted-for-size`,hint:`The screen digest and suggestions were omitted to stay within the response byte budget. See overflow.artifactPath (or retry at --level full) for the complete report.`},suggestions:[],suggestionCount:e.suggestionCount,resume:e.resume,repairHint:e.repairHint,...e.targetBinding?{targetBinding:e.targetBinding}:{}}}function p(e){let t=e?.divergence;if(!t||typeof t!=`object`)return null;let n=t,r=[...E(n.step),...S(n.kind,n.targetBinding),...m(n.repairHint,n.resume),...D(n.screen),...j(n.suggestions,n.suggestionCount),...N(n.overflow,n.artifactUnavailable)];return r.length>0?r.join(` `):null}function m(e,t){if(typeof e!=`string`)return[];let n=v(e,t);return[`Repair hint: ${e}${n?` — ${n}`:``}`]}function h(e){let t=e;if(!t||typeof t.allowed!=`boolean`)return;if(!t.allowed)return{allowed:!1,reason:typeof t.reason==`string`?t.reason:void 0};let{from:n,planDigest:r,alternateFrom:i}=t;if(typeof n==`number`&&typeof r==`string`&&r.length!==0)return{allowed:!0,from:n,planDigest:r,alternateFrom:typeof i==`number`?i:void 0}}function g(e,t){return`replay --from ${e} --plan-digest ${t}`}function _(e){return e?.repairSessionHeld===!0}function v(e,t){let n=y(e,h(t));if(n!==void 0)return _(t)?`${n} Read-only inspection while armed (snapshot -i, get attrs, find, is) is excluded from the healed script by default — no --no-record needed. If the step you are repairing is itself a read, add --record to that command so it lands in the heal.`:n}function y(e,t){switch(e){case`record-and-heal`:return t?.allowed?`press the correct control via a blessed @ref from screen.refs (recorded), then ${g(t.from,t.planDigest)}.`:`press the correct control via a blessed @ref from screen.refs (recorded). ${x(t)}`;case`state-repair`:return t?.allowed?`fix app state with --no-record actions, then ${g(t.from,t.planDigest)} to re-run it.`:`fix app state with --no-record actions. ${x(t)}`;case`caution`:return b({lead:`something already matches the recorded selector; a blind re-press may repeat the mistake.`,guidance:t});case`manual`:return b({lead:`no safe automated repair could be proven; inspect the screen and repair by hand.`,guidance:t});default:return}}function b(e){let{lead:t,guidance:n}=e;if(!n?.allowed)return`${t} ${x(n)}`;let r=`if you fixed app state with --no-record actions: ${g(n.from,n.planDigest)}`;return n.alternateFrom===void 0?`${t} ${r}.`:`${t} ${r}; ${`if you performed the step's intent as a recorded action: ${g(n.alternateFrom,n.planDigest)}`}.`}function x(e){return e&&!e.allowed&&e.reason?`This step cannot currently be resumed automatically (${e.reason}) — run a fresh full replay instead.`:`This step cannot currently be resumed automatically — run a fresh full replay instead.`}function S(e,t){if(typeof e!=`string`||e===`action-failure`)return[];let n=t;return n?[C(e,n.matchCount),...w(n.mismatches),...T(n.candidates)]:[]}function C(e,t){return`Target binding: ${e}${typeof t==`number`?` (matchCount ${t})`:``} — recorded target evidence did not verify.`}function w(e){return!Array.isArray(e)||e.length===0?[]:[` mismatches: ${e.slice(0,5).join(`; `)}`]}function T(e){return!Array.isArray(e)||e.length===0?[]:[` ${e.length} candidate(s) shared the recorded identity:`,...e.slice(0,5).map(e=>` ${A(e)}`)]}function E(e){let t=e;if(typeof t?.index!=`number`)return[];let n=t.source,r=typeof n?.path==`string`&&typeof n.line==`number`?` (${n.path}:${n.line})`:``;return[`Divergence at step ${t.index}${r}`]}function D(e){let t=e;return t?.state===`available`&&Array.isArray(t.refs)?O(t.refs,t.refsGeneration):t?.state===`unavailable`?[k(t)]:[]}function O(e,t){let n=e.slice(0,8).map(A),r=e.length-n.length;return[`Screen: ${e.length} actionable ref(s) captured (refsGeneration ${t}).`,...n,...r>0?[` ... ${r} more`]:[]]}function k(e){let t=typeof e.hint==`string`&&e.hint.length>0?` ${e.hint}`:``;return`Screen: unavailable (${String(e.reason??`unknown`)}).${t}`}function A(e){let t=e,n=typeof t.label==`string`?` "${t.label}"`:``;return` @${String(t.ref)} [${String(t.role)}]${n}`}function j(e,t){return Array.isArray(e)&&e.length>0?[`Suggestions:`,...e.slice(0,5).map(M)]:typeof t==`number`&&t>0?[`Suggestions: ${t} available (omitted at this response level; rerun with --json for the full report).`]:[]}function M(e){let t=e,n=typeof t.label==`string`?` "${t.label}"`:``;return` - [${String(t.basis)}]${n} ${String(t.selector)}`}function N(e,t){return e&&typeof e==`object`?[`Full report written to ${String(e.artifactPath)}.`]:t===!0?[`Full report exceeded the response budget and the overflow artifact could not be written.`]:[]}export{o as a,t as i,s as n,p as r,d as t};