|
7 | 7 | normalizeLowercaseStringOrEmpty, |
8 | 8 | normalizeOptionalString, |
9 | 9 | } from "openclaw/plugin-sdk/string-coerce-runtime"; |
10 | | -import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime"; |
| 10 | +import { truncateUtf16Safe, withTimeout } from "openclaw/plugin-sdk/text-utility-runtime"; |
11 | 11 | import type { Frame, Page } from "playwright-core"; |
12 | 12 | import type { SsrFPolicy } from "../infra/net/ssrf.js"; |
13 | 13 | import { ACT_MAX_VIEWPORT_DIMENSION, resolveBrowserNavigationTimeoutMs } from "./act-policy.js"; |
@@ -218,24 +218,9 @@ export async function snapshotAriaViaPlaywright(opts: { |
218 | 218 | }; |
219 | 219 | }, |
220 | 220 | }); |
221 | | - const res = (await (ariaTimeoutMs === undefined |
222 | | - ? collectAxTree |
223 | | - : (() => { |
224 | | - let timer: ReturnType<typeof setTimeout> | undefined; |
225 | | - const timeout = new Promise<never>((_, reject) => { |
226 | | - timer = setTimeout(() => { |
227 | | - reject(new Error(`Aria snapshot via Playwright timed out after ${ariaTimeoutMs}ms.`)); |
228 | | - }, ariaTimeoutMs); |
229 | | - timer.unref?.(); |
230 | | - }); |
231 | | - return Promise.race([collectAxTree, timeout]).finally(() => { |
232 | | - if (timer) { |
233 | | - clearTimeout(timer); |
234 | | - } |
235 | | - }); |
236 | | - })())) as { |
237 | | - nodes?: RawAXNode[]; |
238 | | - }; |
| 221 | + const res = await withTimeout(collectAxTree, ariaTimeoutMs ?? 0, { |
| 222 | + message: `Aria snapshot via Playwright timed out after ${ariaTimeoutMs}ms.`, |
| 223 | + }); |
239 | 224 | const nodes = Array.isArray(res?.nodes) ? res.nodes : []; |
240 | 225 | const formatted = formatAriaSnapshot(nodes, limit); |
241 | 226 | await storeAriaSnapshotRefsViaPlaywright({ |
@@ -444,16 +429,20 @@ export async function snapshotRoleViaPlaywright(opts: { |
444 | 429 | page, |
445 | 430 | frame: frame ?? page.mainFrame(), |
446 | 431 | run: async (isFrameCurrent) => { |
447 | | - const locator = frame |
448 | | - ? selector |
449 | | - ? frame.locator(selector) |
450 | | - : frame.locator(":root") |
451 | | - : selector |
452 | | - ? page.locator(selector) |
453 | | - : page.locator(":root"); |
454 | | - const selectorMatched = !selector || (await locator.count()) > 0; |
| 432 | + const snapshotScope = frame ?? page; |
| 433 | + const locator = snapshotScope.locator(selector || ":root"); |
| 434 | + const captureDeadline = performance.now() + ariaSnapshotTimeout; |
| 435 | + // Count has no timeout; both capture stages share one budget before refs are published. |
| 436 | + const selectorMatched = |
| 437 | + !selector || |
| 438 | + (await withTimeout(locator.count(), ariaSnapshotTimeout, "Role snapshot selector")) > 0; |
455 | 439 | const ariaSnapshot = selectorMatched |
456 | | - ? await locator.ariaSnapshot({ timeout: ariaSnapshotTimeout }) |
| 440 | + ? await locator.ariaSnapshot({ |
| 441 | + // A zero Playwright timeout disables its deadline. |
| 442 | + timeout: selector |
| 443 | + ? Math.max(1, captureDeadline - performance.now()) |
| 444 | + : ariaSnapshotTimeout, |
| 445 | + }) |
457 | 446 | : ""; |
458 | 447 | const built = buildRoleSnapshotFromAriaSnapshot(ariaSnapshot ?? "", opts.options); |
459 | 448 | return await finalizeRoleSnapshotViaPlaywright({ |
|
0 commit comments