mcp-browser-dev-tools 0.1.0 → 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/README.md +90 -355
- package/package.json +16 -2
- package/src/action-retry.mjs +50 -0
- package/src/browser-adapter.mjs +15 -4
- package/src/browser-requests.mjs +15 -0
- package/src/cdp-client.mjs +104 -10
- package/src/firefox-bidi-client.mjs +26 -3
- package/src/json-schema.mjs +99 -0
- package/src/mcp-server.mjs +34 -2013
- package/src/page-context.mjs +25 -1034
- package/src/page-script.mjs +1372 -0
- package/src/run-steps.mjs +510 -0
- package/src/run-tabs.mjs +291 -0
- package/src/screenshot-output.mjs +141 -0
- package/src/tool-results.mjs +34 -0
- package/src/tool-schemas.mjs +431 -0
- package/src/tools/action-tools.mjs +318 -0
- package/src/tools/browser-tools.mjs +221 -0
- package/src/tools/inspection-tools.mjs +287 -0
- package/src/tools/state-tools.mjs +327 -0
- package/src/top-level-await.mjs +260 -0
- package/src/wait-for.mjs +381 -71
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
import { validateValue } from "./json-schema.mjs";
|
|
2
|
+
import { checkPageCondition, normalizeWaitForOptions } from "./wait-for.mjs";
|
|
3
|
+
import { asImageToolResult, moveScreenshotImage } from "./tool-results.mjs";
|
|
4
|
+
import { conditionSchema } from "./tool-schemas.mjs";
|
|
5
|
+
|
|
6
|
+
// Resolves after ms, or at once when signal aborts.
|
|
7
|
+
function sleep(ms, signal) {
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
const timer = setTimeout(resolve, ms);
|
|
10
|
+
signal?.addEventListener(
|
|
11
|
+
"abort",
|
|
12
|
+
() => {
|
|
13
|
+
clearTimeout(timer);
|
|
14
|
+
resolve();
|
|
15
|
+
},
|
|
16
|
+
{ once: true },
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const MAX_RUN_STEPS = 50;
|
|
22
|
+
|
|
23
|
+
export const MAX_STEP_DEPTH = 4;
|
|
24
|
+
|
|
25
|
+
export const MAX_REPEAT = 10;
|
|
26
|
+
|
|
27
|
+
export const DEFAULT_REPEAT = 5;
|
|
28
|
+
|
|
29
|
+
export const MAX_SLEEP_MS = 30_000;
|
|
30
|
+
|
|
31
|
+
export function sleepStepSchema() {
|
|
32
|
+
return {
|
|
33
|
+
type: "object",
|
|
34
|
+
properties: {
|
|
35
|
+
ms: {
|
|
36
|
+
type: "integer",
|
|
37
|
+
minimum: 0,
|
|
38
|
+
maximum: MAX_SLEEP_MS,
|
|
39
|
+
description: `Milliseconds to wait (at most ${MAX_SLEEP_MS}).`,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
required: ["ms"],
|
|
43
|
+
additionalProperties: false,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function ifStepSchema(conditionOptions) {
|
|
48
|
+
const condition = conditionSchema(conditionOptions);
|
|
49
|
+
const steps = {
|
|
50
|
+
type: "array",
|
|
51
|
+
items: { type: "object" },
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
type: "object",
|
|
55
|
+
properties: {
|
|
56
|
+
condition,
|
|
57
|
+
then: steps,
|
|
58
|
+
elseIf: {
|
|
59
|
+
type: "array",
|
|
60
|
+
items: {
|
|
61
|
+
type: "object",
|
|
62
|
+
properties: { condition, then: steps },
|
|
63
|
+
required: ["condition"],
|
|
64
|
+
additionalProperties: false,
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
else: steps,
|
|
68
|
+
},
|
|
69
|
+
required: ["condition"],
|
|
70
|
+
additionalProperties: false,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function repeatStepSchema(conditionOptions) {
|
|
75
|
+
return {
|
|
76
|
+
type: "object",
|
|
77
|
+
properties: {
|
|
78
|
+
steps: {
|
|
79
|
+
type: "array",
|
|
80
|
+
minItems: 1,
|
|
81
|
+
items: { type: "object" },
|
|
82
|
+
},
|
|
83
|
+
until: conditionSchema(conditionOptions),
|
|
84
|
+
max: {
|
|
85
|
+
type: "integer",
|
|
86
|
+
minimum: 1,
|
|
87
|
+
maximum: MAX_REPEAT,
|
|
88
|
+
description: `Most passes to run (default ${DEFAULT_REPEAT}, at most ${MAX_REPEAT}).`,
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
required: ["steps", "until"],
|
|
92
|
+
additionalProperties: false,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function stepSchema(stepTools) {
|
|
97
|
+
return {
|
|
98
|
+
type: "object",
|
|
99
|
+
properties: {
|
|
100
|
+
tool: {
|
|
101
|
+
type: "string",
|
|
102
|
+
enum: ["sleep", "if", "repeat", ...stepTools],
|
|
103
|
+
description:
|
|
104
|
+
"A tool that takes sessionId, sleep to pause, if to branch, or repeat to loop.",
|
|
105
|
+
},
|
|
106
|
+
arguments: {
|
|
107
|
+
type: "object",
|
|
108
|
+
description:
|
|
109
|
+
"That tool's arguments without sessionId (default {}). sleep takes { ms }. if takes { condition, then, elseIf, else }: each condition has the wait_for condition fields, checked once without waiting, all of which must hold; then and else are step lists; elseIf is a list of { condition, then } checked in order after condition, and the first condition that holds runs its then. repeat takes { steps, until, max }: runs steps, then checks the until condition once, and runs them again until it holds, at most max passes.",
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
required: ["tool"],
|
|
113
|
+
additionalProperties: false,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function runStepsInputSchema(stepTools) {
|
|
118
|
+
return {
|
|
119
|
+
type: "object",
|
|
120
|
+
properties: {
|
|
121
|
+
sessionId: {
|
|
122
|
+
type: "string",
|
|
123
|
+
description:
|
|
124
|
+
"Session id returned by attach_tab. Every step runs on this session.",
|
|
125
|
+
},
|
|
126
|
+
steps: {
|
|
127
|
+
type: "array",
|
|
128
|
+
minItems: 1,
|
|
129
|
+
maxItems: MAX_RUN_STEPS,
|
|
130
|
+
description: `Steps to run in order (at most ${MAX_RUN_STEPS}, counting steps inside if branches and repeat bodies once; if and repeat nest at most ${MAX_STEP_DEPTH} deep).`,
|
|
131
|
+
items: stepSchema(stepTools),
|
|
132
|
+
},
|
|
133
|
+
continueOnError: {
|
|
134
|
+
type: "boolean",
|
|
135
|
+
description:
|
|
136
|
+
"Run the remaining steps after a step fails (default false: stop at the first failure).",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
required: ["sessionId", "steps"],
|
|
140
|
+
additionalProperties: false,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function observeCondition(condition, check) {
|
|
145
|
+
const observed = {};
|
|
146
|
+
if (check.page) {
|
|
147
|
+
observed.url = check.page.url ?? null;
|
|
148
|
+
observed.readyState = check.page.readyState ?? null;
|
|
149
|
+
}
|
|
150
|
+
if (condition.selector) {
|
|
151
|
+
observed.found = Boolean(check.element?.found);
|
|
152
|
+
observed.visible = check.element?.node?.visible === true;
|
|
153
|
+
if (
|
|
154
|
+
condition.textEquals !== null ||
|
|
155
|
+
condition.textIncludes !== null ||
|
|
156
|
+
condition.textExcludes !== null
|
|
157
|
+
) {
|
|
158
|
+
observed.text = check.text;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (condition.expression) {
|
|
162
|
+
observed.expression = check.expression;
|
|
163
|
+
}
|
|
164
|
+
return observed;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Moves screenshot data out of a step result so the client receives it as
|
|
168
|
+
// image content, leaving `image` (1-based position among the images) behind.
|
|
169
|
+
export function extractStepImages(tool, result, images) {
|
|
170
|
+
if (tool === "take_screenshot") {
|
|
171
|
+
return moveScreenshotImage(result, images);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (tool === "capture_debug_report" && result?.screenshot) {
|
|
175
|
+
return {
|
|
176
|
+
...result,
|
|
177
|
+
screenshot: moveScreenshotImage(result.screenshot, images),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Validates and runs run_steps batches against the server's tools.
|
|
185
|
+
export class StepRunner {
|
|
186
|
+
constructor({ getTools, browserAdapter, stepSchema, conditionOptions }) {
|
|
187
|
+
this.getTools = getTools;
|
|
188
|
+
this.browserAdapter = browserAdapter;
|
|
189
|
+
this.stepSchema = stepSchema;
|
|
190
|
+
this.conditionOptions = conditionOptions;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
prepareSteps(steps, context, pathPrefix, depth) {
|
|
194
|
+
return steps.map((step, index) => {
|
|
195
|
+
const stepPath = `${pathPrefix}[${index}]`;
|
|
196
|
+
validateValue(stepPath, step, this.stepSchema);
|
|
197
|
+
context.count += 1;
|
|
198
|
+
if (context.count > MAX_RUN_STEPS) {
|
|
199
|
+
throw new Error(
|
|
200
|
+
`run_steps allows at most ${MAX_RUN_STEPS} steps, counting steps inside if branches and repeat bodies`,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const path = `${stepPath}.arguments`;
|
|
205
|
+
const stepArgs = step.arguments ?? {};
|
|
206
|
+
if (step.tool === "sleep") {
|
|
207
|
+
validateValue(path, stepArgs, sleepStepSchema());
|
|
208
|
+
return { tool: step.tool, args: stepArgs };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (
|
|
212
|
+
(step.tool === "if" || step.tool === "repeat") &&
|
|
213
|
+
depth >= MAX_STEP_DEPTH
|
|
214
|
+
) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
`${path} nests if and repeat deeper than ${MAX_STEP_DEPTH} levels`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (step.tool === "repeat") {
|
|
221
|
+
validateValue(path, stepArgs, repeatStepSchema(this.conditionOptions));
|
|
222
|
+
return {
|
|
223
|
+
tool: step.tool,
|
|
224
|
+
max: stepArgs.max ?? DEFAULT_REPEAT,
|
|
225
|
+
until: normalizeWaitForOptions(stepArgs.until, `${path}.until`),
|
|
226
|
+
steps: this.prepareSteps(
|
|
227
|
+
stepArgs.steps,
|
|
228
|
+
context,
|
|
229
|
+
`${path}.steps`,
|
|
230
|
+
depth + 1,
|
|
231
|
+
),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (step.tool === "if") {
|
|
236
|
+
validateValue(path, stepArgs, ifStepSchema(this.conditionOptions));
|
|
237
|
+
const prepareBranch = (branch, branchPath) => ({
|
|
238
|
+
condition: normalizeWaitForOptions(
|
|
239
|
+
branch.condition,
|
|
240
|
+
`${branchPath}.condition`,
|
|
241
|
+
),
|
|
242
|
+
steps: this.prepareSteps(
|
|
243
|
+
branch.then ?? [],
|
|
244
|
+
context,
|
|
245
|
+
`${branchPath}.then`,
|
|
246
|
+
depth + 1,
|
|
247
|
+
),
|
|
248
|
+
});
|
|
249
|
+
return {
|
|
250
|
+
tool: step.tool,
|
|
251
|
+
branches: [
|
|
252
|
+
{ name: "then", ...prepareBranch(stepArgs, path) },
|
|
253
|
+
...(stepArgs.elseIf ?? []).map((branch, branchIndex) => ({
|
|
254
|
+
name: `elseIf[${branchIndex}]`,
|
|
255
|
+
...prepareBranch(branch, `${path}.elseIf[${branchIndex}]`),
|
|
256
|
+
})),
|
|
257
|
+
],
|
|
258
|
+
else: this.prepareSteps(
|
|
259
|
+
stepArgs.else ?? [],
|
|
260
|
+
context,
|
|
261
|
+
`${path}.else`,
|
|
262
|
+
depth + 1,
|
|
263
|
+
),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (stepArgs.sessionId !== undefined) {
|
|
268
|
+
throw new Error(
|
|
269
|
+
`${path}.sessionId is not allowed; run_steps passes its own sessionId`,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const toolArgs = { ...stepArgs, sessionId: context.sessionId };
|
|
274
|
+
const tool = this.getTools().get(step.tool);
|
|
275
|
+
validateValue(path, toolArgs, tool.definition.inputSchema);
|
|
276
|
+
tool.validate?.(toolArgs);
|
|
277
|
+
return { tool: step.tool, args: toolArgs };
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async checkStepCondition(condition, context) {
|
|
282
|
+
const check = await checkPageCondition({
|
|
283
|
+
getPageState: () => this.browserAdapter.getPageState(context.sessionId),
|
|
284
|
+
inspectElement: (selector, options) =>
|
|
285
|
+
this.browserAdapter.inspectElement(
|
|
286
|
+
context.sessionId,
|
|
287
|
+
selector,
|
|
288
|
+
options,
|
|
289
|
+
),
|
|
290
|
+
evaluate: (expression) =>
|
|
291
|
+
this.browserAdapter.evaluate(context.sessionId, expression),
|
|
292
|
+
normalized: condition,
|
|
293
|
+
});
|
|
294
|
+
if (!condition.anyOf) {
|
|
295
|
+
return {
|
|
296
|
+
matched: check.matched,
|
|
297
|
+
observed: observeCondition(condition, check),
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
matched: check.matched,
|
|
302
|
+
observed: {
|
|
303
|
+
matchedIndex: check.matchedIndex,
|
|
304
|
+
anyOf: check.alternatives.map((alternative, index) =>
|
|
305
|
+
observeCondition(condition.anyOf[index], alternative),
|
|
306
|
+
),
|
|
307
|
+
},
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Runs the steps, then checks until once, and repeats until it holds or max
|
|
312
|
+
// passes have run. A failing step ends the loop even with continueOnError,
|
|
313
|
+
// so a failed action is never retried blindly.
|
|
314
|
+
async runRepeatStep(step, context) {
|
|
315
|
+
const runs = [];
|
|
316
|
+
const finish = (ok, matched, extra = {}) => ({
|
|
317
|
+
ok,
|
|
318
|
+
...extra,
|
|
319
|
+
result: { matched, passes: runs.length, max: step.max, runs },
|
|
320
|
+
});
|
|
321
|
+
for (let pass = 1; pass <= step.max; pass += 1) {
|
|
322
|
+
const steps = await this.executeSteps(step.steps, context);
|
|
323
|
+
if (context.signal?.aborted) {
|
|
324
|
+
runs.push({ pass, steps });
|
|
325
|
+
return finish(false, false, { error: "stopped: the tab timed out" });
|
|
326
|
+
}
|
|
327
|
+
if (!steps.every((result) => result.ok)) {
|
|
328
|
+
runs.push({ pass, steps });
|
|
329
|
+
return finish(false, false);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
let until;
|
|
333
|
+
try {
|
|
334
|
+
until = await this.checkStepCondition(step.until, context);
|
|
335
|
+
} catch (error) {
|
|
336
|
+
runs.push({ pass, steps });
|
|
337
|
+
return finish(false, false, {
|
|
338
|
+
error: `until check failed: ${error.message}`,
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
runs.push({ pass, steps, until });
|
|
342
|
+
if (until.matched) {
|
|
343
|
+
return finish(true, true);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return finish(false, false, {
|
|
348
|
+
error: `until did not hold after ${step.max} passes`,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Checks the then and elseIf conditions in order and runs the first branch
|
|
353
|
+
// whose condition holds, or else when none does.
|
|
354
|
+
async runIfStep(step, context) {
|
|
355
|
+
const checked = [];
|
|
356
|
+
let selected = null;
|
|
357
|
+
for (const branch of step.branches) {
|
|
358
|
+
const { matched, observed } = await this.checkStepCondition(
|
|
359
|
+
branch.condition,
|
|
360
|
+
context,
|
|
361
|
+
);
|
|
362
|
+
checked.push({ branch: branch.name, matched, observed });
|
|
363
|
+
if (matched) {
|
|
364
|
+
selected = branch;
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const steps = await this.executeSteps(
|
|
370
|
+
selected ? selected.steps : step.else,
|
|
371
|
+
context,
|
|
372
|
+
);
|
|
373
|
+
return {
|
|
374
|
+
ok: steps.every((result) => result.ok),
|
|
375
|
+
result: {
|
|
376
|
+
matched: selected !== null,
|
|
377
|
+
branch: selected ? selected.name : "else",
|
|
378
|
+
checked,
|
|
379
|
+
steps,
|
|
380
|
+
},
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
async runStep(step, context) {
|
|
385
|
+
if (step.tool === "if") {
|
|
386
|
+
return this.runIfStep(step, context);
|
|
387
|
+
}
|
|
388
|
+
if (step.tool === "repeat") {
|
|
389
|
+
return this.runRepeatStep(step, context);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
let result;
|
|
393
|
+
if (step.tool === "sleep") {
|
|
394
|
+
await sleep(step.args.ms, context.signal);
|
|
395
|
+
result = { sleptMs: step.args.ms };
|
|
396
|
+
} else {
|
|
397
|
+
// run_tabs prepares steps before it opens their tab, so the session
|
|
398
|
+
// comes from the context rather than the prepared arguments.
|
|
399
|
+
result = await this.getTools()
|
|
400
|
+
.get(step.tool)
|
|
401
|
+
.handler({ ...step.args, sessionId: context.sessionId });
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const entry = {
|
|
405
|
+
ok: true,
|
|
406
|
+
result: extractStepImages(step.tool, result, context.images),
|
|
407
|
+
};
|
|
408
|
+
// Actions report a missing element as found: false instead of throwing;
|
|
409
|
+
// later steps usually depend on it, so treat it as a failure.
|
|
410
|
+
// inspect_element may be checking that something is gone.
|
|
411
|
+
if (result?.found === false && step.tool !== "inspect_element") {
|
|
412
|
+
entry.ok = false;
|
|
413
|
+
entry.error =
|
|
414
|
+
result.error ?? `No element matches selector ${step.args.selector}`;
|
|
415
|
+
}
|
|
416
|
+
return entry;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
async executeSteps(steps, context) {
|
|
420
|
+
const results = [];
|
|
421
|
+
for (const [index, step] of steps.entries()) {
|
|
422
|
+
// run_tabs aborts the signal when a tab times out; start nothing more.
|
|
423
|
+
if (context.signal?.aborted) {
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
const startedAt = Date.now();
|
|
427
|
+
let entry;
|
|
428
|
+
try {
|
|
429
|
+
entry = await this.runStep(step, context);
|
|
430
|
+
} catch (error) {
|
|
431
|
+
entry = { ok: false, error: error.message };
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
results.push({
|
|
435
|
+
index,
|
|
436
|
+
tool: step.tool,
|
|
437
|
+
durationMs: Date.now() - startedAt,
|
|
438
|
+
...entry,
|
|
439
|
+
});
|
|
440
|
+
if (!entry.ok && !context.continueOnError) {
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return results;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// After a failed batch, reports the page and its visible controls with
|
|
448
|
+
// locators, so the agent can fix the batch without looking around first.
|
|
449
|
+
async describePageAfterFailure(sessionId) {
|
|
450
|
+
try {
|
|
451
|
+
const snapshot = await this.browserAdapter.snapshotControls(sessionId);
|
|
452
|
+
if (!snapshot || !Array.isArray(snapshot.controls)) {
|
|
453
|
+
return {};
|
|
454
|
+
}
|
|
455
|
+
return {
|
|
456
|
+
page: {
|
|
457
|
+
url: snapshot.url ?? null,
|
|
458
|
+
title: snapshot.title ?? null,
|
|
459
|
+
controls: snapshot.controls,
|
|
460
|
+
moreControls: snapshot.moreControls ?? 0,
|
|
461
|
+
},
|
|
462
|
+
};
|
|
463
|
+
} catch {
|
|
464
|
+
return {};
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
async runSteps(args) {
|
|
469
|
+
const context = {
|
|
470
|
+
sessionId: args.sessionId,
|
|
471
|
+
continueOnError: args.continueOnError === true,
|
|
472
|
+
images: [],
|
|
473
|
+
count: 0,
|
|
474
|
+
};
|
|
475
|
+
// Validate every step, including both branches of each if, first, so a
|
|
476
|
+
// typo in a late step does not leave the page half-changed.
|
|
477
|
+
const steps = this.prepareSteps(args.steps, context, "arguments.steps", 0);
|
|
478
|
+
const results = await this.executeSteps(steps, context);
|
|
479
|
+
const ok =
|
|
480
|
+
results.length === steps.length && results.every((result) => result.ok);
|
|
481
|
+
|
|
482
|
+
return {
|
|
483
|
+
value: {
|
|
484
|
+
sessionId: args.sessionId,
|
|
485
|
+
ok,
|
|
486
|
+
ranSteps: results.length,
|
|
487
|
+
skippedSteps: steps.length - results.length,
|
|
488
|
+
steps: results,
|
|
489
|
+
...(ok ? {} : await this.describePageAfterFailure(args.sessionId)),
|
|
490
|
+
},
|
|
491
|
+
images: context.images,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
export function runStepsTool(runner, stepTools) {
|
|
497
|
+
return [
|
|
498
|
+
"run_steps",
|
|
499
|
+
{
|
|
500
|
+
definition: {
|
|
501
|
+
name: "run_steps",
|
|
502
|
+
description:
|
|
503
|
+
'Run several tools on one attached session in order, in a single call. Use it to act and check the result together, for example [{"tool":"click","arguments":{"selector":"text=Save"}},{"tool":"sleep","arguments":{"ms":300}},{"tool":"take_screenshot"}]. Each step names a tool that takes sessionId (or sleep) and gives that tool\'s arguments without sessionId. To branch on the page state, use an if step, for example {"tool":"if","arguments":{"condition":{"selector":"#status","textIncludes":"failed"},"then":[{"tool":"click","arguments":{"selector":"text=Retry"}}],"elseIf":[{"condition":{"urlIncludes":"/login"},"then":[{"tool":"take_screenshot","arguments":{"output":"image"}}]}],"else":[]}}; conditions are checked once, in order, without waiting, so put wait_for or sleep before the if when the page may still be changing. The if result reports the branch taken (then, elseIf[i], or else), each checked condition with what was observed, and the branch\'s step results. Conditions and wait_for also take anyOf, a list of conditions of which any one must hold, to end on the first of several outcomes. To retry or poll, use a repeat step, for example {"tool":"repeat","arguments":{"max":5,"steps":[{"tool":"click","arguments":{"selector":"text=Load more"}},{"tool":"wait_for","arguments":{"selector":"#list","textExcludes":"Loading"}}],"until":{"selector":"text=Order #1042","state":"present"}}}; it runs its steps, checks until once without waiting, and runs them again until it holds, at most max passes (default 5, at most 10); it fails if until never holds or one of its steps fails, which ends the loop even with continueOnError, and its result lists each pass. Every step, including branches and repeat bodies, is validated before any runs. A step fails when its tool throws or reports found: false (except inspect_element). Stops at the first failing step unless continueOnError is true, and returns each step\'s result or error. When a step fails, the result also has page: the URL, title, and visible controls with a locator for each, to fix the batch without looking again. Screenshots come back as image content; the step result keeps the metadata plus image, the 1-based position of its image. Prefer wait_for over sleep when there is a condition to wait for.',
|
|
504
|
+
inputSchema: runStepsInputSchema(stepTools),
|
|
505
|
+
},
|
|
506
|
+
handler: async (args) => runner.runSteps(args),
|
|
507
|
+
formatResult: asImageToolResult,
|
|
508
|
+
},
|
|
509
|
+
];
|
|
510
|
+
}
|