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 • 10.2 kB
JavaScript
import{v as e}from"./sdk-contracts.js";import{n as t,t as n}from"./durable-resource.js";import{n as r}from"./async-lifecycle.js";function i(e,t){if(e.resourceKind!==t.resourceKind)return{status:`unreattachable`,reason:`descriptor-invalid`,message:`Durable resource kind ${e.resourceKind} does not match ${t.resourceKind}`};if(e.descriptor.version!==t.version)return{status:`unreattachable`,reason:`descriptor-version-unsupported`,message:`Unsupported ${t.resourceKind} descriptor version: ${e.descriptor.version}`,version:e.descriptor.version};try{let n=t.decode(e.descriptor.body);return n.status===`decoded`?n:{status:`unreattachable`,reason:`descriptor-invalid`,message:n.message}}catch(e){return{status:`unreattachable`,reason:`descriptor-invalid`,message:e instanceof Error?e.message:`Descriptor codec rejected the body`}}}function a(e,t,n={}){return Object.freeze({pendingHandle:new r(e),envelope:t,...n.relaunched?{relaunched:!0}:{}})}function o(e){return Object.freeze({appLogReattach:async({envelope:t})=>{let n=i(t,e.codec);return n.status===`unreattachable`?n:await e.reattach(n.descriptor,s(t))},appLogCleanup:async({envelope:t})=>{let n=i(t,e.codec);return n.status===`decoded`?await e.cleanup(n.descriptor,s(t)):{status:`cleanup-pending`,reason:`manual-recovery-required`,message:n.message}}})}function s(e){return Object.freeze({sessionId:e.sessionId,fence:e.fence})}function c(e){if(typeof e!=`object`||!e||Array.isArray(e))return{status:`invalid`,message:`App-log process marker must be an object`};let t=e;return!Number.isInteger(t.pid)||Number(t.pid)<=0?{status:`invalid`,message:`App-log process marker pid must be a positive integer`}:typeof t.startTime!=`string`||t.startTime.trim().length===0?{status:`invalid`,message:`App-log process marker startTime must be a non-empty string`}:typeof t.command!=`string`||t.command.trim().length===0?{status:`invalid`,message:`App-log process marker command must be a non-empty string`}:{status:`decoded`,marker:Object.freeze({pid:Number(t.pid),startTime:t.startTime,command:t.command})}}function l(e){let t;return Object.freeze({inspect:()=>e.inspect(),finish:()=>t??=e.finish(),...n(()=>e.forceCleanup(),d)})}function u(e){return l({inspect:e.inspect,finish:e.finish,forceCleanup:async()=>{let t=await e.finish();return t.status===`completed`?{status:`cleaned`}:{status:`cleanup-pending`,reason:t.reason,message:t.message}}})}function d(n,r){if(!t(n))throw new e(`COMMAND_FAILED`,n.message??`Durable resource cleanup could not be confirmed`,{reason:n.reason,retriable:n.reason!==`ownership-fence-lost`,hint:n.reason===`ownership-fence-lost`?`Use the current resource owner or recovery record before retrying cleanup.`:`Keep the recovery record and retry cleanup through the exact runtime owner.`},r)}function f(e,t,n){let r=e.artifacts.resolveSession(t);return n.outputPath===r.outputPath&&(n.pidPath===void 0||n.pidPath===r.pidPath)}function p(t,n){if(!f(t,n.sessionId,n))throw new e(`INVALID_ARGS`,`App-log paths do not match the owning session`)}function m(e){for(let t of[/\bstatus(?:Code)?["'=: ]+([1-5]\d{2})\b/i,/\bresponse(?:\s+code)?["'=: ]+([1-5]\d{2})\b/i,/\bHTTP\/[0-9.]+\s+([1-5]\d{2})\b/i]){let n=t.exec(e);if(!n)continue;let r=Number.parseInt(n[1]??``,10);if(Number.isInteger(r))return r}return null}function h(e){return/\b\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z)?\b/.exec(e)?.[0]??/\b\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+\b/.exec(e)?.[0]}function g(e){let t=e.indexOf(`{`),n=e.lastIndexOf(`}`);if(t<0||n<=t)return null;try{let r=JSON.parse(e.slice(t,n+1));return r&&typeof r==`object`&&!Array.isArray(r)?r:null}catch{return null}}function _(e,t){for(let n of t){let t=e?.[n];if(typeof t==`string`&&t.trim())return t.trim()}}function ee(e,t){for(let n of t){let t=e?.[n];if(typeof t==`number`&&Number.isInteger(t))return t;if(typeof t==`string`&&/^\d{3}$/.test(t.trim()))return Number.parseInt(t,10)}return null}function te(e,t){let n=t?.headers??t?.requestHeaders??t?.responseHeaders;return n===void 0?/\bheaders?["'=: ]+(\{.*\})/i.exec(e)?.[1]?.trim():y(n)}function v(e,t,n){for(let r of n){if(t?.[r]!==void 0)return y(t[r]);let n=r.replaceAll(/[.*+?^${}()|[\]\\]/g,String.raw`\$&`),i=RegExp(`\\b${n}["'=: ]+(.+)$`,`i`).exec(e);if(i?.[1])return i[1].trim()}}function y(e){if(typeof e==`string`)return e;try{return JSON.stringify(e)}catch{return String(e)}}function b(e,t,n){let r=C(t,n,5),i=e.packetId??r.map(x).find(e=>typeof e==`string`&&e.length>0);i&&(e.packetId=i);let a=i?C(t,n,12).filter(e=>x(e)===i):r;e.timestamp??=a.map(h).find(e=>typeof e==`string`&&e.length>0),e.status??=a.map(m).find(e=>typeof e==`number`)??void 0,e.durationMs??=a.map(S).find(e=>typeof e==`number`)??void 0}function x(e){return/\bpacket id (\d+)\b/i.exec(e)?.[1]??null}function S(e){let t=/\b(?:duration|elapsed request\/response time, ms)[:= ]+(\d+)\b/i.exec(e);return t?Number.parseInt(t[1]??``,10):null}function C(e,t,n){return e.slice(Math.max(0,t-n),Math.min(e.length,t+n+1)).map(e=>e.trim()).filter(Boolean)}const w=RegExp(`\\b(${[`GET`,`POST`,`PUT`,`PATCH`,`DELETE`,`HEAD`,`OPTIONS`].join(`|`)})\\b\\s+https?:\\/\\/`,`i`),T=/https?:\/\/[^\s"'<>\])]+/i,E=/\[C(\d+)\b[^\]]*?\burl:\s*([^\s,\]]+)/,D=/\bsummary for task (?:success|failure)\s*\{([^}]*)\}/,ne=/\bTask\s+<([0-9A-Fa-f-]+)>\.<(\d+)>/,O=/(?:^|\s)(\S+)\[(\d+):[0-9a-f]+\]/,re=/\burl:\s*(https?:\/\/[^\s,\]]+)/i;function k(e,t,n=e.dump.limits.maxEntries){let r=e.dump.entries.slice(0,n),i=new Set(r.map(U));for(let e of t.dump.entries){if(r.length>=n)break;let t=U(e);i.has(t)||(i.add(t),r.push(e))}let a=new Set([...e.dump.entries,...t.dump.entries].map(e=>e.packetId).filter(e=>e!==void 0)),o=[...new Set([...e.unnamedRequestIds,...t.unnamedRequestIds])].filter(e=>!a.has(e));return Object.freeze({dump:Object.freeze({...e.dump,matchedLines:r.length,entries:Object.freeze(r),unnamedRequests:o.length}),unnamedRequestIds:Object.freeze(o)})}function A(e,t){let n=G(t.maxEntries,25,1,200),r=t.include??`summary`,i=G(t.maxPayloadChars,2048,64,16384),a=G(t.maxScanLines,4e3,100,2e4),o=M(t.lineNumberOffset);if(!t.exists)return Object.freeze({dump:Object.freeze({path:t.path,exists:!1,scannedLines:0,matchedLines:0,entries:Object.freeze([]),unnamedRequests:0,include:r,limits:Object.freeze({maxEntries:n,maxPayloadChars:i,maxScanLines:a})}),unnamedRequestIds:Object.freeze([])});let s=e.split(`
`),c=Math.max(0,s.length-a),l=s.slice(c),u=[],d=j(t.backend)?K(l):void 0,f=d?Y(l,d):[];for(let e=l.length-1;e>=0&&u.length<n;--e){if(!l[e]?.trim())continue;let n=N(l,e,o+c+e+1,t.backend,r,i,d);n&&u.push(n)}return Object.freeze({dump:Object.freeze({path:t.path,exists:!0,scannedLines:l.length,matchedLines:u.length,entries:Object.freeze(u),unnamedRequests:f.length,include:r,limits:Object.freeze({maxEntries:n,maxPayloadChars:i,maxScanLines:a})}),unnamedRequestIds:Object.freeze(f)})}function j(e){return e===`ios-simulator`||e===`ios-device`||e===`macos`}function M(e){if(e===void 0)return 0;if(!Number.isInteger(e)||e<0)throw TypeError(`Network line number offset must be a non-negative integer`);return e}function N(e,t,n,r,i,a,o){let s=e[t]?.trim();if(!s)return null;let c=g(s),l=P(s,c)??(o?J(s,o,t):null);if(!l)return null;let u=z(s,n,l,a);return r===`android`&&b(u,e,t),B(u,s,c,i,a),u}function P(e,t){let n=F(e,t),r=I(e,t);if(!r)return null;let i=L(e,t);return R(e,n.explicit,i)?{method:n.value,url:r,status:i}:null}function F(e,t){let n=_(t,[`method`,`httpMethod`]),r=/\bmethod["'=: ]+([A-Z]+)\b/i.exec(e)?.[1],i=w.exec(e)?.[1],a=n??r??i;return{value:a?.toUpperCase(),explicit:!!a}}function I(e,t){return _(t,[`url`,`requestUrl`])||(re.exec(e)?.[1]??T.exec(e)?.[0])}function L(e,t){return ee(t,[`status`,`statusCode`,`responseCode`])??m(e)??void 0}function R(e,t,n){return t||n!==void 0||/\bURL["'=: ]+https?:\/\//i.test(e)||/\bheaders?["'=: ]+/i.test(e)||/\b(?:requestBody|responseBody|payload|request|response)["'=: ]+/i.test(e)}function z(e,t,n,r){return{...n,timestamp:h(e),packetId:n.packetId??x(e)??void 0,durationMs:n.durationMs??S(e)??void 0,raw:W(e,r),line:t}}function B(e,t,n,r,i){if(V(r)){let r=te(t,n);r&&(e.headers=W(r,i))}if(H(r)){let r=v(t,n,[`requestBody`,`body`,`payload`,`request`]),a=v(t,n,[`responseBody`,`response`]);r&&(e.requestBody=W(r,i)),a&&(e.responseBody=W(a,i))}}function V(e){return e===`headers`||e===`all`}function H(e){return e===`body`||e===`all`}function U(e){return`${e.timestamp??``}|${e.method??``}|${e.url}|${e.status??``}|${e.raw}`}function W(e,t){return e.length<=t?e:`${e.slice(0,t)}...<truncated>`}function G(e,t,n,r){return e===void 0||!Number.isInteger(e)?t:Math.max(n,Math.min(r,e))}function K(e){let t=new Map;for(let[n,r]of e.entries()){let e=E.exec(r);if(!e)continue;let i=q(r,e[1]),a=i===void 0?void 0:ae(e[2]);if(!a||i===void 0)continue;let o=t.get(i);o?o.push({lineIndex:n,origin:a}):t.set(i,[{lineIndex:n,origin:a}])}return t}function q(e,t){let n=O.exec(e);if(n)return`${n[1]}[${n[2]}]#${t}`}function J(e,t,n){let r=D.exec(e);if(!r)return null;let i=Q(r[1]);if(i.get(`reused`)!==`1`)return null;let a=i.get(`connection`),o=a===void 0?void 0:q(e,a),s=o===void 0?void 0:Z(t,o,n);return s?{url:s,status:oe(i.get(`response_status`)),durationMs:$(i.get(`transaction_duration_ms`)),pathUnavailable:!0,packetId:X(e)}:null}function Y(e,t){let n=new Set;for(let[r,i]of e.entries()){let e=ie(i,t,r);e!==void 0&&n.add(e)}return[...n]}function ie(e,t,n){if(!e.includes(`summary for task`))return;let r=D.exec(e);if(!r)return;let i=Q(r[1]);if(i.get(`reused`)!==`1`)return;let a=i.get(`connection`),o=a===void 0?void 0:q(e,a);if(!(o!==void 0&&Z(t,o,n)))return X(e)}function X(e){let t=ne.exec(e);if(!t)return;let n=O.exec(e);return`${n?`${n[1]}[${n[2]}]`:``}#${t[1]}.${t[2]}`}function Z(e,t,n){let r=e.get(t);if(!r)return;let i;for(let e of r){if(e.lineIndex>n)break;i=e.origin}return i}function Q(e){let t=new Map;for(let n of e.split(`,`)){let e=n.indexOf(`=`);e!==-1&&t.set(n.slice(0,e).trim(),n.slice(e+1).trim())}return t}function ae(e){try{return new URL(e).origin}catch{return}}function oe(e){let t=$(e);return t!==void 0&&t>0?t:void 0}function $(e){if(e===void 0)return;let t=Number(e);return Number.isFinite(t)?t:void 0}export{u as a,c,p as i,i as l,A as n,o,f as r,a as s,k as t};