mcp-browser-dev-tools 0.4.0 → 0.5.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 -5
- package/package.json +2 -1
- package/src/action-changes.mjs +47 -9
- package/src/app.mjs +85 -10
- package/src/browser-adapter.mjs +8 -0
- package/src/browser-launch-service.mjs +55 -7
- package/src/cdp-client.mjs +76 -16
- package/src/dialog-answers.mjs +91 -0
- package/src/firefox-bidi-client.mjs +128 -27
- package/src/json-rpc-stdio.mjs +19 -2
- package/src/logger.mjs +38 -5
- package/src/mcp-server.mjs +20 -3
- package/src/page-context.mjs +57 -2
- package/src/page-script.mjs +359 -73
- package/src/run-steps.mjs +299 -20
- package/src/run-tabs.mjs +31 -18
- package/src/tool-results.mjs +96 -0
- package/src/tool-schemas.mjs +1 -1
- package/src/tool-sets.mjs +3 -2
- package/src/tools/action-tools.mjs +36 -0
- package/src/tools/browser-tools.mjs +8 -3
- package/src/tools/inspection-tools.mjs +74 -0
package/src/run-steps.mjs
CHANGED
|
@@ -5,9 +5,13 @@ import { checkPageCondition, normalizeWaitForOptions } from "./wait-for.mjs";
|
|
|
5
5
|
import { asImageToolResult, moveScreenshotImage } from "./tool-results.mjs";
|
|
6
6
|
import { conditionSchema } from "./tool-schemas.mjs";
|
|
7
7
|
|
|
8
|
-
// Resolves after ms, or at once when signal aborts.
|
|
8
|
+
// Resolves after ms, or at once when signal aborts or already has.
|
|
9
9
|
function sleep(ms, signal) {
|
|
10
10
|
return new Promise((resolve) => {
|
|
11
|
+
if (signal?.aborted) {
|
|
12
|
+
resolve();
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
11
15
|
const timer = setTimeout(resolve, ms);
|
|
12
16
|
signal?.addEventListener(
|
|
13
17
|
"abort",
|
|
@@ -30,6 +34,10 @@ export const DEFAULT_REPEAT = 5;
|
|
|
30
34
|
|
|
31
35
|
export const MAX_SLEEP_MS = 30_000;
|
|
32
36
|
|
|
37
|
+
export const MAX_IF_TIMEOUT_MS = 30_000;
|
|
38
|
+
|
|
39
|
+
export const IF_POLL_INTERVAL_MS = 100;
|
|
40
|
+
|
|
33
41
|
export function sleepStepSchema() {
|
|
34
42
|
return {
|
|
35
43
|
type: "object",
|
|
@@ -67,6 +75,12 @@ export function ifStepSchema(conditionOptions) {
|
|
|
67
75
|
},
|
|
68
76
|
},
|
|
69
77
|
else: steps,
|
|
78
|
+
timeoutMs: {
|
|
79
|
+
type: "integer",
|
|
80
|
+
minimum: 0,
|
|
81
|
+
maximum: MAX_IF_TIMEOUT_MS,
|
|
82
|
+
description: `ms to keep checking before running else (default 0: check once; at most ${MAX_IF_TIMEOUT_MS}).`,
|
|
83
|
+
},
|
|
70
84
|
},
|
|
71
85
|
required: ["condition"],
|
|
72
86
|
additionalProperties: false,
|
|
@@ -214,6 +228,74 @@ function batchFrame(steps) {
|
|
|
214
228
|
return frames.size === 1 ? [...frames][0] : null;
|
|
215
229
|
}
|
|
216
230
|
|
|
231
|
+
// Steps that leave the page reacting: requests in flight, the DOM still
|
|
232
|
+
// changing, a transition running.
|
|
233
|
+
const SETTLING_TOOLS = new Set([
|
|
234
|
+
...CHANGE_REPORTING_TOOLS,
|
|
235
|
+
"navigate",
|
|
236
|
+
"reload",
|
|
237
|
+
"scroll",
|
|
238
|
+
"set_viewport",
|
|
239
|
+
]);
|
|
240
|
+
|
|
241
|
+
// Steps that look at the page. After a settling step, the next of these
|
|
242
|
+
// first waits for the page to settle, as an action outside a batch does
|
|
243
|
+
// before it reports its changes, so it does not see the page mid-change.
|
|
244
|
+
// The condition of a repeat, and of an if that checks once, wait the same
|
|
245
|
+
// way. Actions do not: they retry until their element is ready. Neither do
|
|
246
|
+
// wait_for and an if with timeoutMs, which poll for their own conditions.
|
|
247
|
+
const READING_TOOLS = new Set([
|
|
248
|
+
"evaluate_js",
|
|
249
|
+
"take_screenshot",
|
|
250
|
+
"get_snapshot",
|
|
251
|
+
"read_text",
|
|
252
|
+
"query_elements",
|
|
253
|
+
"inspect_element",
|
|
254
|
+
"get_document",
|
|
255
|
+
"get_page_state",
|
|
256
|
+
"get_console_messages",
|
|
257
|
+
"get_network_requests",
|
|
258
|
+
"get_events",
|
|
259
|
+
"get_har",
|
|
260
|
+
"get_cookies",
|
|
261
|
+
"get_storage",
|
|
262
|
+
"get_performance",
|
|
263
|
+
"capture_debug_report",
|
|
264
|
+
"capture_session_snapshot",
|
|
265
|
+
"compare_page_state",
|
|
266
|
+
"compare_selector",
|
|
267
|
+
]);
|
|
268
|
+
|
|
269
|
+
// Most rounds of waiting for a frame and the top page in turn.
|
|
270
|
+
const MAX_SETTLE_ROUNDS = 3;
|
|
271
|
+
|
|
272
|
+
// Whether steps hold one that waits for the page: a reading step, or an if
|
|
273
|
+
// or repeat, whose condition reads. A batch without any is never watched.
|
|
274
|
+
function containsReadingStep(steps) {
|
|
275
|
+
return steps.some(
|
|
276
|
+
(step) =>
|
|
277
|
+
READING_TOOLS.has(step.tool) ||
|
|
278
|
+
step.tool === "if" ||
|
|
279
|
+
step.tool === "repeat",
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function isNavigationStep(step) {
|
|
284
|
+
return step.tool === "navigate" || step.tool === "reload";
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Whether a settling step leaves the page for the next reading step to
|
|
288
|
+
// wait on. A navigation told not to wait for the load is left alone.
|
|
289
|
+
function leavesPageSettling(step) {
|
|
290
|
+
return (
|
|
291
|
+
SETTLING_TOOLS.has(step.tool) &&
|
|
292
|
+
!(
|
|
293
|
+
isNavigationStep(step) &&
|
|
294
|
+
["none", "interactive"].includes(step.args?.waitUntil)
|
|
295
|
+
)
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
217
299
|
// Validates and runs run_steps batches against the server's tools.
|
|
218
300
|
export class StepRunner {
|
|
219
301
|
constructor({
|
|
@@ -288,6 +370,7 @@ export class StepRunner {
|
|
|
288
370
|
});
|
|
289
371
|
return {
|
|
290
372
|
tool: step.tool,
|
|
373
|
+
timeoutMs: stepArgs.timeoutMs ?? 0,
|
|
291
374
|
branches: [
|
|
292
375
|
{ name: "then", ...prepareBranch(stepArgs, path) },
|
|
293
376
|
...(stepArgs.elseIf ?? []).map((branch, branchIndex) => ({
|
|
@@ -318,7 +401,115 @@ export class StepRunner {
|
|
|
318
401
|
});
|
|
319
402
|
}
|
|
320
403
|
|
|
321
|
-
|
|
404
|
+
// Whether the steps a context runs include one that waits for the page.
|
|
405
|
+
// The caller sets context.settles from this before running them.
|
|
406
|
+
readsPage(steps) {
|
|
407
|
+
return Boolean(this.changeTracker) && containsReadingStep(steps);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Watches the page, so the wait before the next reading step can tell
|
|
411
|
+
// when it is quiet: the top document, and the frame the step acts in.
|
|
412
|
+
// One watch covers a run of settling steps and ends with the wait; it
|
|
413
|
+
// takes no snapshot. A frame is watched along with the top document,
|
|
414
|
+
// since what happens in one can change the other.
|
|
415
|
+
async watchPage(context, frameKey = null) {
|
|
416
|
+
if (!context.settles) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
context.watches ??= new Map();
|
|
420
|
+
for (const key of frameKey ? [null, frameKey] : [null]) {
|
|
421
|
+
if (!context.watches.has(key)) {
|
|
422
|
+
const watch = await this.changeTracker.begin(context.sessionId, key, {
|
|
423
|
+
watchOnly: true,
|
|
424
|
+
});
|
|
425
|
+
if (watch) {
|
|
426
|
+
context.watches.set(key, watch);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Leaves the page settling after a navigation that waited for the load:
|
|
433
|
+
// the new document is watched from here, and its requests are counted
|
|
434
|
+
// from since, before the navigation. run_tabs calls this for the page it
|
|
435
|
+
// loads into a tab.
|
|
436
|
+
async settleAfterNavigation(context, since) {
|
|
437
|
+
await this.watchPage(context);
|
|
438
|
+
context.settling = { endedAt: Date.now(), since };
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// Waits for the page to settle when a settling step ran since the last
|
|
442
|
+
// wait. Returns how long that took, or null when there was nothing to
|
|
443
|
+
// wait for.
|
|
444
|
+
async settlePage(context) {
|
|
445
|
+
const settling = context.settling;
|
|
446
|
+
if (!settling || !context.watches?.size) {
|
|
447
|
+
return null;
|
|
448
|
+
}
|
|
449
|
+
context.settling = null;
|
|
450
|
+
const startedAt = Date.now();
|
|
451
|
+
const wait = (watch, since) =>
|
|
452
|
+
this.changeTracker.waitUntilQuiet(watch, {
|
|
453
|
+
endedAt: settling.endedAt,
|
|
454
|
+
since,
|
|
455
|
+
animations: true,
|
|
456
|
+
signal: context.signal,
|
|
457
|
+
});
|
|
458
|
+
let busy = false;
|
|
459
|
+
// A frame and the top page can keep changing each other, so with more
|
|
460
|
+
// than one watch they are waited for in turn until a whole round finds
|
|
461
|
+
// every one of them quiet already. Still waiting after the last round
|
|
462
|
+
// means the page is busy.
|
|
463
|
+
for (let round = 0, waited = true; waited && !busy; round += 1) {
|
|
464
|
+
waited = false;
|
|
465
|
+
for (const [key, watch] of Array.from(context.watches)) {
|
|
466
|
+
let outcome = await wait(watch, settling.since);
|
|
467
|
+
if (outcome.lost && !context.signal?.aborted) {
|
|
468
|
+
// The page dropped the watch, as a document does when it is left;
|
|
469
|
+
// it may be back from the back/forward cache. Watch it afresh,
|
|
470
|
+
// counting requests from when the dropped watch began.
|
|
471
|
+
const fresh = await this.changeTracker.begin(context.sessionId, key, {
|
|
472
|
+
watchOnly: true,
|
|
473
|
+
});
|
|
474
|
+
await this.changeTracker.stop(watch);
|
|
475
|
+
if (fresh) {
|
|
476
|
+
context.watches.set(key, fresh);
|
|
477
|
+
outcome = await wait(fresh, settling.since ?? watch.startedAt);
|
|
478
|
+
} else {
|
|
479
|
+
context.watches.delete(key);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
busy ||= outcome.timedOut;
|
|
483
|
+
waited ||= outcome.polls > 1 && context.watches.size > 1;
|
|
484
|
+
}
|
|
485
|
+
if (waited && round >= MAX_SETTLE_ROUNDS) {
|
|
486
|
+
busy = true;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
await this.stopWatching(context);
|
|
490
|
+
// run_tabs aborts the signal when a tab times out; read nothing more.
|
|
491
|
+
if (context.signal?.aborted) {
|
|
492
|
+
throw new Error("stopped: the tab timed out");
|
|
493
|
+
}
|
|
494
|
+
return {
|
|
495
|
+
settledMs: Date.now() - startedAt,
|
|
496
|
+
...(busy ? { settled: false } : {}),
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// Ends the watches of a context: after a wait, before a navigation, and
|
|
501
|
+
// when its steps are done.
|
|
502
|
+
async stopWatching(context) {
|
|
503
|
+
const watches = Array.from(context.watches?.values() ?? []);
|
|
504
|
+
context.watches?.clear();
|
|
505
|
+
await Promise.all(watches.map((watch) => this.changeTracker.stop(watch)));
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// settle is false for an if that polls with timeoutMs: like wait_for, it
|
|
509
|
+
// watches for its condition from the start, and a wait for the page
|
|
510
|
+
// first could let a state it is looking for come and go unseen.
|
|
511
|
+
async checkStepCondition(condition, context, { settle = true } = {}) {
|
|
512
|
+
const settled = settle ? await this.settlePage(context) : null;
|
|
322
513
|
const check = await checkPageCondition({
|
|
323
514
|
getPageState: () => this.browserAdapter.getPageState(context.sessionId),
|
|
324
515
|
inspectElement: (selector, options) =>
|
|
@@ -334,11 +525,13 @@ export class StepRunner {
|
|
|
334
525
|
if (!condition.anyOf) {
|
|
335
526
|
return {
|
|
336
527
|
matched: check.matched,
|
|
528
|
+
...settled,
|
|
337
529
|
observed: observeCondition(condition, check),
|
|
338
530
|
};
|
|
339
531
|
}
|
|
340
532
|
return {
|
|
341
533
|
matched: check.matched,
|
|
534
|
+
...settled,
|
|
342
535
|
observed: {
|
|
343
536
|
matchedIndex: check.matchedIndex,
|
|
344
537
|
anyOf: check.alternatives.map((alternative, index) =>
|
|
@@ -390,22 +583,64 @@ export class StepRunner {
|
|
|
390
583
|
}
|
|
391
584
|
|
|
392
585
|
// Checks the then and elseIf conditions in order and runs the first branch
|
|
393
|
-
// whose condition holds, or else when none does.
|
|
586
|
+
// whose condition holds, or else when none does. With timeoutMs, a round
|
|
587
|
+
// in which none holds is followed by another every IF_POLL_INTERVAL_MS, and
|
|
588
|
+
// else runs only once the time is up.
|
|
394
589
|
async runIfStep(step, context) {
|
|
395
|
-
const
|
|
590
|
+
const startedAt = Date.now();
|
|
591
|
+
let attempts = 0;
|
|
592
|
+
let checked = [];
|
|
396
593
|
let selected = null;
|
|
397
|
-
for
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
594
|
+
// The wait for the page before the first check, when there was one.
|
|
595
|
+
let settled = {};
|
|
596
|
+
// run_tabs aborts the signal when the tab times out, possibly during a
|
|
597
|
+
// check; no branch is taken then.
|
|
598
|
+
const stopped = () => ({
|
|
599
|
+
ok: false,
|
|
600
|
+
error: "stopped: the tab timed out",
|
|
601
|
+
result: { matched: false, branch: null, checked, steps: [] },
|
|
602
|
+
});
|
|
603
|
+
while (true) {
|
|
604
|
+
attempts += 1;
|
|
605
|
+
checked = [];
|
|
606
|
+
for (const branch of step.branches) {
|
|
607
|
+
let check;
|
|
608
|
+
try {
|
|
609
|
+
check = await this.checkStepCondition(branch.condition, context, {
|
|
610
|
+
settle: step.timeoutMs === 0,
|
|
611
|
+
});
|
|
612
|
+
} catch (error) {
|
|
613
|
+
if (context.signal?.aborted) {
|
|
614
|
+
return stopped();
|
|
615
|
+
}
|
|
616
|
+
throw error;
|
|
617
|
+
}
|
|
618
|
+
if (context.signal?.aborted) {
|
|
619
|
+
return stopped();
|
|
620
|
+
}
|
|
621
|
+
const { matched, observed, ...waitedForPage } = check;
|
|
622
|
+
if ("settledMs" in waitedForPage) {
|
|
623
|
+
settled = waitedForPage;
|
|
624
|
+
}
|
|
625
|
+
checked.push({ branch: branch.name, matched, observed });
|
|
626
|
+
if (matched) {
|
|
627
|
+
selected = branch;
|
|
628
|
+
break;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
const remainingMs = startedAt + step.timeoutMs - Date.now();
|
|
633
|
+
if (selected || remainingMs <= 0) {
|
|
405
634
|
break;
|
|
406
635
|
}
|
|
636
|
+
await sleep(Math.min(IF_POLL_INTERVAL_MS, remainingMs), context.signal);
|
|
637
|
+
if (context.signal?.aborted) {
|
|
638
|
+
return stopped();
|
|
639
|
+
}
|
|
407
640
|
}
|
|
408
641
|
|
|
642
|
+
const waited =
|
|
643
|
+
step.timeoutMs > 0 ? { attempts, waitedMs: Date.now() - startedAt } : {};
|
|
409
644
|
const steps = await this.executeSteps(
|
|
410
645
|
selected ? selected.steps : step.else,
|
|
411
646
|
context,
|
|
@@ -415,6 +650,8 @@ export class StepRunner {
|
|
|
415
650
|
result: {
|
|
416
651
|
matched: selected !== null,
|
|
417
652
|
branch: selected ? selected.name : "else",
|
|
653
|
+
...settled,
|
|
654
|
+
...waited,
|
|
418
655
|
checked,
|
|
419
656
|
steps,
|
|
420
657
|
},
|
|
@@ -430,23 +667,59 @@ export class StepRunner {
|
|
|
430
667
|
}
|
|
431
668
|
|
|
432
669
|
let result;
|
|
670
|
+
let settled = null;
|
|
433
671
|
if (step.tool === "sleep") {
|
|
434
672
|
await sleep(step.args.ms, context.signal);
|
|
435
673
|
result = { sleptMs: step.args.ms };
|
|
436
674
|
} else {
|
|
675
|
+
const settles = leavesPageSettling(step);
|
|
676
|
+
const navigates = isNavigationStep(step);
|
|
677
|
+
let since;
|
|
678
|
+
if (navigates) {
|
|
679
|
+
// Nothing is learned from the page being left.
|
|
680
|
+
await this.stopWatching(context);
|
|
681
|
+
context.settling = null;
|
|
682
|
+
since = new Date().toISOString();
|
|
683
|
+
} else if (settles) {
|
|
684
|
+
await this.watchPage(
|
|
685
|
+
context,
|
|
686
|
+
parseFrameRef(step.args?.selector)?.frameKey ?? null,
|
|
687
|
+
);
|
|
688
|
+
// Set before the step runs, so a step that fails partway still
|
|
689
|
+
// leaves the page to settle for a later step. since stays, when a
|
|
690
|
+
// navigation before this step has not been waited for yet.
|
|
691
|
+
context.settling = { ...context.settling, endedAt: Date.now() };
|
|
692
|
+
} else if (READING_TOOLS.has(step.tool)) {
|
|
693
|
+
settled = await this.settlePage(context);
|
|
694
|
+
}
|
|
437
695
|
// run_tabs prepares steps before it opens their tab, so the session
|
|
438
696
|
// comes from the context rather than the prepared arguments.
|
|
439
697
|
// The batch reports its changes once, at the end.
|
|
440
|
-
|
|
441
|
-
.
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
698
|
+
try {
|
|
699
|
+
result = await this.getTools()
|
|
700
|
+
.get(step.tool)
|
|
701
|
+
.handler(
|
|
702
|
+
{ ...step.args, sessionId: context.sessionId },
|
|
703
|
+
{ reportChanges: false },
|
|
704
|
+
);
|
|
705
|
+
} catch (error) {
|
|
706
|
+
// A navigation that failed, say by timing out on the load, may
|
|
707
|
+
// still have left a new page loading for a later step.
|
|
708
|
+
if (navigates && settles) {
|
|
709
|
+
await this.settleAfterNavigation(context, since).catch(() => {});
|
|
710
|
+
}
|
|
711
|
+
throw error;
|
|
712
|
+
}
|
|
713
|
+
if (navigates && settles) {
|
|
714
|
+
await this.settleAfterNavigation(context, since);
|
|
715
|
+
} else if (settles) {
|
|
716
|
+
context.settling.endedAt = Date.now();
|
|
717
|
+
}
|
|
446
718
|
}
|
|
447
719
|
|
|
448
720
|
const entry = {
|
|
449
721
|
ok: true,
|
|
722
|
+
...settled,
|
|
450
723
|
result: extractStepImages(step.tool, result, context.images),
|
|
451
724
|
};
|
|
452
725
|
// Actions report a missing element as found: false instead of throwing;
|
|
@@ -524,7 +797,13 @@ export class StepRunner {
|
|
|
524
797
|
this.changeTracker && containsChangeReportingStep(steps)
|
|
525
798
|
? await this.changeTracker.begin(args.sessionId, batchFrame(steps))
|
|
526
799
|
: null;
|
|
527
|
-
|
|
800
|
+
context.settles = this.readsPage(steps);
|
|
801
|
+
let results;
|
|
802
|
+
try {
|
|
803
|
+
results = await this.executeSteps(steps, context);
|
|
804
|
+
} finally {
|
|
805
|
+
await this.stopWatching(context);
|
|
806
|
+
}
|
|
528
807
|
const ok =
|
|
529
808
|
results.length === steps.length && results.every((result) => result.ok);
|
|
530
809
|
const changes = baseline
|
|
@@ -552,7 +831,7 @@ export function runStepsTool(runner) {
|
|
|
552
831
|
{
|
|
553
832
|
definition: {
|
|
554
833
|
name: "run_steps",
|
|
555
|
-
description: `Run session tools in order in one call: [{"tool":"type","arguments":{"selector":"ref=e3","text":"Ada"}},{"tool":"click","arguments":{"selector":"text=Save"}}]. Arguments omit sessionId. Also: sleep {ms}; if {condition, then, elseIf: [{condition, then}], else}, which branches
|
|
834
|
+
description: `Run session tools in order in one call: [{"tool":"type","arguments":{"selector":"ref=e3","text":"Ada"}},{"tool":"click","arguments":{"selector":"text=Save"}}]. Arguments omit sessionId. Also: sleep {ms}; if {condition, then, elseIf: [{condition, then}], else, timeoutMs}, which branches on wait_for fields, checked once or until one holds within timeoutMs; repeat {steps, until, max (default ${DEFAULT_REPEAT})}, which reruns steps until until holds. All steps are validated first. A step fails when it throws or reports found: false (not inspect_element); the batch stops there unless continueOnError and reports the page's controls. A batch with actions returns one changes for the whole batch. A step that reads the page after an action or navigation first waits for the page to settle, so it needs no sleep.`,
|
|
556
835
|
inputSchema: runStepsInputSchema(),
|
|
557
836
|
},
|
|
558
837
|
handler: async (args) => runner.runSteps(args),
|
package/src/run-tabs.mjs
CHANGED
|
@@ -142,29 +142,42 @@ export class TabRunner {
|
|
|
142
142
|
let sessionId = tab.sessionId;
|
|
143
143
|
|
|
144
144
|
const work = (async () => {
|
|
145
|
-
|
|
146
|
-
({ targetId: opened } = await this.browserAdapter.createTab(
|
|
147
|
-
"about:blank",
|
|
148
|
-
{ browserFamily: options.browserFamily },
|
|
149
|
-
));
|
|
150
|
-
if (controller.signal.aborted) {
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
({ sessionId } = await this.browserAdapter.attachToTarget(opened));
|
|
154
|
-
if (controller.signal.aborted) {
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
await this.browserAdapter.navigate(sessionId, tab.url, {
|
|
158
|
-
waitUntil: tab.waitUntil,
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
return this.stepRunner.executeSteps(tab.steps, {
|
|
145
|
+
const context = {
|
|
162
146
|
sessionId,
|
|
163
147
|
continueOnError: options.continueOnError,
|
|
164
148
|
images,
|
|
165
149
|
count: 0,
|
|
166
150
|
signal: controller.signal,
|
|
167
|
-
|
|
151
|
+
settles: this.stepRunner.readsPage(tab.steps),
|
|
152
|
+
};
|
|
153
|
+
try {
|
|
154
|
+
if (tab.url !== undefined) {
|
|
155
|
+
({ targetId: opened } = await this.browserAdapter.createTab(
|
|
156
|
+
"about:blank",
|
|
157
|
+
{ browserFamily: options.browserFamily },
|
|
158
|
+
));
|
|
159
|
+
if (controller.signal.aborted) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
({ sessionId } = await this.browserAdapter.attachToTarget(opened));
|
|
163
|
+
context.sessionId = sessionId;
|
|
164
|
+
if (controller.signal.aborted) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
// The first step that reads the page waits for what the page
|
|
168
|
+
// fetches and renders after its load event.
|
|
169
|
+
const since = new Date().toISOString();
|
|
170
|
+
await this.browserAdapter.navigate(sessionId, tab.url, {
|
|
171
|
+
waitUntil: tab.waitUntil,
|
|
172
|
+
});
|
|
173
|
+
if (!["none", "interactive"].includes(tab.waitUntil)) {
|
|
174
|
+
await this.stepRunner.settleAfterNavigation(context, since);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return await this.stepRunner.executeSteps(tab.steps, context);
|
|
178
|
+
} finally {
|
|
179
|
+
await this.stepRunner.stopWatching(context).catch(() => {});
|
|
180
|
+
}
|
|
168
181
|
})();
|
|
169
182
|
|
|
170
183
|
const timeout = timeoutAfter(options.timeoutMs);
|
package/src/tool-results.mjs
CHANGED
|
@@ -32,3 +32,99 @@ export function moveScreenshotImage(screenshot, images) {
|
|
|
32
32
|
images.push({ type: "image", data, mimeType: screenshot.mimeType });
|
|
33
33
|
return { ...metadata, image: images.length };
|
|
34
34
|
}
|
|
35
|
+
|
|
36
|
+
const ELEMENT_TEXT_LIMIT = 80;
|
|
37
|
+
const VALUE_ROLES = [
|
|
38
|
+
"textbox",
|
|
39
|
+
"searchbox",
|
|
40
|
+
"combobox",
|
|
41
|
+
"spinbutton",
|
|
42
|
+
"slider",
|
|
43
|
+
];
|
|
44
|
+
const CHECK_ROLES = ["checkbox", "radio", "switch"];
|
|
45
|
+
|
|
46
|
+
function clipLineText(value) {
|
|
47
|
+
const text =
|
|
48
|
+
typeof value === "string" ? value.replace(/\s+/g, " ").trim() : "";
|
|
49
|
+
return text.length > ELEMENT_TEXT_LIMIT
|
|
50
|
+
? `${text.slice(0, ELEMENT_TEXT_LIMIT)}...`
|
|
51
|
+
: text;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// One line for an element, as get_snapshot writes them but without a ref:
|
|
55
|
+
// its role (or its tag with its id or first class), name, and state.
|
|
56
|
+
// Built from the full description the page returns for the element.
|
|
57
|
+
// Without state, only the role and name: enough to say which element.
|
|
58
|
+
export function elementLine(node, { state = true } = {}) {
|
|
59
|
+
const tag = String(node.tagName ?? "").toLowerCase();
|
|
60
|
+
const attributes = node.attributes ?? {};
|
|
61
|
+
const password =
|
|
62
|
+
tag === "input" &&
|
|
63
|
+
String(attributes.type ?? "").toLowerCase() === "password";
|
|
64
|
+
const firstClass = String(node.className ?? "")
|
|
65
|
+
.split(/\s+/)
|
|
66
|
+
.find(Boolean);
|
|
67
|
+
const parts = [
|
|
68
|
+
node.role ??
|
|
69
|
+
tag + (node.id ? `#${node.id}` : firstClass ? `.${firstClass}` : ""),
|
|
70
|
+
];
|
|
71
|
+
const valued =
|
|
72
|
+
VALUE_ROLES.includes(node.role) &&
|
|
73
|
+
typeof node.value === "string" &&
|
|
74
|
+
!password;
|
|
75
|
+
const name = clipLineText(node.accessibleName);
|
|
76
|
+
// The accessible name of a field can fall back to its value.
|
|
77
|
+
if (name && !(valued && name === clipLineText(node.value))) {
|
|
78
|
+
parts.push(JSON.stringify(name));
|
|
79
|
+
}
|
|
80
|
+
if (!state) {
|
|
81
|
+
return parts.join(" ");
|
|
82
|
+
}
|
|
83
|
+
if (valued) {
|
|
84
|
+
parts.push(`value=${JSON.stringify(clipLineText(node.value))}`);
|
|
85
|
+
}
|
|
86
|
+
if (password && node.value) {
|
|
87
|
+
parts.push("filled");
|
|
88
|
+
}
|
|
89
|
+
if (CHECK_ROLES.includes(node.role)) {
|
|
90
|
+
const checked =
|
|
91
|
+
typeof node.checked === "boolean" && tag === "input"
|
|
92
|
+
? node.checked
|
|
93
|
+
: attributes["aria-checked"] === "true";
|
|
94
|
+
parts.push(checked ? "checked" : "unchecked");
|
|
95
|
+
}
|
|
96
|
+
if (node.disabled) {
|
|
97
|
+
parts.push("disabled");
|
|
98
|
+
}
|
|
99
|
+
if (node.visible === false) {
|
|
100
|
+
parts.push("hidden");
|
|
101
|
+
}
|
|
102
|
+
return parts.join(" ");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Replaces the full description of the element a tool acted on (node)
|
|
106
|
+
// with a line that says which element it was (element): its role and
|
|
107
|
+
// name, without its state. The description was read at the moment of the
|
|
108
|
+
// action, so a state in it, such as a button disabled while it works,
|
|
109
|
+
// would contradict the changes the action reports once the page has
|
|
110
|
+
// settled. inspect_element is the tool that returns the full description.
|
|
111
|
+
export function summarizeElement(result) {
|
|
112
|
+
if (typeof result?.node?.tagName !== "string") {
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
const { node, ...rest } = result;
|
|
116
|
+
return { ...rest, element: elementLine(node, { state: false }) };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// The same for the element wait_for waited for: its line, or null when the
|
|
120
|
+
// condition held without the element being there.
|
|
121
|
+
export function summarizeWaitedElement(result) {
|
|
122
|
+
if (!result?.element || typeof result.element !== "object") {
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
const node = result.element.node;
|
|
126
|
+
return {
|
|
127
|
+
...result,
|
|
128
|
+
element: typeof node?.tagName === "string" ? elementLine(node) : null,
|
|
129
|
+
};
|
|
130
|
+
}
|
package/src/tool-schemas.mjs
CHANGED
|
@@ -28,7 +28,7 @@ export function sessionWithLimitSchema(description) {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
export const LOCATOR_DESCRIPTION =
|
|
31
|
-
'Locator: ref=e3, CSS, text=\u2026, role=button[name="\u2026"],
|
|
31
|
+
'Locator: ref=e3, CSS, text=\u2026, role=button[name="\u2026"], name=\u2026, or a >> b';
|
|
32
32
|
|
|
33
33
|
// A copy of schema without descriptions, for a schema repeated inside another
|
|
34
34
|
// whose fields are already described once; validation is unchanged.
|
package/src/tool-sets.mjs
CHANGED
|
@@ -22,13 +22,12 @@ export const TOOL_GROUPS = {
|
|
|
22
22
|
"run_tabs",
|
|
23
23
|
"get_snapshot",
|
|
24
24
|
"read_text",
|
|
25
|
-
"inspect_element",
|
|
26
25
|
"take_screenshot",
|
|
27
26
|
"evaluate_js",
|
|
28
27
|
"get_console_messages",
|
|
29
28
|
"get_network_requests",
|
|
30
29
|
],
|
|
31
|
-
input: ["drag", "set_viewport"],
|
|
30
|
+
input: ["drag", "set_viewport", "set_dialog_answers"],
|
|
32
31
|
network: ["set_network", "get_har", "get_events"],
|
|
33
32
|
state: [
|
|
34
33
|
"get_cookies",
|
|
@@ -38,6 +37,8 @@ export const TOOL_GROUPS = {
|
|
|
38
37
|
"capture_debug_report",
|
|
39
38
|
"get_page_state",
|
|
40
39
|
"get_document",
|
|
40
|
+
"inspect_element",
|
|
41
|
+
"query_elements",
|
|
41
42
|
],
|
|
42
43
|
compare: ["compare_page_state", "compare_selector"],
|
|
43
44
|
browser: ["browser_status", "launch_browser", "list_sessions", "detach_tab"],
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { retryUntilActionable } from "../action-retry.mjs";
|
|
2
|
+
import { validateDialogAnswers } from "../dialog-answers.mjs";
|
|
2
3
|
import { parseKeyCombo } from "../keyboard.mjs";
|
|
3
4
|
import {
|
|
4
5
|
DEFAULT_DRAG_STEPS,
|
|
@@ -454,5 +455,40 @@ export function actionTools(server) {
|
|
|
454
455
|
}),
|
|
455
456
|
},
|
|
456
457
|
],
|
|
458
|
+
[
|
|
459
|
+
"set_dialog_answers",
|
|
460
|
+
{
|
|
461
|
+
definition: {
|
|
462
|
+
name: "set_dialog_answers",
|
|
463
|
+
description:
|
|
464
|
+
"How this session answers confirm and prompt dialogs (default: confirm accepted, prompt dismissed; alert and beforeunload always accepted).",
|
|
465
|
+
inputSchema: sessionSchema(
|
|
466
|
+
{
|
|
467
|
+
confirm: { type: "string", enum: ["accept", "dismiss"] },
|
|
468
|
+
prompt: { type: "string", enum: ["accept", "dismiss"] },
|
|
469
|
+
promptText: {
|
|
470
|
+
type: "string",
|
|
471
|
+
description: "Default: the prompt's default value.",
|
|
472
|
+
},
|
|
473
|
+
once: {
|
|
474
|
+
type: "boolean",
|
|
475
|
+
description: "Only the next dialog of each given type.",
|
|
476
|
+
},
|
|
477
|
+
},
|
|
478
|
+
[],
|
|
479
|
+
),
|
|
480
|
+
},
|
|
481
|
+
validate: (args) => {
|
|
482
|
+
validateDialogAnswers(args);
|
|
483
|
+
},
|
|
484
|
+
handler: async (args) =>
|
|
485
|
+
server.browserAdapter.setDialogAnswers(args.sessionId, {
|
|
486
|
+
confirm: args.confirm,
|
|
487
|
+
prompt: args.prompt,
|
|
488
|
+
promptText: args.promptText,
|
|
489
|
+
once: args.once === true,
|
|
490
|
+
}),
|
|
491
|
+
},
|
|
492
|
+
],
|
|
457
493
|
];
|
|
458
494
|
}
|