refactor: unify pending history helpers

This commit is contained in:
Peter Steinberger
2026-01-23 22:36:43 +00:00
parent 05e7e06146
commit 521ea4ae5b
13 changed files with 196 additions and 155 deletions

View File

@@ -8,9 +8,9 @@ import type {
} from "clawdbot/plugin-sdk"; } from "clawdbot/plugin-sdk";
import { import {
buildPendingHistoryContextFromMap, buildPendingHistoryContextFromMap,
clearHistoryEntries, clearHistoryEntriesIfEnabled,
DEFAULT_GROUP_HISTORY_LIMIT, DEFAULT_GROUP_HISTORY_LIMIT,
recordPendingHistoryEntry, recordPendingHistoryEntryIfEnabled,
resolveChannelMediaMaxBytes, resolveChannelMediaMaxBytes,
type HistoryEntry, type HistoryEntry,
} from "clawdbot/plugin-sdk"; } from "clawdbot/plugin-sdk";
@@ -534,19 +534,19 @@ export async function monitorMattermostProvider(opts: MonitorMattermostOpts = {}
: ""); : "");
const pendingSender = senderName; const pendingSender = senderName;
const recordPendingHistory = () => { const recordPendingHistory = () => {
if (!historyKey || historyLimit <= 0) return;
const trimmed = pendingBody.trim(); const trimmed = pendingBody.trim();
if (!trimmed) return; recordPendingHistoryEntryIfEnabled({
recordPendingHistoryEntry({
historyMap: channelHistories, historyMap: channelHistories,
historyKey,
limit: historyLimit, limit: historyLimit,
entry: { historyKey: historyKey ?? "",
sender: pendingSender, entry: historyKey && trimmed
body: trimmed, ? {
timestamp: typeof post.create_at === "number" ? post.create_at : undefined, sender: pendingSender,
messageId: post.id ?? undefined, body: trimmed,
}, timestamp: typeof post.create_at === "number" ? post.create_at : undefined,
messageId: post.id ?? undefined,
}
: null,
}); });
}; };
@@ -623,7 +623,7 @@ export async function monitorMattermostProvider(opts: MonitorMattermostOpts = {}
sender: { name: senderName, id: senderId }, sender: { name: senderName, id: senderId },
}); });
let combinedBody = body; let combinedBody = body;
if (historyKey && historyLimit > 0) { if (historyKey) {
combinedBody = buildPendingHistoryContextFromMap({ combinedBody = buildPendingHistoryContextFromMap({
historyMap: channelHistories, historyMap: channelHistories,
historyKey, historyKey,
@@ -772,8 +772,8 @@ export async function monitorMattermostProvider(opts: MonitorMattermostOpts = {}
}, },
}); });
markDispatchIdle(); markDispatchIdle();
if (historyKey && historyLimit > 0) { if (historyKey) {
clearHistoryEntries({ historyMap: channelHistories, historyKey }); clearHistoryEntriesIfEnabled({ historyMap: channelHistories, historyKey, limit: historyLimit });
} }
}; };

View File

