← Back to PRs

#22945: feat(discord): seed channel history from API and persist to disk

by iamhitarth open 2026-02-21 21:13 View on GitHub →
channel: discord size: S
## Summary - When `guildHistories` is empty for a channel (e.g. after gateway restart), automatically fetches the last N messages from the Discord API to seed conversation context. This ensures the bot has awareness of recent messages without requiring users to manually re-establish context. - Persists `guildHistories` to disk (`discord-history-cache.json`) on provider shutdown and loads on startup, so most channels don't need the API call at all — only channels not in the disk cache fall back to API seeding. ### Details **API seeding** (`message-handler.process.ts`): - Triggers when `shouldIncludeChannelHistory` is true, `historyLimit > 0`, and the in-memory map has no entries for the channel - Fetches `min(historyLimit, 50)` messages via `readMessagesDiscord` (existing function) - Labels bot's own messages as `"assistant"`, uses nick/globalName/username fallback for others - One-shot per channel — once seeded, the in-memory map takes over **Disk persistence** (`provider.ts`): - `loadGuildHistoriesFromDisk()` on startup, `saveGuildHistoriesToDisk()` in the `finally` block on shutdown - Caps at 50 entries per channel on save - Cache stored in the config dir alongside other state files Closes #15807 ## Test plan - [ ] Restart gateway — verify disk cache loads (check verbose logs for `loaded N channel histories from disk cache`) - [ ] Send message in a guild channel after restart — verify API seeding triggers for channels not in cache (`seeded N history entries for <channelId> from API`) - [ ] Verify bot responses reference recent conversation context after restart - [ ] Verify no duplicate history entries on subsequent messages (seeding only fires once per channel) - [ ] Verify DMs are unaffected (seeding only applies to guild channels) 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- greptile_comment --> <h3>Greptile Summary</h3> Added automatic seeding of Discord channel history from the API when the in-memory map is empty (e.g., after gateway restart), and implemented disk persistence for `guildHistories` to minimize API calls on subsequent startups. Key changes: - API seeding triggers when `shouldIncludeChannelHistory` is true, `historyLimit > 0`, and no entries exist for the channel in memory - Fetches up to `min(historyLimit, 50)` messages using `readMessagesDiscord` with `before: message.id` to exclude the current message - Bot messages labeled as `"assistant"`, others use nick/globalName/username fallback - Disk cache stored at `discord-history-cache.json` in config directory, loaded on startup and saved on shutdown - Cache capped at 50 entries per channel on save to prevent unbounded growth <h3>Confidence Score: 4/5</h3> - Safe to merge with minor considerations - the changes are well-contained, include proper error handling, and follow existing patterns - The implementation is sound with good error handling and follows the repository's patterns. The seeding logic correctly checks for empty history before making API calls, properly handles the `botUserId` identification, and includes appropriate error logging. Disk persistence uses standard filesystem operations with try-catch blocks. The only minor consideration is ensuring the API seeding doesn't cause rate limiting issues in channels with high message volume, but the one-shot-per-channel design and `min(historyLimit, 50)` cap mitigate this risk. - No files require special attention <sub>Last reviewed commit: acd58af</sub> <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> <!-- /greptile_comment -->

Most Similar PRs