webhand 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +252 -2
- package/THIRD_PARTY_LICENSE_jev-ultrafast.txt +21 -0
- package/assets/recorder.js +113 -0
- package/assets/snapshot.js +556 -0
- package/dist/agent.js +183 -0
- package/dist/bin.js +22 -0
- package/dist/browser.js +236 -0
- package/dist/cli.js +1144 -0
- package/dist/learn.js +79 -0
- package/dist/metrics.js +64 -0
- package/dist/policies/base.js +178 -0
- package/dist/policies/jev.js +198 -0
- package/dist/policies/scripted.js +48 -0
- package/dist/py.js +141 -0
- package/dist/run.js +80 -0
- package/dist/runbook.js +286 -0
- package/dist/session.js +331 -0
- package/dist/tasks.js +37 -0
- package/dist/types.js +34 -0
- package/dist/workspace.js +78 -0
- package/package.json +57 -4
package/dist/agent.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Jev's loop toward Claude's goal: observe, choose, execute. It stops; it never claims success.
|
|
3
|
+
*
|
|
4
|
+
* The loop returns to Claude only when it cannot continue on its own. The task's verify() decides success.
|
|
5
|
+
*/
|
|
6
|
+
import { pyRepr, pyRound } from "./py.js";
|
|
7
|
+
import { MutationUncertain, PolicyError, StalePage, } from "./types.js";
|
|
8
|
+
// Why the loop handed control back to Claude.
|
|
9
|
+
export const GOAL_REPORTED = "GOAL_REPORTED"; // Jev chose DONE. Claude checks the page; finish verifies.
|
|
10
|
+
export const BLOCKED = "BLOCKED";
|
|
11
|
+
export const STUCK = "STUCK"; // Three actions in a row did not change the page.
|
|
12
|
+
export const ATTEMPT_BUDGET = "ATTEMPT_BUDGET";
|
|
13
|
+
export const NEEDS_HUMAN = "NEEDS_HUMAN"; // A person acts in the window: sign-in, codes, CAPTCHA, [person] steps.
|
|
14
|
+
export const NEEDS_VALUE = "NEEDS_VALUE"; // A field needs text and none of Claude's values fits.
|
|
15
|
+
export const JEV_FAILED = "JEV_FAILED"; // No usable decision. Nothing was executed.
|
|
16
|
+
export const UNCERTAIN = "UNCERTAIN"; // A mutation may have happened. Never retried, never replanned.
|
|
17
|
+
/** Per attempt. A replan starts a new attempt with a fresh budget. */
|
|
18
|
+
export class Budget {
|
|
19
|
+
static MAX_ACTIONS = 30;
|
|
20
|
+
static MAX_DECISIONS = 60;
|
|
21
|
+
maxActions;
|
|
22
|
+
maxDecisions;
|
|
23
|
+
constructor(maxActions = Budget.MAX_ACTIONS, maxDecisions = Budget.MAX_DECISIONS) {
|
|
24
|
+
this.maxActions = maxActions;
|
|
25
|
+
this.maxDecisions = maxDecisions;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const MISSING = "missing"; // A history entry for a NOT_VISIBLE choice: nothing ran, but Jev must see it.
|
|
29
|
+
const STALE = "stale"; // A history entry for a choice the browser refused as stale: nothing ran.
|
|
30
|
+
/** Executed actions only; they count against the budget. */
|
|
31
|
+
export function actionsIn(history, attempt) {
|
|
32
|
+
return history.filter((h) => (h.attempt ?? 1) === attempt && !["human", MISSING, STALE].includes(h.kind));
|
|
33
|
+
}
|
|
34
|
+
/** Three entries without progress: unchanged pages, targets Jev could not see, or refused stale choices. */
|
|
35
|
+
function stuck(history, attempt = 1) {
|
|
36
|
+
const recent = history.filter((h) => (h.attempt ?? 1) === attempt && h.kind !== "human").slice(-3);
|
|
37
|
+
return recent.length === 3 && recent.every((s) => s.page_changed === false && s.kind !== "wait");
|
|
38
|
+
}
|
|
39
|
+
/** Runs once. Logged before the next observation so a navigation cannot erase it. */
|
|
40
|
+
export async function execute(browser, page, decision, history, recorder, options) {
|
|
41
|
+
const onChange = options.onChange ?? (() => { });
|
|
42
|
+
const action = decision.action;
|
|
43
|
+
await browser.act(action, page, decision.text);
|
|
44
|
+
const step = {
|
|
45
|
+
step: history.length + 1,
|
|
46
|
+
attempt: options.attempt,
|
|
47
|
+
decided_by: options.decidedBy,
|
|
48
|
+
confidence: decision.confidence,
|
|
49
|
+
operation: decision.operation,
|
|
50
|
+
kind: action.kind,
|
|
51
|
+
action: action.label,
|
|
52
|
+
text: decision.text,
|
|
53
|
+
url: page.url,
|
|
54
|
+
page_changed: null,
|
|
55
|
+
};
|
|
56
|
+
history.push(step);
|
|
57
|
+
recorder.event("step", step);
|
|
58
|
+
onChange(page);
|
|
59
|
+
const newPage = await browser.observe();
|
|
60
|
+
step.page_changed = !sameMarker(newPage.marker, page.marker);
|
|
61
|
+
recorder.event("step_result", { step: step.step, page_changed: step.page_changed });
|
|
62
|
+
onChange(newPage);
|
|
63
|
+
return newPage;
|
|
64
|
+
}
|
|
65
|
+
/** Markers are JSON values from the page; equal content means the same page. */
|
|
66
|
+
function sameMarker(a, b) {
|
|
67
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
68
|
+
}
|
|
69
|
+
/** Runs one attempt until an outcome above. history is extended in place. */
|
|
70
|
+
export async function runAgent(browser, policy, plan, recorder, budget = new Budget(), options = {}) {
|
|
71
|
+
const attempt = plan.attempt ?? 1;
|
|
72
|
+
const history = options.history ?? [];
|
|
73
|
+
const onChange = options.onChange ?? (() => { });
|
|
74
|
+
const onDecision = options.onDecision ?? (() => { });
|
|
75
|
+
let decisions = options.decisions ?? 0;
|
|
76
|
+
const started = performance.now();
|
|
77
|
+
let page = options.page || (await browser.observe());
|
|
78
|
+
const flushCalls = () => {
|
|
79
|
+
for (const call of policy.calls)
|
|
80
|
+
recorder.call(call);
|
|
81
|
+
policy.calls.length = 0;
|
|
82
|
+
};
|
|
83
|
+
const finish = (outcome, detail = null, extra = {}) => ({
|
|
84
|
+
outcome,
|
|
85
|
+
detail,
|
|
86
|
+
page,
|
|
87
|
+
actions: actionsIn(history, attempt).length,
|
|
88
|
+
decisions,
|
|
89
|
+
elapsed_ms: pyRound(performance.now() - started),
|
|
90
|
+
...extra,
|
|
91
|
+
});
|
|
92
|
+
while (true) {
|
|
93
|
+
if (stuck(history, attempt))
|
|
94
|
+
return finish(STUCK, "three actions without a page change");
|
|
95
|
+
if (actionsIn(history, attempt).length >= budget.maxActions || decisions >= budget.maxDecisions) {
|
|
96
|
+
return finish(ATTEMPT_BUDGET, `${actionsIn(history, attempt).length} actions, ${decisions} decisions`);
|
|
97
|
+
}
|
|
98
|
+
let decision = null;
|
|
99
|
+
const executed = history.length;
|
|
100
|
+
try {
|
|
101
|
+
if (!(await browser.fresh(page)))
|
|
102
|
+
page = await browser.observe();
|
|
103
|
+
decisions += 1;
|
|
104
|
+
onDecision(decisions);
|
|
105
|
+
decision = await policy.choose(page, plan, history);
|
|
106
|
+
flushCalls();
|
|
107
|
+
if (decision.operation === "NOT_VISIBLE") {
|
|
108
|
+
const entry = {
|
|
109
|
+
step: history.length + 1,
|
|
110
|
+
attempt,
|
|
111
|
+
operation: "NOT_VISIBLE",
|
|
112
|
+
kind: MISSING,
|
|
113
|
+
action: `Wanted to ${decision.wanted}, but the needed element was not listed. Nothing ran`,
|
|
114
|
+
text: null,
|
|
115
|
+
url: page.url,
|
|
116
|
+
page_changed: false,
|
|
117
|
+
};
|
|
118
|
+
history.push(entry);
|
|
119
|
+
recorder.event("not_visible", { attempt, wanted: decision.wanted, confidence: decision.confidence });
|
|
120
|
+
onChange(page);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (decision.action === null) {
|
|
124
|
+
if (!(await browser.fresh(page)))
|
|
125
|
+
continue;
|
|
126
|
+
const outcome = { DONE: GOAL_REPORTED, NEEDS_HUMAN }[decision.operation] ?? BLOCKED;
|
|
127
|
+
return finish(outcome, `Jev chose ${decision.operation}`);
|
|
128
|
+
}
|
|
129
|
+
if (decision.action.kind === "fill" && decision.text === null) {
|
|
130
|
+
const field = {};
|
|
131
|
+
for (const key of ["label", "role", "value"])
|
|
132
|
+
field[key] = decision.action[key] ?? null;
|
|
133
|
+
return finish(NEEDS_VALUE, "no value from the plan fits this field", { field });
|
|
134
|
+
}
|
|
135
|
+
page = await execute(browser, page, decision, history, recorder, {
|
|
136
|
+
decidedBy: policy.name,
|
|
137
|
+
attempt,
|
|
138
|
+
onChange,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
if (error instanceof StalePage) {
|
|
143
|
+
const observed = page;
|
|
144
|
+
page = await browser.observe();
|
|
145
|
+
// Only a refused action is noted. A step that ran is already in history, whatever failed after it.
|
|
146
|
+
if (decision !== null && decision.action !== null && history.length === executed) {
|
|
147
|
+
const entry = {
|
|
148
|
+
step: history.length + 1,
|
|
149
|
+
attempt,
|
|
150
|
+
operation: "STALE",
|
|
151
|
+
kind: STALE,
|
|
152
|
+
action: `${decision.operation} ${pyRepr(decision.action.label)} did not run: ${error.message}`,
|
|
153
|
+
text: null,
|
|
154
|
+
url: observed.url,
|
|
155
|
+
// Unchanged: the same choice would be refused again, so it counts toward STUCK.
|
|
156
|
+
page_changed: !sameMarker(page.marker, observed.marker),
|
|
157
|
+
};
|
|
158
|
+
history.push(entry);
|
|
159
|
+
recorder.event("stale", {
|
|
160
|
+
attempt,
|
|
161
|
+
wanted: decision.operation,
|
|
162
|
+
target: decision.action.label,
|
|
163
|
+
reason: error.message,
|
|
164
|
+
page_changed: entry.page_changed,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
onChange(page);
|
|
168
|
+
}
|
|
169
|
+
else if (error instanceof PolicyError) {
|
|
170
|
+
return finish(JEV_FAILED, error.message);
|
|
171
|
+
}
|
|
172
|
+
else if (error instanceof MutationUncertain) {
|
|
173
|
+
return finish(UNCERTAIN, `mutation uncertain: ${error.message}`);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
finally {
|
|
180
|
+
flushCalls();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
package/dist/bin.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { CliExit, main, UsageError } from "./cli.js";
|
|
3
|
+
let code;
|
|
4
|
+
try {
|
|
5
|
+
code = await main(process.argv.slice(2));
|
|
6
|
+
}
|
|
7
|
+
catch (error) {
|
|
8
|
+
if (error instanceof CliExit) {
|
|
9
|
+
process.stderr.write(`${error.message}\n`);
|
|
10
|
+
code = 1;
|
|
11
|
+
}
|
|
12
|
+
else if (error instanceof UsageError) {
|
|
13
|
+
process.stderr.write(`${error.message}\n`);
|
|
14
|
+
code = 2;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
process.stderr.write(`${error instanceof Error ? (error.stack ?? error.message) : String(error)}\n`);
|
|
18
|
+
code = 1;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
// A leftover connection must not keep the CLI alive after its output is written.
|
|
22
|
+
process.exit(code);
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/** Playwright launches an isolated Chrome or attaches to the session Chrome; observation and input go through one CDP session. */
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { isDeepStrictEqual } from "node:util";
|
|
4
|
+
import { chromium, } from "playwright-core";
|
|
5
|
+
import { sleep } from "./py.js";
|
|
6
|
+
import { MutationUncertain, StalePage } from "./types.js";
|
|
7
|
+
export function asset(name) {
|
|
8
|
+
return readFileSync(new URL(`../assets/${name}`, import.meta.url), "utf8");
|
|
9
|
+
}
|
|
10
|
+
const SNAPSHOT = asset("snapshot.js").trim();
|
|
11
|
+
/** The snapshot expression for a view ({focus, skip} region patterns). The view never changes freshness. */
|
|
12
|
+
function readState(view = null) {
|
|
13
|
+
return `(${SNAPSHOT})(${JSON.stringify(view ?? {})})`;
|
|
14
|
+
}
|
|
15
|
+
export const READ_STATE = readState();
|
|
16
|
+
const MARKER = `(() => { const state=${READ_STATE}; return state?.marker ?? null; })()`;
|
|
17
|
+
// Read only once the document has loaded and the DOM has stopped changing, so content rendered by
|
|
18
|
+
// DOMContentLoaded handlers or after an XHR is observed. A page that never goes quiet is read at the cap.
|
|
19
|
+
const SETTLE_QUIET_MS = 150;
|
|
20
|
+
export const SETTLE_CAP_MS = 2000;
|
|
21
|
+
const SETTLE = `new Promise(resolve => {
|
|
22
|
+
const start = performance.now();
|
|
23
|
+
let last = start;
|
|
24
|
+
const observer = new MutationObserver(() => { last = performance.now(); });
|
|
25
|
+
observer.observe(document, {subtree: true, childList: true, attributes: true, characterData: true});
|
|
26
|
+
const check = () => {
|
|
27
|
+
const now = performance.now();
|
|
28
|
+
if (now - start >= ${SETTLE_CAP_MS} ||
|
|
29
|
+
(document.readyState === 'complete' && now - last >= ${SETTLE_QUIET_MS})) {
|
|
30
|
+
observer.disconnect();
|
|
31
|
+
resolve();
|
|
32
|
+
} else setTimeout(check, 25);
|
|
33
|
+
};
|
|
34
|
+
check();
|
|
35
|
+
})`;
|
|
36
|
+
// Node ids are code-owned references to observed elements, never model output.
|
|
37
|
+
const RESOLVE_TARGET = `(action => {
|
|
38
|
+
const c=window.__browserLab, e=c?.nodes.get(action.node);
|
|
39
|
+
if (!e?.isConnected || e.matches(':disabled') || c.closest(e,'[aria-disabled="true"],[inert]') ||
|
|
40
|
+
!e.checkVisibility({checkOpacity:true,checkVisibilityCSS:true})) return null;
|
|
41
|
+
if (action.kind==='fill' && (e.readOnly || e.getAttribute('aria-readonly')==='true')) return null;
|
|
42
|
+
// The center of the whole box first. An inline element that wraps can leave that center between its lines,
|
|
43
|
+
// so the center of each line box is tried next.
|
|
44
|
+
for (const r of [e.getBoundingClientRect(), ...e.getClientRects()]) {
|
|
45
|
+
const x=r.x+r.width/2, y=r.y+r.height/2;
|
|
46
|
+
if (!r.width || !r.height || x<0 || y<0 || x>=innerWidth || y>=innerHeight) continue;
|
|
47
|
+
if (c.hit(e,x,y)) return {x,y};
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
})`;
|
|
51
|
+
const SELECT_OPTION = `(action => {
|
|
52
|
+
const e=window.__browserLab.nodes.get(action.node);
|
|
53
|
+
if (e.tagName!=='SELECT' || ![...e.options].some(o=>o.value===action.value &&
|
|
54
|
+
!o.disabled && !o.closest('optgroup[disabled]'))) return false;
|
|
55
|
+
e.value=action.value;
|
|
56
|
+
e.dispatchEvent(new Event('input',{bubbles:true}));
|
|
57
|
+
e.dispatchEvent(new Event('change',{bubbles:true}));
|
|
58
|
+
return true;
|
|
59
|
+
})`;
|
|
60
|
+
export class Browser {
|
|
61
|
+
view;
|
|
62
|
+
attached;
|
|
63
|
+
browser;
|
|
64
|
+
context;
|
|
65
|
+
page;
|
|
66
|
+
cdp;
|
|
67
|
+
constructor(attached, view) {
|
|
68
|
+
this.attached = attached;
|
|
69
|
+
this.view = view;
|
|
70
|
+
}
|
|
71
|
+
/** Launch a throwaway Chrome for url, or attach to the session Chrome on port. */
|
|
72
|
+
static async open(url = null, options = {}) {
|
|
73
|
+
const port = options.port ?? null;
|
|
74
|
+
const instance = new Browser(port !== null, options.view ?? null);
|
|
75
|
+
try {
|
|
76
|
+
if (port !== null) {
|
|
77
|
+
instance.browser = await chromium.connectOverCDP(`http://127.0.0.1:${port}`);
|
|
78
|
+
instance.context = instance.browser.contexts()[0];
|
|
79
|
+
if (url) {
|
|
80
|
+
// A saved profile may restore old tabs. The run owns exactly one fresh tab.
|
|
81
|
+
const stale = [...instance.context.pages()];
|
|
82
|
+
instance.page = await instance.context.newPage();
|
|
83
|
+
for (const page of stale)
|
|
84
|
+
await page.close();
|
|
85
|
+
await instance.page.goto(url, { waitUntil: "load" });
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
instance.page = instance.context.pages()[0];
|
|
89
|
+
}
|
|
90
|
+
instance.cdp = await instance.context.newCDPSession(instance.page);
|
|
91
|
+
return instance;
|
|
92
|
+
}
|
|
93
|
+
// launch() uses a throwaway profile. Never point this at a signed-in profile.
|
|
94
|
+
instance.browser = await chromium.launch({ channel: "chrome", headless: options.headless ?? true });
|
|
95
|
+
instance.context = await instance.browser.newContext({ viewport: { width: 1120, height: 780 } });
|
|
96
|
+
instance.page = await instance.context.newPage();
|
|
97
|
+
instance.cdp = await instance.context.newCDPSession(instance.page);
|
|
98
|
+
if (url)
|
|
99
|
+
await instance.page.goto(url, { waitUntil: "load" });
|
|
100
|
+
return instance;
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
await instance.close();
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
get version() {
|
|
108
|
+
return this.browser.version();
|
|
109
|
+
}
|
|
110
|
+
// biome-ignore lint/suspicious/noExplicitAny: CDP methods and results are untyped here.
|
|
111
|
+
call(method, params = {}) {
|
|
112
|
+
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
113
|
+
return this.cdp.send(method, params);
|
|
114
|
+
}
|
|
115
|
+
/** Read-only evaluation. A navigation in progress surfaces as StalePage. */
|
|
116
|
+
// biome-ignore lint/suspicious/noExplicitAny: page values are untyped JSON.
|
|
117
|
+
async evaluate(expression, options = {}) {
|
|
118
|
+
let response;
|
|
119
|
+
try {
|
|
120
|
+
response = await this.call("Runtime.evaluate", {
|
|
121
|
+
expression,
|
|
122
|
+
returnByValue: true,
|
|
123
|
+
awaitPromise: options.awaitPromise ?? false,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
throw new StalePage("Document changed during evaluation");
|
|
128
|
+
}
|
|
129
|
+
if (response.exceptionDetails)
|
|
130
|
+
throw new StalePage("Document changed during evaluation");
|
|
131
|
+
return response.result?.value ?? null;
|
|
132
|
+
}
|
|
133
|
+
async observe() {
|
|
134
|
+
for (let i = 0; i < 10; i++) {
|
|
135
|
+
let state;
|
|
136
|
+
try {
|
|
137
|
+
await this.evaluate(SETTLE, { awaitPromise: true });
|
|
138
|
+
state = await this.evaluate(readState(this.view));
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
if (!(error instanceof StalePage))
|
|
142
|
+
throw error;
|
|
143
|
+
state = null;
|
|
144
|
+
}
|
|
145
|
+
if (state !== null)
|
|
146
|
+
return state;
|
|
147
|
+
await sleep(20);
|
|
148
|
+
}
|
|
149
|
+
throw new StalePage("Page did not settle");
|
|
150
|
+
}
|
|
151
|
+
async fresh(page, action = null) {
|
|
152
|
+
if (action !== null && (action.kind === "click" || action.kind === "select")) {
|
|
153
|
+
const node = action.node;
|
|
154
|
+
if (!Number.isInteger(node))
|
|
155
|
+
return false;
|
|
156
|
+
const current = await this.evaluate("(() => { const c=window.__browserLab; " +
|
|
157
|
+
`return c ? [c.pageKey(),c.guard(c.nodes.get(${node}))] : null; })()`);
|
|
158
|
+
return isDeepStrictEqual(current, [page.page_key, page.guards?.[String(node)] ?? null]);
|
|
159
|
+
}
|
|
160
|
+
return isDeepStrictEqual(await this.evaluate(MARKER), page.marker);
|
|
161
|
+
}
|
|
162
|
+
/** Execute once. StalePage means nothing happened; MutationUncertain means do not retry. */
|
|
163
|
+
async act(action, page, text = null) {
|
|
164
|
+
if (!(await this.fresh(page, action)))
|
|
165
|
+
throw new StalePage("Page changed since this decision");
|
|
166
|
+
const kind = action.kind;
|
|
167
|
+
if (kind === "wait") {
|
|
168
|
+
await sleep(100);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
let target = null;
|
|
172
|
+
if (kind !== "scroll") {
|
|
173
|
+
target = await this.evaluate(`${RESOLVE_TARGET}(${JSON.stringify(action)})`);
|
|
174
|
+
if (target === null)
|
|
175
|
+
throw new StalePage("Target changed or is covered");
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
if (kind === "scroll") {
|
|
179
|
+
await this.call("Input.dispatchMouseEvent", {
|
|
180
|
+
type: "mouseWheel",
|
|
181
|
+
x: 550,
|
|
182
|
+
y: 650,
|
|
183
|
+
deltaX: 0,
|
|
184
|
+
deltaY: action.delta,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
else if (kind === "select") {
|
|
188
|
+
await this.select(action);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
const { x, y } = target;
|
|
192
|
+
await this.click(x, y);
|
|
193
|
+
if (kind === "fill")
|
|
194
|
+
await this.replaceText(text);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
if (error instanceof MutationUncertain)
|
|
199
|
+
throw error;
|
|
200
|
+
throw new MutationUncertain(`${kind} was interrupted`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
async click(x, y) {
|
|
204
|
+
for (const type of ["mousePressed", "mouseReleased"]) {
|
|
205
|
+
await this.call("Input.dispatchMouseEvent", { type, x, y, button: "left", clickCount: 1 });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
async select(action) {
|
|
209
|
+
const response = await this.call("Runtime.evaluate", {
|
|
210
|
+
expression: `${SELECT_OPTION}(${JSON.stringify(action)})`,
|
|
211
|
+
returnByValue: true,
|
|
212
|
+
});
|
|
213
|
+
if (response?.result?.value !== true)
|
|
214
|
+
throw new MutationUncertain("Dropdown change was not confirmed");
|
|
215
|
+
}
|
|
216
|
+
async replaceText(text) {
|
|
217
|
+
if (text === null)
|
|
218
|
+
throw new MutationUncertain("Field was focused without text");
|
|
219
|
+
const modifiers = process.platform === "darwin" ? 4 : 2;
|
|
220
|
+
const key = { key: "a", code: "KeyA", modifiers };
|
|
221
|
+
await this.call("Input.dispatchKeyEvent", { type: "keyDown", commands: ["selectAll"], ...key });
|
|
222
|
+
await this.call("Input.dispatchKeyEvent", { type: "keyUp", ...key });
|
|
223
|
+
await this.call("Input.insertText", { text });
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* An attached session Chrome stays alive; only session.stopChrome() ends it. Closing a browser that
|
|
227
|
+
* connectOverCDP attached to only disconnects: Chrome keeps its tabs and pages.
|
|
228
|
+
*/
|
|
229
|
+
async close() {
|
|
230
|
+
if (!this.browser)
|
|
231
|
+
return;
|
|
232
|
+
if (this.attached)
|
|
233
|
+
await this.cdp?.detach().catch(() => { });
|
|
234
|
+
await this.browser.close().catch(() => { });
|
|
235
|
+
}
|
|
236
|
+
}
|