@@ -1,8 +1,8 @@
import { import {
buildPendingHistoryContextFromMap, buildPendingHistoryContextFromMap,
clearHistoryEntries, clearHistoryEntriesIfEnabled,
DEFAULT_GROUP_HISTORY_LIMIT, DEFAULT_GROUP_HISTORY_LIMIT,
recordPendingHistoryEntry, recordPendingHistoryEntryIfEnabled,
resolveMentionGating, resolveMentionGating,
formatAllowlistMatchMeta, formatAllowlistMatchMeta,
type HistoryEntry, type HistoryEntry,
@@ -371,19 +371,17 @@ export function createMSTeamsMessageHandler(deps: MSTeamsMessageHandlerDeps) {
requireMention, requireMention,
mentioned, mentioned,
}); });
if (historyLimit > 0) { recordPendingHistoryEntryIfEnabled({
recordPendingHistoryEntry({ historyMap: conversationHistories,
historyMap: conversationHistories, historyKey: conversationId,
historyKey: conversationId, limit: historyLimit,
limit: historyLimit, entry: {
entry: { sender: senderName,
sender: senderName, body: rawBody,
body: rawBody, timestamp: timestamp?.getTime(),
timestamp: timestamp?.getTime(), messageId: activity.id ?? undefined,
messageId: activity.id ?? undefined, },
}, });
});
}
return; return;
} }
} }
@@ -426,7 +424,7 @@ export function createMSTeamsMessageHandler(deps: MSTeamsMessageHandlerDeps) {
let combinedBody = body; let combinedBody = body;
const isRoomish = !isDirectMessage; const isRoomish = !isDirectMessage;
const historyKey = isRoomish ? conversationId : undefined; const historyKey = isRoomish ? conversationId : undefined;
if (isRoomish && historyKey && historyLimit > 0) { if (isRoomish && historyKey) {
combinedBody = buildPendingHistoryContextFromMap({ combinedBody = buildPendingHistoryContextFromMap({
historyMap: conversationHistories, historyMap: conversationHistories,
historyKey, historyKey,
@@ -512,10 +510,11 @@ export function createMSTeamsMessageHandler(deps: MSTeamsMessageHandlerDeps) {
const didSendReply = counts.final + counts.tool + counts.block > 0; const didSendReply = counts.final + counts.tool + counts.block > 0;
if (!queuedFinal) { if (!queuedFinal) {
if (isRoomish && historyKey && historyLimit > 0) { if (isRoomish && historyKey) {
clearHistoryEntries({ clearHistoryEntriesIfEnabled({
historyMap: conversationHistories, historyMap: conversationHistories,
historyKey, historyKey,
limit: historyLimit,
}); });
} }
return; return;
@@ -524,8 +523,12 @@ export function createMSTeamsMessageHandler(deps: MSTeamsMessageHandlerDeps) {
logVerboseMessage( logVerboseMessage(
`msteams: delivered ${finalCount} reply${finalCount === 1 ? "" : "ies"} to ${teamsTo}`, `msteams: delivered ${finalCount} reply${finalCount === 1 ? "" : "ies"} to ${teamsTo}`,
); );
if (isRoomish && historyKey && historyLimit > 0) { if (isRoomish && historyKey) {
clearHistoryEntries({ historyMap: conversationHistories, historyKey }); clearHistoryEntriesIfEnabled({
historyMap: conversationHistories,
historyKey,
limit: historyLimit,
});
} }
} catch (err) { } catch (err) {
log.error("dispatch failed", { error: String(err) }); log.error("dispatch failed", { error: String(err) });

View File

@@ -47,6 +47,22 @@ export function recordPendingHistoryEntry<T extends HistoryEntry>(params: {
return appendHistoryEntry(params); return appendHistoryEntry(params);
} }
export function recordPendingHistoryEntryIfEnabled<T extends HistoryEntry>(params: {
historyMap: Map<string, T[]>;
historyKey: string;
entry?: T | null;
limit: number;
}): T[] {
if (!params.entry) return [];
if (params.limit <= 0) return [];
return recordPendingHistoryEntry({
historyMap: params.historyMap,
historyKey: params.historyKey,
entry: params.entry,
limit: params.limit,
});
}
export function buildPendingHistoryContextFromMap(params: { export function buildPendingHistoryContextFromMap(params: {
historyMap: Map<string, HistoryEntry[]>; historyMap: Map<string, HistoryEntry[]>;
historyKey: string; historyKey: string;
@@ -101,6 +117,15 @@ export function clearHistoryEntries(params: {
params.historyMap.set(params.historyKey, []); params.historyMap.set(params.historyKey, []);
} }
export function clearHistoryEntriesIfEnabled(params: {
historyMap: Map<string, HistoryEntry[]>;
historyKey: string;
limit: number;
}): void {
if (params.limit <= 0) return;
clearHistoryEntries({ historyMap: params.historyMap, historyKey: params.historyKey });
}
export function buildHistoryContextFromEntries(params: { export function buildHistoryContextFromEntries(params: {
entries: HistoryEntry[]; entries: HistoryEntry[];
currentMessage: string; currentMessage: string;

View File

@@ -2,7 +2,10 @@ import { ChannelType, MessageType, type User } from "@buape/carbon";
import { hasControlCommand } from "../../auto-reply/command-detection.js"; import { hasControlCommand } from "../../auto-reply/command-detection.js";
import { shouldHandleTextCommands } from "../../auto-reply/commands-registry.js"; import { shouldHandleTextCommands } from "../../auto-reply/commands-registry.js";
import { recordPendingHistoryEntry, type HistoryEntry } from "../../auto-reply/reply/history.js"; import {
recordPendingHistoryEntryIfEnabled,
type HistoryEntry,
} from "../../auto-reply/reply/history.js";
import { buildMentionRegexes, matchesMentionPatterns } from "../../auto-reply/reply/mentions.js"; import { buildMentionRegexes, matchesMentionPatterns } from "../../auto-reply/reply/mentions.js";
import { logVerbose, shouldLogVerbose } from "../../globals.js"; import { logVerbose, shouldLogVerbose } from "../../globals.js";
import { recordChannelActivity } from "../../infra/channel-activity.js"; import { recordChannelActivity } from "../../infra/channel-activity.js";
@@ -410,14 +413,12 @@ export async function preflightDiscordMessage(
}, },
"discord: skipping guild message", "discord: skipping guild message",
); );
if (historyEntry && params.historyLimit > 0) { recordPendingHistoryEntryIfEnabled({
recordPendingHistoryEntry({ historyMap: params.guildHistories,
historyMap: params.guildHistories, historyKey: message.channelId,
historyKey: message.channelId, limit: params.historyLimit,
limit: params.historyLimit, entry: historyEntry ?? null,
entry: historyEntry, });
});
}
return null; return null;
} }
} }

View File

@@ -20,7 +20,7 @@ import {
import { dispatchInboundMessage } from "../../auto-reply/dispatch.js"; import { dispatchInboundMessage } from "../../auto-reply/dispatch.js";
import { import {
buildPendingHistoryContextFromMap, buildPendingHistoryContextFromMap,
clearHistoryEntries, clearHistoryEntriesIfEnabled,
} from "../../auto-reply/reply/history.js"; } from "../../auto-reply/reply/history.js";
import { finalizeInboundContext } from "../../auto-reply/reply/inbound-context.js"; import { finalizeInboundContext } from "../../auto-reply/reply/inbound-context.js";
import { createReplyDispatcherWithTyping } from "../../auto-reply/reply/reply-dispatcher.js"; import { createReplyDispatcherWithTyping } from "../../auto-reply/reply/reply-dispatcher.js";
@@ -383,10 +383,11 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
}); });
markDispatchIdle(); markDispatchIdle();
if (!queuedFinal) { if (!queuedFinal) {
if (isGuildMessage && historyLimit > 0) { if (isGuildMessage) {
clearHistoryEntries({ clearHistoryEntriesIfEnabled({
historyMap: guildHistories, historyMap: guildHistories,
historyKey: message.channelId, historyKey: message.channelId,
limit: historyLimit,
}); });
} }
return; return;
@@ -409,10 +410,11 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
); );
}, },
}); });
if (isGuildMessage && historyLimit > 0) { if (isGuildMessage) {
clearHistoryEntries({ clearHistoryEntriesIfEnabled({
historyMap: guildHistories, historyMap: guildHistories,
historyKey: message.channelId, historyKey: message.channelId,
limit: historyLimit,
}); });
} }
} }

View File

@@ -24,9 +24,9 @@ import { dispatchInboundMessage } from "../../auto-reply/dispatch.js";
import { finalizeInboundContext } from "../../auto-reply/reply/inbound-context.js"; import { finalizeInboundContext } from "../../auto-reply/reply/inbound-context.js";
import { import {
buildPendingHistoryContextFromMap, buildPendingHistoryContextFromMap,
clearHistoryEntries, clearHistoryEntriesIfEnabled,
DEFAULT_GROUP_HISTORY_LIMIT, DEFAULT_GROUP_HISTORY_LIMIT,
recordPendingHistoryEntry, recordPendingHistoryEntryIfEnabled,
type HistoryEntry, type HistoryEntry,
} from "../../auto-reply/reply/history.js"; } from "../../auto-reply/reply/history.js";
import { buildMentionRegexes, matchesMentionPatterns } from "../../auto-reply/reply/mentions.js"; import { buildMentionRegexes, matchesMentionPatterns } from "../../auto-reply/reply/mentions.js";
@@ -405,19 +405,19 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P
const effectiveWasMentioned = mentioned || shouldBypassMention; const effectiveWasMentioned = mentioned || shouldBypassMention;
if (isGroup && requireMention && canDetectMention && !mentioned && !shouldBypassMention) { if (isGroup && requireMention && canDetectMention && !mentioned && !shouldBypassMention) {
logVerbose(`imessage: skipping group message (no mention)`); logVerbose(`imessage: skipping group message (no mention)`);
if (historyKey && historyLimit > 0) { recordPendingHistoryEntryIfEnabled({
recordPendingHistoryEntry({ historyMap: groupHistories,
historyMap: groupHistories, historyKey: historyKey ?? "",
historyKey, limit: historyLimit,
limit: historyLimit, entry: historyKey
entry: { ? {
sender: senderNormalized, sender: senderNormalized,
body: bodyText, body: bodyText,
timestamp: createdAt, timestamp: createdAt,
messageId: message.id ? String(message.id) : undefined, messageId: message.id ? String(message.id) : undefined,
}, }
}); : null,
} });
return; return;
} }
@@ -454,7 +454,7 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P
envelope: envelopeOptions, envelope: envelopeOptions,
}); });
let combinedBody = body; let combinedBody = body;
if (isGroup && historyKey && historyLimit > 0) { if (isGroup && historyKey) {
combinedBody = buildPendingHistoryContextFromMap({ combinedBody = buildPendingHistoryContextFromMap({
historyMap: groupHistories, historyMap: groupHistories,
historyKey, historyKey,
@@ -584,13 +584,17 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P
}, },
}); });
if (!queuedFinal) { if (!queuedFinal) {
if (isGroup && historyKey && historyLimit > 0) { if (isGroup && historyKey) {
clearHistoryEntries({ historyMap: groupHistories, historyKey }); clearHistoryEntriesIfEnabled({
historyMap: groupHistories,
historyKey,
limit: historyLimit,
});
} }
return; return;
} }
if (isGroup && historyKey && historyLimit > 0) { if (isGroup && historyKey) {
clearHistoryEntries({ historyMap: groupHistories, historyKey }); clearHistoryEntriesIfEnabled({ historyMap: groupHistories, historyKey, limit: historyLimit });
} }
} }

