@howaboua/pi-browser 0.0.4 → 0.0.5
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/CHANGELOG.md +7 -0
- package/dist/remote-worker.js +62 -54
- package/dist/src/browser/bounds.js +7 -1
- package/dist/src/browser/help.js +4 -4
- package/dist/src/browser-parameters.js +5 -8
- package/dist/src/cdp/actions/element.js +4 -0
- package/dist/src/cdp/snapshot-contract.d.ts +2 -0
- package/dist/src/cdp/snapshot.js +5 -0
- package/package.json +5 -5
- package/src/browser/bounds.ts +7 -1
- package/src/browser/help.ts +5 -4
- package/src/browser-parameters.ts +130 -129
- package/src/cdp/actions/element.ts +4 -0
- package/src/cdp/snapshot-contract.ts +2 -0
- package/src/cdp/snapshot.ts +6 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @howaboua/pi-browser
|
|
2
2
|
|
|
3
|
+
## 0.0.5
|
|
4
|
+
|
|
5
|
+
- Browser snapshots now include link destinations.
|
|
6
|
+
|
|
7
|
+
- Browser help and schemas now describe single-action requests, shared batch fields and JSON results with less repetition.
|
|
8
|
+
- Click errors now identify zero-sized browser viewports.
|
|
9
|
+
|
|
3
10
|
## 0.0.4
|
|
4
11
|
|
|
5
12
|
- Added session-owned background browser work and direct form controls.
|
package/dist/remote-worker.js
CHANGED
|
@@ -311,18 +311,18 @@ function parseActionRequest(value) {
|
|
|
311
311
|
};
|
|
312
312
|
}
|
|
313
313
|
if (action === "open") {
|
|
314
|
-
const
|
|
314
|
+
const refId = optionalString(value["ref_id"], "ref_id");
|
|
315
315
|
const url = optionalString(value["url"], "url");
|
|
316
|
-
if (Boolean(
|
|
316
|
+
if (Boolean(refId) === Boolean(url)) {
|
|
317
317
|
throw new Error("open requires exactly one of ref_id or url");
|
|
318
318
|
}
|
|
319
319
|
if (url)
|
|
320
320
|
return { action, url };
|
|
321
|
-
if (!
|
|
321
|
+
if (!refId)
|
|
322
322
|
throw new Error("open requires ref_id or url");
|
|
323
323
|
return {
|
|
324
324
|
action,
|
|
325
|
-
ref_id:
|
|
325
|
+
ref_id: refId,
|
|
326
326
|
lineno: line(value["lineno"]),
|
|
327
327
|
response_length: responseLength(value["response_length"])
|
|
328
328
|
};
|
|
@@ -347,8 +347,8 @@ function parseActionRequest(value) {
|
|
|
347
347
|
return { action, handle: resultHandle(value["handle"]) };
|
|
348
348
|
}
|
|
349
349
|
if (action === "stop") {
|
|
350
|
-
const
|
|
351
|
-
return { action, ...
|
|
350
|
+
const refId = optionalString(value["ref_id"], "ref_id");
|
|
351
|
+
return { action, ...refId ? { ref_id: refId } : {} };
|
|
352
352
|
}
|
|
353
353
|
const refId = requiredRef(value["ref_id"], action);
|
|
354
354
|
if (action === "network" || action === "show" || action === "close")
|
|
@@ -442,7 +442,7 @@ function parseActionRequest(value) {
|
|
|
442
442
|
}
|
|
443
443
|
if (action === "wait") {
|
|
444
444
|
const conditions = ["selector", "text", "url_includes"];
|
|
445
|
-
const provided = conditions.filter((
|
|
445
|
+
const provided = conditions.filter((field) => value[field] !== undefined);
|
|
446
446
|
const field = provided[0];
|
|
447
447
|
if (provided.length !== 1 || !field) {
|
|
448
448
|
throw new Error("wait requires exactly one of selector, text, or url_includes");
|
|
@@ -1214,7 +1214,7 @@ class BrowserTabs {
|
|
|
1214
1214
|
}
|
|
1215
1215
|
async resolve(refId, signal) {
|
|
1216
1216
|
const pages = await this.list(signal);
|
|
1217
|
-
const ids = pages.map((
|
|
1217
|
+
const ids = pages.map((page) => page.targetId);
|
|
1218
1218
|
const targetId = resolvePrefix(refId, ids, "target", "Run tabs.");
|
|
1219
1219
|
const page = pages.find((candidate) => candidate.targetId === targetId);
|
|
1220
1220
|
if (!page)
|
|
@@ -1488,8 +1488,8 @@ class BrowserCdpSession {
|
|
|
1488
1488
|
}
|
|
1489
1489
|
async stop(refId) {
|
|
1490
1490
|
if (!refId) {
|
|
1491
|
-
for (const
|
|
1492
|
-
this.stopPendingTab(
|
|
1491
|
+
for (const targetId of this.tabPromises.keys()) {
|
|
1492
|
+
this.stopPendingTab(targetId, "All tab bridges were stopped");
|
|
1493
1493
|
}
|
|
1494
1494
|
for (const bridge of this.tabs.values())
|
|
1495
1495
|
bridge.close();
|
|
@@ -1659,17 +1659,17 @@ async function pruneArtifacts(now = Date.now()) {
|
|
|
1659
1659
|
}
|
|
1660
1660
|
return removed;
|
|
1661
1661
|
}
|
|
1662
|
-
function chunkForJson(value,
|
|
1662
|
+
function chunkForJson(value, offset = 0, budget = TEXT_BUDGET_BYTES) {
|
|
1663
1663
|
let bytes = 2;
|
|
1664
|
-
let end =
|
|
1665
|
-
for (const character of value.slice(
|
|
1664
|
+
let end = offset;
|
|
1665
|
+
for (const character of value.slice(offset)) {
|
|
1666
1666
|
const next = Buffer.byteLength(JSON.stringify(character)) - 2;
|
|
1667
1667
|
if (bytes + next > budget)
|
|
1668
1668
|
break;
|
|
1669
1669
|
bytes += next;
|
|
1670
1670
|
end += character.length;
|
|
1671
1671
|
}
|
|
1672
|
-
return { text: value.slice(
|
|
1672
|
+
return { text: value.slice(offset, end), end };
|
|
1673
1673
|
}
|
|
1674
1674
|
async function cacheResult(value) {
|
|
1675
1675
|
const handle = crypto.randomUUID();
|
|
@@ -1729,19 +1729,19 @@ async function discardCachedResult(handle) {
|
|
|
1729
1729
|
function browserHelp(hosts = []) {
|
|
1730
1730
|
const routed = hosts.length > 0;
|
|
1731
1731
|
return {
|
|
1732
|
-
input: "Code
|
|
1732
|
+
input: "Request={action,...fields}; Code/Notebook: JSON.stringify(request), JSON.parse(result)",
|
|
1733
1733
|
...routed ? {
|
|
1734
1734
|
host: `${hosts.join("|")} optional; keep a user-named host on every call and its refs/handles`
|
|
1735
1735
|
} : {},
|
|
1736
1736
|
refs: "tabs -> open; keep ref_id/id/cursors with their result",
|
|
1737
1737
|
safety: "Ask before unfamiliar low-trust navigation or consequential action unless authorized; never close shared browser",
|
|
1738
|
-
batch: "
|
|
1738
|
+
batch: "Code/Notebook: {action_name:[{...fields}],...}; nonempty arrays; items omit action; host/response_length at request root; independent only",
|
|
1739
1739
|
actions: {
|
|
1740
1740
|
tabs: "query? offset? owned_only? -> ref_id title url owned",
|
|
1741
|
-
open: "ref_id lineno?
|
|
1741
|
+
open: "ref_id lineno? | url; new tabs open in background",
|
|
1742
1742
|
show: "ref_id; bring tab to foreground",
|
|
1743
1743
|
close: "ref_id",
|
|
1744
|
-
find: "ref_id pattern lineno?
|
|
1744
|
+
find: "ref_id pattern lineno?",
|
|
1745
1745
|
click: "ref_id id|selector|x+y",
|
|
1746
1746
|
type: "ref_id text id?; id focuses",
|
|
1747
1747
|
fill: "ref_id id|selector value; replace/clear text, select option value/label, boolean checks/unchecks",
|
|
@@ -1829,6 +1829,10 @@ async function backendCenter(cdp, sessionId, backendNodeId, scroll = true, signa
|
|
|
1829
1829
|
const response = await withBackendObject(cdp, sessionId, backendNodeId, (objectId) => cdp.send("Runtime.callFunctionOn", {
|
|
1830
1830
|
objectId,
|
|
1831
1831
|
functionDeclaration: `function() {
|
|
1832
|
+
const view = this.ownerDocument.defaultView;
|
|
1833
|
+
if (view && (view.innerWidth <= 0 || view.innerHeight <= 0)) {
|
|
1834
|
+
return { ok: false, error: 'Browser viewport is ' + view.innerWidth + 'x' + view.innerHeight + '; restore or resize its window, then retry' };
|
|
1835
|
+
}
|
|
1832
1836
|
const rect = this.getBoundingClientRect();
|
|
1833
1837
|
const style = getComputedStyle(this);
|
|
1834
1838
|
const disabled = this.matches(':disabled') ||
|
|
@@ -2020,10 +2024,10 @@ function prepareFill(value) {
|
|
|
2020
2024
|
}
|
|
2021
2025
|
const indeterminate = this.type === "checkbox" && this.indeterminate;
|
|
2022
2026
|
if (indeterminate && this.checked === value) {
|
|
2023
|
-
const
|
|
2024
|
-
if (!
|
|
2027
|
+
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "checked")?.set;
|
|
2028
|
+
if (!setter)
|
|
2025
2029
|
throw new Error("Checkbox has no native checked setter");
|
|
2026
|
-
|
|
2030
|
+
setter.call(this, !value);
|
|
2027
2031
|
}
|
|
2028
2032
|
return {
|
|
2029
2033
|
kind: "checked",
|
|
@@ -2057,8 +2061,8 @@ function prepareFill(value) {
|
|
|
2057
2061
|
if (this.multiple)
|
|
2058
2062
|
throw new Error("fill requires a single-select control");
|
|
2059
2063
|
const options = Array.from(this.options);
|
|
2060
|
-
const byValue = options.filter((
|
|
2061
|
-
const matches = byValue.length ? byValue : options.filter((
|
|
2064
|
+
const byValue = options.filter((option) => option.value === value);
|
|
2065
|
+
const matches = byValue.length ? byValue : options.filter((option) => option.label.trim() === value);
|
|
2062
2066
|
const option = matches[0];
|
|
2063
2067
|
if (matches.length !== 1 || !option)
|
|
2064
2068
|
throw new Error("Option value/label must match exactly one option");
|
|
@@ -2550,7 +2554,7 @@ function isNavigationContextError(error) {
|
|
|
2550
2554
|
}
|
|
2551
2555
|
async function waitForCondition(cdp, sessionId, condition, timeoutMs, signal) {
|
|
2552
2556
|
const details = conditionDetails(condition);
|
|
2553
|
-
const
|
|
2557
|
+
const timeoutError = new Error(`Timed out after ${timeoutMs}ms waiting for ${details.label}`);
|
|
2554
2558
|
const deadline = Date.now() + timeoutMs;
|
|
2555
2559
|
const controller = new AbortController;
|
|
2556
2560
|
let timedOut = false;
|
|
@@ -2564,7 +2568,7 @@ async function waitForCondition(cdp, sessionId, condition, timeoutMs, signal) {
|
|
|
2564
2568
|
}
|
|
2565
2569
|
const timer = setTimeout(() => {
|
|
2566
2570
|
timedOut = true;
|
|
2567
|
-
controller.abort(
|
|
2571
|
+
controller.abort(timeoutError);
|
|
2568
2572
|
}, timeoutMs);
|
|
2569
2573
|
try {
|
|
2570
2574
|
while (Date.now() < deadline) {
|
|
@@ -2586,10 +2590,10 @@ async function waitForCondition(cdp, sessionId, condition, timeoutMs, signal) {
|
|
|
2586
2590
|
break;
|
|
2587
2591
|
await sleep(Math.min(POLL_INTERVAL_MS, remaining), controller.signal);
|
|
2588
2592
|
}
|
|
2589
|
-
throw
|
|
2593
|
+
throw timeoutError;
|
|
2590
2594
|
} catch (error) {
|
|
2591
2595
|
if (timedOut)
|
|
2592
|
-
throw
|
|
2596
|
+
throw timeoutError;
|
|
2593
2597
|
throw error;
|
|
2594
2598
|
} finally {
|
|
2595
2599
|
clearTimeout(timer);
|
|
@@ -2716,7 +2720,7 @@ function interactiveText(role, name, value, state) {
|
|
|
2716
2720
|
}
|
|
2717
2721
|
async function snapshotData(cdp, sessionId, elementRefs, options = {}) {
|
|
2718
2722
|
const lineno = positiveInteger(options.lineno ?? 1, "line cursor");
|
|
2719
|
-
const
|
|
2723
|
+
const responseLength = snapshotResponseLength(options.responseLength);
|
|
2720
2724
|
const response = asRecord(await cdp.send("Accessibility.getFullAXTree", {}, sessionId, options.signal), "Accessibility response");
|
|
2721
2725
|
if (!Array.isArray(response["nodes"])) {
|
|
2722
2726
|
throw new Error("Accessibility response has no nodes");
|
|
@@ -2780,10 +2784,12 @@ async function snapshotData(cdp, sessionId, elementRefs, options = {}) {
|
|
|
2780
2784
|
addLine(text, undefined, "interactive");
|
|
2781
2785
|
renderedName = name;
|
|
2782
2786
|
} else {
|
|
2787
|
+
const href = role === "link" ? node.properties?.find((property) => property.name === "url")?.value?.value : undefined;
|
|
2783
2788
|
const element = {
|
|
2784
2789
|
id: nextElementId(),
|
|
2785
2790
|
role,
|
|
2786
2791
|
...name ? { name } : {},
|
|
2792
|
+
...typeof href === "string" && href ? { href } : {},
|
|
2787
2793
|
...value === "" || value == null ? {} : { value },
|
|
2788
2794
|
...state
|
|
2789
2795
|
};
|
|
@@ -2812,10 +2818,10 @@ async function snapshotData(cdp, sessionId, elementRefs, options = {}) {
|
|
|
2812
2818
|
visit(node);
|
|
2813
2819
|
const metadata = asRecord(await evaluate(cdp, sessionId, "({title: document.title, url: location.href})", options.signal), "page metadata");
|
|
2814
2820
|
const pattern = options.pattern?.toLowerCase();
|
|
2815
|
-
const matching = pattern ? lines.filter((
|
|
2821
|
+
const matching = pattern ? lines.filter((line) => line.text.toLowerCase().includes(pattern)) : lines;
|
|
2816
2822
|
const start = lineno - 1;
|
|
2817
|
-
const content = matching.slice(start, start + SNAPSHOT_LIMITS[
|
|
2818
|
-
const visibleIds = new Set(content.flatMap((
|
|
2823
|
+
const content = matching.slice(start, start + SNAPSHOT_LIMITS[responseLength]).map(({ kind: _kind, ...line }) => line);
|
|
2824
|
+
const visibleIds = new Set(content.flatMap((line) => line.element_id === undefined ? [] : [line.element_id]));
|
|
2819
2825
|
const hasMore = start + content.length < matching.length;
|
|
2820
2826
|
return {
|
|
2821
2827
|
...options.refId ? { ref_id: options.refId } : {},
|
|
@@ -2847,6 +2853,7 @@ function truncateUtf8(value, maxBytes) {
|
|
|
2847
2853
|
return new StringDecoder("utf8").write(bytes.subarray(0, maxBytes));
|
|
2848
2854
|
}
|
|
2849
2855
|
function boundedElement(element) {
|
|
2856
|
+
const { href, ...rest } = element;
|
|
2850
2857
|
let value = element.value;
|
|
2851
2858
|
if (typeof value === "string") {
|
|
2852
2859
|
value = truncateUtf8(value, SNAPSHOT_ELEMENT_VALUE_BYTES);
|
|
@@ -2857,13 +2864,14 @@ function boundedElement(element) {
|
|
|
2857
2864
|
}
|
|
2858
2865
|
}
|
|
2859
2866
|
return {
|
|
2860
|
-
...
|
|
2867
|
+
...rest,
|
|
2868
|
+
...href === undefined ? {} : Buffer.byteLength(href) > SNAPSHOT_URL_BYTES ? { href_omitted: true } : { href },
|
|
2861
2869
|
role: truncateUtf8(element.role, 128),
|
|
2862
2870
|
...element.name === undefined ? {} : { name: truncateUtf8(element.name, SNAPSHOT_ELEMENT_NAME_BYTES) },
|
|
2863
2871
|
...value === undefined ? {} : { value }
|
|
2864
2872
|
};
|
|
2865
2873
|
}
|
|
2866
|
-
function boundTabs(pages, query,
|
|
2874
|
+
function boundTabs(pages, query, offset, ownedOnly = false) {
|
|
2867
2875
|
const prefixLength = getDisplayPrefixLength(pages.map((page) => page.targetId));
|
|
2868
2876
|
let filtered = ownedOnly ? pages.filter((page) => page.owned === true) : pages;
|
|
2869
2877
|
if (query) {
|
|
@@ -2871,11 +2879,11 @@ function boundTabs(pages, query, offset2, ownedOnly = false) {
|
|
|
2871
2879
|
filtered = filtered.filter((page) => `${page.title}
|
|
2872
2880
|
${page.url}`.toLowerCase().includes(normalized));
|
|
2873
2881
|
}
|
|
2874
|
-
if (
|
|
2875
|
-
throw new Error(`offset ${
|
|
2882
|
+
if (offset > filtered.length) {
|
|
2883
|
+
throw new Error(`offset ${offset} exceeds tab count ${filtered.length}`);
|
|
2876
2884
|
}
|
|
2877
2885
|
const tabs = [];
|
|
2878
|
-
for (const page of filtered.slice(
|
|
2886
|
+
for (const page of filtered.slice(offset)) {
|
|
2879
2887
|
const compact = {
|
|
2880
2888
|
ref_id: page.targetId.slice(0, prefixLength),
|
|
2881
2889
|
title: page.title.slice(0, 500),
|
|
@@ -2887,14 +2895,14 @@ ${page.url}`.toLowerCase().includes(normalized));
|
|
|
2887
2895
|
}
|
|
2888
2896
|
tabs.push(compact);
|
|
2889
2897
|
}
|
|
2890
|
-
const remaining = filtered.length -
|
|
2898
|
+
const remaining = filtered.length - offset - tabs.length;
|
|
2891
2899
|
return {
|
|
2892
|
-
offset
|
|
2900
|
+
offset,
|
|
2893
2901
|
tabs,
|
|
2894
2902
|
...remaining > 0 ? {
|
|
2895
2903
|
truncated: true,
|
|
2896
2904
|
omitted_tabs: remaining,
|
|
2897
|
-
next_offset:
|
|
2905
|
+
next_offset: offset + tabs.length
|
|
2898
2906
|
} : {}
|
|
2899
2907
|
};
|
|
2900
2908
|
}
|
|
@@ -2910,11 +2918,11 @@ function boundSnapshot(page) {
|
|
|
2910
2918
|
const content = [];
|
|
2911
2919
|
const includedElements = [];
|
|
2912
2920
|
const includedIds = new Set;
|
|
2913
|
-
for (const
|
|
2914
|
-
const nextContent = [...content,
|
|
2921
|
+
for (const line of page.content) {
|
|
2922
|
+
const nextContent = [...content, line];
|
|
2915
2923
|
const nextElements = [...includedElements];
|
|
2916
|
-
if (
|
|
2917
|
-
const element = elements.get(
|
|
2924
|
+
if (line.element_id !== undefined && !includedIds.has(line.element_id) && elements.has(line.element_id)) {
|
|
2925
|
+
const element = elements.get(line.element_id);
|
|
2918
2926
|
if (element)
|
|
2919
2927
|
nextElements.push(element);
|
|
2920
2928
|
}
|
|
@@ -2925,10 +2933,10 @@ function boundSnapshot(page) {
|
|
|
2925
2933
|
}) > OUTPUT_BUDGET_BYTES) {
|
|
2926
2934
|
break;
|
|
2927
2935
|
}
|
|
2928
|
-
content.push(
|
|
2929
|
-
if (
|
|
2930
|
-
includedIds.add(
|
|
2931
|
-
const element = elements.get(
|
|
2936
|
+
content.push(line);
|
|
2937
|
+
if (line.element_id !== undefined && !includedIds.has(line.element_id) && elements.has(line.element_id)) {
|
|
2938
|
+
includedIds.add(line.element_id);
|
|
2939
|
+
const element = elements.get(line.element_id);
|
|
2932
2940
|
if (element)
|
|
2933
2941
|
includedElements.push(element);
|
|
2934
2942
|
}
|
|
@@ -3193,13 +3201,13 @@ class BrowserOperationExecutor {
|
|
|
3193
3201
|
async screenshot(operation, signal) {
|
|
3194
3202
|
const file = await screenshotPath(operation);
|
|
3195
3203
|
return this.cdp.withTab(operation.ref_id, signal, async (tab) => {
|
|
3196
|
-
const
|
|
3204
|
+
const capture = operation.id !== undefined ? await captureRef(tab.cdp, tab.sessionId, tab.elementRefs, operation.id, file, signal) : operation.selector ? await captureSelector(tab.cdp, tab.sessionId, operation.selector, file, signal) : await captureViewport(tab.cdp, tab.sessionId, file, signal);
|
|
3197
3205
|
return {
|
|
3198
3206
|
ref_id: tab.refId,
|
|
3199
3207
|
...operation.id === undefined ? {} : { id: operation.id },
|
|
3200
3208
|
...operation.selector ? { selector: operation.selector } : {},
|
|
3201
|
-
file:
|
|
3202
|
-
dpr:
|
|
3209
|
+
file: capture.file,
|
|
3210
|
+
dpr: capture.dpr,
|
|
3203
3211
|
coordinates: "CSS pixels; screenshot pixels / DPR"
|
|
3204
3212
|
};
|
|
3205
3213
|
});
|
|
@@ -3499,8 +3507,8 @@ class BrowserRuntime {
|
|
|
3499
3507
|
if (first) {
|
|
3500
3508
|
options.onOperation?.(first, 0, request.operations.length);
|
|
3501
3509
|
}
|
|
3502
|
-
const
|
|
3503
|
-
return { host: request.host, ...
|
|
3510
|
+
const result = await executeRemoteBrowser(route, request.operations, ownerId, options.signal);
|
|
3511
|
+
return { host: request.host, ...result };
|
|
3504
3512
|
}
|
|
3505
3513
|
}
|
|
3506
3514
|
const result = await this.executeLocal(request.operations, ownerId, options);
|
|
@@ -3756,11 +3764,11 @@ async function requestOnce(input, workerId, signal) {
|
|
|
3756
3764
|
signal?.addEventListener("abort", abort, { once: true });
|
|
3757
3765
|
socket.once("connect", () => socket.write(`${input}
|
|
3758
3766
|
`));
|
|
3759
|
-
readLine(socket).then((
|
|
3767
|
+
readLine(socket).then((line) => {
|
|
3760
3768
|
signal?.removeEventListener("abort", abort);
|
|
3761
3769
|
socket.destroy();
|
|
3762
3770
|
try {
|
|
3763
|
-
const response = JSON.parse(
|
|
3771
|
+
const response = JSON.parse(line);
|
|
3764
3772
|
if (!isRecordValue(response) || typeof response["ok"] !== "boolean") {
|
|
3765
3773
|
throw new Error("Browser worker returned an invalid response");
|
|
3766
3774
|
}
|
|
@@ -16,6 +16,7 @@ function truncateUtf8(value, maxBytes) {
|
|
|
16
16
|
return new StringDecoder("utf8").write(bytes.subarray(0, maxBytes));
|
|
17
17
|
}
|
|
18
18
|
function boundedElement(element) {
|
|
19
|
+
const { href, ...rest } = element;
|
|
19
20
|
let value = element.value;
|
|
20
21
|
if (typeof value === "string") {
|
|
21
22
|
value = truncateUtf8(value, SNAPSHOT_ELEMENT_VALUE_BYTES);
|
|
@@ -28,7 +29,12 @@ function boundedElement(element) {
|
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
return {
|
|
31
|
-
...
|
|
32
|
+
...rest,
|
|
33
|
+
...(href === undefined
|
|
34
|
+
? {}
|
|
35
|
+
: Buffer.byteLength(href) > SNAPSHOT_URL_BYTES
|
|
36
|
+
? { href_omitted: true }
|
|
37
|
+
: { href }),
|
|
32
38
|
role: truncateUtf8(element.role, 128),
|
|
33
39
|
...(element.name === undefined
|
|
34
40
|
? {}
|
package/dist/src/browser/help.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export function browserHelp(hosts = []) {
|
|
2
2
|
const routed = hosts.length > 0;
|
|
3
3
|
return {
|
|
4
|
-
input: "Code
|
|
4
|
+
input: "Request={action,...fields}; Code/Notebook: JSON.stringify(request), JSON.parse(result)",
|
|
5
5
|
...(routed
|
|
6
6
|
? {
|
|
7
7
|
host: `${hosts.join("|")} optional; keep a user-named host on every call and its refs/handles`,
|
|
@@ -9,13 +9,13 @@ export function browserHelp(hosts = []) {
|
|
|
9
9
|
: {}),
|
|
10
10
|
refs: "tabs -> open; keep ref_id/id/cursors with their result",
|
|
11
11
|
safety: "Ask before unfamiliar low-trust navigation or consequential action unless authorized; never close shared browser",
|
|
12
|
-
batch: "
|
|
12
|
+
batch: "Code/Notebook: {action_name:[{...fields}],...}; nonempty arrays; items omit action; host/response_length at request root; independent only",
|
|
13
13
|
actions: {
|
|
14
14
|
tabs: "query? offset? owned_only? -> ref_id title url owned",
|
|
15
|
-
open: "ref_id lineno?
|
|
15
|
+
open: "ref_id lineno? | url; new tabs open in background",
|
|
16
16
|
show: "ref_id; bring tab to foreground",
|
|
17
17
|
close: "ref_id",
|
|
18
|
-
find: "ref_id pattern lineno?
|
|
18
|
+
find: "ref_id pattern lineno?",
|
|
19
19
|
click: "ref_id id|selector|x+y",
|
|
20
20
|
type: "ref_id text id?; id focuses",
|
|
21
21
|
fill: "ref_id id|selector value; replace/clear text, select option value/label, boolean checks/unchecks",
|
|
@@ -3,26 +3,21 @@ import { Type } from "typebox";
|
|
|
3
3
|
export function browserParameters(hosts) {
|
|
4
4
|
const route = hosts.length > 0
|
|
5
5
|
? {
|
|
6
|
-
host: Type.Optional(StringEnum(hosts
|
|
6
|
+
host: Type.Optional(StringEnum(hosts)),
|
|
7
7
|
}
|
|
8
8
|
: {};
|
|
9
9
|
const request = (properties) => Type.Object({ ...route, ...properties }, { additionalProperties: false });
|
|
10
10
|
const refId = Type.String({
|
|
11
11
|
minLength: 1,
|
|
12
|
-
description: "Tab ref_id returned by tabs",
|
|
13
12
|
});
|
|
14
13
|
const elementId = Type.Integer({
|
|
15
14
|
minimum: 1,
|
|
16
|
-
description: "Element id returned by open or find",
|
|
17
15
|
});
|
|
18
16
|
const responseLength = StringEnum(["short", "medium", "long"]);
|
|
19
17
|
const handle = Type.String({
|
|
20
18
|
minLength: 1,
|
|
21
|
-
description: "Continuation handle returned by browser",
|
|
22
|
-
});
|
|
23
|
-
const fillValue = Type.Union([Type.String(), Type.Boolean()], {
|
|
24
|
-
description: "Replacement text/option; boolean for checkbox/radio",
|
|
25
19
|
});
|
|
20
|
+
const fillValue = Type.Union([Type.String(), Type.Boolean()]);
|
|
26
21
|
const timeout = Type.Optional(Type.Integer({ minimum: 1, maximum: 60_000 }));
|
|
27
22
|
return Type.Union([
|
|
28
23
|
request({ action: StringEnum(["help", "start"]) }),
|
|
@@ -141,5 +136,7 @@ export function browserParameters(hosts) {
|
|
|
141
136
|
action: Type.Literal("stop"),
|
|
142
137
|
ref_id: Type.Optional(refId),
|
|
143
138
|
}),
|
|
144
|
-
]
|
|
139
|
+
], {
|
|
140
|
+
description: "ref_id: tab from tabs; id: element from open/find; handle: continuation from browser; fill value: replacement text/option or checkbox/radio boolean",
|
|
141
|
+
});
|
|
145
142
|
}
|
|
@@ -42,6 +42,10 @@ export async function backendCenter(cdp, sessionId, backendNodeId, scroll = true
|
|
|
42
42
|
const response = await withBackendObject(cdp, sessionId, backendNodeId, (objectId) => cdp.send("Runtime.callFunctionOn", {
|
|
43
43
|
objectId,
|
|
44
44
|
functionDeclaration: `function() {
|
|
45
|
+
const view = this.ownerDocument.defaultView;
|
|
46
|
+
if (view && (view.innerWidth <= 0 || view.innerHeight <= 0)) {
|
|
47
|
+
return { ok: false, error: 'Browser viewport is ' + view.innerWidth + 'x' + view.innerHeight + '; restore or resize its window, then retry' };
|
|
48
|
+
}
|
|
45
49
|
const rect = this.getBoundingClientRect();
|
|
46
50
|
const style = getComputedStyle(this);
|
|
47
51
|
const disabled = this.matches(':disabled') ||
|
package/dist/src/cdp/snapshot.js
CHANGED
|
@@ -205,10 +205,15 @@ export async function snapshotData(cdp, sessionId, elementRefs, options = {}) {
|
|
|
205
205
|
renderedName = name;
|
|
206
206
|
}
|
|
207
207
|
else {
|
|
208
|
+
const href = role === "link"
|
|
209
|
+
? node.properties?.find((property) => property.name === "url")
|
|
210
|
+
?.value?.value
|
|
211
|
+
: undefined;
|
|
208
212
|
const element = {
|
|
209
213
|
id: nextElementId(),
|
|
210
214
|
role,
|
|
211
215
|
...(name ? { name } : {}),
|
|
216
|
+
...(typeof href === "string" && href ? { href } : {}),
|
|
212
217
|
...(value === "" || value == null ? {} : { value }),
|
|
213
218
|
...state,
|
|
214
219
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@howaboua/pi-browser",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Logged-in browser inspection and control for Pi, Code Mode and Notebook Mode.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"pack:dry": "npm pack --dry-run"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"@howaboua/pi-codex-conversion": ">=3.0.
|
|
49
|
+
"@howaboua/pi-codex-conversion": ">=3.0.40",
|
|
50
50
|
"@earendil-works/pi-ai": ">=0.84.3",
|
|
51
51
|
"@earendil-works/pi-coding-agent": ">=0.84.3",
|
|
52
52
|
"typebox": "*",
|
|
@@ -58,10 +58,10 @@
|
|
|
58
58
|
}
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@howaboua/pi-codex-conversion": "3.0.
|
|
61
|
+
"@howaboua/pi-codex-conversion": "3.0.40",
|
|
62
62
|
"@biomejs/biome": "^2.5.10",
|
|
63
|
-
"@earendil-works/pi-ai": "0.
|
|
64
|
-
"@earendil-works/pi-coding-agent": "0.
|
|
63
|
+
"@earendil-works/pi-ai": "0.99.1",
|
|
64
|
+
"@earendil-works/pi-coding-agent": "0.99.1",
|
|
65
65
|
"@types/node": "^26.2.0",
|
|
66
66
|
"tsx": "^4.23.12",
|
|
67
67
|
"typebox": "1.3.18",
|
package/src/browser/bounds.ts
CHANGED
|
@@ -24,6 +24,7 @@ function truncateUtf8(value: string, maxBytes: number): string {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
function boundedElement(element: SnapshotElement): SnapshotElement {
|
|
27
|
+
const { href, ...rest } = element;
|
|
27
28
|
let value = element.value;
|
|
28
29
|
if (typeof value === "string") {
|
|
29
30
|
value = truncateUtf8(value, SNAPSHOT_ELEMENT_VALUE_BYTES);
|
|
@@ -37,7 +38,12 @@ function boundedElement(element: SnapshotElement): SnapshotElement {
|
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
return {
|
|
40
|
-
...
|
|
41
|
+
...rest,
|
|
42
|
+
...(href === undefined
|
|
43
|
+
? {}
|
|
44
|
+
: Buffer.byteLength(href) > SNAPSHOT_URL_BYTES
|
|
45
|
+
? { href_omitted: true as const }
|
|
46
|
+
: { href }),
|
|
41
47
|
role: truncateUtf8(element.role, 128),
|
|
42
48
|
...(element.name === undefined
|
|
43
49
|
? {}
|
package/src/browser/help.ts
CHANGED
|
@@ -3,7 +3,8 @@ export function browserHelp(
|
|
|
3
3
|
): Record<string, unknown> {
|
|
4
4
|
const routed = hosts.length > 0;
|
|
5
5
|
return {
|
|
6
|
-
input:
|
|
6
|
+
input:
|
|
7
|
+
"Request={action,...fields}; Code/Notebook: JSON.stringify(request), JSON.parse(result)",
|
|
7
8
|
...(routed
|
|
8
9
|
? {
|
|
9
10
|
host: `${hosts.join("|")} optional; keep a user-named host on every call and its refs/handles`,
|
|
@@ -13,13 +14,13 @@ export function browserHelp(
|
|
|
13
14
|
safety:
|
|
14
15
|
"Ask before unfamiliar low-trust navigation or consequential action unless authorized; never close shared browser",
|
|
15
16
|
batch:
|
|
16
|
-
"
|
|
17
|
+
"Code/Notebook: {action_name:[{...fields}],...}; nonempty arrays; items omit action; host/response_length at request root; independent only",
|
|
17
18
|
actions: {
|
|
18
19
|
tabs: "query? offset? owned_only? -> ref_id title url owned",
|
|
19
|
-
open: "ref_id lineno?
|
|
20
|
+
open: "ref_id lineno? | url; new tabs open in background",
|
|
20
21
|
show: "ref_id; bring tab to foreground",
|
|
21
22
|
close: "ref_id",
|
|
22
|
-
find: "ref_id pattern lineno?
|
|
23
|
+
find: "ref_id pattern lineno?",
|
|
23
24
|
click: "ref_id id|selector|x+y",
|
|
24
25
|
type: "ref_id text id?; id focuses",
|
|
25
26
|
fill: "ref_id id|selector value; replace/clear text, select option value/label, boolean checks/unchecks",
|
|
@@ -5,148 +5,149 @@ export function browserParameters(hosts: readonly string[]) {
|
|
|
5
5
|
const route: Record<string, TSchema> =
|
|
6
6
|
hosts.length > 0
|
|
7
7
|
? {
|
|
8
|
-
host: Type.Optional(
|
|
9
|
-
StringEnum(hosts, { description: "Configured browser host" }),
|
|
10
|
-
),
|
|
8
|
+
host: Type.Optional(StringEnum(hosts)),
|
|
11
9
|
}
|
|
12
10
|
: {};
|
|
13
11
|
const request = (properties: Record<string, TSchema>) =>
|
|
14
12
|
Type.Object({ ...route, ...properties }, { additionalProperties: false });
|
|
15
13
|
const refId = Type.String({
|
|
16
14
|
minLength: 1,
|
|
17
|
-
description: "Tab ref_id returned by tabs",
|
|
18
15
|
});
|
|
19
16
|
const elementId = Type.Integer({
|
|
20
17
|
minimum: 1,
|
|
21
|
-
description: "Element id returned by open or find",
|
|
22
18
|
});
|
|
23
19
|
const responseLength = StringEnum(["short", "medium", "long"] as const);
|
|
24
20
|
const handle = Type.String({
|
|
25
21
|
minLength: 1,
|
|
26
|
-
description: "Continuation handle returned by browser",
|
|
27
|
-
});
|
|
28
|
-
const fillValue = Type.Union([Type.String(), Type.Boolean()], {
|
|
29
|
-
description: "Replacement text/option; boolean for checkbox/radio",
|
|
30
22
|
});
|
|
23
|
+
const fillValue = Type.Union([Type.String(), Type.Boolean()]);
|
|
31
24
|
const timeout = Type.Optional(Type.Integer({ minimum: 1, maximum: 60_000 }));
|
|
32
|
-
return Type.Union(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
25
|
+
return Type.Union(
|
|
26
|
+
[
|
|
27
|
+
request({ action: StringEnum(["help", "start"] as const) }),
|
|
28
|
+
request({
|
|
29
|
+
action: Type.Literal("tabs"),
|
|
30
|
+
query: Type.Optional(Type.String({ minLength: 1 })),
|
|
31
|
+
offset: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
32
|
+
owned_only: Type.Optional(Type.Boolean()),
|
|
33
|
+
}),
|
|
34
|
+
request({
|
|
35
|
+
action: Type.Literal("open"),
|
|
36
|
+
ref_id: refId,
|
|
37
|
+
lineno: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
38
|
+
response_length: Type.Optional(responseLength),
|
|
39
|
+
}),
|
|
40
|
+
request({
|
|
41
|
+
action: Type.Literal("open"),
|
|
42
|
+
url: Type.String({ minLength: 1 }),
|
|
43
|
+
}),
|
|
44
|
+
request({
|
|
45
|
+
action: Type.Literal("find"),
|
|
46
|
+
ref_id: refId,
|
|
47
|
+
pattern: Type.String({ minLength: 1 }),
|
|
48
|
+
lineno: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
49
|
+
response_length: Type.Optional(responseLength),
|
|
50
|
+
}),
|
|
51
|
+
request({
|
|
52
|
+
action: StringEnum(["show", "close"] as const),
|
|
53
|
+
ref_id: refId,
|
|
54
|
+
}),
|
|
55
|
+
request({ action: Type.Literal("click"), ref_id: refId, id: elementId }),
|
|
56
|
+
request({
|
|
57
|
+
action: Type.Literal("click"),
|
|
58
|
+
ref_id: refId,
|
|
59
|
+
selector: Type.String({ minLength: 1 }),
|
|
60
|
+
}),
|
|
61
|
+
request({
|
|
62
|
+
action: Type.Literal("click"),
|
|
63
|
+
ref_id: refId,
|
|
64
|
+
x: Type.Number(),
|
|
65
|
+
y: Type.Number(),
|
|
66
|
+
}),
|
|
67
|
+
request({
|
|
68
|
+
action: Type.Literal("type"),
|
|
69
|
+
ref_id: refId,
|
|
70
|
+
id: Type.Optional(elementId),
|
|
71
|
+
text: Type.String({ minLength: 1 }),
|
|
72
|
+
}),
|
|
73
|
+
request({
|
|
74
|
+
action: Type.Literal("fill"),
|
|
75
|
+
ref_id: refId,
|
|
76
|
+
id: elementId,
|
|
77
|
+
value: fillValue,
|
|
78
|
+
}),
|
|
79
|
+
request({
|
|
80
|
+
action: Type.Literal("fill"),
|
|
81
|
+
ref_id: refId,
|
|
82
|
+
selector: Type.String({ minLength: 1 }),
|
|
83
|
+
value: fillValue,
|
|
84
|
+
}),
|
|
85
|
+
request({
|
|
86
|
+
action: Type.Literal("press"),
|
|
87
|
+
ref_id: refId,
|
|
88
|
+
key: Type.String({ minLength: 1 }),
|
|
89
|
+
}),
|
|
90
|
+
...(["selector", "text", "url_includes"] as const).map((condition) =>
|
|
91
|
+
request({
|
|
92
|
+
action: Type.Literal("wait"),
|
|
93
|
+
ref_id: refId,
|
|
94
|
+
[condition]: Type.String({ minLength: 1 }),
|
|
95
|
+
timeout_ms: timeout,
|
|
96
|
+
}),
|
|
97
|
+
),
|
|
98
|
+
request({
|
|
99
|
+
action: StringEnum(["screenshot", "html"] as const),
|
|
100
|
+
ref_id: refId,
|
|
101
|
+
}),
|
|
102
|
+
request({
|
|
103
|
+
action: StringEnum(["screenshot", "html"] as const),
|
|
104
|
+
ref_id: refId,
|
|
105
|
+
id: elementId,
|
|
106
|
+
}),
|
|
107
|
+
request({
|
|
108
|
+
action: StringEnum(["screenshot", "html"] as const),
|
|
109
|
+
ref_id: refId,
|
|
110
|
+
selector: Type.String({ minLength: 1 }),
|
|
111
|
+
}),
|
|
112
|
+
request({
|
|
113
|
+
action: Type.Literal("navigate"),
|
|
114
|
+
ref_id: refId,
|
|
115
|
+
url: Type.String({ minLength: 1 }),
|
|
116
|
+
}),
|
|
117
|
+
request({
|
|
118
|
+
action: Type.Literal("evaluate"),
|
|
119
|
+
ref_id: refId,
|
|
120
|
+
expression: Type.String({ minLength: 1 }),
|
|
121
|
+
}),
|
|
122
|
+
request({ action: Type.Literal("network"), ref_id: refId }),
|
|
123
|
+
request({
|
|
124
|
+
action: Type.Literal("load_all"),
|
|
125
|
+
ref_id: refId,
|
|
126
|
+
selector: Type.String({ minLength: 1 }),
|
|
127
|
+
interval_ms: Type.Optional(
|
|
128
|
+
Type.Integer({ minimum: 0, maximum: 60_000 }),
|
|
129
|
+
),
|
|
130
|
+
}),
|
|
131
|
+
request({
|
|
132
|
+
action: Type.Literal("raw"),
|
|
133
|
+
ref_id: refId,
|
|
134
|
+
method: Type.String({ minLength: 1 }),
|
|
135
|
+
params: Type.Optional(Type.Object({}, { additionalProperties: true })),
|
|
136
|
+
}),
|
|
137
|
+
request({
|
|
138
|
+
action: Type.Literal("read_result"),
|
|
139
|
+
handle,
|
|
140
|
+
offset: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
141
|
+
}),
|
|
142
|
+
request({ action: Type.Literal("discard_result"), handle }),
|
|
143
|
+
request({
|
|
144
|
+
action: Type.Literal("stop"),
|
|
145
|
+
ref_id: Type.Optional(refId),
|
|
146
|
+
}),
|
|
147
|
+
],
|
|
148
|
+
{
|
|
149
|
+
description:
|
|
150
|
+
"ref_id: tab from tabs; id: element from open/find; handle: continuation from browser; fill value: replacement text/option or checkbox/radio boolean",
|
|
151
|
+
},
|
|
152
|
+
);
|
|
152
153
|
}
|
|
@@ -117,6 +117,10 @@ export async function backendCenter(
|
|
|
117
117
|
{
|
|
118
118
|
objectId,
|
|
119
119
|
functionDeclaration: `function() {
|
|
120
|
+
const view = this.ownerDocument.defaultView;
|
|
121
|
+
if (view && (view.innerWidth <= 0 || view.innerHeight <= 0)) {
|
|
122
|
+
return { ok: false, error: 'Browser viewport is ' + view.innerWidth + 'x' + view.innerHeight + '; restore or resize its window, then retry' };
|
|
123
|
+
}
|
|
120
124
|
const rect = this.getBoundingClientRect();
|
|
121
125
|
const style = getComputedStyle(this);
|
|
122
126
|
const disabled = this.matches(':disabled') ||
|
|
@@ -16,6 +16,8 @@ export interface SnapshotElement {
|
|
|
16
16
|
id: number;
|
|
17
17
|
role: string;
|
|
18
18
|
name?: string | undefined;
|
|
19
|
+
href?: string | undefined;
|
|
20
|
+
href_omitted?: true | undefined;
|
|
19
21
|
value?: unknown;
|
|
20
22
|
checked?: boolean | "mixed" | undefined;
|
|
21
23
|
selected?: boolean | undefined;
|
package/src/cdp/snapshot.ts
CHANGED
|
@@ -275,10 +275,16 @@ export async function snapshotData(
|
|
|
275
275
|
addLine(text, undefined, "interactive");
|
|
276
276
|
renderedName = name;
|
|
277
277
|
} else {
|
|
278
|
+
const href =
|
|
279
|
+
role === "link"
|
|
280
|
+
? node.properties?.find((property) => property.name === "url")
|
|
281
|
+
?.value?.value
|
|
282
|
+
: undefined;
|
|
278
283
|
const element: SnapshotElement = {
|
|
279
284
|
id: nextElementId(),
|
|
280
285
|
role,
|
|
281
286
|
...(name ? { name } : {}),
|
|
287
|
+
...(typeof href === "string" && href ? { href } : {}),
|
|
282
288
|
...(value === "" || value == null ? {} : { value }),
|
|
283
289
|
...state,
|
|
284
290
|
};
|