refactor: dedupe auto-reply string helpers

This commit is contained in:
Peter Steinberger
2026-04-07 09:39:56 +01:00
parent 649de6d156
commit 85c75f6573
7 changed files with 23 additions and 62 deletions

View File

@@ -11,23 +11,14 @@ const DEFAULT_BLOCK_STREAM_MIN = 800;
const DEFAULT_BLOCK_STREAM_MAX = 1200;
const DEFAULT_BLOCK_STREAM_COALESCE_IDLE_MS = 1000;
function normalizeChunkProvider(provider?: string): TextChunkProvider | undefined {
if (!provider) {
return undefined;
}
const normalized = normalizeMessageChannel(provider);
if (!normalized) {
return undefined;
}
return normalized as TextChunkProvider;
}
function resolveProviderChunkContext(
cfg: OpenClawConfig | undefined,
provider?: string,
accountId?: string | null,
) {
const providerKey = normalizeChunkProvider(provider);
const providerKey = provider
? (normalizeMessageChannel(provider) as TextChunkProvider | undefined)
: undefined;
const providerId = providerKey ? normalizeChannelId(providerKey) : null;
const providerChunkLimit = providerId
? getChannelPlugin(providerId)?.outbound?.textChunkLimit

View File

@@ -14,21 +14,8 @@ function loadGroupsRuntime() {
return groupsRuntimePromise;
}
function resolveGroupId(raw: string | undefined | null): string | undefined {
const trimmed = (raw ?? "").trim();
return extractExplicitGroupId(trimmed) ?? (trimmed || undefined);
}
function resolveLooseChannelId(raw?: string | null): string | null {
const normalized = normalizeOptionalString(raw)?.toLowerCase();
if (!normalized) {
return null;
}
return normalized;
}
async function resolveRuntimeChannelId(raw?: string | null): Promise<string | null> {
const normalized = resolveLooseChannelId(raw);
const normalized = normalizeOptionalString(raw)?.toLowerCase();
if (!normalized) {
return null;
}
@@ -58,7 +45,9 @@ export async function resolveGroupRequireMention(params: {
if (!channel) {
return true;
}
const groupId = groupResolution?.id ?? resolveGroupId(ctx.From);
const rawGroupId = (ctx.From ?? "").trim();
const groupId =
groupResolution?.id ?? extractExplicitGroupId(rawGroupId) ?? (rawGroupId || undefined);
const groupChannel =
normalizeOptionalString(ctx.GroupChannel) ?? normalizeOptionalString(ctx.GroupSubject);
const groupSpace = normalizeOptionalString(ctx.GroupSpace);

View File

@@ -18,9 +18,6 @@ const inboundDedupeCache: DedupeCache = resolveGlobalDedupeCache(INBOUND_DEDUPE_
maxSize: DEFAULT_INBOUND_DEDUPE_MAX,
});
const normalizeProvider = (value?: string | null) =>
normalizeOptionalString(value)?.toLowerCase() || "";
const resolveInboundPeerId = (ctx: MsgContext) =>
ctx.OriginatingTo ?? ctx.To ?? ctx.From ?? ctx.SessionKey;
@@ -44,7 +41,9 @@ function resolveInboundDedupeSessionScope(ctx: MsgContext): string {
}
export function buildInboundDedupeKey(ctx: MsgContext): string | null {
const provider = normalizeProvider(ctx.OriginatingChannel ?? ctx.Provider ?? ctx.Surface);
const provider =
normalizeOptionalString(ctx.OriginatingChannel ?? ctx.Provider ?? ctx.Surface)?.toLowerCase() ||
"";
const messageId = normalizeOptionalString(ctx.MessageSid);
if (!provider || !messageId) {
return null;

View File

@@ -1,17 +1,13 @@
import { normalizeOptionalString } from "../../shared/string-coerce.js";
import type { OriginatingChannelType } from "../templating.js";
function normalizeProviderValue(value?: string): string | undefined {
const normalized = normalizeOptionalString(value)?.toLowerCase();
return normalized || undefined;
}
export function resolveOriginMessageProvider(params: {
originatingChannel?: OriginatingChannelType;
provider?: string;
}): string | undefined {
return (
normalizeProviderValue(params.originatingChannel) ?? normalizeProviderValue(params.provider)
normalizeOptionalString(params.originatingChannel)?.toLowerCase() ??
normalizeOptionalString(params.provider)?.toLowerCase()
);
}

View File

@@ -23,20 +23,13 @@ function normalizeReplyToModeChatType(
: undefined;
}
function resolveReplyToModeChannelKey(channel?: OriginatingChannelType): string | undefined {
const normalized = normalizePluginChannelId(channel);
if (normalized) {
return normalized;
}
return normalizeOptionalString(channel)?.toLowerCase();
}
export function resolveConfiguredReplyToMode(
cfg: OpenClawConfig,
channel?: OriginatingChannelType,
chatType?: string | null,
): ReplyToMode {
const provider = resolveReplyToModeChannelKey(channel);
const provider =
normalizePluginChannelId(channel) ?? normalizeOptionalString(channel)?.toLowerCase();
if (!provider) {
return "all";
}

View File

@@ -45,18 +45,14 @@ function findRegisteredChannelPluginEntry(
function findRegisteredChannelPluginEntryById(
id: string,
): RegisteredChannelPluginEntry | undefined {
const normalizedId = normalizeChannelKey(id);
const normalizedId = normalizeOptionalString(id)?.toLowerCase();
if (!normalizedId) {
return undefined;
}
return listRegisteredChannelPluginEntries().find(
(entry) => normalizeChannelKey(entry.plugin.id) === normalizedId,
(entry) => normalizeOptionalString(entry.plugin.id)?.toLowerCase() === normalizedId,
);
}
const normalizeChannelKey = (raw?: string | null): string | undefined => {
return normalizeOptionalString(raw)?.toLowerCase();
};
export {
CHAT_CHANNEL_ALIASES,
getChatChannelMeta,
@@ -76,7 +72,7 @@ export function normalizeChannelId(raw?: string | null): ChatChannelId | null {
// Keep this light: we do not import channel plugins here (those are "heavy" and can pull in
// monitors, web login, etc). The plugin registry must be initialized first.
export function normalizeAnyChannelId(raw?: string | null): ChannelId | null {
const key = normalizeChannelKey(raw);
const key = normalizeOptionalString(raw)?.toLowerCase();
if (!key) {
return null;
}

View File

@@ -1,15 +1,12 @@
import { normalizeOptionalString } from "../shared/string-coerce.js";
import type { NodeRegistry } from "./node-registry.js";
const isMobilePlatform = (platform: unknown): boolean => {
const p = normalizeOptionalString(platform)?.toLowerCase() ?? "";
if (!p) {
return false;
}
return p.startsWith("ios") || p.startsWith("ipados") || p.startsWith("android");
};
export function hasConnectedMobileNode(registry: NodeRegistry): boolean {
const connected = registry.listConnected();
return connected.some((n) => isMobilePlatform(n.platform));
return connected.some((n) => {
const platform = normalizeOptionalString(n.platform)?.toLowerCase() ?? "";
return (
platform.startsWith("ios") || platform.startsWith("ipados") || platform.startsWith("android")
);
});
}