refactor: share allowFrom stringification helpers

This commit is contained in:
Peter Steinberger
2026-03-07 23:24:45 +00:00
parent 99d14a820a
commit c5bd84309a
7 changed files with 16 additions and 16 deletions

View File

@@ -8,6 +8,7 @@ import {
logAckFailure, logAckFailure,
logInboundDrop, logInboundDrop,
logTypingFailure, logTypingFailure,
mapAllowFromEntries,
readStoreAllowFromForDmPolicy, readStoreAllowFromForDmPolicy,
recordPendingHistoryEntryIfEnabled, recordPendingHistoryEntryIfEnabled,
resolveAckReaction, resolveAckReaction,
@@ -510,7 +511,7 @@ export async function processMessage(
const dmPolicy = account.config.dmPolicy ?? "pairing"; const dmPolicy = account.config.dmPolicy ?? "pairing";
const groupPolicy = account.config.groupPolicy ?? "allowlist"; const groupPolicy = account.config.groupPolicy ?? "allowlist";
const configuredAllowFrom = (account.config.allowFrom ?? []).map((entry) => String(entry)); const configuredAllowFrom = mapAllowFromEntries(account.config.allowFrom);
const storeAllowFrom = await readStoreAllowFromForDmPolicy({ const storeAllowFrom = await readStoreAllowFromForDmPolicy({
provider: "bluebubbles", provider: "bluebubbles",
accountId: account.accountId, accountId: account.accountId,

View File

@@ -159,10 +159,8 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
if (groupPolicy !== "open") { if (groupPolicy !== "open") {
return []; return [];
} }
const explicitGroupAllowFrom = (account.config.groupAllowFrom ?? []).map((entry) => const explicitGroupAllowFrom = mapAllowFromEntries(account.config.groupAllowFrom);
String(entry), const dmAllowFrom = mapAllowFromEntries(account.config.allowFrom);
);
const dmAllowFrom = (account.config.allowFrom ?? []).map((entry) => String(entry));
const effectiveAllowFrom = const effectiveAllowFrom =
explicitGroupAllowFrom.length > 0 ? explicitGroupAllowFrom : dmAllowFrom; explicitGroupAllowFrom.length > 0 ? explicitGroupAllowFrom : dmAllowFrom;
if (effectiveAllowFrom.length > 0) { if (effectiveAllowFrom.length > 0) {

View File

@@ -1,3 +1,4 @@
import { mapAllowFromEntries } from "../../plugin-sdk/channel-config-helpers.js";
import type { RuntimeEnv } from "../../runtime.js"; import type { RuntimeEnv } from "../../runtime.js";
export type AllowlistUserResolutionLike = { export type AllowlistUserResolutionLike = {
@@ -28,10 +29,7 @@ export function mergeAllowlist(params: {
existing?: Array<string | number>; existing?: Array<string | number>;
additions: string[]; additions: string[];
}): string[] { }): string[] {
return dedupeAllowlistEntries([ return dedupeAllowlistEntries([...mapAllowFromEntries(params.existing), ...params.additions]);
...(params.existing ?? []).map((entry) => String(entry)),
...params.additions,
]);
} }
export function buildAllowlistResolutionSummary<T extends AllowlistUserResolutionLike>( export function buildAllowlistResolutionSummary<T extends AllowlistUserResolutionLike>(

View File

@@ -1,5 +1,6 @@
import type { OpenClawConfig } from "../../config/types.js"; import type { OpenClawConfig } from "../../config/types.js";
import { inspectDiscordAccount } from "../../discord/account-inspect.js"; import { inspectDiscordAccount } from "../../discord/account-inspect.js";
import { mapAllowFromEntries } from "../../plugin-sdk/channel-config-helpers.js";
import { inspectSlackAccount } from "../../slack/account-inspect.js"; import { inspectSlackAccount } from "../../slack/account-inspect.js";
import { inspectTelegramAccount } from "../../telegram/account-inspect.js"; import { inspectTelegramAccount } from "../../telegram/account-inspect.js";
import { resolveWhatsAppAccount } from "../../web/accounts.js"; import { resolveWhatsAppAccount } from "../../web/accounts.js";
@@ -161,7 +162,7 @@ export async function listTelegramDirectoryPeersFromConfig(
): Promise<ChannelDirectoryEntry[]> { ): Promise<ChannelDirectoryEntry[]> {
const account = inspectTelegramAccount({ cfg: params.cfg, accountId: params.accountId }); const account = inspectTelegramAccount({ cfg: params.cfg, accountId: params.accountId });
const raw = [ const raw = [
...(account.config.allowFrom ?? []).map((entry) => String(entry)), ...mapAllowFromEntries(account.config.allowFrom),
...Object.keys(account.config.dms ?? {}), ...Object.keys(account.config.dms ?? {}),
]; ];
const ids = Array.from( const ids = Array.from(

View File

@@ -5,6 +5,7 @@ import type { OpenClawConfig } from "../../config/config.js";
import type { SessionEntry } from "../../config/sessions.js"; import type { SessionEntry } from "../../config/sessions.js";
import type { AgentDefaultsConfig } from "../../config/types.agent-defaults.js"; import type { AgentDefaultsConfig } from "../../config/types.agent-defaults.js";
import { parseDiscordTarget } from "../../discord/targets.js"; import { parseDiscordTarget } from "../../discord/targets.js";
import { mapAllowFromEntries } from "../../plugin-sdk/channel-config-helpers.js";
import { normalizeAccountId } from "../../routing/session-key.js"; import { normalizeAccountId } from "../../routing/session-key.js";
import { parseSlackTarget } from "../../slack/targets.js"; import { parseSlackTarget } from "../../slack/targets.js";
import { parseTelegramTarget, resolveTelegramTargetChatType } from "../../telegram/targets.js"; import { parseTelegramTarget, resolveTelegramTargetChatType } from "../../telegram/targets.js";
@@ -203,7 +204,7 @@ export function resolveOutboundTarget(params: {
accountId: params.accountId ?? undefined, accountId: params.accountId ?? undefined,
}) })
: undefined); : undefined);
const allowFrom = allowFromRaw?.map((entry) => String(entry)); const allowFrom = allowFromRaw ? mapAllowFromEntries(allowFromRaw) : undefined;
// Fall back to per-channel defaultTo when no explicit target is provided. // Fall back to per-channel defaultTo when no explicit target is provided.
const effectiveTo = const effectiveTo =
@@ -496,9 +497,7 @@ function resolveHeartbeatSenderId(params: {
provider && lastTo ? `${provider}:${lastTo}` : undefined, provider && lastTo ? `${provider}:${lastTo}` : undefined,
].filter((val): val is string => Boolean(val?.trim())); ].filter((val): val is string => Boolean(val?.trim()));
const allowList = allowFrom const allowList = mapAllowFromEntries(allowFrom).filter((entry) => entry && entry !== "*");
.map((entry) => String(entry))
.filter((entry) => entry && entry !== "*");
if (allowFrom.includes("*")) { if (allowFrom.includes("*")) {
return candidates[0] ?? "heartbeat"; return candidates[0] ?? "heartbeat";
} }
@@ -536,7 +535,7 @@ export function resolveHeartbeatSenderContext(params: {
accountId, accountId,
}) ?? []) }) ?? [])
: []; : [];
const allowFrom = allowFromRaw.map((entry) => String(entry)); const allowFrom = mapAllowFromEntries(allowFromRaw);
const sender = resolveHeartbeatSenderId({ const sender = resolveHeartbeatSenderId({
allowFrom, allowFrom,

View File

@@ -1,3 +1,5 @@
import { mapAllowFromEntries } from "../plugin-sdk/channel-config-helpers.js";
export function normalizeNonEmptyString(value: unknown): string | null { export function normalizeNonEmptyString(value: unknown): string | null {
if (typeof value !== "string") { if (typeof value !== "string") {
return null; return null;
@@ -7,5 +9,5 @@ export function normalizeNonEmptyString(value: unknown): string | null {
} }
export function normalizeStringArray(value: unknown): string[] { export function normalizeStringArray(value: unknown): string[] {
return Array.isArray(value) ? value.map((entry) => String(entry)) : []; return Array.isArray(value) ? mapAllowFromEntries(value) : [];
} }

View File

@@ -88,6 +88,7 @@ export { formatDocsLink } from "../terminal/links.js";
export type { WizardPrompter } from "../wizard/prompts.js"; export type { WizardPrompter } from "../wizard/prompts.js";
export { isAllowedParsedChatSender } from "./allow-from.js"; export { isAllowedParsedChatSender } from "./allow-from.js";
export { readBooleanParam } from "./boolean-param.js"; export { readBooleanParam } from "./boolean-param.js";
export { mapAllowFromEntries } from "./channel-config-helpers.js";
export { createScopedPairingAccess } from "./pairing-access.js"; export { createScopedPairingAccess } from "./pairing-access.js";
export { issuePairingChallenge } from "../pairing/pairing-challenge.js"; export { issuePairingChallenge } from "../pairing/pairing-challenge.js";
export { resolveRequestUrl } from "./request-url.js"; export { resolveRequestUrl } from "./request-url.js";