View File

@@ -108,8 +108,10 @@ export { SILENT_REPLY_TOKEN, isSilentReplyText } from "../auto-reply/tokens.js";
export { export {
buildPendingHistoryContextFromMap, buildPendingHistoryContextFromMap,
clearHistoryEntries, clearHistoryEntries,
clearHistoryEntriesIfEnabled,
DEFAULT_GROUP_HISTORY_LIMIT, DEFAULT_GROUP_HISTORY_LIMIT,
recordPendingHistoryEntry, recordPendingHistoryEntry,
recordPendingHistoryEntryIfEnabled,
} from "../auto-reply/reply/history.js"; } from "../auto-reply/reply/history.js";
export type { HistoryEntry } from "../auto-reply/reply/history.js"; export type { HistoryEntry } from "../auto-reply/reply/history.js";
export { mergeAllowlist, summarizeMapping } from "../channels/allowlists/resolve-utils.js"; export { mergeAllowlist, summarizeMapping } from "../channels/allowlists/resolve-utils.js";

View File

@@ -20,7 +20,7 @@ import {
import { dispatchInboundMessage } from "../../auto-reply/dispatch.js"; import { dispatchInboundMessage } from "../../auto-reply/dispatch.js";
import { import {
buildPendingHistoryContextFromMap, buildPendingHistoryContextFromMap,
clearHistoryEntries, clearHistoryEntriesIfEnabled,
} from "../../auto-reply/reply/history.js"; } from "../../auto-reply/reply/history.js";
import { finalizeInboundContext } from "../../auto-reply/reply/inbound-context.js"; import { finalizeInboundContext } from "../../auto-reply/reply/inbound-context.js";
import { createReplyDispatcherWithTyping } from "../../auto-reply/reply/reply-dispatcher.js"; import { createReplyDispatcherWithTyping } from "../../auto-reply/reply/reply-dispatcher.js";
@@ -111,7 +111,7 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
}); });
let combinedBody = body; let combinedBody = body;
const historyKey = entry.isGroup ? String(entry.groupId ?? "unknown") : undefined; const historyKey = entry.isGroup ? String(entry.groupId ?? "unknown") : undefined;
if (entry.isGroup && historyKey && deps.historyLimit > 0) { if (entry.isGroup && historyKey) {
combinedBody = buildPendingHistoryContextFromMap({ combinedBody = buildPendingHistoryContextFromMap({
historyMap: deps.groupHistories, historyMap: deps.groupHistories,
historyKey, historyKey,
@@ -244,13 +244,21 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
}); });
markDispatchIdle(); markDispatchIdle();
if (!queuedFinal) { if (!queuedFinal) {
if (entry.isGroup && historyKey && deps.historyLimit > 0) { if (entry.isGroup && historyKey) {
clearHistoryEntries({ historyMap: deps.groupHistories, historyKey }); clearHistoryEntriesIfEnabled({
historyMap: deps.groupHistories,
historyKey,
limit: deps.historyLimit,
});
} }
return; return;
} }
if (entry.isGroup && historyKey && deps.historyLimit > 0) { if (entry.isGroup && historyKey) {
clearHistoryEntries({ historyMap: deps.groupHistories, historyKey }); clearHistoryEntriesIfEnabled({
historyMap: deps.groupHistories,
historyKey,
limit: deps.historyLimit,
});
} }
} }

