CRITICAL BUG FIXED: The /ai ask slash command was passing incorrect parameters to AIManager.chat(), causing undefined serverDocument and channel values, which prevented proper context building and conversation history retrieval. This resulted in repetitive AI responses.
Status: ✅ Fixed in Internals/SlashCommands/commands/ai.js
Your AI system is well-architected with multi-provider support (OpenAI, Anthropic, Groq, Ollama) and comprehensive features. The system prompt is limited at 4,000 characters while modern LLMs support 128K-200K context windows, creating an opportunity to enhance AI capabilities with richer prompting.
AIManager.js (26.5 KB)
- Central orchestration for all AI operations
- Manages provider resolution, context building, memory integration
- Handles conversation history, vector memory, and rate limiting
- Integrates with 5+ LLM providers
Providers (7 files)
- OpenAI: gpt-4o (128K context), gpt-4o-mini, gpt-4-turbo
- Anthropic: Claude 3.5 Sonnet (200K context), Claude 3 variants
- Groq: Llama 3.1, Mixtral (8K-32K context)
- Ollama: Local model support
- OpenAI-Compatible: Generic API support
Memory Systems
- ConversationMemory: Per-channel/per-user history (configurable 1-50 messages)
- VectorMemory: Qdrant integration for semantic search (optional)
- RateLimiter: Cooldowns, per-user/channel limits
- UsageTracker: Token counting, cost estimation, budget enforcement
Database Schema (serverAISchema.js)
- 287 lines, comprehensive configuration
- Supports per-server customization
- Governance, budget controls, tool access management
Location: serverDocument.config.ai.systemPrompt
Limits:
- Max length: 4,000 characters
- Default: "You are a helpful AI assistant in a Discord server. Be concise, friendly, and helpful."
- UI: 10-row textarea in admin panel
Context Building Flow (AIManager.js:236-268):
1. Load base system prompt (4K max)
2. Optionally inject vector memory context
3. Append conversation history
4. Send to provider
Problem: 4,000 characters is ~1,000 tokens, leaving 127K unused in gpt-4o's context window.
| Provider | Model | Context | Utilization |
|---|---|---|---|
| OpenAI | gpt-4o | 128,000 | 3.1% |
| OpenAI | gpt-4o-mini | 128,000 | 3.1% |
| Anthropic | Claude 3.5 Sonnet | 200,000 | 2% |
| Groq | Mixtral | 32,768 | 12% |
| Groq | Llama 3.1 | 8,192 | 48% |
Current system prompt uses only 2-12% of available context.
- Current: 4,000 characters
- Recommended: 16,000-32,000 characters
- Effort: 30 minutes
- Impact: High
Changes needed:
- Update
serverAISchema.jsmaxlength - Update
admin-ai-personality.ejstextarea rows & maxlength - Update
Web/controllers/dashboard/ai.jssubstring limit
Create pre-built templates for common use cases:
- Community Manager (moderation, engagement)
- Support Bot (customer service, FAQ)
- Creative Assistant (brainstorming, content)
- Technical Helper (code, debugging)
- Gaming Bot (game knowledge, events)
- Educational (teaching, learning)
Storage: New aiPromptTemplates collection
UI: Dropdown selector with preview
Effort: 2-3 hours
Impact: High (reduces setup friction, improves quality)
Automatically inject server-specific context:
- Server name, member count, creation date
- Roles and their purposes
- Channel structure and purposes
- Server rules/guidelines
- Enabled features
- Language preference
Implementation: New _buildServerContext() method in AIManager
Effort: 1-2 hours
Impact: Medium (improves relevance, reduces manual setup)
Track system prompt changes:
- Version history in database
- Rollback capability
- Change reasons/audit trail
- UI history browser
Effort: 1 hour Impact: Low (operational benefit, debugging)
Tailor prompts based on provider:
- Claude: Leverage 200K context, detailed reasoning
- GPT-4o: Structured outputs, complex instructions
- Groq: Concise prompts, fast inference
Effort: 1-2 hours Impact: Medium (better quality per provider)
Add to system prompt:
- Few-shot examples
- Constraint specifications
- Tone & style guidelines
- Output format requirements
Effort: 30 minutes Impact: High (improves response quality)
- Character counter with visual indicator
- Token usage estimator
- Template preview modal
- Test console for prompt validation
- Prompt analytics
Effort: 2-3 hours Impact: Medium (UX improvement)
- ✅ Increase system prompt limit to 16,000 characters
- ✅ Update UI textarea (25 rows, maxlength="16000")
- ✅ Add character counter in UI
- ✅ Add token estimator (chars / 4)
- Create
aiPromptTemplatescollection - Seed 6 default templates
- Add template selector UI
- Implement server context injection
- Add prompt history tracking
- Provider-specific optimizations
- Advanced prompt engineering features
- Test console for validation
- Documentation updates
File: Database/Schemas/serverAISchema.js
systemPrompt: {
type: String,
default: "You are a helpful AI assistant in a Discord server. Be concise, friendly, and helpful.",
maxlength: 16000, // Increased from 4000
}File: Web/controllers/dashboard/ai.js
controllers.personality.post = async (req, res) => {
const serverQueryDocument = req.svr.queryDocument;
serverQueryDocument.set(
"config.ai.systemPrompt",
req.body.systemPrompt ? req.body.systemPrompt.substring(0, 16000) : ""
);
save(req, res, true);
};File: Web/views/pages/admin-ai-personality.ejs
<textarea name="systemPrompt"
class="textarea is-primary"
rows="25"
maxlength="16000"
placeholder="You are a helpful AI assistant...">
<%= configData.ai.systemPrompt %>
</textarea>
<p class="help">
<span id="charCount">0</span> / 16000 characters
(~<span id="tokenEstimate">0</span> tokens)
</p>
<script>
const textarea = document.querySelector('[name="systemPrompt"]');
function updateCounts() {
const chars = textarea.value.length;
const tokens = Math.ceil(chars / 4);
document.getElementById('charCount').textContent = chars;
document.getElementById('tokenEstimate').textContent = tokens;
}
textarea.addEventListener('input', updateCounts);
updateCounts();
</script>File: Modules/AI/AIManager.js (buildContext method)
async buildContext (serverDocument, channelId, user, currentMessage = null) {
const aiConfig = serverDocument.config.ai || {};
const memoryConfig = aiConfig.memory || {};
const vectorConfig = aiConfig.vectorMemory || {};
// Get system prompt
let systemPrompt = aiConfig.systemPrompt ||
"You are a helpful AI assistant in a Discord server. Be concise and helpful.";
// Inject server context (NEW)
const serverContext = await this._buildServerContext(serverDocument, channelId);
if (serverContext) {
systemPrompt += `\n\n${serverContext}`;
}
// Inject vector memory context
if (vectorConfig.enabled && vectorConfig.injectContext && currentMessage) {
const vectorContext = await this.searchVectorMemory(...);
if (vectorContext && vectorContext.length > 0) {
const contextPrefix = vectorConfig.contextPrefix || "Relevant context from memory:";
const contextText = vectorContext.map(m => `- ${m.content}`).join("\n");
systemPrompt += `\n\n${contextPrefix}\n${contextText}`;
}
}
const messages = [{ role: "system", content: systemPrompt }];
const history = await this.memory.getHistory(...);
messages.push(...history);
return messages;
}
async _buildServerContext (serverDocument, channelId) {
const guild = this.client.guilds.cache.get(serverDocument._id);
if (!guild) return null;
const channel = guild.channels.cache.get(channelId);
const rolesList = guild.roles.cache
.filter(r => !r.isManaged() && r.id !== guild.id)
.map(r => `- ${r.name}`)
.join("\n");
return `## Server Context
**Server**: ${guild.name} (${guild.memberCount} members)
**Channel**: ${channel?.name || "unknown"}
**Roles**:
${rolesList}`;
}- Support detailed instructions and guidelines
- Add constraint specifications
- Include tone & style guidelines
- Better moderation capabilities
- Reduce setup friction for new servers
- Improve consistency across servers
- Enable specialized use cases (support, moderation, gaming)
- Better out-of-box experience
- Leverage full power of 128K-200K context windows
- Enable complex multi-step reasoning
- Support advanced prompt engineering techniques
- Better AI quality across all use cases
- Prompt Injection: Validate system prompts don't contain malicious instructions
- Audit Trail: Log all system prompt changes
- Rate Limiting: Apply to system prompt updates
- Tier Gating: Limit prompt size by subscription tier
- Content Filtering: Scan for inappropriate content
- Add
systemPromptHistoryto track changes - Implement change reason field
- Log user ID and timestamp
- Consider tier-based limits (starter: 8K, premium: 16K+)
- Unit Tests: Token estimation accuracy
- Integration Tests: Context building with large prompts
- Load Tests: Maximum prompt size handling
- Provider Tests: Each provider with large prompts
- UI Tests: Textarea behavior, character counter
✅ Fully backward compatible
- Existing prompts under 4,000 chars continue to work
- Default prompt unchanged
- No breaking API changes
- Gradual UI migration
- Review this document - Confirm approach aligns with your vision
- Implement Phase 1 - Quick wins (1-2 hours)
- Test thoroughly - Ensure no regressions
- Implement Phase 2 - Templates & context (3-4 hours)
- Gather feedback - Monitor usage patterns
- Iterate - Refine based on real-world usage
Your AI system is solid and well-designed. The main opportunity is leveraging the massive context windows available in modern LLMs. By increasing the system prompt limit from 4,000 to 16,000+ characters and implementing templates/dynamic context injection, you can significantly improve AI quality with minimal effort.
The recommended approach is:
- Phase 1: Increase limit to 16,000 chars (quick win)
- Phase 2: Add templates and server context (medium effort, high impact)
- Phase 3: Polish and advanced features (optional enhancements)
This positions your bot to provide superior AI experiences compared to competitors while maintaining full backward compatibility.