























1+import {
2+parseStrictInteger,
3+parseStrictNonNegativeInteger,
4+parseStrictPositiveInteger,
5+} from "openclaw/plugin-sdk/number-runtime";
16import {
27ACT_MAX_BATCH_ACTIONS,
38ACT_MAX_CLICK_DELAY_MS,
@@ -71,6 +76,50 @@ function normalizeBatchAction(value: unknown): BrowserActRequest {
7176return normalizeActRequest(value as Record<string, unknown>, { source: "batch" });
7277}
737879+function readActionPositiveInteger(body: Record<string, unknown>, key: string): number | undefined {
80+const value = parseStrictPositiveInteger(body[key]);
81+if (value === undefined && body[key] != null) {
82+throw new Error(`${key} must be a positive integer.`);
83+}
84+return value;
85+}
86+87+function readActionNonNegativeInteger(
88+body: Record<string, unknown>,
89+key: string,
90+): number | undefined {
91+const value = parseStrictNonNegativeInteger(body[key]);
92+if (value === undefined && body[key] != null) {
93+throw new Error(`${key} must be a non-negative integer.`);
94+}
95+return value;
96+}
97+98+function readActionTimeoutMs(body: Record<string, unknown>): number | undefined {
99+return readActionPositiveInteger(body, "timeoutMs");
100+}
101+102+function readBoundedActionDurationMs(
103+body: Record<string, unknown>,
104+key: string,
105+fieldName: string,
106+maxMs: number,
107+): number | undefined {
108+return normalizeActBoundedNonNegativeMs(
109+readActionNonNegativeInteger(body, key),
110+fieldName,
111+maxMs,
112+);
113+}
114+115+function readResizeDimension(body: Record<string, unknown>, key: "width" | "height") {
116+const value = parseStrictInteger(body[key]);
117+if (value === undefined && Object.hasOwn(body, key)) {
118+throw new Error("resize requires positive width and height");
119+}
120+return value;
121+}
122+74123export function normalizeActRequest(
75124body: Record<string, unknown>,
76125options?: { source?: "request" | "batch" },
@@ -96,12 +145,13 @@ export function normalizeActRequest(
96145throw new Error(parsedModifiers.error);
97146}
98147const doubleClick = toBoolean(body.doubleClick);
99-const delayMs = normalizeActBoundedNonNegativeMs(
100-toNumber(body.delayMs),
148+const delayMs = readBoundedActionDurationMs(
149+body,
150+"delayMs",
101151"click delayMs",
102152ACT_MAX_CLICK_DELAY_MS,
103153);
104-const timeoutMs = toNumber(body.timeoutMs);
154+const timeoutMs = readActionTimeoutMs(body);
105155const targetId = toStringOrEmpty(body.targetId) || undefined;
106156return {
107157 kind,
@@ -127,12 +177,13 @@ export function normalizeActRequest(
127177throw new Error("clickCoords button must be left|right|middle");
128178}
129179const doubleClick = toBoolean(body.doubleClick);
130-const delayMs = normalizeActBoundedNonNegativeMs(
131-toNumber(body.delayMs),
180+const delayMs = readBoundedActionDurationMs(
181+body,
182+"delayMs",
132183"clickCoords delayMs",
133184ACT_MAX_CLICK_DELAY_MS,
134185);
135-const timeoutMs = toNumber(body.timeoutMs);
186+const timeoutMs = readActionTimeoutMs(body);
136187const targetId = toStringOrEmpty(body.targetId) || undefined;
137188return {
138189 kind,
@@ -158,7 +209,7 @@ export function normalizeActRequest(
158209const targetId = toStringOrEmpty(body.targetId) || undefined;
159210const submit = toBoolean(body.submit);
160211const slowly = toBoolean(body.slowly);
161-const timeoutMs = toNumber(body.timeoutMs);
212+const timeoutMs = readActionTimeoutMs(body);
162213return {
163214 kind,
164215 ...(ref ? { ref } : {}),
@@ -176,7 +227,7 @@ export function normalizeActRequest(
176227throw new Error("press requires key");
177228}
178229const targetId = toStringOrEmpty(body.targetId) || undefined;
179-const delayMs = toNumber(body.delayMs);
230+const delayMs = readActionNonNegativeInteger(body, "delayMs");
180231return {
181232 kind,
182233 key,
@@ -192,7 +243,7 @@ export function normalizeActRequest(
192243throw new Error(`${kind} requires ref or selector`);
193244}
194245const targetId = toStringOrEmpty(body.targetId) || undefined;
195-const timeoutMs = toNumber(body.timeoutMs);
246+const timeoutMs = readActionTimeoutMs(body);
196247return {
197248 kind,
198249 ...(ref ? { ref } : {}),
@@ -213,7 +264,7 @@ export function normalizeActRequest(
213264throw new Error("drag requires endRef or endSelector");
214265}
215266const targetId = toStringOrEmpty(body.targetId) || undefined;
216-const timeoutMs = toNumber(body.timeoutMs);
267+const timeoutMs = readActionTimeoutMs(body);
217268return {
218269 kind,
219270 ...(startRef ? { startRef } : {}),
@@ -232,7 +283,7 @@ export function normalizeActRequest(
232283throw new Error("select requires ref/selector and values");
233284}
234285const targetId = toStringOrEmpty(body.targetId) || undefined;
235-const timeoutMs = toNumber(body.timeoutMs);
286+const timeoutMs = readActionTimeoutMs(body);
236287return {
237288 kind,
238289 ...(ref ? { ref } : {}),
@@ -248,7 +299,7 @@ export function normalizeActRequest(
248299throw new Error("fill requires fields");
249300}
250301const targetId = toStringOrEmpty(body.targetId) || undefined;
251-const timeoutMs = toNumber(body.timeoutMs);
302+const timeoutMs = readActionTimeoutMs(body);
252303return {
253304 kind,
254305 fields,
@@ -257,8 +308,8 @@ export function normalizeActRequest(
257308};
258309}
259310case "resize": {
260-const width = toNumber(body.width);
261-const height = toNumber(body.height);
311+const width = readResizeDimension(body, "width");
312+const height = readResizeDimension(body, "height");
262313if (width === undefined || height === undefined || width <= 0 || height <= 0) {
263314throw new Error("resize requires positive width and height");
264315}
@@ -281,8 +332,9 @@ export function normalizeActRequest(
281332loadStateRaw === "networkidle"
282333 ? loadStateRaw
283334 : undefined;
284-const timeMs = normalizeActBoundedNonNegativeMs(
285-toNumber(body.timeMs),
335+const timeMs = readBoundedActionDurationMs(
336+body,
337+"timeMs",
286338"wait timeMs",
287339ACT_MAX_WAIT_TIME_MS,
288340);
@@ -297,7 +349,7 @@ export function normalizeActRequest(
297349);
298350}
299351const targetId = toStringOrEmpty(body.targetId) || undefined;
300-const timeoutMs = toNumber(body.timeoutMs);
352+const timeoutMs = readActionTimeoutMs(body);
301353return {
302354 kind,
303355 ...(timeMs !== undefined ? { timeMs } : {}),
@@ -318,7 +370,7 @@ export function normalizeActRequest(
318370}
319371const ref = toStringOrEmpty(body.ref) || undefined;
320372const targetId = toStringOrEmpty(body.targetId) || undefined;
321-const timeoutMs = toNumber(body.timeoutMs);
373+const timeoutMs = readActionTimeoutMs(body);
322374return {
323375 kind,
324376 fn,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。