@droconnell/agent-inbox-cli 2.11.1 → 2.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/agent-inbox.mjs +444 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -76,6 +76,11 @@ While `connect` runs, a command in the Assistant's answer in Next gets **Run on*
|
|
|
76
76
|
computer shows what will run and asks `Run it? [y] yes [a] always [n] no` before each one; only `agent-inbox` commands
|
|
77
77
|
from a short list run, only in this folder, with no shell. Admins only.
|
|
78
78
|
|
|
79
|
+
The same line runs an app's browser tools: a few fixed steps on one website (open a page, fill a field, press a
|
|
80
|
+
button, read some text), in a browser on this computer with your own sign-in to that site. Your computer shows the tool,
|
|
81
|
+
the site and each step and asks `Run it? [y] yes [a] always for this tool [n] no`; it only reads, never leaves the
|
|
82
|
+
site, and stops after 2 minutes. It needs Playwright (`npm install -g playwright && npx playwright install chromium`).
|
|
83
|
+
|
|
79
84
|
## Update
|
|
80
85
|
|
|
81
86
|
```bash
|
package/agent-inbox.mjs
CHANGED
|
@@ -29,8 +29,9 @@
|
|
|
29
29
|
// agent-inbox script <file> [--propose] a file of terminal commands, one per line, as one plan; --propose sends
|
|
30
30
|
// it to Approvals (exit 0 nothing to change, 2 a plan, 1 problems)
|
|
31
31
|
// agent-inbox connect [--name "..."] in your support folder: Next's Assistant may offer to run agent-inbox commands
|
|
32
|
-
// here,
|
|
33
|
-
// browser
|
|
32
|
+
// here, Knowledge may ask it to check help steps in your product in a
|
|
33
|
+
// browser, and apps may ask it to run a browser tool that reads one site;
|
|
34
|
+
// each runs only after you say yes on this computer (Ctrl+C stops it)
|
|
34
35
|
// agent-inbox logout forget the sign-in on this computer
|
|
35
36
|
// agent-inbox update how to update, for how it was installed (a downloaded file updates itself)
|
|
36
37
|
//
|
|
@@ -44,7 +45,7 @@ import { spawn, spawnSync } from "node:child_process";
|
|
|
44
45
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
45
46
|
import { createRequire } from "node:module";
|
|
46
47
|
|
|
47
|
-
export const VERSION = "2.
|
|
48
|
+
export const VERSION = "2.13.0";
|
|
48
49
|
/** The npm package; its command is agent-inbox. (The name agent-inbox itself belongs to someone else on npm.) */
|
|
49
50
|
export const PACKAGE = "@droconnell/agent-inbox-cli";
|
|
50
51
|
const DIR = join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "agent-inbox");
|
|
@@ -1096,17 +1097,36 @@ async function update() {
|
|
|
1096
1097
|
if (!url) stop(`Sign in first with ${ME} login <your console's address>, so the CLI knows where to get the newest version. ${updateAdvice(how, url)}`);
|
|
1097
1098
|
if (!latest) stop(`Could not ask ${url} for the newest version. Check your connection and try again. ${updateAdvice(how, url)}`);
|
|
1098
1099
|
if (!newer(latest, VERSION)) return;
|
|
1099
|
-
|
|
1100
|
+
const problem = await replaceSelf(url, latest);
|
|
1101
|
+
if (problem) stop(`${problem} ${updateAdvice(how, url)}`);
|
|
1102
|
+
say(`Updated ${relative(process.cwd(), SELF) || SELF} to ${latest}.`);
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
/**
|
|
1106
|
+
* Replace this downloaded file with the server's copy of `latest`: written beside it first, checked, then moved over it
|
|
1107
|
+
* in one step. Null when it worked, otherwise what went wrong (nothing was changed).
|
|
1108
|
+
*/
|
|
1109
|
+
async function replaceSelf(url, latest) {
|
|
1100
1110
|
let body;
|
|
1101
1111
|
try {
|
|
1102
1112
|
const r = await fetch(`${url}/cli/agent-inbox.mjs`, { signal: AbortSignal.timeout(30_000) });
|
|
1103
1113
|
body = r.ok ? await r.text() : "";
|
|
1104
1114
|
} catch { body = ""; }
|
|
1105
|
-
if (!body.startsWith("#!/usr/bin/env node") || !body.includes(`const VERSION = "${latest}"`))
|
|
1115
|
+
if (!body.startsWith("#!/usr/bin/env node") || !body.includes(`const VERSION = "${latest}"`)) return `The download from ${url} did not look right, so nothing was changed.`;
|
|
1106
1116
|
const next = `${SELF}.download`;
|
|
1107
|
-
try { writeFileSync(next, body, { mode: 0o755 }); renameSync(next, SELF); }
|
|
1108
|
-
catch { rmSync(next, { force: true });
|
|
1109
|
-
|
|
1117
|
+
try { writeFileSync(next, body, { mode: 0o755 }); renameSync(next, SELF); return null; }
|
|
1118
|
+
catch { rmSync(next, { force: true }); return `Could not replace ${SELF}: it may belong to another user.`; }
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
/**
|
|
1122
|
+
* What updating to `version` runs on this computer, for how this copy was installed: npm or Homebrew (a fixed command
|
|
1123
|
+
* with fixed words, the version checked), or replacing the downloaded file. Shown to the person before they say yes.
|
|
1124
|
+
*/
|
|
1125
|
+
export function updateStep(how, version) {
|
|
1126
|
+
if (!/^\d+\.\d+\.\d+$/.test(String(version))) return null;
|
|
1127
|
+
if (how === "npm") return { shown: `npm i -g ${PACKAGE}@${version}`, command: "npm", args: ["i", "-g", `${PACKAGE}@${version}`] };
|
|
1128
|
+
if (how === "brew") return { shown: "brew upgrade agent-inbox", command: "brew", args: ["upgrade", "agent-inbox"] };
|
|
1129
|
+
return { shown: `download agent-inbox ${version} over ${SELF}`, command: null, args: [] };
|
|
1110
1130
|
}
|
|
1111
1131
|
|
|
1112
1132
|
// ---- For agents: an MCP server on this computer, and Claude Code -----------------------------------------------------
|
|
@@ -1693,6 +1713,274 @@ export async function checkPages(chromium, check, { profile = BROWSER_PROFILE, h
|
|
|
1693
1713
|
return results;
|
|
1694
1714
|
}
|
|
1695
1715
|
|
|
1716
|
+
// ---- Browser tools (prd/apps.md, "What only Next can build", 2) ----------------------------------------------------
|
|
1717
|
+
|
|
1718
|
+
/**
|
|
1719
|
+
* Next may also ask this computer to run an app's browser tool: a few steps on one site (open a path, fill a field by
|
|
1720
|
+
* its label with one of the tool's inputs, press a button or link, wait for some text, read text into a named output),
|
|
1721
|
+
* never code, and for now only tools that read. The same rules as validBrowserRun in src/lib/apps/browser-tools.ts
|
|
1722
|
+
* (src/lib/apps/browser-tools.test.ts checks they agree), checked here again, and every page the browser lands on is
|
|
1723
|
+
* checked against the site.
|
|
1724
|
+
*/
|
|
1725
|
+
export const BROWSER_STEPS = 20;
|
|
1726
|
+
export const BROWSER_READS = 10;
|
|
1727
|
+
export const BROWSER_INPUT_MAX = 500;
|
|
1728
|
+
export const BROWSER_OUTPUT_MAX = 2_000;
|
|
1729
|
+
export const BROWSER_OUTPUTS_MAX = 8_000;
|
|
1730
|
+
export const BROWSER_SECONDS = 120;
|
|
1731
|
+
/** A browser tool's screenshot is a JPEG of at most this many bytes. */
|
|
1732
|
+
export const BROWSER_SHOT_MAX = 1_000_000;
|
|
1733
|
+
const STEP_NAME = /^[a-z][a-z0-9_]{0,39}$/;
|
|
1734
|
+
const STEP_INPUT = /\{([a-z][a-z0-9_]{0,39})\}/g;
|
|
1735
|
+
const CONTROL_CHAR = /[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e]/;
|
|
1736
|
+
const plainOr = (x, max) => (typeof x === "string" && x.length >= 1 && x.length <= max && cleanText(x) === x ? x : null);
|
|
1737
|
+
|
|
1738
|
+
/** A browser tool's site as a whole origin (https, or http on this computer itself), or null. */
|
|
1739
|
+
export function browserSite(raw) {
|
|
1740
|
+
const url = productAddress(raw);
|
|
1741
|
+
if (!url) return null;
|
|
1742
|
+
const u = new URL(url);
|
|
1743
|
+
return u.pathname === "/" && !u.search ? url : null;
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
/** Where an open step goes: its path with each {input} filled in, encoded, on the site. Null when it leaves the site. */
|
|
1747
|
+
export function browserOpenUrl(site, path, input) {
|
|
1748
|
+
const filled = path.replace(STEP_INPUT, (_, k) => encodeURIComponent(input[k] ?? ""));
|
|
1749
|
+
let u;
|
|
1750
|
+
try { u = new URL(filled, site); } catch { return null; }
|
|
1751
|
+
u.hash = "";
|
|
1752
|
+
return onProductSite(u.toString(), site) ? u.toString() : null;
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
function browserInputs(steps) {
|
|
1756
|
+
const out = new Set();
|
|
1757
|
+
for (const s of steps) {
|
|
1758
|
+
if (s.do === "fill") out.add(s.input);
|
|
1759
|
+
if (s.do === "open") for (const m of s.path.matchAll(STEP_INPUT)) out.add(m[1]);
|
|
1760
|
+
}
|
|
1761
|
+
return [...out];
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
function browserStep(x, site) {
|
|
1765
|
+
if (!x || typeof x !== "object" || Array.isArray(x)) return null;
|
|
1766
|
+
const keys = Object.keys(x).sort().join(",");
|
|
1767
|
+
switch (x.do) {
|
|
1768
|
+
case "open": {
|
|
1769
|
+
const { path } = x;
|
|
1770
|
+
if (keys !== "do,path" || typeof path !== "string" || path.length < 1 || path.length > 200 || !path.startsWith("/") || path.startsWith("//")
|
|
1771
|
+
|| /[\s\\]/.test(path) || CONTROL_CHAR.test(path) || path.includes("#")) return null;
|
|
1772
|
+
if ([...path.matchAll(/\{([^}]*)\}/g)].some((m) => !STEP_NAME.test(m[1])) || /[{}]/.test(path.replace(STEP_INPUT, ""))) return null;
|
|
1773
|
+
return browserOpenUrl(site, path, {}) ? { do: "open", path } : null;
|
|
1774
|
+
}
|
|
1775
|
+
case "fill": {
|
|
1776
|
+
const label = plainOr(x.label, 80);
|
|
1777
|
+
return keys === "do,input,label" && label && typeof x.input === "string" && STEP_NAME.test(x.input) ? { do: "fill", label, input: x.input } : null;
|
|
1778
|
+
}
|
|
1779
|
+
case "press": {
|
|
1780
|
+
const label = plainOr(x.label, 80);
|
|
1781
|
+
return keys === "do,label" && label ? { do: "press", label } : null;
|
|
1782
|
+
}
|
|
1783
|
+
case "wait": {
|
|
1784
|
+
const text = plainOr(x.text, 120);
|
|
1785
|
+
return keys === "do,text" && text ? { do: "wait", text } : null;
|
|
1786
|
+
}
|
|
1787
|
+
case "read": {
|
|
1788
|
+
if (typeof x.into !== "string" || !STEP_NAME.test(x.into)) return null;
|
|
1789
|
+
if (keys === "do,into,label") { const label = plainOr(x.label, 80); return label ? { do: "read", label, into: x.into } : null; }
|
|
1790
|
+
if (keys === "css,do,into") {
|
|
1791
|
+
const { css } = x;
|
|
1792
|
+
if (typeof css !== "string" || css.length < 1 || css.length > 200 || CONTROL_CHAR.test(css) || css.includes(">>") || /^\s*[a-z_-]+\s*=/i.test(css) || css.trim() !== css) return null;
|
|
1793
|
+
return { do: "read", css, into: x.into };
|
|
1794
|
+
}
|
|
1795
|
+
return null;
|
|
1796
|
+
}
|
|
1797
|
+
default: return null;
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
/** A browser tool's steps on its site, as this computer accepts them, or null. */
|
|
1802
|
+
export function browserSteps(raw, site) {
|
|
1803
|
+
if (!Array.isArray(raw) || raw.length < 1 || raw.length > BROWSER_STEPS) return null;
|
|
1804
|
+
const out = [];
|
|
1805
|
+
for (const x of raw) { const s = browserStep(x, site); if (!s) return null; out.push(s); }
|
|
1806
|
+
if (out[0].do !== "open") return null;
|
|
1807
|
+
const reads = out.filter((s) => s.do === "read");
|
|
1808
|
+
if (reads.length > BROWSER_READS || new Set(reads.map((r) => r.into)).size !== reads.length) return null;
|
|
1809
|
+
return out;
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
/** A browser tool's run from Next, as this computer accepts it, or null (refused without asking). */
|
|
1813
|
+
export function browserRunRequest(x) {
|
|
1814
|
+
if (!x || typeof x !== "object") return null;
|
|
1815
|
+
const app = typeof x.app === "string" ? cleanText(x.app).slice(0, 60) : "";
|
|
1816
|
+
const site = browserSite(x.site);
|
|
1817
|
+
if (!app || typeof x.appId !== "string" || !/^[0-9a-f-]{36}$/i.test(x.appId) || typeof x.tool !== "string" || !STEP_NAME.test(x.tool)) return null;
|
|
1818
|
+
if (!Number.isInteger(x.version) || x.version < 1 || !site || site !== x.site || x.changes !== false) return null;
|
|
1819
|
+
const steps = browserSteps(x.steps, site);
|
|
1820
|
+
if (!steps) return null;
|
|
1821
|
+
const raw = x.input && typeof x.input === "object" && !Array.isArray(x.input) ? x.input : null;
|
|
1822
|
+
if (!raw) return null;
|
|
1823
|
+
const used = browserInputs(steps);
|
|
1824
|
+
if (Object.keys(raw).some((k) => !used.includes(k))) return null;
|
|
1825
|
+
const input = {};
|
|
1826
|
+
for (const k of used) {
|
|
1827
|
+
const v = raw[k];
|
|
1828
|
+
if (typeof v !== "string" || v.length > BROWSER_INPUT_MAX || CONTROL_CHAR.test(v.replace(/[\n\t]/g, ""))) return null;
|
|
1829
|
+
input[k] = v;
|
|
1830
|
+
}
|
|
1831
|
+
for (const s of steps) if (s.do === "open" && !browserOpenUrl(site, s.path, input)) return null;
|
|
1832
|
+
const forWhom = typeof x.for === "string" ? cleanText(x.for).slice(0, 120) : "";
|
|
1833
|
+
return { app, appId: x.appId, tool: x.tool, version: x.version, site, steps, input, for: forWhom, changes: false };
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
/** A step as this terminal shows it, with the value it fills in. */
|
|
1837
|
+
export function browserStepText(s, input = {}) {
|
|
1838
|
+
const v = (k) => `“${plainText(input[k] ?? "", 60)}”`;
|
|
1839
|
+
switch (s.do) {
|
|
1840
|
+
case "open": return `open ${plainText(s.path.replace(STEP_INPUT, (_, k) => encodeURIComponent(input[k] ?? "")), 120)}`;
|
|
1841
|
+
case "fill": return `fill “${plainText(s.label, 80)}” with ${v(s.input)}`;
|
|
1842
|
+
case "press": return `press “${plainText(s.label, 80)}”`;
|
|
1843
|
+
case "wait": return `wait for “${plainText(s.text, 120)}”`;
|
|
1844
|
+
case "read": return "css" in s ? `read ${plainText(s.css, 120)} as ${s.into}` : `read “${plainText(s.label, 80)}” as ${s.into}`;
|
|
1845
|
+
default: return "";
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
/** Where Always is kept for browser tools: per Next address, app, tool, version and site, so a new version asks again. */
|
|
1850
|
+
const BROWSER_ALWAYS = join(DIR, "browser-always.json");
|
|
1851
|
+
export const browserAlwaysKey = (nextHost, req) => [nextHost, req.appId, req.tool, req.version, req.site].join(" ");
|
|
1852
|
+
function browserAlways() {
|
|
1853
|
+
try { const x = JSON.parse(readFileSync(BROWSER_ALWAYS, "utf8")); return Array.isArray(x) ? x.filter((s) => typeof s === "string") : []; } catch { return []; }
|
|
1854
|
+
}
|
|
1855
|
+
function rememberBrowserAlways(key) {
|
|
1856
|
+
mkdirSync(DIR, { recursive: true, mode: 0o700 });
|
|
1857
|
+
writeFileSync(BROWSER_ALWAYS, `${JSON.stringify([...new Set([...browserAlways(), key])].slice(-200), null, 2)}\n`, { mode: 0o600 });
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
/** Text read from a page: no control or invisible characters (lines kept), spaces collapsed, at most BROWSER_OUTPUT_MAX. */
|
|
1861
|
+
const readText = (s) => String(s ?? "").replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e]/g, "").replace(/[ \t]+/g, " ").replace(/\n{3,}/g, "\n\n").trim().slice(0, BROWSER_OUTPUT_MAX);
|
|
1862
|
+
|
|
1863
|
+
/** The first of several ways to find something that is on screen, or null. */
|
|
1864
|
+
async function firstShown(ways) {
|
|
1865
|
+
for (const way of ways) {
|
|
1866
|
+
try { const n = Math.min(await way.count(), 5); for (let i = 0; i < n; i++) if (await way.nth(i).isVisible()) return way.nth(i); } catch { /* not this way */ }
|
|
1867
|
+
}
|
|
1868
|
+
return null;
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
/** What a field or a label shows: a field's value, else the text beside the label (a row, a list item, "Status: Delivered"). */
|
|
1872
|
+
async function readByLabel(page, label) {
|
|
1873
|
+
const field = await firstShown([page.getByLabel(label, { exact: true }), page.getByLabel(label)]);
|
|
1874
|
+
if (field) {
|
|
1875
|
+
const v = await field.evaluate((el) => ("value" in el && typeof el.value === "string" ? el.value : el.innerText)).catch(() => null);
|
|
1876
|
+
if (v !== null) return v;
|
|
1877
|
+
}
|
|
1878
|
+
const at = await firstShown([page.getByText(label, { exact: true }), page.getByText(label)]);
|
|
1879
|
+
if (!at) return null;
|
|
1880
|
+
return at.evaluate((el, label) => {
|
|
1881
|
+
const own = (el.innerText || "").trim();
|
|
1882
|
+
// "Status: Delivered" in one element.
|
|
1883
|
+
if (own.length > label.length && own.toLowerCase().startsWith(label.toLowerCase())) return own.slice(label.length).replace(/^\s*[:\-–]\s*/, "");
|
|
1884
|
+
// <dt>Status</dt><dd>Delivered</dd>, <th>Status</th><td>Delivered</td>, <span>Status</span> <span>Delivered</span>.
|
|
1885
|
+
const next = el.nextElementSibling;
|
|
1886
|
+
if (next && (next.innerText || "").trim()) return next.innerText;
|
|
1887
|
+
const parent = el.parentElement;
|
|
1888
|
+
const rest = parent ? (parent.innerText || "").replace(own, "").replace(/^\s*[:\-–]\s*/, "") : "";
|
|
1889
|
+
return rest.trim() ? rest : "";
|
|
1890
|
+
}, label).catch(() => null);
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1893
|
+
async function readByCss(page, css) {
|
|
1894
|
+
const el = await firstShown([page.locator(`css=${css}`)]);
|
|
1895
|
+
if (!el) return null;
|
|
1896
|
+
return el.evaluate((e) => ("value" in e && typeof e.value === "string" ? e.value : e.innerText)).catch(() => null);
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
/** Wait for a page to settle after it opened or something was pressed: loaded, then quiet for a moment if it gets there. */
|
|
1900
|
+
async function settled(page, ms) {
|
|
1901
|
+
await page.waitForLoadState("domcontentloaded", { timeout: Math.min(10_000, ms) }).catch(() => {});
|
|
1902
|
+
await page.waitForLoadState("networkidle", { timeout: Math.min(3_000, ms) }).catch(() => {});
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
/**
|
|
1906
|
+
* Run a browser tool's steps in a real browser with its own profile (the one step checks use), on its site only: a
|
|
1907
|
+
* navigation of the tab to anywhere else is blocked, other tabs are closed, and after each step the page must still
|
|
1908
|
+
* be on the site. `onStep` gets each step's result, a JPEG of the page after a step that opened or pressed something
|
|
1909
|
+
* (or failed), and what was read so far. Stops at the first step that fails, at `until`, or once `stopped()` says so.
|
|
1910
|
+
*/
|
|
1911
|
+
export async function runBrowserSteps(chromium, req, { profile = BROWSER_PROFILE, headless = false, until = Date.now() + BROWSER_SECONDS * 1000, stopped = () => false, onBrowser = () => {}, signIn = async () => true, onStep = async () => {} } = {}) {
|
|
1912
|
+
const fresh = !existsSync(profile);
|
|
1913
|
+
mkdirSync(profile, { recursive: true, mode: 0o700 });
|
|
1914
|
+
const context = await chromium.launchPersistentContext(profile, { headless, viewport: { width: 1280, height: 860 } });
|
|
1915
|
+
onBrowser(context);
|
|
1916
|
+
const steps = [], outputs = {};
|
|
1917
|
+
let problem = null, room = BROWSER_OUTPUTS_MAX;
|
|
1918
|
+
try {
|
|
1919
|
+
const page = context.pages()[0] ?? await context.newPage();
|
|
1920
|
+
// The first time, there is no sign-in in this profile yet: open the site and let the person sign in (anywhere).
|
|
1921
|
+
let go = true;
|
|
1922
|
+
if (fresh) {
|
|
1923
|
+
await page.goto(req.site, { waitUntil: "domcontentloaded", timeout: 30_000 }).catch(() => {});
|
|
1924
|
+
go = await signIn();
|
|
1925
|
+
}
|
|
1926
|
+
// From here only the site: the tab's own navigations elsewhere (a link, a redirect, a script) are blocked.
|
|
1927
|
+
let blocked = null;
|
|
1928
|
+
await context.route("**/*", (route) => {
|
|
1929
|
+
const r = route.request();
|
|
1930
|
+
let top = false;
|
|
1931
|
+
try { top = r.isNavigationRequest() && r.frame() === page.mainFrame(); } catch { /* not a page's request */ }
|
|
1932
|
+
if (top && !onProductSite(r.url(), req.site)) { blocked = r.url(); return route.abort("blockedbyclient"); }
|
|
1933
|
+
return route.continue();
|
|
1934
|
+
});
|
|
1935
|
+
context.on("page", (p) => { if (p !== page) p.close().catch(() => {}); });
|
|
1936
|
+
const left = () => Math.max(1_000, until - Date.now());
|
|
1937
|
+
for (let i = 0; i < req.steps.length; i++) {
|
|
1938
|
+
const s = req.steps[i];
|
|
1939
|
+
if (!go || stopped() || Date.now() > until) { problem = "stopped"; break; }
|
|
1940
|
+
let result = { ok: true }, changed = false;
|
|
1941
|
+
blocked = null;
|
|
1942
|
+
try {
|
|
1943
|
+
if (s.do === "open") {
|
|
1944
|
+
const url = browserOpenUrl(req.site, s.path, req.input);
|
|
1945
|
+
if (!url) result = { ok: false, problem: "off_site" };
|
|
1946
|
+
else { await page.goto(url, { waitUntil: "domcontentloaded", timeout: Math.min(30_000, left()) }); await settled(page, left()); changed = true; }
|
|
1947
|
+
} else if (s.do === "fill") {
|
|
1948
|
+
const field = await firstShown([page.getByLabel(s.label, { exact: true }), page.getByLabel(s.label)]);
|
|
1949
|
+
if (!field) result = { ok: false, problem: "not_found" };
|
|
1950
|
+
else await field.fill(req.input[s.input] ?? "", { timeout: Math.min(10_000, left()) });
|
|
1951
|
+
} else if (s.do === "press") {
|
|
1952
|
+
const target = await firstShown([page.getByRole("button", { name: s.label, exact: true }), page.getByRole("link", { name: s.label, exact: true }),
|
|
1953
|
+
page.getByRole("button", { name: s.label }), page.getByRole("link", { name: s.label }), page.getByRole("tab", { name: s.label }), page.getByRole("menuitem", { name: s.label })]);
|
|
1954
|
+
if (!target) result = { ok: false, problem: "not_found" };
|
|
1955
|
+
else { await target.click({ timeout: Math.min(10_000, left()) }); await settled(page, left()); changed = true; }
|
|
1956
|
+
} else if (s.do === "wait") {
|
|
1957
|
+
try { await page.getByText(s.text).first().waitFor({ state: "visible", timeout: Math.min(15_000, left()) }); } catch { result = { ok: false, problem: "timeout" }; }
|
|
1958
|
+
} else if (s.do === "read") {
|
|
1959
|
+
const text = "css" in s ? await readByCss(page, s.css) : await readByLabel(page, s.label);
|
|
1960
|
+
if (text === null) result = { ok: false, problem: "not_found" };
|
|
1961
|
+
else { outputs[s.into] = readText(text).slice(0, room); room -= outputs[s.into].length; }
|
|
1962
|
+
}
|
|
1963
|
+
} catch {
|
|
1964
|
+
result = { ok: false, problem: s.do === "open" ? "not_loaded" : "not_found" };
|
|
1965
|
+
}
|
|
1966
|
+
// Wherever the tab is now must be the site; a blocked navigation says where it tried to go.
|
|
1967
|
+
const at = page.url();
|
|
1968
|
+
if (blocked) result = { ok: false, problem: "off_site", at: blocked };
|
|
1969
|
+
else if (at !== "about:blank" && !onProductSite(at, req.site)) result = { ok: false, problem: "off_site", at };
|
|
1970
|
+
const onSite = result.problem !== "off_site";
|
|
1971
|
+
let shot = (changed || !result.ok) && onSite ? await page.screenshot({ type: "jpeg", quality: 60 }).catch(() => null) : null;
|
|
1972
|
+
if (shot && shot.length > BROWSER_SHOT_MAX) shot = await page.screenshot({ type: "jpeg", quality: 25 }).catch(() => null);
|
|
1973
|
+
if (shot && shot.length > BROWSER_SHOT_MAX) shot = null;
|
|
1974
|
+
steps.push(result);
|
|
1975
|
+
await onStep(i, result, shot, { ...outputs });
|
|
1976
|
+
if (!result.ok) break;
|
|
1977
|
+
}
|
|
1978
|
+
} finally {
|
|
1979
|
+
await context.close().catch(() => {});
|
|
1980
|
+
}
|
|
1981
|
+
return { steps, outputs, ...(problem ? { problem } : {}) };
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1696
1984
|
/** Text from Next shown in this terminal: no control characters, so nothing sent can move the cursor or recolour the screen. */
|
|
1697
1985
|
export const plainText = (s, max = 120) => String(s ?? "").replace(/[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e]/g, "").slice(0, max);
|
|
1698
1986
|
|
|
@@ -1754,7 +2042,7 @@ async function connect(words) {
|
|
|
1754
2042
|
return { status: r.status, data: await r.json().catch(() => ({})) };
|
|
1755
2043
|
};
|
|
1756
2044
|
|
|
1757
|
-
const hello = await line("POST", { action: "connect", name, folder: shown, version: VERSION, always: [] });
|
|
2045
|
+
const hello = await line("POST", { action: "connect", name, folder: shown, version: VERSION, installed: installedBy(real(SELF)), always: [] });
|
|
1758
2046
|
if (!hello) stop(`Could not reach ${url}. Check your connection and try again.`);
|
|
1759
2047
|
if (hello.status !== 200 || !hello.data?.id) stop(hello.data?.error || "Next did not accept this computer. Try again.");
|
|
1760
2048
|
const { id, line: lineId } = hello.data;
|
|
@@ -1876,9 +2164,153 @@ async function connect(words) {
|
|
|
1876
2164
|
: `Checked ${n} ${pagesWord} in ${secs} s: ${missing === 0 ? "every label was found" : `${missing} label${missing === 1 ? "" : "s"} not found`}. Next shows them on Knowledge.`);
|
|
1877
2165
|
};
|
|
1878
2166
|
|
|
2167
|
+
/**
|
|
2168
|
+
* An app's browser tool (prd/apps.md, "What only Next can build", 2): not a command. Shows the tool, its site, each
|
|
2169
|
+
* step with what it fills in, and who it is for, and asks; Always is offered because it only reads, and is kept for
|
|
2170
|
+
* that tool, site and version. Then runs the steps in a real browser with the profile step checks use, sending each
|
|
2171
|
+
* step's outcome and screenshot to Next as it goes, and what it read at the end. Stops after 2 minutes or at Ctrl+C.
|
|
2172
|
+
*/
|
|
2173
|
+
const browserTool = async (run) => {
|
|
2174
|
+
const req = browserRunRequest(run.browser);
|
|
2175
|
+
const ago = Math.floor((Date.now() - Date.parse(run.askedAt)) / 60_000);
|
|
2176
|
+
const when = ago >= 1 ? ` ${ago} minute${ago === 1 ? "" : "s"} ago` : "";
|
|
2177
|
+
if (!req) {
|
|
2178
|
+
say(`\n${clock()} ${plainText(run.from, 40)} sent a browser tool this computer does not accept. Not run: it only runs browser tools that read one site, with at most ${BROWSER_STEPS} steps.`);
|
|
2179
|
+
await report(run, { status: "declined", output: `This computer refused it: it only runs browser tools that read one site, with at most ${BROWSER_STEPS} steps.\n` });
|
|
2180
|
+
return;
|
|
2181
|
+
}
|
|
2182
|
+
const host = new URL(req.site).host;
|
|
2183
|
+
const profile = BROWSER_PROFILE.startsWith(homedir() + sep) ? `~${BROWSER_PROFILE.slice(homedir().length)}` : BROWSER_PROFILE;
|
|
2184
|
+
const list = req.steps.map((s, i) => ` ${i + 1}. ${browserStepText(s, req.input)}`).join("\n");
|
|
2185
|
+
const toolName = plainText(req.tool.replace(/_/g, " "), 40), appName = plainText(req.app, 60);
|
|
2186
|
+
say(`\n${clock()} ${plainText(run.from, 40)} wants to run ${appName}'s browser tool ${toolName} (version ${req.version}) on ${host}, for ${plainText(req.for || run.askedBy, 120)}, asked by ${plainText(run.askedBy, 60)}${when}:\n\n${list}\n\n` +
|
|
2187
|
+
`A browser opens with its own profile (${profile}) and stays on ${host}. It only reads: it fills in and presses only what is listed, and stops after ${BROWSER_SECONDS / 60} minutes.\n` +
|
|
2188
|
+
`What it reads, and a screenshot after each page it opens, go to Next, where only admins see the screenshots. They show whatever ${host} shows you.\n`);
|
|
2189
|
+
const key = browserAlwaysKey(new URL(url).host, req);
|
|
2190
|
+
let approved = browserAlways().includes(key) ? "always" : null;
|
|
2191
|
+
if (approved) say(`Running: you chose Always for ${toolName} on ${host}.`);
|
|
2192
|
+
while (!approved) {
|
|
2193
|
+
const a = await question(`Run it? [y] yes [a] always for this tool [n] no › `);
|
|
2194
|
+
if (a === "y" || a === "yes") approved = "once";
|
|
2195
|
+
else if (a === "a" || a === "always") { approved = "always"; rememberBrowserAlways(key); }
|
|
2196
|
+
else if (a === "n" || a === "no") break;
|
|
2197
|
+
else say("Type y, a or n, then Enter.");
|
|
2198
|
+
}
|
|
2199
|
+
if (!approved) { say("Not run."); await report(run, { status: "declined" }); return; }
|
|
2200
|
+
if (!(await report(run, { status: "running", approved }))) { say("Not run: the request ended in Next before you answered (no one was waiting for it any more, or it was disconnected)."); return; }
|
|
2201
|
+
const found = findPlaywright(folder);
|
|
2202
|
+
let chromium = null;
|
|
2203
|
+
if (found) { try { const pw = await import(pathToFileURL(found).href); chromium = pw.chromium ?? pw.default?.chromium ?? null; } catch { /* reported below */ } }
|
|
2204
|
+
if (!chromium) {
|
|
2205
|
+
say(`Not run: Playwright, which opens the browser, is not on this computer. Install it with:\n\n ${PLAYWRIGHT_INSTALL}\n`);
|
|
2206
|
+
await report(run, { status: "failed", problem: "no_playwright", result: { steps: [], outputs: {} } });
|
|
2207
|
+
return;
|
|
2208
|
+
}
|
|
2209
|
+
const started = Date.now(), n = req.steps.length;
|
|
2210
|
+
const done = [];
|
|
2211
|
+
let read = {};
|
|
2212
|
+
onLeave = () => report(run, { status: "failed", problem: "stopped", result: { steps: done, outputs: read } });
|
|
2213
|
+
say(`${clock()} Opening the browser…`);
|
|
2214
|
+
let result;
|
|
2215
|
+
try {
|
|
2216
|
+
result = await runBrowserSteps(chromium, req, {
|
|
2217
|
+
headless: process.env.AGENT_INBOX_HEADLESS === "1",
|
|
2218
|
+
until: started + BROWSER_SECONDS * 1000,
|
|
2219
|
+
stopped: () => leaving,
|
|
2220
|
+
onBrowser: (b) => { browser = b; },
|
|
2221
|
+
signIn: async () => {
|
|
2222
|
+
const a = await question(`This is the first browser run on this computer. Sign in to ${host} in the browser that opened, then press Enter here (n stops) › `);
|
|
2223
|
+
return a !== "n" && a !== "no";
|
|
2224
|
+
},
|
|
2225
|
+
onStep: async (i, r, shot, outputs) => {
|
|
2226
|
+
done.push(r); read = outputs;
|
|
2227
|
+
const how = r.ok ? "done" : r.problem === "off_site" ? `left ${host}${r.at ? ` for ${plainText(new URL(r.at).host, 80)}` : ""}, so it stopped` : r.problem === "not_loaded" ? "did not load" : r.problem === "timeout" ? "did not show" : "not found";
|
|
2228
|
+
say(`${i + 1}/${n} ${browserStepText(req.steps[i], req.input)} ${how}`);
|
|
2229
|
+
if (shot && shot.length <= BROWSER_SHOT_MAX) await line("POST", { action: "shot", id, line: lineId, run: run.id, page: i, image: shot.toString("base64") });
|
|
2230
|
+
await report(run, { status: "running", result: { steps: done, outputs } });
|
|
2231
|
+
},
|
|
2232
|
+
});
|
|
2233
|
+
} catch {
|
|
2234
|
+
browser = null; onLeave = null;
|
|
2235
|
+
say(`Not run: the browser did not start. If Playwright's browser is missing, install it with:\n\n ${PLAYWRIGHT_INSTALL}\n`);
|
|
2236
|
+
await report(run, { status: "failed", problem: "no_browser", result: { steps: done, outputs: read } });
|
|
2237
|
+
return;
|
|
2238
|
+
}
|
|
2239
|
+
browser = null; onLeave = null;
|
|
2240
|
+
if (leaving) return;
|
|
2241
|
+
const failed = Boolean(result.problem) || result.steps.length < n || result.steps.some((s) => !s.ok);
|
|
2242
|
+
await report(run, { status: failed ? "failed" : "done", ...(result.problem ? { problem: result.problem } : {}), result: { steps: result.steps, outputs: result.outputs }, exitCode: failed ? 1 : 0 });
|
|
2243
|
+
const secs = Math.max(1, Math.round((Date.now() - started) / 1000));
|
|
2244
|
+
const outs = Object.keys(result.outputs).length;
|
|
2245
|
+
say(failed
|
|
2246
|
+
? (result.problem === "stopped" ? `Stopped after ${BROWSER_SECONDS / 60} minutes at step ${result.steps.length + 1} of ${n}. Next shows how far it got.` : `Stopped at step ${result.steps.length} of ${n}. Next shows why.`)
|
|
2247
|
+
: `Done in ${secs} s: read ${outs} ${outs === 1 ? "thing" : "things"}. Next has what it read.`);
|
|
2248
|
+
};
|
|
2249
|
+
|
|
2250
|
+
/**
|
|
2251
|
+
* Update this CLI (prd/run-on-my-computer.md, "Updates"): Next sends only the version it offers. This computer shows
|
|
2252
|
+
* exactly what it will run for how it was installed, asks (never Always), runs it, checks the newly installed CLI says
|
|
2253
|
+
* the new version, and then starts connect again with it, in this same window.
|
|
2254
|
+
*/
|
|
2255
|
+
const updateSelf = async (run) => {
|
|
2256
|
+
const how = installedBy(real(SELF));
|
|
2257
|
+
const step = updateStep(how, run.version);
|
|
2258
|
+
const ago = Math.floor((Date.now() - Date.parse(run.askedAt)) / 60_000);
|
|
2259
|
+
const when = ago >= 1 ? ` ${ago} minute${ago === 1 ? "" : "s"} ago` : "";
|
|
2260
|
+
if (!step || !newer(run.version, VERSION)) {
|
|
2261
|
+
say(`\n${clock()} Next asked to update agent-inbox, but this computer already has ${VERSION}. Not updated.`);
|
|
2262
|
+
await report(run, { status: "declined", output: `This computer already has agent-inbox ${VERSION}.\n` });
|
|
2263
|
+
return;
|
|
2264
|
+
}
|
|
2265
|
+
say(`\n${clock()} ${plainText(run.from, 40)} wants to update agent-inbox from ${VERSION} to ${run.version}, asked by ${plainText(run.askedBy, 60)}${when}:\n\n ${step.shown}\n\nThen connect starts again with the new version, here.\n`);
|
|
2266
|
+
let yes = null;
|
|
2267
|
+
while (yes === null) {
|
|
2268
|
+
const a = await question("Update it? [y] yes [n] no › ");
|
|
2269
|
+
if (a === "y" || a === "yes") yes = true;
|
|
2270
|
+
else if (a === "n" || a === "no") yes = false;
|
|
2271
|
+
else say("Type y or n, then Enter.");
|
|
2272
|
+
}
|
|
2273
|
+
if (!yes) { say("Not updated."); await report(run, { status: "declined" }); return; }
|
|
2274
|
+
if (!(await report(run, { status: "running", approved: "once" }))) { say("Not updated: the request ended in Next before you answered (after 10 minutes, or it was disconnected). Ask again from Next."); return; }
|
|
2275
|
+
let out = "";
|
|
2276
|
+
const take = (t) => { process.stdout.write(t); out += t; };
|
|
2277
|
+
let code = 0;
|
|
2278
|
+
if (step.command) {
|
|
2279
|
+
code = await new Promise((done) => {
|
|
2280
|
+
// A fixed command with fixed words. On Windows npm is npm.cmd, which Node only starts through the shell.
|
|
2281
|
+
child = spawn(step.command, step.args, { shell: process.platform === "win32", windowsHide: true, stdio: ["ignore", "pipe", "pipe"], env: { ...process.env, NO_COLOR: "1" } });
|
|
2282
|
+
child.stdout.on("data", (b) => take(b.toString("utf8"))); child.stderr.on("data", (b) => take(b.toString("utf8")));
|
|
2283
|
+
const limit = setTimeout(() => { take("\nStopped after 10 minutes.\n"); child?.kill(); }, 10 * 60_000);
|
|
2284
|
+
child.on("error", () => { clearTimeout(limit); take(`Could not start ${step.command}: is it installed on this computer?\n`); done(-1); });
|
|
2285
|
+
child.on("close", (c) => { clearTimeout(limit); done(c ?? -1); });
|
|
2286
|
+
});
|
|
2287
|
+
child = null;
|
|
2288
|
+
} else {
|
|
2289
|
+
const problem = await replaceSelf(url, run.version);
|
|
2290
|
+
if (problem) { take(`${problem}\n`); code = 1; }
|
|
2291
|
+
}
|
|
2292
|
+
// The CLI now installed, asked for its version: the update counts only if it says the new one.
|
|
2293
|
+
const got = code === 0 ? spawnSync(process.execPath, [process.argv[1], "--version"], { encoding: "utf8", windowsHide: true, timeout: 30_000 }).stdout?.trim() : "";
|
|
2294
|
+
const ok = code === 0 && got && !newer(run.version, got);
|
|
2295
|
+
if (!ok && code === 0) take(how === "brew" ? `Homebrew has no newer agent-inbox yet (this computer has ${got || VERSION}). Try again later.\n` : `The update ran, but agent-inbox still says ${got || VERSION}.\n`);
|
|
2296
|
+
if (ok) take(`agent-inbox is now ${got}.\n`);
|
|
2297
|
+
await report(run, { status: ok ? "done" : "failed", exitCode: ok ? 0 : code || 1, output: out });
|
|
2298
|
+
if (!ok) { say("Not updated. Next shows what it printed."); return; }
|
|
2299
|
+
say(`${clock()} Updated. Starting connect again with ${got}…`);
|
|
2300
|
+
// The new version takes over this window: this process only waits for it and ends when it does.
|
|
2301
|
+
leaving = true;
|
|
2302
|
+
rl.close();
|
|
2303
|
+
for (const sig of ["SIGINT", "SIGTERM", "SIGHUP"]) { process.removeAllListeners(sig); process.on(sig, () => {}); }
|
|
2304
|
+
const again = spawn(process.execPath, process.argv.slice(1), { stdio: "inherit", shell: false });
|
|
2305
|
+
again.on("exit", (c) => process.exit(c ?? 0));
|
|
2306
|
+
again.on("error", () => { say(`Could not start connect again. Run ${ME} connect.`); process.exit(1); });
|
|
2307
|
+
};
|
|
2308
|
+
|
|
1879
2309
|
/** One request: check it, show it, ask, then run this CLI with its words in this folder, and send back what it printed. */
|
|
1880
2310
|
const handle = async (run) => {
|
|
1881
2311
|
if (run.kind === "steps") return checkSteps(run);
|
|
2312
|
+
if (run.kind === "update") return updateSelf(run);
|
|
2313
|
+
if (run.kind === "browser") return browserTool(run);
|
|
1882
2314
|
// Run all 3: two or three commands that may run without asking, shown together and asked about once.
|
|
1883
2315
|
const several = Array.isArray(run.steps);
|
|
1884
2316
|
const steps = several ? runnableSequence(run.steps) : (() => { const argv = runnable(run.sub, run.args); return argv ? [{ sub: run.sub, argv }] : null; })();
|
|
@@ -1948,7 +2380,10 @@ async function connect(words) {
|
|
|
1948
2380
|
};
|
|
1949
2381
|
let lostSince = 0;
|
|
1950
2382
|
for (;;) {
|
|
2383
|
+
// Once leaving (an update started connect again, which now asks for work), this one asks no more.
|
|
2384
|
+
if (leaving) return new Promise(() => {});
|
|
1951
2385
|
const got = await line("GET", null, `?id=${id}&line=${lineId}&wait=25000&always=${encodeURIComponent([...always].join(","))}`);
|
|
2386
|
+
if (leaving) return new Promise(() => {});
|
|
1952
2387
|
if (!got || got.status !== 200) {
|
|
1953
2388
|
if (!lostSince) { lostSince = Date.now(); say(`${clock()} Lost the line to Next. Trying again…`); }
|
|
1954
2389
|
if (Date.now() - lostSince > 10 * 60_000) await leave("Could not reach Next for 10 minutes, so this computer disconnected. Run connect again.");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@droconnell/agent-inbox-cli",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.0",
|
|
4
4
|
"description": "The Next CLI, built for agents: JSON on every command, an MCP server for any AI tool, live events, apps and webhooks from the terminal, and a support workspace's setup as files in git.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|