|
| 1 | +import type { Content } from "@elizaos/core"; |
| 2 | +import type { Message as DiscordMessage } from "discord.js"; |
| 3 | + |
| 4 | +export type DiscordStalenessBehavior = "tag" | "skip" | "ignore"; |
| 5 | + |
| 6 | +export interface DiscordStalenessConfig { |
| 7 | + enabled: boolean; |
| 8 | + behavior: DiscordStalenessBehavior; |
| 9 | + threshold: number; |
| 10 | +} |
| 11 | + |
| 12 | +const DEFAULT_THRESHOLD = 2; |
| 13 | +const channelSequences = new WeakMap<object, Map<string, number>>(); |
| 14 | +const lastMessageIds = new WeakMap<object, Map<string, string | undefined>>(); |
| 15 | + |
| 16 | +function parseBoolean(value: unknown, fallback: boolean): boolean { |
| 17 | + if (value === undefined || value === null) { |
| 18 | + return fallback; |
| 19 | + } |
| 20 | + return String(value).trim().toLowerCase() === "true"; |
| 21 | +} |
| 22 | + |
| 23 | +function parseNonNegativeInteger(value: unknown, fallback: number): number { |
| 24 | + const parsed = Number.parseInt(String(value ?? ""), 10); |
| 25 | + return Number.isFinite(parsed) && parsed >= 0 ? parsed : fallback; |
| 26 | +} |
| 27 | + |
| 28 | +function parseBehavior(value: unknown): DiscordStalenessBehavior { |
| 29 | + const normalized = String(value ?? "tag") |
| 30 | + .trim() |
| 31 | + .toLowerCase(); |
| 32 | + return normalized === "skip" || |
| 33 | + normalized === "ignore" || |
| 34 | + normalized === "tag" |
| 35 | + ? normalized |
| 36 | + : "tag"; |
| 37 | +} |
| 38 | + |
| 39 | +function ensureSequenceMap(owner: object): Map<string, number> { |
| 40 | + let map = channelSequences.get(owner); |
| 41 | + if (!map) { |
| 42 | + map = new Map<string, number>(); |
| 43 | + channelSequences.set(owner, map); |
| 44 | + } |
| 45 | + return map; |
| 46 | +} |
| 47 | + |
| 48 | +export function getDiscordStalenessConfig( |
| 49 | + getSetting: (key: string) => unknown, |
| 50 | +): DiscordStalenessConfig { |
| 51 | + return { |
| 52 | + enabled: parseBoolean(getSetting("DISCORD_STALENESS_ENABLED"), false), |
| 53 | + behavior: parseBehavior(getSetting("DISCORD_STALENESS_BEHAVIOR")), |
| 54 | + threshold: parseNonNegativeInteger( |
| 55 | + getSetting("DISCORD_STALENESS_THRESHOLD"), |
| 56 | + DEFAULT_THRESHOLD, |
| 57 | + ), |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +export function recordDiscordChannelMessageSeen( |
| 62 | + owner: object | undefined, |
| 63 | + channelId: string | undefined, |
| 64 | + messageId?: string, |
| 65 | +): number { |
| 66 | + if (!owner || !channelId) { |
| 67 | + return 0; |
| 68 | + } |
| 69 | + const sequences = ensureSequenceMap(owner); |
| 70 | + const next = (sequences.get(channelId) ?? 0) + 1; |
| 71 | + sequences.set(channelId, next); |
| 72 | + |
| 73 | + let lastIds = lastMessageIds.get(owner); |
| 74 | + if (!lastIds) { |
| 75 | + lastIds = new Map<string, string | undefined>(); |
| 76 | + lastMessageIds.set(owner, lastIds); |
| 77 | + } |
| 78 | + lastIds.set(channelId, messageId); |
| 79 | + |
| 80 | + return next; |
| 81 | +} |
| 82 | + |
| 83 | +export function getDiscordChannelMessageSequence( |
| 84 | + owner: object | undefined, |
| 85 | + channelId: string | undefined, |
| 86 | +): number { |
| 87 | + if (!owner || !channelId) { |
| 88 | + return 0; |
| 89 | + } |
| 90 | + return ensureSequenceMap(owner).get(channelId) ?? 0; |
| 91 | +} |
| 92 | + |
| 93 | +export interface DiscordStalenessDecision { |
| 94 | + shouldSend: boolean; |
| 95 | + stale: boolean; |
| 96 | + messagesSinceTurnStart: number; |
| 97 | + behavior: DiscordStalenessBehavior; |
| 98 | +} |
| 99 | + |
| 100 | +export function applyDiscordStalenessGuard(options: { |
| 101 | + config: DiscordStalenessConfig; |
| 102 | + owner: object | undefined; |
| 103 | + message: DiscordMessage; |
| 104 | + startSequence: number; |
| 105 | + content: Content; |
| 106 | +}): DiscordStalenessDecision { |
| 107 | + const { config, owner, message, startSequence, content } = options; |
| 108 | + if (!config.enabled || config.behavior === "ignore") { |
| 109 | + return { |
| 110 | + shouldSend: true, |
| 111 | + stale: false, |
| 112 | + messagesSinceTurnStart: 0, |
| 113 | + behavior: config.behavior, |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + const currentSequence = getDiscordChannelMessageSequence( |
| 118 | + owner, |
| 119 | + message.channel?.id, |
| 120 | + ); |
| 121 | + const messagesSinceTurnStart = Math.max(0, currentSequence - startSequence); |
| 122 | + const stale = messagesSinceTurnStart > config.threshold; |
| 123 | + if (!stale) { |
| 124 | + return { |
| 125 | + shouldSend: true, |
| 126 | + stale: false, |
| 127 | + messagesSinceTurnStart, |
| 128 | + behavior: config.behavior, |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + if (config.behavior === "skip") { |
| 133 | + return { |
| 134 | + shouldSend: false, |
| 135 | + stale: true, |
| 136 | + messagesSinceTurnStart, |
| 137 | + behavior: config.behavior, |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + if ( |
| 142 | + config.behavior === "tag" && |
| 143 | + typeof content.text === "string" && |
| 144 | + content.text.trim().length > 0 && |
| 145 | + !/^(\s*\(catching up:\))/i.test(content.text) |
| 146 | + ) { |
| 147 | + content.text = `(catching up:) ${content.text}`; |
| 148 | + } |
| 149 | + |
| 150 | + return { |
| 151 | + shouldSend: true, |
| 152 | + stale: true, |
| 153 | + messagesSinceTurnStart, |
| 154 | + behavior: config.behavior, |
| 155 | + }; |
| 156 | +} |
0 commit comments