|
| 1 | +# 🛠️ Development Guide |
| 2 | + |
| 3 | +## Setting Up Development Environment |
| 4 | + |
| 5 | +### 1. Clone and Setup |
| 6 | + |
| 7 | +```bash |
| 8 | +git clone https://github.com/your-username/CodeRAG.git |
| 9 | +cd CodeRAG |
| 10 | +python -m venv venv |
| 11 | +source venv/bin/activate # Windows: venv\Scripts\activate |
| 12 | +pip install -r requirements.txt |
| 13 | +``` |
| 14 | + |
| 15 | +### 2. Configure Pre-commit Hooks |
| 16 | + |
| 17 | +```bash |
| 18 | +pip install pre-commit |
| 19 | +pre-commit install |
| 20 | +``` |
| 21 | + |
| 22 | +This will run code quality checks on every commit: |
| 23 | +- **Black**: Code formatting |
| 24 | +- **isort**: Import sorting |
| 25 | +- **Flake8**: Linting and style checks |
| 26 | +- **MyPy**: Type checking |
| 27 | +- **Basic hooks**: Trailing whitespace, file endings, etc. |
| 28 | + |
| 29 | +### 3. Environment Variables |
| 30 | + |
| 31 | +Copy `example.env` to `.env` and configure: |
| 32 | + |
| 33 | +```bash |
| 34 | +cp example.env .env |
| 35 | +``` |
| 36 | + |
| 37 | +Required variables: |
| 38 | +```env |
| 39 | +OPENAI_API_KEY=your_key_here # Required for embeddings and chat |
| 40 | +WATCHED_DIR=/path/to/code # Directory to index (default: current dir) |
| 41 | +``` |
| 42 | + |
| 43 | +## Code Quality Standards |
| 44 | + |
| 45 | +### Type Hints |
| 46 | +All functions should have type hints: |
| 47 | + |
| 48 | +```python |
| 49 | +def process_file(filepath: str, content: str) -> Optional[np.ndarray]: |
| 50 | + \"\"\"Process a file and return embeddings.\"\"\" |
| 51 | + ... |
| 52 | +``` |
| 53 | + |
| 54 | +### Error Handling |
| 55 | +Use structured logging and proper exception handling: |
| 56 | + |
| 57 | +```python |
| 58 | +import logging |
| 59 | +logger = logging.getLogger(__name__) |
| 60 | + |
| 61 | +try: |
| 62 | + result = risky_operation() |
| 63 | +except SpecificError as e: |
| 64 | + logger.error(f"Operation failed: {str(e)}") |
| 65 | + return None |
| 66 | +``` |
| 67 | + |
| 68 | +### Documentation |
| 69 | +Use concise docstrings for public functions: |
| 70 | + |
| 71 | +```python |
| 72 | +def search_code(query: str, k: int = 5) -> List[Dict[str, Any]]: |
| 73 | + \"\"\"Search the FAISS index using a text query. |
| 74 | + |
| 75 | + Args: |
| 76 | + query: The search query text |
| 77 | + k: Number of results to return |
| 78 | + |
| 79 | + Returns: |
| 80 | + List of search results with metadata |
| 81 | + \"\"\" |
| 82 | +``` |
| 83 | + |
| 84 | +## Testing Your Changes |
| 85 | + |
| 86 | +### Manual Testing |
| 87 | +```bash |
| 88 | +# Test backend indexing |
| 89 | +python main.py |
| 90 | + |
| 91 | +# Test Streamlit UI (separate terminal) |
| 92 | +streamlit run app.py |
| 93 | +``` |
| 94 | + |
| 95 | +### Code Quality Checks |
| 96 | +```bash |
| 97 | +# Format code |
| 98 | +black . |
| 99 | +isort . |
| 100 | + |
| 101 | +# Check linting |
| 102 | +flake8 . |
| 103 | + |
| 104 | +# Type checking |
| 105 | +mypy . |
| 106 | + |
| 107 | +# Run all pre-commit checks |
| 108 | +pre-commit run --all-files |
| 109 | +``` |
| 110 | + |
| 111 | +## Adding New Features |
| 112 | + |
| 113 | +1. **Create feature branch**: `git checkout -b feature/new-feature` |
| 114 | +2. **Add logging**: Use the logger for all operations |
| 115 | +3. **Add type hints**: Follow existing patterns |
| 116 | +4. **Handle errors**: Graceful degradation and user-friendly messages |
| 117 | +5. **Update tests**: Add tests for new functionality |
| 118 | +6. **Update docs**: Update README if needed |
| 119 | + |
| 120 | +## Architecture Guidelines |
| 121 | + |
| 122 | +### Keep It Simple |
| 123 | +- Maintain the single-responsibility principle |
| 124 | +- Avoid unnecessary abstractions |
| 125 | +- Focus on the core RAG functionality |
| 126 | + |
| 127 | +### Error Handling Strategy |
| 128 | +- Log errors with context |
| 129 | +- Return None/empty lists for failures |
| 130 | +- Show user-friendly messages in UI |
| 131 | +- Don't crash the application |
| 132 | + |
| 133 | +### Performance Considerations |
| 134 | +- Limit search results (default: 5) |
| 135 | +- Truncate long content for context |
| 136 | +- Cache embeddings when possible |
| 137 | +- Monitor memory usage with large codebases |
| 138 | + |
| 139 | +## Debugging Tips |
| 140 | + |
| 141 | +### Enable Debug Logging |
| 142 | +```python |
| 143 | +logging.basicConfig(level=logging.DEBUG) |
| 144 | +``` |
| 145 | + |
| 146 | +### Check Index Status |
| 147 | +```python |
| 148 | +from coderag.index import inspect_metadata |
| 149 | +inspect_metadata(5) # Show first 5 entries |
| 150 | +``` |
| 151 | + |
| 152 | +### Test Embeddings |
| 153 | +```python |
| 154 | +from coderag.embeddings import generate_embeddings |
| 155 | +result = generate_embeddings("test code") |
| 156 | +print(f"Shape: {result.shape if result is not None else 'None'}") |
| 157 | +``` |
| 158 | + |
| 159 | +## Common Development Issues |
| 160 | + |
| 161 | +**Import Errors** |
| 162 | +- Ensure you're in the virtual environment |
| 163 | +- Check PYTHONPATH includes project root |
| 164 | +- Verify all dependencies are installed |
| 165 | + |
| 166 | +**OpenAI API Issues** |
| 167 | +- Check API key validity |
| 168 | +- Monitor rate limits and usage |
| 169 | +- Test with a simple embedding request |
| 170 | + |
| 171 | +**FAISS Index Corruption** |
| 172 | +- Delete existing index files and rebuild |
| 173 | +- Check file permissions |
| 174 | +- Ensure consistent embedding dimensions |
| 175 | + |
| 176 | +## Project Structure |
| 177 | + |
| 178 | +``` |
| 179 | +CodeRAG/ |
| 180 | +├── coderag/ # Core library |
| 181 | +│ ├── __init__.py |
| 182 | +│ ├── config.py # Configuration management |
| 183 | +│ ├── embeddings.py # OpenAI integration |
| 184 | +│ ├── index.py # FAISS operations |
| 185 | +│ ├── search.py # Search functionality |
| 186 | +│ └── monitor.py # File monitoring |
| 187 | +├── scripts/ # Utility scripts |
| 188 | +├── tests/ # Test files |
| 189 | +├── .github/ # GitHub workflows |
| 190 | +├── main.py # Backend service |
| 191 | +├── app.py # Streamlit frontend |
| 192 | +├── prompt_flow.py # RAG orchestration |
| 193 | +└── requirements.txt # Dependencies |
| 194 | +``` |
0 commit comments