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.

4 lines • 14.8 kB
import{D as e,S as t,f as n,s as r,u as i,w as a}from"./sdk-batch.js";import{B as o,U as s,gt as c,i as l,v as u}from"./sdk-contracts.js";import{g as d,m as f}from"./sdk-batch-runner.js";import{n as p,o as m,r as h}from"./sdk-device.js";import{A as g,C as _,F as v,G as ee,I as y,L as b,M as x,N as S,P as C,S as te,U as ne,V as w,W as T,_ as re,a as E,b as ie,d as ae,h as oe,i as D,j as O,m as se,o as ce,p as le,s as ue,u as de,v as fe,w as pe,y as me,z as he}from"./src3.js";import{r as ge,t as _e}from"./recording-stop-observation.js";import{i as ve,t as ye}from"./merge-flags.js";import{n as k,r as A}from"./cli-config.js";import{a as j}from"./output-common.js";import{t as M}from"./error.js";import{r as N}from"./result-serialization.js";import{t as P}from"./src7.js";const F=v({field:y(),artifactType:y(),path:y(),localPath:y(),fileName:y()},[`field`]),be={record:{type:`object`,oneOf:[v({recording:O(`started`),outPath:y(),sessionStateDir:y(),recordingBackend:y(),recordingScope:y(),recordOnlySession:g(),activeSessionApp:S(),showTouches:g()},[`recording`,`outPath`,`sessionStateDir`,`showTouches`]),v({recording:O(`stopped`),outPath:y(),telemetryPath:y(),artifacts:{type:`array`,items:F},recordingBackend:y(),recordingScope:y(),recordOnlySession:g(),activeSessionApp:S(),durationMs:C(),capturedDurationMs:C(),recorder:x(_e,`What the recorder was observed doing when the recording was stopped: confirmed, or lost when the session holding it died. ADR 0024 reserves unconfirmed for the step that gains the probe.`),nativePathDisposition:x(ge,`What became of the artifact path the recorder writes to: retirable while it still owes a removal, retired once that removal was verified. ADR 0024 reserves pending.`),showTouches:g(),warning:y(),overlayWarning:y(),chunks:{type:`array`,items:S()}},[`recording`,`outPath`,`artifacts`,`durationMs`,`showTouches`])]},trace:{type:`object`,oneOf:[v({trace:O(`started`),outPath:y()},[`trace`,`outPath`]),v({trace:O(`stopped`),outPath:y(),artifacts:{type:`array`,items:F}},[`trace`,`outPath`,`artifacts`])]}},xe=v({settled:g(`Whether the UI held the quiet window before the deadline. false is advisory, not failure.`),waitedMs:C(),captures:C(),quietMs:C(),timeoutMs:C(),refsGeneration:C(`Snapshot generation of the stored settled tree; refs on added diff lines were minted from it.`),refs:{type:`array`,items:v({ref:y(`Plain ref body (e12) minted from the stored settled tree.`)},[`ref`])},surfaceChange:ie,diff:v({summary:v({additions:C(),removals:C(),unchanged:C()},[`additions`,`removals`,`unchanged`]),lines:{type:`array`,description:`Changed lines. Absent at the digest response level, which keeps summary and settle.refs.`,items:v({kind:x([`added`,`removed`]),text:y(),ref:y(`Plain ref body (e12) for added lines.`)},[`kind`,`text`])},truncated:g(`Lines were capped to the response bound.`)},[`summary`],`Settled diff vs the pre-action tree (changed lines only).`),tail:{type:`array`,description:`Unchanged interactive refs tail: still-present, actionable elements from the settled tree, attached only when diff carries zero added-line refs (a modal-dismiss/toast-only diff).`,items:v({ref:y(`Plain ref body (e12) minted from the stored settled tree.`),role:y(),label:y()},[`ref`,`role`])},tailTruncated:g(`Present (true) when tail candidates exceeded the response cap.`),hint:y()},[`settled`,`waitedMs`,`captures`,`quietMs`,`timeoutMs`]);function I(e){return e.oneOf?{...e,oneOf:e.oneOf.map(I)}:{...e,properties:{...e.properties??{},settle:xe}}}function Se(e){let t={...e};for(let e of Object.keys(t))n(e)&&(t[e]=I(t[e]));return t}const Ce=Se({scroll:v({direction:x([`up`,`down`,`left`,`right`]),edge:x([`top`,`bottom`]),until:y(`Until scrolls only: the selector the passes stopped on.`),passes:C(`Edge and until scrolls only: how many scroll-and-check passes ran.`),amount:C(),pixels:C(),durationMs:C(),message:y(),keyboardAvoided:g(`Present only when an on-screen keyboard forced the swipe into the band above it; the reported pixels were planned against the shorter referenceHeight.`),keyboardMinY:C(`Where the keyboard began, in the same unit as the gesture coordinates. Clipped scrolls only.`),movement:x([`moved`,`at-edge`,`unchanged`,`unobserved`],"Directional scrolls only: what the owner observed after its gesture. `moved` means the content inside the scroller the swipe ran in differs from the tree the session stored immediately before the gesture; `at-edge` and `unchanged` mean it did not change, with no hidden content left to reveal and with no end-of-content signal to read, respectively; `unobserved` means the pair could not back a claim in either direction — nothing comparable was available (no stored tree, a capture from another lineage, a surface that never came to rest), or every difference sits outside the scroller that was swiped, which a changing status bar does on Android — so the reported distance rests on the gesture plan alone. The field is absent — which is never a claim that nothing moved — on the tiers that verify per pass (`scroll top`/`bottom`, `--until`), on a runtime bound without a capture, on a platform whose scroll dispatches no swipe (the Linux wheel), and where the caller already owns that observation (`--settle`, an operator opt-out of post-gesture stabilization, or a replay with `postGestureStabilization: false`).")},[`direction`]),..._,...pe,...te,...oe,...re,...fe,...se,...le,...me,...be,...ae,...de}),we={devices:{type:`object`,properties:{devices:{type:`array`,items:{type:`object`,properties:{platform:{type:`string`,enum:m},target:{type:`string`,enum:h},kind:{type:`string`,enum:p},id:{type:`string`},name:{type:`string`}},required:[`platform`,`target`,`kind`,`id`,`name`]}}},required:[`devices`]},apps:{type:`object`,properties:{apps:{type:`array`,items:{type:`string`}}},required:[`apps`]}},L={...Ce,...we},Te={stateDir:{schema:{type:`string`},audience:T({operatorConfig:!0})},cost:{schema:{type:`boolean`},audience:T({operatorConfig:!0})},responseLevel:{schema:{type:`string`,enum:[`digest`,`default`,`full`]},audience:T({operatorConfig:!0})}},R=Object.entries(Te),z=new Set(R.map(([e])=>e)),Ee={config:T({operatorPath:`Point the process serving these tools at a config file with the AGENT_DEVICE_CONFIG environment variable.`}),remoteConfig:T({operatorPath:`Configure the remote profile on the process serving these tools, not per tool call.`})},De=Object.fromEntries(R.map(([e,t])=>[e,t.audience]));function Oe(){return Object.fromEntries(R.map(([e,t])=>[e,t.schema]))}function ke(e){return o(e)}function B(e){let{summary:t,body:n}=M(e,{reader:`model`}),r=[t,...n];return e.supportedOn&&r.push(`Supported on: ${e.supportedOn}`),r.join(` `)}function Ae(e,t,n,r){let i=A({command:e,cwd:r.cwd,env:r.env,cliFlags:t}),a=i.flags,o=i.devicePin,s={};for(let[t,r]of Object.entries(a))Object.hasOwn(n,t)&&ve(t,e)&&(s[t]=r);return{input:ye(s,t),binding:k({env:r.env,policyOverrides:{sessionLock:a.sessionLock},configuredPlatform:a.platform,configuredSession:a.session}),devicePin:o}}const je={devices:async e=>({devices:e.map(N)}),apps:async e=>({apps:e})};async function Me(e,t){let n=je[e];return n?await n(t):t}function Ne(){let e=new Map;return{pinInput:(t,n,r)=>Je(t,n,Re(e,r,n.session)),mergeCommandResult:(t,n,r,i)=>ze(e,t,n,r,i),mergeErrorDetails:(t,n,r)=>{let i=V(n,r);Pe(e,i,t),Ke(e,i,t)}}}function Pe(e,t,n){let r=n?.refsGeneration;typeof r==`number`&&H(e,t,s(n),r)}const Fe=new Set([`snapshot`,`find`,`audit`]),Ie=new Set(i.map(e=>e.name).filter(E).filter(e=>n(e))),Le=new Set([`wait`,`press`,`click`,`fill`,`longpress`,`hover`,`get`]);function Re(e,t,n){return e.get(V(t,n))}function V(e,t){let n=typeof t==`string`&&t.length>0?t:`default`;return`${e??``}\u0000${n}`}function ze(e,t,n,r,i){let a=V(r,i);if(Ie.has(t)){Ue(e,a,We(n));return}Fe.has(t)&&(t===`find`?Ve(e,a,n):t===`audit`?He(e,a,n):Be(e,a,n))}function Be(e,t,n){let r=n.refsGeneration;if(typeof r!=`number`){e.delete(t);return}H(e,t,[...c(n.refs??[]),...c(n.nodes??[])],r)}function Ve(e,t,n){let r=n.refsGeneration;if(typeof r!=`number`)return;let i=[];typeof n.ref==`string`&&n.ref.startsWith(`@`)&&i.push(n.ref.slice(1));for(let e of n.matches??[])typeof e.ref==`string`&&e.ref.length>0&&i.push(e.ref.startsWith(`@`)?e.ref.slice(1):e.ref);H(e,t,i,r)}function He(e,t,n){let r=n.refsGeneration;typeof r==`number`&&H(e,t,n.findings.map(e=>e.ref.replace(/^@/,``)),r)}function Ue(e,t,n){n?.refsGeneration!==void 0&&H(e,t,[...n.diff?.lines??[],...n.refs??[],...n.tail??[]].map(e=>e.ref).filter(e=>typeof e==`string`),n.refsGeneration)}function We(e){let t=e.settle;return typeof t==`object`&&t?t:void 0}function H(e,t,n,r){if(n.length===0)return;let i=e.get(t)??new Map;e.set(t,i),Ge(i,n,r)}function Ge(e,t,n){for(let r of t)e.delete(r),e.set(r,n);for(;e.size>1e3;)e.delete(e.keys().next().value)}function Ke(e,t,n){let r=f(n?.divergence),i=f(r?.screen);if(i?.state!==`available`)return;let a=i.refsGeneration;if(typeof a!=`number`)return;let o=[];qe(i.refs,o),H(e,t,o,a)}function qe(e,t){if(Array.isArray(e))for(let n of e){let e=f(n)?.ref;typeof e==`string`&&e.length>0&&t.push(e)}}function Je(e,t,n){return n===void 0||n.size===0?t:Le.has(e)?Ye(t,n)??t:t}function Ye(e,t){if(typeof e.ref!=`string`)return;let n=U(e.ref,t);return n===e.ref?void 0:{...e,ref:n}}function U(e,t){if(!e.startsWith(`@`))return e;if(e.includes(`,`))return e.split(`,`).map(e=>U(e.trim(),t)).join(`,`);if(e.includes(`~`))return e;let n=t.get(e.slice(1));return n===void 0?e:`${e}~s${n}`}const Xe=Object.fromEntries(Object.entries(w).filter(([,e])=>e.kind===`alias`)),Ze=Object.fromEntries(Object.entries(w).filter(([,e])=>e.kind!==`alias`)),Qe=Object.fromEntries(ce().flatMap(e=>Object.entries(e.inputAudience).filter(([,e])=>e.kind===`operator`)));function W(e){return{...Xe,...e.inputAudience,...Ze,...De,...Ee,...Qe}}function $e(e){return ue().map(t=>{let n=e.outputSchemas&&t.name in L?L[t.name]:void 0,r=q(t.name,t);return{name:t.name,title:t.title,annotations:{readOnlyHint:t.effect===`read`,destructiveHint:t.effect===`destructive`},description:dt(t.name,he(t)),inputSchema:r,...n?{outputSchema:n}:{},_meta:et(t)}})}function et(e){return{...e.searchHint===void 0?{}:{"anthropic/searchHint":e.searchHint},...t(e.name)===`loop`?{"anthropic/alwaysLoad":!0}:{}}}function tt(e={}){let t=Ne();return{errorResult:async n=>await G(n,e.structuredContent,t,void 0,void 0),execute:async(n,r,i={})=>{if(!E(n))throw new u(`INVALID_ARGS`,`Unknown command tool: ${n}`);let a=D(n),o=J(n,a,r)??Y(a.inputSchema,r,n);if(o)return await G(new u(`INVALID_ARGS`,o.message,lt(o.hint)),e.structuredContent,t,void 0,r.session);let{captureHostEnvironment:s}=await import(`./sdk-io.js`).then(e=>e.r),c=s(process),l=Z(n,a.inputSchema).properties,{input:d,binding:f,devicePin:p}=Ae(n,r,l,c),m=at(d,f),h=K(d),g=t.pinInput(n,h,m.client.stateDir),_=await nt(e,m.client,c,i.signal,p);try{let r=await(e.runCommand??rt)(_,n,g);return t.mergeCommandResult(n,r,m.client.stateDir,h.session),{isError:!1,...it(e.structuredContent,n,m)?{structuredContent:await Me(n,r)}:{},content:[{type:`text`,text:await ft({name:n,input:h,result:r,env:c.env,config:m})}]}}catch(n){return await G(n,e.structuredContent,t,m.client.stateDir,h.session)}}}}async function G(e,t,n,r,i){let a=ke(e);return n.mergeErrorDetails(a.details,r,i),{isError:!0,...t?{structuredContent:a}:{},content:[{type:`text`,text:B(a)}]}}async function nt(e,t,n,r,i){if(e.createClient)return await e.createClient(t,i);let{createAgentDeviceClient:a}=await import(`./src.js`).then(e=>e.n),{createEnsuredDaemonTransport:o}=await import(`./src4.js`).then(e=>e.a),s=o({host:n});return a(t,{host:n,devicePin:i,transport:async(e,t)=>await s(e,r?{...t,signal:r}:t)})}async function rt(e,t,n){return await(await import(`./src3.js`).then(e=>e.n)).runCommand(e,t,n)}function it(e,t,n){return e===`always`?!0:e===`declared`?t in L||Q(n):!1}function at(e,t){return{client:{...st(e),...ot(t)}}}function ot(e){return e.lockPolicy?{lockPolicy:e.lockPolicy,...e.defaultPlatform?{lockPlatform:e.defaultPlatform}:{}}:{}}function st(e){let t=e.stateDir,n=e.cost,r=e.responseLevel,i={};if(t!==void 0){if(typeof t!=`string`||t.length===0)throw new u(`INVALID_ARGS`,`Expected stateDir to be a non-empty string.`);i.stateDir=t}if(n!==void 0){if(typeof n!=`boolean`)throw new u(`INVALID_ARGS`,`Expected cost to be a boolean.`);n&&(i.cost=!0)}let a=ct(r);return a!==void 0&&(i.responseLevel=a),i}function ct(e){if(e===void 0)return;let t=l.find(t=>t===e);if(t===void 0)throw new u(`INVALID_ARGS`,`Expected responseLevel to be one of 'digest', 'default', or 'full'.`);return t}function K(e){return Object.fromEntries(Object.entries(e).filter(([e])=>!z.has(e)))}function lt(e){return e===void 0?void 0:{hint:e}}function q(e,t){let n=W(t),r=Z(e,t.inputSchema);return{...r,properties:Object.fromEntries(Object.entries(r.properties).filter(([e])=>n[e]===void 0))}}function J(e,t,n){let r=q(e,t).properties,i=W(t);for(let a of Object.keys(n)){if(Object.hasOwn(r,a))continue;let n=Object.hasOwn(i,a)?i[a]:void 0;if(n?.kind!==`retired`)return n?.kind===`operator`?{message:ee(a,n.source)}:n?.kind===`alias`?{message:ne(a,n.of)}:{message:`${a} is not an accepted argument for the ${e} tool.`,hint:`Example call: ${e} ${JSON.stringify(b(t))}`}}}function Y(e,t,n){if(!(!e||t===void 0)){if(Array.isArray(t))return X(t,(t,r)=>Y(e.items,t,`${n}[${r}]`));if(d(t))return X(e.oneOf??[],e=>Y(e,t,n))??X(Object.entries(e.properties??{}),([e,r])=>Object.hasOwn(t,e)?ut(r,t,e,`${n}.${e}`):void 0)}}function ut(e,t,n,i){let a=e.commandInputFor;if(a===void 0)return Y(e,t[n],i);let o=t[n],s=r(t[a]);if(s===void 0||!d(o))return;let c=D(s),l=J(s,c,o);if(l===void 0)return;let u={[a]:s,[n]:b(c)};return{message:`${i}: ${l.message}`,...l.hint===void 0?{}:{hint:`Example step: ${JSON.stringify(u)}`}}}function X(e,t){for(let[n,r]of e.entries()){let e=t(r,n);if(e)return e}}function dt(t,n){let r=e(t);if(r.envelopeMs===`unbounded`)return`${n} Streams progress; no fixed client timeout.`;let i=Math.round(r.envelopeMs/1e3);return r.budget.source===`none`?`${n} Times out after ${i}s.`:`${n} Times out after ${i}s; a caller-supplied budget extends it.`}function Z(e,t){let n=a(e);return{...t,properties:{...t.properties,...n&&!t.properties?.noRecord?{noRecord:{type:`boolean`,description:`Do not record this action.`}}:{},...Oe()}}}async function ft(e){if(Q(e.config))return $(e.result);let t=await P({name:e.name,input:e.input,result:e.result,env:e.env,stderrIsTTY:!!process.stderr.isTTY,reader:`model`});return typeof t?.text==`string`?pt(e.name,t):$(t?.data??e.result)}function pt(e,t){let n=t.text??``;if(j(D(e))!==`stderr`)return n;let r=t.stderr?.trimEnd()??``;return r===``?n:`${n}${n.endsWith(` `)?``:` `}${r}`}function Q(e){let{responseLevel:t,cost:n}=e.client;return t!==void 0&&t!=="default"||n===!0}function $(e){return typeof e==`string`?e:JSON.stringify(e)}export{z as i,$e as n,B as r,tt as t};