refactor(extensions): extract feishu dedup and mattermost onchar helpers

This commit is contained in:
Peter Steinberger
2026-02-13 18:15:19 +00:00
parent 6310b8b7fc
commit a750a195e5
4 changed files with 60 additions and 56 deletions

View File

@@ -10,6 +10,7 @@ import type { FeishuMessageContext, FeishuMediaInfo, ResolvedFeishuAccount } fro
import type { DynamicAgentCreationConfig } from "./types.js";
import { resolveFeishuAccount } from "./accounts.js";
import { createFeishuClient } from "./client.js";
import { tryRecordMessage } from "./dedup.js";
import { maybeCreateDynamicAgent } from "./dynamic-agent.js";
import { downloadImageFeishu, downloadMessageResourceFeishu } from "./media.js";
import { extractMentionTargets, extractMessageBody, isMentionForwardRequest } from "./mention.js";
@@ -23,37 +24,6 @@ import { createFeishuReplyDispatcher } from "./reply-dispatcher.js";
import { getFeishuRuntime } from "./runtime.js";
import { getMessageFeishu, sendMessageFeishu } from "./send.js";
// --- Message deduplication ---
// Prevent duplicate processing when WebSocket reconnects or Feishu redelivers messages.
const DEDUP_TTL_MS = 30 * 60 * 1000; // 30 minutes
const DEDUP_MAX_SIZE = 1_000;
const DEDUP_CLEANUP_INTERVAL_MS = 5 * 60 * 1000; // cleanup every 5 minutes
const processedMessageIds = new Map<string, number>(); // messageId -> timestamp
let lastCleanupTime = Date.now();
function tryRecordMessage(messageId: string): boolean {
const now = Date.now();
// Throttled cleanup: evict expired entries at most once per interval
if (now - lastCleanupTime > DEDUP_CLEANUP_INTERVAL_MS) {
for (const [id, ts] of processedMessageIds) {
if (now - ts > DEDUP_TTL_MS) processedMessageIds.delete(id);
}
lastCleanupTime = now;
}
if (processedMessageIds.has(messageId)) return false;
// Evict oldest entries if cache is full
if (processedMessageIds.size >= DEDUP_MAX_SIZE) {
const first = processedMessageIds.keys().next().value!;
processedMessageIds.delete(first);
}
processedMessageIds.set(messageId, now);
return true;
}
// --- Permission error extraction ---
// Extract permission grant URL from Feishu API error response.
type PermissionError = {