View File

@@ -8,7 +8,7 @@ import {
type ResponsePrefixContext, type ResponsePrefixContext,
} from "../../../auto-reply/reply/response-prefix-template.js"; } from "../../../auto-reply/reply/response-prefix-template.js";
import { dispatchInboundMessage } from "../../../auto-reply/dispatch.js"; import { dispatchInboundMessage } from "../../../auto-reply/dispatch.js";
import { clearHistoryEntries } from "../../../auto-reply/reply/history.js"; import { clearHistoryEntriesIfEnabled } from "../../../auto-reply/reply/history.js";
import { removeAckReactionAfterReply } from "../../../channels/ack-reactions.js"; import { removeAckReactionAfterReply } from "../../../channels/ack-reactions.js";
import { createReplyDispatcherWithTyping } from "../../../auto-reply/reply/reply-dispatcher.js"; import { createReplyDispatcherWithTyping } from "../../../auto-reply/reply/reply-dispatcher.js";
import { resolveStorePath, updateLastRoute } from "../../../config/sessions.js"; import { resolveStorePath, updateLastRoute } from "../../../config/sessions.js";
@@ -137,10 +137,11 @@ export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessag
} }
if (!queuedFinal) { if (!queuedFinal) {
if (prepared.isRoomish && ctx.historyLimit > 0) { if (prepared.isRoomish) {
clearHistoryEntries({ clearHistoryEntriesIfEnabled({
historyMap: ctx.channelHistories, historyMap: ctx.channelHistories,
historyKey: prepared.historyKey, historyKey: prepared.historyKey,
limit: ctx.historyLimit,
}); });
} }
return; return;
@@ -174,10 +175,11 @@ export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessag
}, },
}); });
if (prepared.isRoomish && ctx.historyLimit > 0) { if (prepared.isRoomish) {
clearHistoryEntries({ clearHistoryEntriesIfEnabled({
historyMap: ctx.channelHistories, historyMap: ctx.channelHistories,
historyKey: prepared.historyKey, historyKey: prepared.historyKey,
limit: ctx.historyLimit,
}); });
} }
} }

