5.7 KiB
5.7 KiB
✅ Prompt Versioning Refactor - Complete
Date: November 17, 2024
Status: Production Ready
🎯 What Changed
Before:
lib/ai/chat-modes.ts (297 lines)
└─ All 6 mode prompts in one giant file
After:
lib/ai/
├─ chat-modes.ts (38 lines) - Just type definitions & imports
└─ prompts/
├─ README.md - Documentation
├─ index.ts - Exports all prompts
├─ shared.ts - Shared components
├─ collector.ts - Collector mode (versioned)
├─ extraction-review.ts - Extraction review mode (versioned)
├─ vision.ts - Vision mode (versioned)
├─ mvp.ts - MVP mode (versioned)
├─ marketing.ts - Marketing mode (versioned)
└─ general-chat.ts - General chat mode (versioned)
✨ Benefits
1. Clean Separation
Each prompt is now in its own file:
- Easy to find and edit
- Clear git diffs
- No accidentally changing the wrong prompt
2. Version Control
Each file tracks versions:
const COLLECTOR_V1: PromptVersion = {
version: 'v1',
createdAt: '2024-11-17',
description: 'Initial version with GitHub analysis',
prompt: `...`
};
export const collectorPrompts = {
v1: COLLECTOR_V1,
current: 'v1', // ← Change this to switch versions
};
3. Easy Rollback
Problem with a new prompt? Just change one line:
current: 'v1' // Rolled back instantly
4. A/B Testing Ready
Can test multiple versions:
const version = userInExperiment ? 'v2' : 'v1';
const prompt = collectorPrompts[version].prompt;
5. Documentation Built-In
Each version has metadata:
version- Version identifiercreatedAt- When it was createddescription- What changedprompt- The actual prompt text
📝 How to Use
View Current Prompts
import { MODE_SYSTEM_PROMPTS } from '@/lib/ai/chat-modes';
// Same API as before - no breaking changes!
const prompt = MODE_SYSTEM_PROMPTS['collector_mode'];
Access Version History
import { collectorPrompts } from '@/lib/ai/prompts';
console.log(collectorPrompts.v1.prompt); // Old version
console.log(collectorPrompts.current); // 'v1'
console.log(collectorPrompts.v1.description); // Why it changed
Add a New Version
- Open the relevant file (e.g.,
prompts/collector.ts) - Add new version:
const COLLECTOR_V2: PromptVersion = {
version: 'v2',
createdAt: '2024-12-01',
description: 'Added context-aware chunking instructions',
prompt: `...`,
};
- Update exports:
export const collectorPrompts = {
v1: COLLECTOR_V1,
v2: COLLECTOR_V2, // Add
current: 'v2', // Switch
};
Rollback a Prompt
Just change the current field:
export const collectorPrompts = {
v1: COLLECTOR_V1,
v2: COLLECTOR_V2,
current: 'v1', // ← Back to v1
};
🔍 Verification
All Tests Pass
✅ Server starts successfully
✅ No import errors
✅ No linter errors
✅ Prompts load correctly
✅ AI chat working
File Structure
lib/ai/prompts/
├── README.md (4.5 KB) - Full documentation
├── collector.ts (3.6 KB)
├── extraction-review.ts (2.1 KB)
├── vision.ts (2.3 KB)
├── mvp.ts (2.0 KB)
├── marketing.ts (2.1 KB)
├── general-chat.ts (2.1 KB)
├── shared.ts (851 B)
└── index.ts (1.2 KB)
No Duplicates
- ✅ Old 297-line file replaced with 38-line import file
- ✅ All prompts moved to separate versioned files
- ✅ No redundant code
🚀 Next Steps
Immediate:
- ✅ Server is running with new structure
- ✅ Test AI chat to verify prompts work
- ✅ Commit changes to git
Future Enhancements:
1. Context-Aware Chunking
Add to each prompt:
**Retrieved Context Format**:
When vector search returns chunks, they include:
- Document title and type
- Chunk number and total chunks
- Source metadata (importance, origin)
Always acknowledge the source when using retrieved information.
2. Analytics Tracking
await logPromptUsage({
mode: 'collector_mode',
version: collectorPrompts.current,
responseTime: 1234,
userSatisfaction: 4.5,
});
3. A/B Testing Framework
const { version, prompt } = await getPromptForUser(
userId,
'collector_mode'
);
// Returns v1 or v2 based on experiment assignment
4. Database Storage
Move to Firestore for:
- No-deploy prompt updates
- Per-user customization
- Instant rollbacks
- Usage analytics
📚 Documentation
Full guide available in: lib/ai/prompts/README.md
Topics covered:
- How to add new versions
- How to rollback
- Best practices
- Future enhancements
- Example workflows
✅ Migration Checklist
- Create
lib/ai/prompts/directory - Extract shared components to
shared.ts - Create versioned prompt files for all 6 modes
- Add version metadata (version, date, description)
- Create index file with exports
- Update
chat-modes.tsto import from new files - Write comprehensive README
- Test server startup
- Verify no import errors
- Verify no linter errors
- Verify AI chat works
- Document migration
🎉 Summary
Your prompts are now:
- ✅ Organized - One file per mode
- ✅ Versioned - Full history tracking
- ✅ Documented - Metadata for each version
- ✅ Flexible - Easy to update, rollback, or A/B test
- ✅ Scalable - Ready for database storage if needed
No breaking changes - existing code works exactly the same, just with better structure under the hood!
🚀 Ready to add context-aware chunking to prompts whenever you want!