This feature adds the ability for users to attach code context (repository URLs or zip files) to their chat sessions, enabling AI assistants to provide more relevant and context-aware responses.
- ✅ Upload zip files containing code
- ✅ Add repository URLs (GitHub, GitLab, etc.)
- ✅ View context information (type, source, file count)
- ✅ Remove context from sessions
- ✅ Visual badges showing context status
- ✅ Automatic code file extraction and filtering
- ✅ 3 new REST API endpoints
- ✅ Enhanced data models with context support
- ✅ Smart file processing (30+ supported extensions)
- ✅ Complete UI with modal dialog
- ✅ Full authentication and authorization
- ✅ Comprehensive error handling
| File | Purpose |
|---|---|
| QUICK_START_GUIDE.md | User guide with step-by-step instructions |
| CODE_CONTEXT_FEATURE.md | Complete technical documentation |
| CONTEXT_FEATURE_SUMMARY.md | Implementation summary and overview |
| test_context_feature.py | Automated test script |
-
Start the application:
docker-compose up
-
Open the UI:
- Navigate to:
http://localhost:8000/static/chat_sessions.html
- Navigate to:
-
Add context:
- Login → Select/Create session
- Click "+ Add Code Context"
- Choose: Repository URL or Upload Zip
- Done!
📖 Read: QUICK_START_GUIDE.md for detailed user instructions
-
Review the implementation:
# Models app/models/chat_sessions.py # CodeContext model # Schemas app/schemas/chat_sessions.py # API schemas # Endpoints app/routers/chat_sessions.py # Context endpoints # UI app/static/chat_sessions.html # Complete interface
-
Run tests:
python3 test_context_feature.py
-
Test manually:
# Add repository curl -X POST "http://localhost:8000/chat/sessions/{id}/context/repository" \ -H "Authorization: Bearer TOKEN" \ -H "Content-Type: application/json" \ -d '{"repository_url": "https://github.com/user/repo"}' # Upload zip curl -X POST "http://localhost:8000/chat/sessions/{id}/context/upload" \ -H "Authorization: Bearer TOKEN" \ -F "file=@code.zip"
📖 Read: CODE_CONTEXT_FEATURE.md for technical details
┌─────────────────────────────────────────────────────────────┐
│ User Interface │
│ (chat_sessions.html - Modal Dialog + JavaScript) │
└────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ REST API Layer │
│ POST /chat/sessions/{id}/context/upload │
│ POST /chat/sessions/{id}/context/repository │
│ DELETE /chat/sessions/{id}/context │
└────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Business Logic │
│ • Zip file extraction and validation │
│ • File type filtering (30+ extensions) │
│ • URL validation │
│ • File size checking (1MB limit) │
└────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Data Layer (MongoDB) │
│ ChatSession.context = { │
│ type: "repository" | "zip", │
│ source: string, │
│ files: [{ path, content, size }], │
│ added_at: datetime │
│ } │
└─────────────────────────────────────────────────────────────┘
Python, JavaScript, TypeScript, JSX, TSX, Java, C, C++, C#, Go, Rust, Ruby, PHP, Swift, Kotlin, Scala, SQL, HTML, CSS, JSON, XML, YAML, Markdown, Shell scripts, and more.
POST /chat/sessions/{session_id}/context/upload
Content-Type: multipart/form-data
Authorization: Bearer {token}
Body: file (zip file)
Response: ChatSessionResponse with context
POST /chat/sessions/{session_id}/context/repository
Content-Type: application/json
Authorization: Bearer {token}
Body: {"repository_url": "https://github.com/user/repo"}
Response: ChatSessionResponse with context
DELETE /chat/sessions/{session_id}/context
Authorization: Bearer {token}
Response: {"message": "Context removed successfully"}
class CodeContext(BaseModel):
type: str # "repository" or "zip"
source: Optional[str] # URL or filename
files: Optional[List[Dict]] # Extracted files
added_at: datetime # Timestamp
class ChatSession(BaseModel):
# ... existing fields
context: Optional[CodeContext] # NEW: Optional context- Add Context Button - Green button in chat header
- Modal Dialog - Clean interface for adding context
- Context Badge - Visual indicator showing context type
- Context Info Panel - Displays context details
Select Session → Click "+ Add Code Context" → Choose Method →
[Repository: Enter URL → Submit] OR [Zip: Choose File → Upload] →
Badge Appears → Context Available
python3 test_context_feature.pyTests include:
- Login authentication
- Session creation
- Zip file upload with sample code
- Repository URL addition
- Context removal
- Session retrieval with context
- Login to chat sessions UI
- Create new chat session
- Click "+ Add Code Context" button appears
- Upload zip file successfully
- Badge appears showing "Zip"
- View context details in modal
- Remove context successfully
- Add repository URL
- Badge updates to "Repository"
- Test with different file types
- Files Modified: 4 (models, schemas, routers, HTML)
- Lines Added: ~800 lines
- New Endpoints: 3 REST APIs
- New UI Components: Modal + 5 interactive elements
- Supported Languages: 30+
- Max File Size: 1MB per file
- File Type Detection: Automatic
- Binary File Handling: Automatic skip
- ✅ Authentication required for all operations
- ✅ User can only access their own sessions
- ✅ File type validation
- ✅ Size limits enforced
- ✅ Safe UTF-8 decoding with error handling
- ✅ URL validation
-
GitHub Integration
- OAuth authentication
- Automatic repository cloning
- Branch selection
-
Enhanced File Management
- File tree preview
- Selective file inclusion
- File search within context
-
Context Analytics
- Track file usage in chats
- Most referenced files
- Context effectiveness metrics
-
Advanced Features
- Multiple contexts per session
- Context versioning
- Shared contexts between users
- Context templates
- Add loading animations
- Implement file preview before upload
- Add context size limits per user
- Enable context export/import
- Add syntax highlighting for preview
app/
├── models/
│ └── chat_sessions.py ← Added CodeContext model
├── schemas/
│ └── chat_sessions.py ← Added context schemas
├── routers/
│ └── chat_sessions.py ← Added 3 endpoints
└── static/
└── chat_sessions.html ← Added UI components
New Files:
├── CODE_CONTEXT_FEATURE.md ← Technical documentation
├── CONTEXT_FEATURE_SUMMARY.md ← Implementation summary
├── QUICK_START_GUIDE.md ← User guide
├── test_context_feature.py ← Automated tests
└── FEATURE_README.md ← This file
- Review existing code in
app/routers/chat_sessions.py - Follow the established patterns
- Add tests in
test_context_feature.py - Update relevant documentation
- Follow PEP 8 for Python
- Use type hints
- Add docstrings to functions
- Include error handling
- User Guide: QUICK_START_GUIDE.md
- Technical Docs: CODE_CONTEXT_FEATURE.md
- Summary: CONTEXT_FEATURE_SUMMARY.md
- Check server logs for errors
- Verify MongoDB connection
- Test endpoints with curl
- Run automated test script
- Login fails: Check user credentials and token
- Upload fails: Verify file is valid zip under size limit
- No files extracted: Check file extensions are supported
- Badge doesn't show: Refresh page, check browser console
The Code Context feature is now fully implemented and ready to use! It provides a seamless way for users to add their codebase to chat sessions, enabling more contextual and relevant AI interactions.
Key Benefits:
- ✨ Easy to use - Just 2 clicks to add context
- 🚀 Fast - Automatic file processing
- 🔒 Secure - Full authentication
- 📊 Smart - Filters non-code files automatically
- 🎨 Beautiful - Clean, modern UI
Start using it today! Open http://localhost:8000/static/chat_sessions.html and try adding context to your chat sessions.
Built with ❤️ using FastAPI, MongoDB, and vanilla JavaScript