Overview
The context cache is Junkie’s intelligent message storage and retrieval system. It uses PostgreSQL for persistent storage, enabling the bot to maintain conversation history across restarts and build context-aware prompts for AI responses.Architecture
The system consists of three main components:- Message Storage: PostgreSQL database for persistent caching
- Context Retrieval: Efficient message fetching with API fallback
- Prompt Building: Context-aware prompt construction with metadata
Configuration
Environment Variables
discord_bot/context_cache.py:25-34
Key Settings
- CACHE_TTL: Cache time-to-live in seconds (default: 120)
- CONTEXT_AGENT_MAX_MESSAGES: Maximum messages to include in context
- DISCORD_TIMEZONE: Timezone for timestamp formatting (default: Asia/Kolkata)
- BACKFILL_MAX_FETCH_LIMIT: Maximum messages to fetch in one operation (default: 1000)
Timestamp Formatting
The system uses intelligent relative timestamps for better context understanding:discord_bot/context_cache.py:43-76
Timestamp Examples
- Recent:
[just now],[5m ago],[2h ago] - This week:
[3d ago] - Older:
[Feb 15, 14:30]
Message Retrieval
Primary Retrieval Flow
Theget_recent_context function implements a smart two-tier retrieval strategy:
discord_bot/context_cache.py:83-104
API Fallback Strategy
When the database doesn’t have enough messages, the system falls back to Discord’s API:discord_bot/context_cache.py:106-132
API Fetch and Cache
Thefetch_and_cache_from_api function handles Discord API interaction:
discord_bot/context_cache.py:149-160
Message Content Enrichment
Messages are enriched with attachments and embeds:discord_bot/context_cache.py:204-224
Context Prompt Building
Thebuild_context_prompt function is the heart of context-aware conversations:
discord_bot/context_cache.py:245-257
Metadata Injection
The system adds channel and guild metadata for better context:discord_bot/context_cache.py:260-272
Reply Context Integration
When users reply to messages, that context is explicitly highlighted:discord_bot/context_cache.py:285-296
Final Prompt Structure
The complete prompt combines all elements:discord_bot/context_cache.py:298-308
Cache Updates
Appending New Messages
discord_bot/context_cache.py:315-333
Updating Edited Messages
discord_bot/context_cache.py:336-365
Deleting Messages
discord_bot/context_cache.py:368-373
Performance Optimizations
Loop Prevention
The system includes safeguards against infinite recursion:discord_bot/context_cache.py:134-142
Fetch Limits
API requests are capped to prevent overwhelming Discord:discord_bot/context_cache.py:157-160
Timezone Support
Optional pytz integration for accurate timezone handling:discord_bot/context_cache.py:31-40
Error Handling
The system gracefully handles common errors:discord_bot/context_cache.py:233-238
Database Integration
The cache integrates with PostgreSQL through the core database module:store_message(): Insert or update message (upsert)get_messages(): Retrieve messages for a channeldelete_message(): Remove deleted messagesis_channel_fully_backfilled(): Check backfill statusmark_channel_fully_backfilled(): Update backfill statusget_message_count(): Count messages in channelget_latest_message_id(): Get newest message IDget_oldest_message_id(): Get oldest message ID