Skip to content

Commit febc700

Browse files
authored
refactor: consolidate exact boolean coercion (#99750)
* refactor: consolidate exact boolean parsing * test: fix normalization subpath resolution * fix: resolve normalization boolean subpath
1 parent faa4f47 commit febc700

19 files changed

Lines changed: 131 additions & 70 deletions

‎extensions/whatsapp/src/channel-react-action.ts‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Whatsapp plugin module implements channel react action behavior.
2+
import { readBooleanParam } from "openclaw/plugin-sdk/boolean-param";
23
import { jsonResult } from "openclaw/plugin-sdk/channel-actions";
34
import {
45
isWhatsAppGroupJid,
@@ -54,24 +55,6 @@ function readUploadFileCaptionText(args: Record<string, unknown>): string {
5455
);
5556
}
5657

57-
function readBooleanParam(args: Record<string, unknown>, key: string): boolean | undefined {
58-
const value = args[key];
59-
if (typeof value === "boolean") {
60-
return value;
61-
}
62-
if (typeof value !== "string") {
63-
return undefined;
64-
}
65-
const normalized = value.trim().toLowerCase();
66-
if (normalized === "true") {
67-
return true;
68-
}
69-
if (normalized === "false") {
70-
return false;
71-
}
72-
return undefined;
73-
}
74-
7558
function hasUploadFileBufferPayload(args: Record<string, unknown>): boolean {
7659
return readStringParam(args, "buffer", { trim: false }) !== undefined;
7760
}

‎packages/normalization-core/package.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"import": "./dist/index.mjs",
1515
"default": "./dist/index.mjs"
1616
},
17+
"./boolean-coercion": {
18+
"types": "./dist/boolean-coercion.d.mts",
19+
"import": "./dist/boolean-coercion.mjs",
20+
"default": "./dist/boolean-coercion.mjs"
21+
},
1722
"./number-coercion": {
1823
"types": "./dist/number-coercion.d.mts",
1924
"import": "./dist/number-coercion.mjs",
@@ -36,6 +41,6 @@
3641
}
3742
},
3843
"scripts": {
39-
"build": "tsdown src/index.ts src/number-coercion.ts src/record-coerce.ts src/string-coerce.ts src/string-normalization.ts --no-config --platform node --format esm --dts --out-dir dist --clean"
44+
"build": "tsdown src/index.ts src/boolean-coercion.ts src/number-coercion.ts src/record-coerce.ts src/string-coerce.ts src/string-normalization.ts --no-config --platform node --format esm --dts --out-dir dist --clean"
4045
}
4146
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Normalization Core tests cover boolean coerce behavior.
2+
import { parseBoolean } from "@openclaw/normalization-core/boolean-coercion";
3+
import { describe, expect, it } from "vitest";
4+
5+
describe("normalization-core/boolean-coercion", () => {
6+
it.each([
7+
[true, true],
8+
[false, false],
9+
["true", true],
10+
[" FALSE ", false],
11+
["TrUe", true],
12+
])("parses %j as %s", (value, expected) => {
13+
expect(parseBoolean(value)).toBe(expected);
14+
});
15+
16+
it.each([undefined, null, 0, 1, "", "yes", "no", "on", "off", "1", "0"])(
17+
"rejects unsupported value %j",
18+
(value) => {
19+
expect(parseBoolean(value)).toBeUndefined();
20+
},
21+
);
22+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** Parses booleans and case-insensitive `true`/`false` string tokens. */
2+
export function parseBoolean(value: unknown): boolean | undefined {
3+
if (typeof value === "boolean") {
4+
return value;
5+
}
6+
if (typeof value !== "string") {
7+
return undefined;
8+
}
9+
const normalized = value.trim().toLowerCase();
10+
if (normalized === "true") {
11+
return true;
12+
}
13+
if (normalized === "false") {
14+
return false;
15+
}
16+
return undefined;
17+
}

‎packages/normalization-core/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Public barrel for shared coercion and normalization helpers.
22

3+
export * from "./boolean-coercion.js";
34
export * from "./number-coercion.js";
45
export * from "./record-coerce.js";
56
export * from "./string-coerce.js";

‎scripts/prepare-extension-package-boundary-artifacts.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ const PACKAGE_DTS_REQUIRED_OUTPUTS = [
181181
"packages/plugin-sdk/dist/packages/model-catalog-core/src/provider-model-id-normalization.d.ts",
182182
"packages/plugin-sdk/dist/packages/model-catalog-core/src/provider-model-id-normalize.d.ts",
183183
"packages/plugin-sdk/dist/packages/normalization-core/src/index.d.ts",
184+
"packages/plugin-sdk/dist/packages/normalization-core/src/boolean-coercion.d.ts",
184185
"packages/plugin-sdk/dist/packages/normalization-core/src/number-coercion.d.ts",
185186
"packages/plugin-sdk/dist/packages/normalization-core/src/record-coerce.d.ts",
186187
"packages/plugin-sdk/dist/packages/normalization-core/src/string-coerce.d.ts",

‎src/agents/tools/media-tool-shared.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { withEnv } from "../../test-utils/env.js";
88
import {
99
hasGenerationToolAvailability,
1010
isCapabilityProviderConfigured,
11+
readBooleanToolParam,
1112
resolveMediaToolInboundRoots,
1213
resolveCapabilityModelConfigForTool,
1314
resolveMediaToolLocalRoots,
@@ -59,6 +60,14 @@ function createModelRegistryStub(resolve: (provider: string, modelId: string) =>
5960
};
6061
}
6162

63+
describe("readBooleanToolParam", () => {
64+
it("parses booleans and true/false string tokens", () => {
65+
expect(readBooleanToolParam({ audio: true }, "audio")).toBe(true);
66+
expect(readBooleanToolParam({ audio: " FALSE " }, "audio")).toBe(false);
67+
expect(readBooleanToolParam({ audio: "yes" }, "audio")).toBeUndefined();
68+
});
69+
});
70+
6271
describe("resolveMediaToolLocalRoots", () => {
6372
it("does not widen default local roots from media sources", () => {
6473
const stateDir = path.join("/tmp", "openclaw-media-tool-roots-state");

‎src/agents/tools/media-tool-shared.ts‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import { normalizeInboundPathRoots } from "@openclaw/media-core/inbound-path-policy";
77
import { normalizeProviderId } from "@openclaw/model-catalog-core/provider-id";
8+
import { parseBoolean } from "@openclaw/normalization-core/boolean-coercion";
89
import {
910
normalizeOptionalLowercaseString,
1011
normalizeOptionalString,
@@ -470,20 +471,7 @@ export function readBooleanToolParam(
470471
params: Record<string, unknown>,
471472
key: string,
472473
): boolean | undefined {
473-
const raw = readSnakeCaseParamRaw(params, key);
474-
if (typeof raw === "boolean") {
475-
return raw;
476-
}
477-
if (typeof raw === "string") {
478-
const normalized = normalizeOptionalLowercaseString(raw);
479-
if (normalized === "true") {
480-
return true;
481-
}
482-
if (normalized === "false") {
483-
return false;
484-
}
485-
}
486-
return undefined;
474+
return parseBoolean(readSnakeCaseParamRaw(params, key));
487475
}
488476

489477
/**

‎src/cron/normalize.ts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** Normalizes cron create/patch payloads before validation and persistence. */
2+
import { parseBoolean } from "@openclaw/normalization-core/boolean-coercion";
23
import { timestampMsToIsoString } from "@openclaw/normalization-core/number-coercion";
34
import {
45
normalizeLowercaseStringOrEmpty,
@@ -558,17 +559,9 @@ export function normalizeCronJobInput(
558559
}
559560

560561
if ("enabled" in base) {
561-
const enabled = base.enabled;
562-
if (typeof enabled === "boolean") {
562+
const enabled = parseBoolean(base.enabled);
563+
if (enabled !== undefined) {
563564
next.enabled = enabled;
564-
} else if (typeof enabled === "string") {
565-
const trimmed = normalizeOptionalLowercaseString(enabled);
566-
if (trimmed === "true") {
567-
next.enabled = true;
568-
}
569-
if (trimmed === "false") {
570-
next.enabled = false;
571-
}
572565
}
573566
}
574567

‎src/memory-host-sdk/dreaming.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ describe("memory dreaming host helpers", () => {
5757
expect(resolved.phases.deep.maxAgeDays).toBe(30);
5858
});
5959

60+
it("parses true/false strings while keeping invalid-value defaults local", () => {
61+
const resolved = resolveMemoryDreamingConfig({
62+
pluginConfig: {
63+
dreaming: {
64+
enabled: " TRUE ",
65+
verboseLogging: "false",
66+
storage: { separateReports: "invalid" },
67+
phases: { light: { enabled: " FALSE " } },
68+
},
69+
},
70+
});
71+
72+
expect(resolved.enabled).toBe(true);
73+
expect(resolved.verboseLogging).toBe(false);
74+
expect(resolved.storage.separateReports).toBe(false);
75+
expect(resolved.phases.light.enabled).toBe(false);
76+
});
77+
6078
it("lets execution defaults and phase execution override the top-level dreaming model", () => {
6179
const resolved = resolveMemoryDreamingConfig({
6280
pluginConfig: {

0 commit comments

Comments
 (0)