refactor(channels): centralize match metadata

This commit is contained in:
Peter Steinberger
2026-01-21 18:21:19 +00:00
parent b52ab96e2c
commit 88d76d4be5
11 changed files with 73 additions and 38 deletions

View File

@@ -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: {

View File

@@ -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})`,