refactor: unify inbound debounce policy and split gateway/models helpers

This commit is contained in:
Peter Steinberger
2026-03-03 00:54:28 +00:00
parent 7de4204e57
commit 47083460ea
13 changed files with 415 additions and 192 deletions

View File

@@ -1,9 +1,8 @@
import type { Client } from "@buape/carbon";
import { hasControlCommand } from "../../auto-reply/command-detection.js";
import {
createInboundDebouncer,
resolveInboundDebounceMs,
} from "../../auto-reply/inbound-debounce.js";
createChannelInboundDebouncer,
shouldDebounceTextInbound,
} from "../../channels/inbound-debounce-policy.js";
import { resolveOpenProviderRuntimeGroupPolicy } from "../../config/runtime-group-policy.js";
import { danger } from "../../globals.js";
import type { DiscordMessageEvent, DiscordMessageHandler } from "./listeners.js";
@@ -33,10 +32,12 @@ export function createDiscordMessageHandler(
params.discordConfig?.ackReactionScope ??
params.cfg.messages?.ackReactionScope ??
"group-mentions";
const debounceMs = resolveInboundDebounceMs({ cfg: params.cfg, channel: "discord" });
const debouncer = createInboundDebouncer<{ data: DiscordMessageEvent; client: Client }>({
debounceMs,
const { debouncer } = createChannelInboundDebouncer<{
data: DiscordMessageEvent;
client: Client;
}>({
cfg: params.cfg,
channel: "discord",
buildKey: (entry) => {
const message = entry.data.message;
const authorId = entry.data.author?.id;
@@ -57,17 +58,15 @@ export function createDiscordMessageHandler(
if (!message) {
return false;
}
if (message.attachments && message.attachments.length > 0) {
return false;
}
if (hasDiscordMessageStickers(message)) {
return false;
}
const baseText = resolveDiscordMessageText(message, { includeForwarded: false });
if (!baseText.trim()) {
return false;
}
return !hasControlCommand(baseText, params.cfg);
return shouldDebounceTextInbound({
text: baseText,
cfg: params.cfg,
hasMedia: Boolean(
(message.attachments && message.attachments.length > 0) ||
hasDiscordMessageStickers(message),
),
});
},
onFlush: async (entries) => {
const last = entries.at(-1);