|
3 | 3 | from pydantic import BaseModel |
4 | 4 |
|
5 | 5 | from dependencies import ( |
6 | | - dependency_analyzer, style_analyzer, |
| 6 | + dependency_analyzer, style_analyzer, dna_extractor, |
7 | 7 | get_repo_or_404 |
8 | 8 | ) |
9 | 9 | from services.input_validator import InputValidator |
@@ -133,3 +133,50 @@ async def get_style_analysis( |
133 | 133 | return {**style_data, "cached": False} |
134 | 134 | except Exception as e: |
135 | 135 | raise HTTPException(status_code=500, detail=str(e)) |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +@router.get("/{repo_id}/dna") |
| 140 | +async def get_codebase_dna( |
| 141 | + repo_id: str, |
| 142 | + format: str = "json", |
| 143 | + auth: AuthContext = Depends(require_auth) |
| 144 | +): |
| 145 | + """ |
| 146 | + Extract codebase DNA - architectural patterns, conventions, and constraints. |
| 147 | + |
| 148 | + This endpoint analyzes your codebase and returns a DNA profile that helps |
| 149 | + AI assistants understand how to write code consistent with your patterns. |
| 150 | + |
| 151 | + Args: |
| 152 | + repo_id: Repository identifier |
| 153 | + format: Output format - 'json' or 'markdown' (default: json) |
| 154 | + |
| 155 | + Returns: |
| 156 | + DNA profile with auth patterns, service patterns, database patterns, etc. |
| 157 | + """ |
| 158 | + try: |
| 159 | + repo = get_repo_or_404(repo_id, auth.user_id) |
| 160 | + |
| 161 | + # Try cache first |
| 162 | + cached_dna = dna_extractor.load_from_cache(repo_id) |
| 163 | + if cached_dna: |
| 164 | + logger.debug("Using cached DNA", repo_id=repo_id) |
| 165 | + if format == "markdown": |
| 166 | + return {"dna": cached_dna.to_markdown(), "cached": True} |
| 167 | + return {**cached_dna.to_dict(), "cached": True} |
| 168 | + |
| 169 | + # Extract fresh DNA |
| 170 | + logger.info("Extracting codebase DNA", repo_id=repo_id) |
| 171 | + metrics.increment("dna_extractions") |
| 172 | + |
| 173 | + dna = dna_extractor.extract_dna(repo["local_path"], repo_id) |
| 174 | + dna_extractor.save_to_cache(repo_id, dna) |
| 175 | + |
| 176 | + if format == "markdown": |
| 177 | + return {"dna": dna.to_markdown(), "cached": False} |
| 178 | + return {**dna.to_dict(), "cached": False} |
| 179 | + |
| 180 | + except Exception as e: |
| 181 | + logger.error("Error extracting DNA", repo_id=repo_id, error=str(e)) |
| 182 | + raise HTTPException(status_code=500, detail=str(e)) |
0 commit comments