refactor: centralize messaging dedupe helpers

This commit is contained in:
Peter Steinberger
2026-01-10 16:02:47 +01:00
parent 99e9e506be
commit 70c1732dd1
5 changed files with 199 additions and 211 deletions

View File

@@ -517,7 +517,7 @@ export function validateGeminiTurns(messages: AgentMessage[]): AgentMessage[] {
}
// ── Messaging tool duplicate detection ──────────────────────────────────────
// When the agent uses a messaging tool (telegram, discord, slack, sessions_send)
// When the agent uses a messaging tool (telegram, discord, slack, message, sessions_send)
// to send a message, we track the text so we can suppress duplicate block replies.
// The LLM sometimes elaborates or wraps the same content, so we use substring matching.
@@ -539,6 +539,22 @@ export function normalizeTextForComparison(text: string): string {
.trim();
}
export function isMessagingToolDuplicateNormalized(
normalized: string,
normalizedSentTexts: string[],
): boolean {
if (normalizedSentTexts.length === 0) return false;
if (!normalized || normalized.length < MIN_DUPLICATE_TEXT_LENGTH)
return false;
return normalizedSentTexts.some((normalizedSent) => {
if (!normalizedSent || normalizedSent.length < MIN_DUPLICATE_TEXT_LENGTH)
return false;
return (
normalized.includes(normalizedSent) || normalizedSent.includes(normalized)
);
});
}
/**
* Check if a text is a duplicate of any previously sent messaging tool text.
* Uses substring matching to handle LLM elaboration (e.g., wrapping in quotes,
@@ -577,13 +593,8 @@ export function isMessagingToolDuplicate(
const normalized = normalizeTextForComparison(text);
if (!normalized || normalized.length < MIN_DUPLICATE_TEXT_LENGTH)
return false;
return sentTexts.some((sent) => {
const normalizedSent = normalizeTextForComparison(sent);
if (!normalizedSent || normalizedSent.length < MIN_DUPLICATE_TEXT_LENGTH)
return false;
// Substring match: either text contains the other
return (
normalized.includes(normalizedSent) || normalizedSent.includes(normalized)
);
});
return isMessagingToolDuplicateNormalized(
normalized,
sentTexts.map(normalizeTextForComparison),
);
}