View File

@@ -9,7 +9,7 @@ import {
} from "../../../auto-reply/envelope.js"; } from "../../../auto-reply/envelope.js";
import { import {
buildPendingHistoryContextFromMap, buildPendingHistoryContextFromMap,
recordPendingHistoryEntry, recordPendingHistoryEntryIfEnabled,
} from "../../../auto-reply/reply/history.js"; } from "../../../auto-reply/reply/history.js";
import { finalizeInboundContext } from "../../../auto-reply/reply/inbound-context.js"; import { finalizeInboundContext } from "../../../auto-reply/reply/inbound-context.js";
import { buildMentionRegexes, matchesMentionPatterns } from "../../../auto-reply/reply/mentions.js"; import { buildMentionRegexes, matchesMentionPatterns } from "../../../auto-reply/reply/mentions.js";
@@ -292,28 +292,26 @@ export async function prepareSlackMessage(params: {
const effectiveWasMentioned = mentionGate.effectiveWasMentioned; const effectiveWasMentioned = mentionGate.effectiveWasMentioned;
if (isRoom && shouldRequireMention && mentionGate.shouldSkip) { if (isRoom && shouldRequireMention && mentionGate.shouldSkip) {
ctx.logger.info({ channel: message.channel, reason: "no-mention" }, "skipping channel message"); ctx.logger.info({ channel: message.channel, reason: "no-mention" }, "skipping channel message");
if (ctx.historyLimit > 0) { const pendingText = (message.text ?? "").trim();
const pendingText = (message.text ?? "").trim(); const fallbackFile = message.files?.[0]?.name
const fallbackFile = message.files?.[0]?.name ? `[Slack file: ${message.files[0].name}]`
? `[Slack file: ${message.files[0].name}]` : message.files?.length
: message.files?.length ? "[Slack file]"
? "[Slack file]" : "";
: ""; const pendingBody = pendingText || fallbackFile;
const pendingBody = pendingText || fallbackFile; recordPendingHistoryEntryIfEnabled({
if (pendingBody) { historyMap: ctx.channelHistories,
recordPendingHistoryEntry({ historyKey,
historyMap: ctx.channelHistories, limit: ctx.historyLimit,
historyKey, entry: pendingBody
limit: ctx.historyLimit, ? {
entry: {
sender: senderName, sender: senderName,
body: pendingBody, body: pendingBody,
timestamp: message.ts ? Math.round(Number(message.ts) * 1000) : undefined, timestamp: message.ts ? Math.round(Number(message.ts) * 1000) : undefined,
messageId: message.ts, messageId: message.ts,
}, }
}); : null,
} });
}
return null; return null;
} }

View File

@@ -6,7 +6,7 @@ import { normalizeCommandBody } from "../auto-reply/commands-registry.js";
import { formatInboundEnvelope, resolveEnvelopeFormatOptions } from "../auto-reply/envelope.js"; import { formatInboundEnvelope, resolveEnvelopeFormatOptions } from "../auto-reply/envelope.js";
import { import {
buildPendingHistoryContextFromMap, buildPendingHistoryContextFromMap,
recordPendingHistoryEntry, recordPendingHistoryEntryIfEnabled,
type HistoryEntry, type HistoryEntry,
} from "../auto-reply/reply/history.js"; } from "../auto-reply/reply/history.js";
import { finalizeInboundContext } from "../auto-reply/reply/inbound-context.js"; import { finalizeInboundContext } from "../auto-reply/reply/inbound-context.js";
@@ -350,19 +350,19 @@ export const buildTelegramMessageContext = async ({
if (isGroup && requireMention && canDetectMention) { if (isGroup && requireMention && canDetectMention) {
if (mentionGate.shouldSkip) { if (mentionGate.shouldSkip) {
logger.info({ chatId, reason: "no-mention" }, "skipping group message"); logger.info({ chatId, reason: "no-mention" }, "skipping group message");
if (historyKey && historyLimit > 0) { recordPendingHistoryEntryIfEnabled({
recordPendingHistoryEntry({ historyMap: groupHistories,
historyMap: groupHistories, historyKey: historyKey ?? "",
historyKey, limit: historyLimit,
limit: historyLimit, entry: historyKey
entry: { ? {
sender: buildSenderLabel(msg, senderId || chatId), sender: buildSenderLabel(msg, senderId || chatId),
body: rawBody, body: rawBody,
timestamp: msg.date ? msg.date * 1000 : undefined, timestamp: msg.date ? msg.date * 1000 : undefined,
messageId: typeof msg.message_id === "number" ? String(msg.message_id) : undefined, messageId: typeof msg.message_id === "number" ? String(msg.message_id) : undefined,
}, }
}); : null,
} });
return null; return null;
} }
} }

View File

@@ -5,7 +5,7 @@ import {
type ResponsePrefixContext, type ResponsePrefixContext,
} from "../auto-reply/reply/response-prefix-template.js"; } from "../auto-reply/reply/response-prefix-template.js";
import { EmbeddedBlockChunker } from "../agents/pi-embedded-block-chunker.js"; import { EmbeddedBlockChunker } from "../agents/pi-embedded-block-chunker.js";
import { clearHistoryEntries } from "../auto-reply/reply/history.js"; import { clearHistoryEntriesIfEnabled } from "../auto-reply/reply/history.js";
import { dispatchReplyWithBufferedBlockDispatcher } from "../auto-reply/reply/provider-dispatcher.js"; import { dispatchReplyWithBufferedBlockDispatcher } from "../auto-reply/reply/provider-dispatcher.js";
import { removeAckReactionAfterReply } from "../channels/ack-reactions.js"; import { removeAckReactionAfterReply } from "../channels/ack-reactions.js";
import { danger, logVerbose } from "../globals.js"; import { danger, logVerbose } from "../globals.js";
@@ -180,8 +180,8 @@ export const dispatchTelegramMessage = async ({
}); });
draftStream?.stop(); draftStream?.stop();
if (!queuedFinal) { if (!queuedFinal) {
if (isGroup && historyKey && historyLimit > 0) { if (isGroup && historyKey) {
clearHistoryEntries({ historyMap: groupHistories, historyKey }); clearHistoryEntriesIfEnabled({ historyMap: groupHistories, historyKey, limit: historyLimit });
} }
return; return;
} }
@@ -197,7 +197,7 @@ export const dispatchTelegramMessage = async ({
); );
}, },
}); });
if (isGroup && historyKey && historyLimit > 0) { if (isGroup && historyKey) {
clearHistoryEntries({ historyMap: groupHistories, historyKey }); clearHistoryEntriesIfEnabled({ historyMap: groupHistories, historyKey, limit: historyLimit });
} }
}; };

