|
| 1 | +import type { |
| 2 | + TelegramAccountConfig, |
| 3 | + TelegramDirectConfig, |
| 4 | + TelegramGroupConfig, |
| 5 | + TelegramTopicConfig, |
| 6 | +} from "openclaw/plugin-sdk/config-runtime"; |
| 7 | + |
| 8 | +export type TelegramErrorPolicy = "always" | "once" | "silent"; |
| 9 | + |
| 10 | +type TelegramErrorConfig = |
| 11 | + | TelegramAccountConfig |
| 12 | + | TelegramDirectConfig |
| 13 | + | TelegramGroupConfig |
| 14 | + | TelegramTopicConfig; |
| 15 | + |
| 16 | +const errorCooldownStore = new Map<string, Map<string, number>>(); |
| 17 | +const DEFAULT_ERROR_COOLDOWN_MS = 14400000; |
| 18 | + |
| 19 | +function pruneExpiredCooldowns(messageStore: Map<string, number>, now: number) { |
| 20 | + for (const [message, expiresAt] of messageStore) { |
| 21 | + if (expiresAt <= now) { |
| 22 | + messageStore.delete(message); |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +export function resolveTelegramErrorPolicy(params: { |
| 28 | + accountConfig?: TelegramAccountConfig; |
| 29 | + groupConfig?: TelegramDirectConfig | TelegramGroupConfig; |
| 30 | + topicConfig?: TelegramTopicConfig; |
| 31 | +}): { |
| 32 | + policy: TelegramErrorPolicy; |
| 33 | + cooldownMs: number; |
| 34 | +} { |
| 35 | + const configs: Array<TelegramErrorConfig | undefined> = [ |
| 36 | + params.accountConfig, |
| 37 | + params.groupConfig, |
| 38 | + params.topicConfig, |
| 39 | + ]; |
| 40 | + let policy: TelegramErrorPolicy = "always"; |
| 41 | + let cooldownMs = DEFAULT_ERROR_COOLDOWN_MS; |
| 42 | + |
| 43 | + for (const config of configs) { |
| 44 | + if (config?.errorPolicy) { |
| 45 | + policy = config.errorPolicy; |
| 46 | + } |
| 47 | + if (typeof config?.errorCooldownMs === "number") { |
| 48 | + cooldownMs = config.errorCooldownMs; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return { policy, cooldownMs }; |
| 53 | +} |
| 54 | + |
| 55 | +export function buildTelegramErrorScopeKey(params: { |
| 56 | + accountId: string; |
| 57 | + chatId: string | number; |
| 58 | + threadId?: string | number | null; |
| 59 | +}): string { |
| 60 | + const threadId = params.threadId == null ? "main" : String(params.threadId); |
| 61 | + return `${params.accountId}:${String(params.chatId)}:${threadId}`; |
| 62 | +} |
| 63 | + |
| 64 | +export function shouldSuppressTelegramError(params: { |
| 65 | + scopeKey: string; |
| 66 | + cooldownMs: number; |
| 67 | + errorMessage?: string; |
| 68 | +}): boolean { |
| 69 | + const { scopeKey, cooldownMs, errorMessage } = params; |
| 70 | + const now = Date.now(); |
| 71 | + const messageKey = errorMessage ?? ""; |
| 72 | + const scopeStore = errorCooldownStore.get(scopeKey); |
| 73 | + |
| 74 | + if (scopeStore) { |
| 75 | + pruneExpiredCooldowns(scopeStore, now); |
| 76 | + if (scopeStore.size === 0) { |
| 77 | + errorCooldownStore.delete(scopeKey); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (errorCooldownStore.size > 100) { |
| 82 | + for (const [scope, messageStore] of errorCooldownStore) { |
| 83 | + pruneExpiredCooldowns(messageStore, now); |
| 84 | + if (messageStore.size === 0) { |
| 85 | + errorCooldownStore.delete(scope); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + const expiresAt = scopeStore?.get(messageKey); |
| 91 | + if (typeof expiresAt === "number" && expiresAt > now) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + const nextScopeStore = scopeStore ?? new Map<string, number>(); |
| 96 | + nextScopeStore.set(messageKey, now + cooldownMs); |
| 97 | + errorCooldownStore.set(scopeKey, nextScopeStore); |
| 98 | + return false; |
| 99 | +} |
| 100 | + |
| 101 | +export function isSilentErrorPolicy(policy: TelegramErrorPolicy): boolean { |
| 102 | + return policy === "silent"; |
| 103 | +} |
| 104 | + |
| 105 | +export function resetTelegramErrorPolicyStoreForTest() { |
| 106 | + errorCooldownStore.clear(); |
| 107 | +} |
0 commit comments