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.

1 lines • 2.91 kB
import{at as e}from"./sdk-io.js";import{n as t}from"./version.js";import"./command.js";import n from"node:fs";import r from"node:path";import{fileURLToPath as i}from"node:url";const a=`agent-device`,o=`--agent-device-run-update-check`;function s(e){if(!l(e))return;let t=Date.now(),n=r.join(e.stateDir,`update-check.json`),i=f(n);u(i,e.currentVersion)&&(process.stderr.write(`Update available: ${a} ${e.currentVersion} -> ${i.latestVersion}. Run \`npm install -g ${a}@${_(e.currentVersion)}\` to upgrade the CLI.\n`),p(n,{...i,prompted:!0})),d(i,e.currentVersion,t)&&h(n,e.currentVersion)}async function c(e){let n=Date.now(),r=f(e.cachePath),i=_(e.currentVersion);try{let a=await v(i)??void 0;if(!a||t(a,e.currentVersion)<=0){p(e.cachePath,{checkedAt:new Date(n).toISOString(),distTag:i});return}p(e.cachePath,{checkedAt:new Date(n).toISOString(),distTag:i,latestVersion:a,prompted:r.latestVersion===a&&r.prompted===!0})}catch{let t=g(r)===i?r:{};p(e.cachePath,{...t,checkedAt:new Date(n).toISOString(),distTag:i})}}function l(e){return!e.command||e.command===`help`||e.command===`test`||e.flags.help||e.flags.version||e.flags.json||process.env.CI?.trim()||process.env.NODE_ENV===`test`||process.env.AGENT_DEVICE_NO_UPDATE_NOTIFIER?.trim()?!1:!!process.stderr.isTTY}function u(e,n){return!e.latestVersion||g(e)!==_(n)||t(e.latestVersion,n)<=0?!1:e.prompted!==!0}function d(e,t,n){if(g(e)!==_(t))return!0;let r=y(e.checkedAt);return r===void 0||n-r>=12096e5}function f(e){try{let t=JSON.parse(n.readFileSync(e,`utf8`));return{checkedAt:typeof t.checkedAt==`string`?t.checkedAt:void 0,distTag:typeof t.distTag==`string`?t.distTag:void 0,latestVersion:typeof t.latestVersion==`string`?t.latestVersion:void 0,prompted:t.prompted===!0}}catch{return{}}}function p(e,t){try{n.mkdirSync(r.dirname(e),{recursive:!0}),n.writeFileSync(e,`${JSON.stringify(t,null,2)}\n`,`utf8`)}catch{}}function m(){let e=i(import.meta.url),t=r.extname(e)||`.js`,a=[r.join(r.dirname(e),`update-check-entry${t}`),r.join(r.dirname(e),`internal`,`update-check-entry${t}`)].find(e=>n.existsSync(e));if(!a)throw Error(`Update check entrypoint not found. Rebuild the package to include the update-check worker entry.`);return a}function h(t,n){let r=m(),i=r.endsWith(`.ts`)?[`--experimental-strip-types`]:[];e(process.execPath,[...i,r,o,t,n])}function g(e){return e.distTag??`latest`}function _(e){return e.includes(`-nightly.`)?`nightly`:`latest`}async function v(e){let t=await fetch(`https://registry.npmjs.org/${a}/${e}`,{signal:AbortSignal.timeout(3500),headers:{accept:`application/json`}});if(!t.ok)return;let n=await t.json();return typeof n.version==`string`&&n.version.trim().length>0?n.version.trim():void 0}function y(e){if(!e)return;let t=Date.parse(e);return Number.isNaN(t)?void 0:t}function b(e){if(e[0]!==o)return null;let t=e[1]?.trim(),n=e[2]?.trim();return!t||!n?null:{cachePath:t,currentVersion:n}}export{b as n,c as r,s as t};