Files
openclaw/src/poll-params.ts
Peter Steinberger 00d8d7ead0 refactor: extract normalization core package
Extract shared normalization/coercion helpers into private @openclaw/normalization-core workspace package while preserving existing plugin SDK helper subpaths.\n\nAlso keeps direct normalization-core imports internal, wires UI/build/loader resolution, and replaces the slow PR network CodeQL lane with a fast added-line boundary scan while retaining full CodeQL for scheduled/manual runs.\n\nVerification: local moved tests, plugin SDK boundary tests, extension loader tests, agents-support shard, UI build/test, build artifacts, lint, workflow guards, autoreview, and GitHub CI passed on PR head 963d893715.
2026-05-31 01:33:00 +01:00

126 lines
4.1 KiB
TypeScript

import { parseStrictFiniteNumber } from "@openclaw/normalization-core/number-coercion";
import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
import { readSnakeCaseParamRaw } from "./param-key.js";
type PollCreationParamKind = "string" | "stringArray" | "positiveInteger" | "boolean";
type PollCreationParamDef = {
kind: PollCreationParamKind;
};
const SHARED_POLL_CREATION_PARAM_DEFS = {
pollQuestion: { kind: "string" },
pollOption: { kind: "stringArray" },
pollDurationHours: { kind: "positiveInteger" },
pollMulti: { kind: "boolean" },
} satisfies Record<string, PollCreationParamDef>;
export const POLL_CREATION_PARAM_DEFS: Record<string, PollCreationParamDef> =
SHARED_POLL_CREATION_PARAM_DEFS;
type SharedPollCreationParamName = keyof typeof SHARED_POLL_CREATION_PARAM_DEFS;
export const SHARED_POLL_CREATION_PARAM_NAMES = Object.keys(
SHARED_POLL_CREATION_PARAM_DEFS,
) as SharedPollCreationParamName[];
const SHARED_POLL_CREATION_PARAM_KEY_SET = new Set(
SHARED_POLL_CREATION_PARAM_NAMES.map(normalizePollParamKey),
);
const POLL_VOTE_PARAM_KEY_SET = new Set(
["pollId", "pollOptionId", "pollOptionIds", "pollOptionIndex", "pollOptionIndexes"].map(
normalizePollParamKey,
),
);
function readPollParamRaw(params: Record<string, unknown>, key: string): unknown {
return readSnakeCaseParamRaw(params, key);
}
function normalizePollParamKey(key: string): string {
return normalizeLowercaseStringOrEmpty(key.replaceAll("_", ""));
}
function isChannelPollCreationParamName(key: string): boolean {
const normalized = normalizePollParamKey(key);
return (
normalized.startsWith("poll") &&
!SHARED_POLL_CREATION_PARAM_KEY_SET.has(normalized) &&
!POLL_VOTE_PARAM_KEY_SET.has(normalized)
);
}
function hasExplicitUnknownPollValue(key: string, value: unknown): boolean {
if (value === true) {
return true;
}
if (typeof value === "number") {
return Number.isFinite(value) && value !== 0;
}
if (typeof value === "string") {
const trimmed = value.trim();
if (trimmed.length === 0) {
return false;
}
if (normalizePollParamKey(key).includes("duration")) {
const parsed = parseStrictFiniteNumber(trimmed);
return Number.isFinite(parsed) && parsed !== 0;
}
const normalized = normalizeLowercaseStringOrEmpty(trimmed);
return normalized !== "false" && normalized !== "0";
}
if (Array.isArray(value)) {
return value.some((entry) => hasExplicitUnknownPollValue(key, entry));
}
return false;
}
export function hasPollCreationParams(params: Record<string, unknown>): boolean {
for (const key of SHARED_POLL_CREATION_PARAM_NAMES) {
const def = POLL_CREATION_PARAM_DEFS[key];
const value = readPollParamRaw(params, key);
if (def.kind === "string" && typeof value === "string" && value.trim().length > 0) {
return true;
}
if (def.kind === "stringArray") {
if (
Array.isArray(value) &&
value.some((entry) => typeof entry === "string" && entry.trim())
) {
return true;
}
if (typeof value === "string" && value.trim().length > 0) {
return true;
}
}
if (def.kind === "positiveInteger") {
// Treat zero-valued numeric defaults as unset, but preserve any non-zero
// numeric value as explicit poll intent so invalid durations still hit
// the poll-only validation path.
if (typeof value === "number" && Number.isFinite(value) && value !== 0) {
return true;
}
if (typeof value === "string") {
const trimmed = value.trim();
const parsed = parseStrictFiniteNumber(trimmed);
if (parsed !== undefined && parsed !== 0) {
return true;
}
}
}
if (def.kind === "boolean") {
if (value === true) {
return true;
}
if (typeof value === "string" && normalizeLowercaseStringOrEmpty(value) === "true") {
return true;
}
}
}
for (const [key, value] of Object.entries(params)) {
if (isChannelPollCreationParamName(key) && hasExplicitUnknownPollValue(key, value)) {
return true;
}
}
return false;
}