|
| 1 | +import { parseStrictPositiveInteger } from "openclaw/plugin-sdk/number-runtime"; |
1 | 2 | import { formatErrorMessage } from "../../infra/errors.js"; |
2 | 3 | import { |
3 | 4 | clickChromeMcpElement, |
@@ -41,7 +42,7 @@ import {
|
41 | 42 | import { resolveTargetIdAfterNavigate } from "./agent.snapshot-target.js"; |
42 | 43 | import { EXISTING_SESSION_LIMITS } from "./existing-session-limits.js"; |
43 | 44 | import type { BrowserRouteRegistrar } from "./types.js"; |
44 | | -import { asyncBrowserRoute, jsonError, toNumber, toStringOrEmpty } from "./utils.js"; |
| 45 | +import { asyncBrowserRoute, jsonError, toStringOrEmpty } from "./utils.js"; |
45 | 46 | |
46 | 47 | function sleep(ms: number): Promise<void> { |
47 | 48 | return new Promise((resolve) => setTimeout(resolve, ms)); |
@@ -347,6 +348,14 @@ function getExistingSessionUnsupportedMessage(action: BrowserActRequest): string
|
347 | 348 | throw new Error("Unsupported browser act kind"); |
348 | 349 | } |
349 | 350 | |
| 351 | +function readRoutePositiveInteger(value: unknown, fieldName: string): number | undefined { |
| 352 | +const parsed = parseStrictPositiveInteger(value); |
| 353 | +if (parsed === undefined && value != null) { |
| 354 | +throw new Error(`${fieldName} must be a positive integer.`); |
| 355 | +} |
| 356 | +return parsed; |
| 357 | +} |
| 358 | + |
350 | 359 | export function registerBrowserAgentActRoutes( |
351 | 360 | app: BrowserRouteRegistrar, |
352 | 361 | ctx: BrowserRouteContext, |
@@ -697,8 +706,14 @@ export function registerBrowserAgentActRoutes(
|
697 | 706 | const body = readBody(req); |
698 | 707 | const targetId = resolveTargetIdFromBody(body); |
699 | 708 | const url = toStringOrEmpty(body.url); |
700 | | -const timeoutMs = toNumber(body.timeoutMs); |
701 | | -const maxChars = toNumber(body.maxChars); |
| 709 | +let timeoutMs: number | undefined; |
| 710 | +let maxChars: number | undefined; |
| 711 | +try { |
| 712 | +timeoutMs = readRoutePositiveInteger(body.timeoutMs, "timeoutMs"); |
| 713 | +maxChars = readRoutePositiveInteger(body.maxChars, "maxChars"); |
| 714 | +} catch (err) { |
| 715 | +return jsonError(res, 400, formatErrorMessage(err)); |
| 716 | +} |
702 | 717 | if (!url) { |
703 | 718 | return jsonError(res, 400, "url is required"); |
704 | 719 | } |
|