Files
openclaw/src/channels/mention-pattern-policy.ts
Peter Steinberger 77f1359612 refactor: extract media and ACP core packages (#88534)
* refactor: extract media and acp core packages

* refactor: remove relocated media and acp sources

* build: wire new core packages into dependency checks

* test: alias new core packages in vitest

* build: keep media sniffer runtime dependency

* docs: refresh plugin sdk api baseline

* fix: keep normalized proposal queries non-empty

* test: keep channel timer tests isolated

* fix: keep rebased plugin checks green

* fix: preserve sms numeric allowlist entries

* test: harden exec foreground timeout failure

* test: remove duplicate skill workshop assertion

* fix: remove channel config lint suppression

* test: refresh lint suppression allowlist
2026-05-31 11:30:33 +01:00

69 lines
2.4 KiB
TypeScript

import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import type { MentionPatternsMode, MentionPatternsPolicyConfig } from "../config/types.messages.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
export type ResolveMentionPatternPolicyParams = {
cfg?: OpenClawConfig;
provider?: string;
conversationId?: string | null;
providerPolicy?: MentionPatternsPolicyConfig;
agentId?: string;
};
export type ResolvedMentionPatternPolicy = {
effectiveMode: MentionPatternsMode;
allowMatched: boolean;
denyMatched: boolean;
enabled: boolean;
};
function normalizeIdList(values?: string[]): Set<string> {
const normalized = new Set<string>();
for (const value of values ?? []) {
const next = normalizeOptionalString(value);
if (next) {
normalized.add(next);
}
}
return normalized;
}
function isMentionPatternsPolicyConfig(value: unknown): value is MentionPatternsPolicyConfig {
return value != null && typeof value === "object" && !Array.isArray(value);
}
function isRecord(value: unknown): value is Record<string, unknown> {
return value != null && typeof value === "object" && !Array.isArray(value);
}
function resolveProviderMentionPatternsPolicy(
cfg: OpenClawConfig | undefined,
provider: string | undefined,
): MentionPatternsPolicyConfig | undefined {
if (!cfg || !provider) {
return undefined;
}
const channelConfig = cfg.channels?.[provider];
const policy = isRecord(channelConfig) ? channelConfig.mentionPatterns : undefined;
return isMentionPatternsPolicyConfig(policy) ? policy : undefined;
}
export function resolveMentionPatternPolicy(
params: ResolveMentionPatternPolicyParams,
): ResolvedMentionPatternPolicy {
const conversationId = normalizeOptionalString(params.conversationId ?? undefined) ?? undefined;
const providerPolicy =
params.providerPolicy ?? resolveProviderMentionPatternsPolicy(params.cfg, params.provider);
const effectiveMode =
providerPolicy?.mode === "allow" || providerPolicy?.mode === "deny"
? providerPolicy.mode
: "allow";
const allowMatched =
conversationId != null && normalizeIdList(providerPolicy?.allowIn).has(conversationId);
const denyMatched =
conversationId != null && normalizeIdList(providerPolicy?.denyIn).has(conversationId);
const enabled = effectiveMode === "allow" ? !denyMatched : allowMatched && !denyMatched;
return { effectiveMode, allowMatched, denyMatched, enabled };
}