refactor: dedupe reply lowercase helpers

This commit is contained in:
Peter Steinberger
2026-04-07 10:48:12 +01:00
parent 3139d2007e
commit fbdb20ffd3
7 changed files with 22 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
import { logWarn } from "../logger.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
import {
describeHttpMcpServerLaunchConfig,
resolveHttpMcpServerLaunchConfig,
@@ -58,7 +59,7 @@ function getRequestedTransport(rawServer: unknown): string {
) {
return "";
}
return ((rawServer as { transport?: string }).transport ?? "").trim().toLowerCase();
return normalizeLowercaseStringOrEmpty((rawServer as { transport?: string }).transport);
}
function resolveHttpTransportConfig(

View File

@@ -1,5 +1,7 @@
import { normalizeLowercaseStringOrEmpty } from "../../shared/string-coerce.js";
function isGenericCompactionCancelledReason(reason: string): boolean {
const normalized = reason.trim().toLowerCase();
const normalized = normalizeLowercaseStringOrEmpty(reason);
return normalized === "compaction cancelled" || normalized === "error: compaction cancelled";
}
@@ -14,7 +16,7 @@ export function resolveCompactionFailureReason(params: {
}
export function classifyCompactionReason(reason?: string): string {
const text = (reason ?? "").trim().toLowerCase();
const text = normalizeLowercaseStringOrEmpty(reason);
if (!text) {
return "unknown";
}

View File

@@ -1,5 +1,6 @@
import type { OpenClawConfig } from "../../config/config.js";
import { isSubagentSessionKey, resolveAgentIdFromSessionKey } from "../../routing/session-key.js";
import { normalizeLowercaseStringOrEmpty } from "../../shared/string-coerce.js";
import {
listSpawnedSessionKeys,
resolveInternalSessionKey,
@@ -23,7 +24,7 @@ export type SessionAccessResult =
export function resolveSessionToolsVisibility(cfg: OpenClawConfig): SessionToolsVisibility {
const raw = (cfg.tools as { sessions?: { visibility?: unknown } } | undefined)?.sessions
?.visibility;
const value = typeof raw === "string" ? raw.trim().toLowerCase() : "";
const value = normalizeLowercaseStringOrEmpty(raw);
if (value === "self" || value === "tree" || value === "agent" || value === "all") {
return value;
}

View File

@@ -2,6 +2,7 @@ import { getChannelPlugin, listChannelPlugins } from "../channels/plugins/index.
import type { ChannelId, ChannelPlugin } from "../channels/plugins/types.js";
import { normalizeAnyChannelId } from "../channels/registry.js";
import type { OpenClawConfig } from "../config/config.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
import { normalizeStringEntries } from "../shared/string-normalization.js";
import {
INTERNAL_MESSAGE_CHANNEL,
@@ -466,7 +467,7 @@ function shouldUseFromAsSenderFallback(params: {
if (!from) {
return false;
}
const chatType = (params.chatType ?? "").trim().toLowerCase();
const chatType = normalizeLowercaseStringOrEmpty(params.chatType);
if (chatType && chatType !== "direct") {
return false;
}

View File

@@ -1,4 +1,5 @@
import type { OpenClawConfig } from "../../config/config.js";
import { normalizeLowercaseStringOrEmpty } from "../../shared/string-coerce.js";
import { resolveCommandAuthorization } from "../command-auth.js";
import { normalizeCommandBody } from "../commands-registry.js";
import type { MsgContext } from "../templating.js";
@@ -20,8 +21,8 @@ export function buildCommandContext(params: {
cfg,
commandAuthorized: params.commandAuthorized,
});
const surface = (ctx.Surface ?? ctx.Provider ?? "").trim().toLowerCase();
const channel = (ctx.Provider ?? surface).trim().toLowerCase();
const surface = normalizeLowercaseStringOrEmpty(ctx.Surface ?? ctx.Provider);
const channel = normalizeLowercaseStringOrEmpty(ctx.Provider ?? surface);
const abortKey = sessionKey ?? (auth.from || undefined) ?? (auth.to || undefined);
const rawBodyNormalized = triggerBodyNormalized;
const commandBodyNormalized = normalizeCommandBody(

View File

@@ -1,7 +1,10 @@
import type { SessionEntry } from "../../config/sessions.js";
import { buildAgentMainSessionKey } from "../../routing/session-key.js";
import { parseAgentSessionKey } from "../../sessions/session-key-utils.js";
import { normalizeOptionalString } from "../../shared/string-coerce.js";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalString,
} from "../../shared/string-coerce.js";
import {
deliveryContextFromSession,
deliveryContextKey,
@@ -34,9 +37,9 @@ function resolveSessionKeyChannelHint(sessionKey?: string): string | undefined {
function isMainSessionKey(sessionKey?: string): boolean {
const parsed = parseAgentSessionKey(sessionKey);
if (!parsed) {
return (sessionKey ?? "").trim().toLowerCase() === "main";
return normalizeLowercaseStringOrEmpty(sessionKey) === "main";
}
return parsed.rest.trim().toLowerCase() === "main";
return normalizeLowercaseStringOrEmpty(parsed.rest) === "main";
}
const DIRECT_SESSION_MARKERS = new Set(["direct", "dm"]);
@@ -59,7 +62,7 @@ function hasStrictDirectSessionTail(parts: string[], markerIndex: number): boole
}
function isDirectSessionKey(sessionKey?: string): boolean {
const raw = (sessionKey ?? "").trim().toLowerCase();
const raw = normalizeLowercaseStringOrEmpty(sessionKey);
if (!raw) {
return false;
}

View File

@@ -1,4 +1,5 @@
import { formatToolSummary, resolveToolDisplay } from "../agents/tool-display.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
import { shortenHomeInString, shortenHomePath } from "../utils.js";
type ToolAggregateOptions = {
@@ -80,7 +81,7 @@ function formatMetaForDisplay(
meta: string,
markdown?: boolean,
): string {
const normalized = (toolName ?? "").trim().toLowerCase();
const normalized = normalizeLowercaseStringOrEmpty(toolName);
if (normalized === "exec" || normalized === "bash") {
const { flags, body } = splitExecFlags(meta);
if (flags.length > 0) {