refactor(channels): centralize match metadata
This commit is contained in:
@@ -16,6 +16,8 @@ export type AllowlistMatch<TSource extends string = AllowlistMatchSource> = {
|
||||
matchSource?: TSource;
|
||||
};
|
||||
|
||||
export function formatAllowlistMatchMeta(match?: AllowlistMatch | null): string {
|
||||
export function formatAllowlistMatchMeta(
|
||||
match?: { matchKey?: string; matchSource?: string } | null,
|
||||
): string {
|
||||
return `matchKey=${match?.matchKey ?? "none"} matchSource=${match?.matchSource ?? "none"}`;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import {
|
||||
resolveChannelEntryMatch,
|
||||
resolveChannelEntryMatchWithFallback,
|
||||
resolveNestedAllowlistDecision,
|
||||
applyChannelMatchMeta,
|
||||
resolveChannelMatchConfig,
|
||||
} from "./channel-config.js";
|
||||
|
||||
describe("buildChannelKeyCandidates", () => {
|
||||
@@ -90,6 +92,33 @@ describe("resolveChannelEntryMatchWithFallback", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyChannelMatchMeta", () => {
|
||||
it("copies match metadata onto resolved configs", () => {
|
||||
const resolved = applyChannelMatchMeta(
|
||||
{ allowed: true },
|
||||
{ matchKey: "general", matchSource: "direct" },
|
||||
);
|
||||
expect(resolved.matchKey).toBe("general");
|
||||
expect(resolved.matchSource).toBe("direct");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveChannelMatchConfig", () => {
|
||||
it("returns null when no entry is matched", () => {
|
||||
const resolved = resolveChannelMatchConfig({ matchKey: "x" }, () => ({ allowed: true }));
|
||||
expect(resolved).toBeNull();
|
||||
});
|
||||
|
||||
it("resolves entry and applies match metadata", () => {
|
||||
const resolved = resolveChannelMatchConfig(
|
||||
{ entry: { allow: true }, matchKey: "*", matchSource: "wildcard" },
|
||||
() => ({ allowed: true }),
|
||||
);
|
||||
expect(resolved?.matchKey).toBe("*");
|
||||
expect(resolved?.matchSource).toBe("wildcard");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveNestedAllowlistDecision", () => {
|
||||
it("allows when outer allowlist is disabled", () => {
|
||||
expect(
|
||||
|
||||
@@ -11,6 +11,24 @@ export type ChannelEntryMatch<T> = {
|
||||
matchSource?: ChannelMatchSource;
|
||||
};
|
||||
|
||||
export function applyChannelMatchMeta<
|
||||
TResult extends { matchKey?: string; matchSource?: ChannelMatchSource },
|
||||
>(result: TResult, match: ChannelEntryMatch<unknown>): TResult {
|
||||
if (match.matchKey && match.matchSource) {
|
||||
result.matchKey = match.matchKey;
|
||||
result.matchSource = match.matchSource;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function resolveChannelMatchConfig<
|
||||
TEntry,
|
||||
TResult extends { matchKey?: string; matchSource?: ChannelMatchSource },
|
||||
>(match: ChannelEntryMatch<TEntry>, resolveEntry: (entry: TEntry) => TResult): TResult | null {
|
||||
if (!match.entry) return null;
|
||||
return applyChannelMatchMeta(resolveEntry(match.entry), match);
|
||||
}
|
||||
|
||||
export function normalizeChannelSlug(value: string): string {
|
||||
return value
|
||||
.trim()
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
export type { ChannelEntryMatch, ChannelMatchSource } from "../channel-config.js";
|
||||
export {
|
||||
applyChannelMatchMeta,
|
||||
buildChannelKeyCandidates,
|
||||
normalizeChannelSlug,
|
||||
resolveChannelEntryMatch,
|
||||
resolveChannelEntryMatchWithFallback,
|
||||
resolveChannelMatchConfig,
|
||||
resolveNestedAllowlistDecision,
|
||||
} from "../channel-config.js";
|
||||
|
||||
@@ -60,10 +60,12 @@ export {
|
||||
listWhatsAppDirectoryPeersFromConfig,
|
||||
} from "./directory-config.js";
|
||||
export {
|
||||
applyChannelMatchMeta,
|
||||
buildChannelKeyCandidates,
|
||||
normalizeChannelSlug,
|
||||
resolveChannelEntryMatch,
|
||||
resolveChannelEntryMatchWithFallback,
|
||||
resolveChannelMatchConfig,
|
||||
resolveNestedAllowlistDecision,
|
||||
type ChannelEntryMatch,
|
||||
type ChannelMatchSource,
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { Guild, User } from "@buape/carbon";
|
||||
import {
|
||||
buildChannelKeyCandidates,
|
||||
resolveChannelEntryMatchWithFallback,
|
||||
resolveChannelMatchConfig,
|
||||
type ChannelMatchSource,
|
||||
} from "../../channels/channel-config.js";
|
||||
import type { AllowlistMatch } from "../../channels/allowlist-match.js";
|
||||
@@ -205,8 +206,6 @@ function resolveDiscordChannelEntryMatch(
|
||||
|
||||
function resolveDiscordChannelConfigEntry(
|
||||
entry: DiscordChannelEntry,
|
||||
matchKey: string | undefined,
|
||||
matchSource: ChannelMatchSource,
|
||||
): DiscordChannelConfigResolved {
|
||||
const resolved: DiscordChannelConfigResolved = {
|
||||
allowed: entry.allow !== false,
|
||||
@@ -217,8 +216,6 @@ function resolveDiscordChannelConfigEntry(
|
||||
systemPrompt: entry.systemPrompt,
|
||||
autoThread: entry.autoThread,
|
||||
};
|
||||
if (matchKey) resolved.matchKey = matchKey;
|
||||
resolved.matchSource = matchSource;
|
||||
return resolved;
|
||||
}
|
||||
|
||||
@@ -236,8 +233,8 @@ export function resolveDiscordChannelConfig(params: {
|
||||
name: channelName,
|
||||
slug: channelSlug,
|
||||
});
|
||||
if (!match.entry || !match.matchKey || !match.matchSource) return { allowed: false };
|
||||
return resolveDiscordChannelConfigEntry(match.entry, match.matchKey, match.matchSource);
|
||||
const resolved = resolveChannelMatchConfig(match, resolveDiscordChannelConfigEntry);
|
||||
return resolved ?? { allowed: false };
|
||||
}
|
||||
|
||||
export function resolveDiscordChannelConfigWithFallback(params: {
|
||||
@@ -279,10 +276,7 @@ export function resolveDiscordChannelConfigWithFallback(params: {
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
if (match.entry && match.matchKey && match.matchSource) {
|
||||
return resolveDiscordChannelConfigEntry(match.entry, match.matchKey, match.matchSource);
|
||||
}
|
||||
return { allowed: false };
|
||||
return resolveChannelMatchConfig(match, resolveDiscordChannelConfigEntry) ?? { allowed: false };
|
||||
}
|
||||
|
||||
export function resolveDiscordShouldRequireMention(params: {
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
} from "../../pairing/pairing-store.js";
|
||||
import { resolveAgentRoute } from "../../routing/resolve-route.js";
|
||||
import { resolveMentionGatingWithBypass } from "../../channels/mention-gating.js";
|
||||
import { formatAllowlistMatchMeta } from "../../channels/allowlist-match.js";
|
||||
import { sendMessageDiscord } from "../send.js";
|
||||
import { resolveControlCommandGate } from "../../channels/command-gating.js";
|
||||
import {
|
||||
@@ -100,9 +101,7 @@ export async function preflightDiscordMessage(
|
||||
},
|
||||
})
|
||||
: { allowed: false };
|
||||
const allowMatchMeta = `matchKey=${allowMatch.matchKey ?? "none"} matchSource=${
|
||||
allowMatch.matchSource ?? "none"
|
||||
}`;
|
||||
const allowMatchMeta = formatAllowlistMatchMeta(allowMatch);
|
||||
const permitted = allowMatch.allowed;
|
||||
if (!permitted) {
|
||||
commandAuthorized = false;
|
||||
@@ -262,9 +261,7 @@ export async function preflightDiscordMessage(
|
||||
scope: threadChannel ? "thread" : "channel",
|
||||
})
|
||||
: null;
|
||||
const channelMatchMeta = `matchKey=${channelConfig?.matchKey ?? "none"} matchSource=${
|
||||
channelConfig?.matchSource ?? "none"
|
||||
}`;
|
||||
const channelMatchMeta = formatAllowlistMatchMeta(channelConfig);
|
||||
if (isGuildMessage && channelConfig?.enabled === false) {
|
||||
logVerbose(
|
||||
`Blocked discord channel ${message.channelId} (channel disabled, ${channelMatchMeta})`,
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import type { SlackReactionNotificationMode } from "../../config/config.js";
|
||||
import type { SlackMessageEvent } from "../types.js";
|
||||
import {
|
||||
applyChannelMatchMeta,
|
||||
buildChannelKeyCandidates,
|
||||
resolveChannelEntryMatchWithFallback,
|
||||
type ChannelMatchSource,
|
||||
} from "../../channels/channel-config.js";
|
||||
import { allowListMatches, normalizeAllowListLower, normalizeSlackSlug } from "./allow-list.js";
|
||||
|
||||
@@ -14,7 +16,7 @@ export type SlackChannelConfigResolved = {
|
||||
skills?: string[];
|
||||
systemPrompt?: string;
|
||||
matchKey?: string;
|
||||
matchSource?: "direct" | "wildcard";
|
||||
matchSource?: ChannelMatchSource;
|
||||
};
|
||||
|
||||
function firstDefined<T>(...values: Array<T | undefined>) {
|
||||
@@ -89,16 +91,12 @@ export function resolveSlackChannelConfig(params: {
|
||||
directName,
|
||||
normalizedName,
|
||||
);
|
||||
const {
|
||||
entry: matched,
|
||||
wildcardEntry: fallback,
|
||||
matchKey,
|
||||
matchSource,
|
||||
} = resolveChannelEntryMatchWithFallback({
|
||||
const match = resolveChannelEntryMatchWithFallback({
|
||||
entries,
|
||||
keys: candidates,
|
||||
wildcardKey: "*",
|
||||
});
|
||||
const { entry: matched, wildcardEntry: fallback } = match;
|
||||
|
||||
const requireMentionDefault = defaultRequireMention ?? true;
|
||||
if (keys.length === 0) {
|
||||
@@ -127,11 +125,7 @@ export function resolveSlackChannelConfig(params: {
|
||||
skills,
|
||||
systemPrompt,
|
||||
};
|
||||
if (matchKey) result.matchKey = matchKey;
|
||||
if (matchSource === "direct" || matchSource === "wildcard") {
|
||||
result.matchSource = matchSource;
|
||||
}
|
||||
return result;
|
||||
return applyChannelMatchMeta(result, match);
|
||||
}
|
||||
|
||||
export type { SlackMessageEvent };
|
||||
|
||||
@@ -8,6 +8,7 @@ import { createDedupeCache } from "../../infra/dedupe.js";
|
||||
import { getChildLogger } from "../../logging.js";
|
||||
import type { RuntimeEnv } from "../../runtime.js";
|
||||
import type { SlackMessageEvent } from "../types.js";
|
||||
import { formatAllowlistMatchMeta } from "../../channels/allowlist-match.js";
|
||||
|
||||
import { normalizeAllowList, normalizeAllowListLower, normalizeSlackSlug } from "./allow-list.js";
|
||||
import { resolveSlackChannelConfig } from "./channel-config.js";
|
||||
@@ -310,9 +311,7 @@ export function createSlackMonitorContext(params: {
|
||||
channels: params.channelsConfig,
|
||||
defaultRequireMention,
|
||||
});
|
||||
const channelMatchMeta = `matchKey=${channelConfig?.matchKey ?? "none"} matchSource=${
|
||||
channelConfig?.matchSource ?? "none"
|
||||
}`;
|
||||
const channelMatchMeta = formatAllowlistMatchMeta(channelConfig);
|
||||
const channelAllowed = channelConfig?.allowed !== false;
|
||||
const channelAllowlistConfigured =
|
||||
Boolean(params.channelsConfig) && Object.keys(params.channelsConfig ?? {}).length > 0;
|
||||
|
||||
@@ -22,6 +22,7 @@ import { resolveThreadSessionKeys } from "../../../routing/session-key.js";
|
||||
import { resolveMentionGatingWithBypass } from "../../../channels/mention-gating.js";
|
||||
import { resolveConversationLabel } from "../../../channels/conversation-label.js";
|
||||
import { resolveControlCommandGate } from "../../../channels/command-gating.js";
|
||||
import { formatAllowlistMatchMeta } from "../../../channels/allowlist-match.js";
|
||||
import {
|
||||
readSessionUpdatedAt,
|
||||
recordSessionMetaFromInbound,
|
||||
@@ -131,9 +132,7 @@ export async function prepareSlackMessage(params: {
|
||||
allowList: allowFromLower,
|
||||
id: directUserId,
|
||||
});
|
||||
const allowMatchMeta = `matchKey=${allowMatch.matchKey ?? "none"} matchSource=${
|
||||
allowMatch.matchSource ?? "none"
|
||||
}`;
|
||||
const allowMatchMeta = formatAllowlistMatchMeta(allowMatch);
|
||||
if (!allowMatch.allowed) {
|
||||
if (ctx.dmPolicy === "pairing") {
|
||||
const sender = await ctx.resolveUserName(directUserId);
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
import { resolveAgentRoute } from "../../routing/resolve-route.js";
|
||||
import { resolveConversationLabel } from "../../channels/conversation-label.js";
|
||||
import { resolveCommandAuthorizedFromAuthorizers } from "../../channels/command-gating.js";
|
||||
import { formatAllowlistMatchMeta } from "../../channels/allowlist-match.js";
|
||||
|
||||
import type { ResolvedSlackAccount } from "../accounts.js";
|
||||
|
||||
@@ -206,9 +207,7 @@ export function registerSlackMonitorSlashCommands(params: {
|
||||
id: command.user_id,
|
||||
name: senderName,
|
||||
});
|
||||
const allowMatchMeta = `matchKey=${allowMatch.matchKey ?? "none"} matchSource=${
|
||||
allowMatch.matchSource ?? "none"
|
||||
}`;
|
||||
const allowMatchMeta = formatAllowlistMatchMeta(allowMatch);
|
||||
if (!allowMatch.allowed) {
|
||||
if (ctx.dmPolicy === "pairing") {
|
||||
const { code, created } = await upsertChannelPairingRequest({
|
||||
|
||||
Reference in New Issue
Block a user