shellway 0.8.8 → 0.8.10
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/package.json +1 -1
- package/shellway.js +50 -15
package/package.json
CHANGED
package/shellway.js
CHANGED
|
@@ -10,7 +10,7 @@ import { readFile, writeFile, appendFile, readdir, stat, lstat, mkdir, rename, c
|
|
|
10
10
|
import { createServer as netServer, connect as netConnect } from 'node:net';
|
|
11
11
|
import { randomBytes, timingSafeEqual, createHash } from 'node:crypto';
|
|
12
12
|
|
|
13
|
-
const AGENT_VERSION = '0.8.
|
|
13
|
+
const AGENT_VERSION = '0.8.10'; // keep equal to agent/package.json
|
|
14
14
|
if (Number(process.versions.node.split('.')[0]) < 22) {
|
|
15
15
|
console.error(`Shellway needs Node.js 22 or newer (you have ${process.versions.node}). Get it at https://nodejs.org`);
|
|
16
16
|
process.exit(1);
|
|
@@ -1349,8 +1349,17 @@ async function refPoint(s, ref, text) {
|
|
|
1349
1349
|
for (const e of __all(sel)) { const r = e.getBoundingClientRect(); if (r.width < 1 || r.height < 1) continue;
|
|
1350
1350
|
const l = (e.getAttribute('aria-label') || e.innerText || e.value || e.getAttribute('placeholder') || e.getAttribute('title') || hostLabel(e) || '').replace(/\\s+/g, ' ').trim().toLowerCase();
|
|
1351
1351
|
if (l === want) return e; if (!partial && l && l.includes(want)) partial = e; } return partial; })()`;
|
|
1352
|
-
|
|
1353
|
-
|
|
1352
|
+
// 0.8.10 (owner's live test 29 Nov): on pages with CSS "scroll-behavior: smooth" the scroll was still moving when the
|
|
1353
|
+
// position was measured, so a click on an item below the fold landed on the old spot and did nothing (the Check line
|
|
1354
|
+
// showed NO CHANGE). Scroll instantly, then measure again a moment later and use the item's final position.
|
|
1355
|
+
const r0 = await evalJs(s, `(() => { ${DEEP_JS} const e = ${pick}; if (!e) return null; window.__swPick = e; e.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' }); return true; })()`);
|
|
1356
|
+
let r = null;
|
|
1357
|
+
if (r0) {
|
|
1358
|
+
await sleep(120);
|
|
1359
|
+
r = await evalJs(s, `(() => { const e = window.__swPick; if (!e || !e.isConnected) return null; let b = e.getBoundingClientRect();
|
|
1360
|
+
if (b.bottom < 0 || b.top > innerHeight || b.right < 0 || b.left > innerWidth) { e.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' }); b = e.getBoundingClientRect(); }
|
|
1361
|
+
const tag = e.tagName.toLowerCase(); return { x: b.left + b.width / 2, y: b.top + b.height / 2, tag, type: (e.getAttribute('type') || '').toLowerCase() }; })()`);
|
|
1362
|
+
}
|
|
1354
1363
|
if (!r) throw new Error(ref !== undefined && ref !== null && ref !== '' ? `Item [${ref}] was not found: the page has changed. Call browser_read again for fresh numbers.` : `No clickable item with the text "${text}" was found. Call browser_read to see what is on the page.`);
|
|
1355
1364
|
return r;
|
|
1356
1365
|
}
|
|
@@ -1442,23 +1451,45 @@ function proofText(b, a, what) {
|
|
|
1442
1451
|
const noisy = b.noise > 0;
|
|
1443
1452
|
if (upd > 0) ch.push(`${upd} update${upd > 1 ? 's' : ''} inside the page${noisy ? ` (the page also changes by itself: ${b.noise} in 0.3 s before the ${what}, so this alone proves nothing)` : ''}`);
|
|
1444
1453
|
const real = ch.length > (upd > 0 && noisy ? 1 : 0);
|
|
1454
|
+
// 0.8.10 (owner's live test on claude.ai): a missed click still caused a few page updates (hover effects), and the
|
|
1455
|
+
// Check said "had an effect". Updates alone are now reported as a weak sign, not as proof.
|
|
1456
|
+
if (real && upd > 0 && ch.length === 1) return `Check: only a small change after the ${what}: ${upd} update${upd > 1 ? 's' : ''} inside the page, same address and title${b.ref ? ', the item looks the same' : ''}. That can be a hover effect rather than the ${what} itself: if you expected a new page, a dialog or a changed item, confirm with browser_read before relying on it.`;
|
|
1445
1457
|
return real ? `Check: the ${what} had an effect: ${ch.join('; ')}.`
|
|
1446
1458
|
: `Check: NO CHANGE DETECTED after the ${what}${upd > 0 ? ` (only ${ch.join('; ')})` : ''}: same page, same address and title${b.ref ? ', the item looks the same' : ''}. It may not have worked, or it changed something this check cannot see (a download, a canvas, another tab or window). Confirm with browser_read or browser_screenshot before relying on it.`;
|
|
1447
1459
|
}
|
|
1448
1460
|
const MINIMISED = 'The browser window is minimised or hidden, so the page has no size and cannot be captured (Shellway tried to restore it). Restore the browser window (focus_window with its title, or click it on the taskbar) and take the screenshot again.';
|
|
1449
1461
|
// 0.8.8 (Ruslan 28 Nov): "Cannot take screenshot with 0 width" = a minimised browser window. On Windows, restore the
|
|
1450
1462
|
// minimised window of that browser (the one whose title starts with the tab's title, or the only minimised one).
|
|
1451
|
-
async function restoreBrowserWindow(tabTitle) {
|
|
1463
|
+
async function restoreBrowserWindow(tabTitle, focus = false) {
|
|
1452
1464
|
if (platform() !== 'win32') return false;
|
|
1453
1465
|
const procs = { edge: 'msedge', chrome: 'chrome', brave: 'brave', opera: 'opera' };
|
|
1454
1466
|
const names = extMode() && ext.cur && procs[ext.cur.browser] ? [procs[ext.cur.browser]] : ['msedge', 'chrome', 'brave'];
|
|
1455
1467
|
try {
|
|
1456
1468
|
const out = await runPs(`${WIN_API}\n$t=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${b64(String(tabTitle || '').slice(0, 60))}'))\n`
|
|
1457
|
-
+ `$w=@(Get-Process -Name ${names.map((x) => `'${x}'`).join(',')} -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowHandle -ne 0 -and [SwWin.W]::IsIconic($_.MainWindowHandle) })\n`
|
|
1458
|
-
+ "$p=@($w | Where-Object { $t -and $_.MainWindowTitle.StartsWith($t) })[0]\nif (-not $p -and $w.Count -eq 1) { $p=$w[0] }\
|
|
1469
|
+
+ `$w=@(Get-Process -Name ${names.map((x) => `'${x}'`).join(',')} -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowHandle -ne 0 -and (${focus ? '$true' : '$false'} -or [SwWin.W]::IsIconic($_.MainWindowHandle)) })\n`
|
|
1470
|
+
+ "$p=@($w | Where-Object { $t -and $_.MainWindowTitle.StartsWith($t) })[0]\nif (-not $p -and $w.Count -eq 1) { $p=$w[0] }\n"
|
|
1471
|
+
+ "if ($p) { $h=$p.MainWindowHandle; if ([SwWin.W]::IsIconic($h)) { [void][SwWin.W]::ShowWindowAsync($h,9); Start-Sleep -Milliseconds 250 }; "
|
|
1472
|
+
+ (focus ? "[void](New-Object -ComObject WScript.Shell).AppActivate($p.Id); [void][SwWin.W]::SetForegroundWindow($h); " : '')
|
|
1473
|
+
+ "'RESTORED' } else { 'NONE' }");
|
|
1459
1474
|
return /RESTORED/.test(out);
|
|
1460
1475
|
} catch { return false; }
|
|
1461
1476
|
}
|
|
1477
|
+
// 0.8.9 (owner's live test 29 Nov): a screenshot of a page in a minimised browser window waited until it timed out.
|
|
1478
|
+
// Before a screenshot Shellway brings the tab forward and, on Windows, restores the window and brings it to the front.
|
|
1479
|
+
// Clicks, typing and keys still work in a minimised window (tested), so for them only the tab is brought forward inside
|
|
1480
|
+
// its window (full = false): Shellway does not pull the browser in front of what the user is doing.
|
|
1481
|
+
async function ensureVisible(s, t, full = true) {
|
|
1482
|
+
const vis = async () => { try { return await evalJs(s, 'document.visibilityState', 5000); } catch { return 'unknown'; } };
|
|
1483
|
+
if ((await vis()) !== 'hidden') return 'visible';
|
|
1484
|
+
try { await cdpJson(`/json/activate/${t.id}`); } catch {}
|
|
1485
|
+
try { await s.send('Page.bringToFront', {}, 5000); } catch {}
|
|
1486
|
+
await sleep(300);
|
|
1487
|
+
if ((await vis()) !== 'hidden' || !full) return 'shown';
|
|
1488
|
+
if (await restoreBrowserWindow(t.title, true)) { await sleep(700); if ((await vis()) !== 'hidden') return 'restored'; }
|
|
1489
|
+
return 'hidden';
|
|
1490
|
+
}
|
|
1491
|
+
const visNote = (v) => (v === 'hidden' ? '\nNote: the browser window stayed hidden (minimised or covered by another window), so the page may not react. Bring it to the front with focus_window and try again.'
|
|
1492
|
+
: v === 'restored' ? '\n(The browser window was minimised or covered; Shellway brought it to the front first.)' : '');
|
|
1462
1493
|
// 0.8.8 (Ruslan 28 Nov): tabs keep their own id (stable while the browser runs, unlike the list number) and an optional
|
|
1463
1494
|
// label the AI gives them (e.g. the name of its run), so a long run can find and keep using its own tabs.
|
|
1464
1495
|
const tabLabels = new Map();
|
|
@@ -1484,7 +1515,8 @@ const browserHandlers = {
|
|
|
1484
1515
|
async browser_click({ ref, text: t, double, x, y }) {
|
|
1485
1516
|
// 0.7.4: click a point of the latest browser_screenshot (for things that have no number, e.g. inside a canvas).
|
|
1486
1517
|
if ((ref === undefined || ref === null || ref === '') && !t && Number.isFinite(Number(x)) && Number.isFinite(Number(y)) && x !== null && y !== null) {
|
|
1487
|
-
return withPage(async (s) => {
|
|
1518
|
+
return withPage(async (s, tab) => {
|
|
1519
|
+
const shown = await ensureVisible(s, tab, false);
|
|
1488
1520
|
const vp = await evalJs(s, '({ sy: scrollY, sx: scrollX, h: innerHeight, w: innerWidth })');
|
|
1489
1521
|
const px = Number(x) / lastShot.scale, pyPage = Number(y) / lastShot.scale + lastShot.top;
|
|
1490
1522
|
let vy = pyPage - vp.sy;
|
|
@@ -1493,17 +1525,18 @@ const browserHandlers = {
|
|
|
1493
1525
|
const before = await proofBefore(s, null);
|
|
1494
1526
|
await mouseClick(s, px, vy, double ? 2 : 1);
|
|
1495
1527
|
const page = await afterAction(s);
|
|
1496
|
-
return `Clicked at x=${x}, y=${y} of the screenshot.\n${page}\n${proofText(before, await pageState(s, null), 'click')}`;
|
|
1528
|
+
return `Clicked at x=${x}, y=${y} of the screenshot.\n${page}\n${proofText(before, await pageState(s, null), 'click')}${visNote(shown)}`;
|
|
1497
1529
|
});
|
|
1498
1530
|
}
|
|
1499
1531
|
if ((ref === undefined || ref === null || ref === '') && !t) throw new Error('ref (the [number] from browser_read), text, or x and y (pixels in the latest browser_screenshot) is required');
|
|
1500
|
-
return withPage(async (s) => {
|
|
1532
|
+
return withPage(async (s, tab) => {
|
|
1533
|
+
const shown = await ensureVisible(s, tab, false);
|
|
1501
1534
|
const p = await refPoint(s, ref, t);
|
|
1502
1535
|
const n = ref !== undefined && ref !== null && ref !== '' ? Number(ref) | 0 : null;
|
|
1503
1536
|
const before = await proofBefore(s, n);
|
|
1504
1537
|
await mouseClick(s, p.x, p.y, double ? 2 : 1);
|
|
1505
1538
|
const page = await afterAction(s);
|
|
1506
|
-
return `Clicked ${n !== null ? `[${ref}]` : `"${t}"`}.\n${page}\n${proofText(before, await pageState(s, n), 'click')}`;
|
|
1539
|
+
return `Clicked ${n !== null ? `[${ref}]` : `"${t}"`}.\n${page}\n${proofText(before, await pageState(s, n), 'click')}${visNote(shown)}`;
|
|
1507
1540
|
});
|
|
1508
1541
|
},
|
|
1509
1542
|
async browser_type({ ref, text: t, clear, submit }) {
|
|
@@ -1511,7 +1544,8 @@ const browserHandlers = {
|
|
|
1511
1544
|
if (t.length > 10_000) throw new Error('text is too long (max 10,000 characters)');
|
|
1512
1545
|
if (ref === undefined || ref === null || ref === '') throw new Error('ref is required: the [number] of the field from browser_read');
|
|
1513
1546
|
const n = Number(ref) | 0;
|
|
1514
|
-
return withPage(async (s) => {
|
|
1547
|
+
return withPage(async (s, tab) => {
|
|
1548
|
+
const shown = await ensureVisible(s, tab, false);
|
|
1515
1549
|
const p = await refPoint(s, ref);
|
|
1516
1550
|
// 0.7.4: a drop-down list (<select>): choose the option whose text or value matches.
|
|
1517
1551
|
if (p.tag === 'select') {
|
|
@@ -1556,17 +1590,17 @@ const browserHandlers = {
|
|
|
1556
1590
|
}
|
|
1557
1591
|
}
|
|
1558
1592
|
}
|
|
1559
|
-
if (submit) { await pressKeys(s, 'enter'); const page = await afterAction(s); return `Typed ${t.length} characters into [${ref}] and pressed Enter.\n${page}\n${proofText(before, await pageState(s, n), 'typing and Enter')}`; }
|
|
1593
|
+
if (submit) { await pressKeys(s, 'enter'); const page = await afterAction(s); return `Typed ${t.length} characters into [${ref}] and pressed Enter.\n${page}\n${proofText(before, await pageState(s, n), 'typing and Enter')}${visNote(shown)}`; }
|
|
1560
1594
|
const after = await pageState(s, n);
|
|
1561
1595
|
// 0.8.8: say plainly whether the field now holds the text (for a password field only its length is compared)
|
|
1562
1596
|
const holds = after && after.item ? (after.item.value_tail !== null ? (t ? after.item.value_tail.endsWith(t.slice(-Math.min(t.length, 40))) : after.item.value_len === 0) : after.item.value_len !== null ? after.item.value_len >= t.length : null) : null;
|
|
1563
1597
|
const fieldLine = holds === true ? `Check: the field now holds the typed text (${after.item.value_len} characters).` : holds === false ? `Check: the field does NOT hold the typed text (it has ${after.item.value_len} characters). The page may have rejected or changed it: check with browser_read.` : proofText(before, after, 'typing');
|
|
1564
|
-
return `Typed ${t.length} characters into [${ref}]${p.type === 'password' ? ' (password field)' : ''}.\n${fieldLine}`;
|
|
1598
|
+
return `Typed ${t.length} characters into [${ref}]${p.type === 'password' ? ' (password field)' : ''}.\n${fieldLine}${visNote(shown)}`;
|
|
1565
1599
|
});
|
|
1566
1600
|
},
|
|
1567
1601
|
async browser_press({ keys }) {
|
|
1568
1602
|
cdpKeyEvents(keys); // validate first
|
|
1569
|
-
return withPage(async (s) => { const before = await proofBefore(s, null); await pressKeys(s, keys); const page = await afterAction(s, 500); return `Pressed ${keys}.\n${page}\n${proofText(before, await pageState(s, null), 'key press')}`; });
|
|
1603
|
+
return withPage(async (s, tab) => { const shown = await ensureVisible(s, tab, false); const before = await proofBefore(s, null); await pressKeys(s, keys); const page = await afterAction(s, 500); return `Pressed ${keys}.\n${page}\n${proofText(before, await pageState(s, null), 'key press')}${visNote(shown)}`; });
|
|
1570
1604
|
},
|
|
1571
1605
|
async browser_scroll({ direction, pages: n }) {
|
|
1572
1606
|
const k = Math.min(Math.max(Number(n) || 1, 1), 20) * (direction === 'up' ? -1 : 1);
|
|
@@ -1579,6 +1613,7 @@ const browserHandlers = {
|
|
|
1579
1613
|
async browser_screenshot({ full_page }) {
|
|
1580
1614
|
return withPage(async (s, t) => {
|
|
1581
1615
|
const VP = '({ w: innerWidth, h: innerHeight, H: document.documentElement.scrollHeight })';
|
|
1616
|
+
if ((await ensureVisible(s, t)) === 'hidden') throw new Error(MINIMISED);
|
|
1582
1617
|
let vp = await evalJs(s, VP);
|
|
1583
1618
|
if (!(vp.w > 0 && vp.h > 0)) {
|
|
1584
1619
|
// 0.8.8 (Ruslan 28 Nov): a minimised or hidden window has no size: bring the tab forward and restore the window once
|
|
@@ -1596,7 +1631,7 @@ const browserHandlers = {
|
|
|
1596
1631
|
// Keep the image small enough for the AI apps (about 500 KB at most): lower quality first, then size.
|
|
1597
1632
|
let sc = scale;
|
|
1598
1633
|
let r;
|
|
1599
|
-
try { r = await shot(sc, full_page ? 45 : 60); } catch (e) { if (/0 width|0 height/i.test(String(e && e.message))) throw new Error(MINIMISED); throw e; }
|
|
1634
|
+
try { r = await shot(sc, full_page ? 45 : 60); } catch (e) { if (/0 width|0 height|did not answer|timed? ?out/i.test(String(e && e.message))) throw new Error(MINIMISED); throw e; }
|
|
1600
1635
|
for (let k = 0.7; r.data.length > 700_000 && k > 0.2; k -= 0.2) { sc = scale * k; r = await shot(sc, 40); }
|
|
1601
1636
|
lastShot = { scale: sc, top: y }; // for browser_click with x, y
|
|
1602
1637
|
const info = await evalJs(s, '({ title: document.title, url: location.href })');
|