View File

@@ -6,7 +6,7 @@ import { resolveMentionGating } from "../../../channels/mention-gating.js";
import type { MentionConfig } from "../mentions.js"; import type { MentionConfig } from "../mentions.js";
import { buildMentionConfig, debugMention, resolveOwnerList } from "../mentions.js"; import { buildMentionConfig, debugMention, resolveOwnerList } from "../mentions.js";
import type { WebInboundMsg } from "../types.js"; import type { WebInboundMsg } from "../types.js";
import { recordPendingHistoryEntry } from "../../../auto-reply/reply/history.js"; import { recordPendingHistoryEntryIfEnabled } from "../../../auto-reply/reply/history.js";
import { stripMentionsForCommand } from "./commands.js"; import { stripMentionsForCommand } from "./commands.js";
import { resolveGroupActivationFor, resolveGroupPolicyFor } from "./group-activation.js"; import { resolveGroupActivationFor, resolveGroupPolicyFor } from "./group-activation.js";
import { noteGroupMember } from "./group-members.js"; import { noteGroupMember } from "./group-members.js";
@@ -66,24 +66,22 @@ export function applyGroupGating(params: {
if (activationCommand.hasCommand && !owner) { if (activationCommand.hasCommand && !owner) {
params.logVerbose(`Ignoring /activation from non-owner in group ${params.conversationId}`); params.logVerbose(`Ignoring /activation from non-owner in group ${params.conversationId}`);
if (params.groupHistoryLimit > 0) { const sender =
const sender = params.msg.senderName && params.msg.senderE164
params.msg.senderName && params.msg.senderE164 ? `${params.msg.senderName} (${params.msg.senderE164})`
? `${params.msg.senderName} (${params.msg.senderE164})` : (params.msg.senderName ?? params.msg.senderE164 ?? "Unknown");
: (params.msg.senderName ?? params.msg.senderE164 ?? "Unknown"); recordPendingHistoryEntryIfEnabled({
recordPendingHistoryEntry({ historyMap: params.groupHistories,
historyMap: params.groupHistories, historyKey: params.groupHistoryKey,
historyKey: params.groupHistoryKey, limit: params.groupHistoryLimit,
limit: params.groupHistoryLimit, entry: {
entry: { sender,
sender, body: params.msg.body,
body: params.msg.body, timestamp: params.msg.timestamp,
timestamp: params.msg.timestamp, id: params.msg.id,
id: params.msg.id, senderJid: params.msg.senderJid,
senderJid: params.msg.senderJid, },
}, });
});
}
return { shouldProcess: false }; return { shouldProcess: false };
} }
@@ -126,24 +124,22 @@ export function applyGroupGating(params: {
params.logVerbose( params.logVerbose(
`Group message stored for context (no mention detected) in ${params.conversationId}: ${params.msg.body}`, `Group message stored for context (no mention detected) in ${params.conversationId}: ${params.msg.body}`,
); );
if (params.groupHistoryLimit > 0) { const sender =
const sender = params.msg.senderName && params.msg.senderE164
params.msg.senderName && params.msg.senderE164 ? `${params.msg.senderName} (${params.msg.senderE164})`
? `${params.msg.senderName} (${params.msg.senderE164})` : (params.msg.senderName ?? params.msg.senderE164 ?? "Unknown");
: (params.msg.senderName ?? params.msg.senderE164 ?? "Unknown"); recordPendingHistoryEntryIfEnabled({
recordPendingHistoryEntry({ historyMap: params.groupHistories,
historyMap: params.groupHistories, historyKey: params.groupHistoryKey,
historyKey: params.groupHistoryKey, limit: params.groupHistoryLimit,
limit: params.groupHistoryLimit, entry: {
entry: { sender,
sender, body: params.msg.body,
body: params.msg.body, timestamp: params.msg.timestamp,
timestamp: params.msg.timestamp, id: params.msg.id,
id: params.msg.id, senderJid: params.msg.senderJid,
senderJid: params.msg.senderJid, },
}, });
});
}
return { shouldProcess: false }; return { shouldProcess: false };
} }