Spaces:
Sleeping
Sleeping
File size: 22,579 Bytes
60344c1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
import ast
import os
import hashlib
from pathlib import Path
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass
from datetime import datetime
import json
import logging
logger = logging.getLogger("code_compass")
@dataclass
class CodeChunk:
"""Represents a hierarchical code chunk with metadata"""
id: str
content: str
chunk_type: str # 'file', 'class', 'function', 'block'
metadata: Dict[str, Any]
embedding: Optional[List[float]] = None
def to_dict(self) -> Dict[str, Any]:
"""Convert chunk to dictionary for storage"""
return {
'id': self.id,
'content': self.content,
'chunk_type': self.chunk_type,
'metadata': self.metadata,
'embedding': self.embedding
}
class HierarchicalChunker:
"""
Advanced hierarchical code chunker that creates multiple levels of chunks:
Level 1: File-level context
Level 2: Class-level chunks
Level 3: Function-level chunks
Level 4: Code block chunks (for complex functions)
"""
def __init__(self, complexity_threshold: int = 20):
self.complexity_threshold = complexity_threshold
self.supported_extensions = {
'.py': self._parse_python,
'.js': self._parse_javascript,
'.ts': self._parse_typescript,
'.java': self._parse_java,
'.cpp': self._parse_cpp,
'.c': self._parse_c,
'.go': self._parse_go,
'.rs': self._parse_rust,
# Add more as needed
}
def chunk_repository(self, repo_path: str) -> List[CodeChunk]:
"""
Main method to chunk entire repository hierarchically
"""
chunks = []
repo_name = os.path.basename(repo_path)
logger.info(f"π Starting hierarchical chunking of {repo_name}...")
# Walk through repository
for root, dirs, files in os.walk(repo_path):
# Skip common non-code directories
dirs[:] = [d for d in dirs if not d.startswith('.') and
d not in ['node_modules', '__pycache__', 'venv', 'env', 'dist', 'build']]
for file in files:
if self._should_process_file(file):
file_path = os.path.join(root, file)
relative_path = os.path.relpath(file_path, repo_path)
try:
file_chunks = self._process_file(file_path, relative_path, repo_name)
logger.debug(f"File chunks: {[chunk.to_dict() for chunk in file_chunks]}")
chunks.extend(file_chunks)
logger.info(f"β
Processed {relative_path} -> {len(file_chunks)} chunks")
except Exception as e:
logger.info(f"β Error processing {relative_path}: {str(e)}")
continue
logger.info(f"π Chunking complete! Generated {len(chunks)} total chunks")
return chunks
def _should_process_file(self, filename: str) -> bool:
"""Check if file should be processed for chunking"""
ext = Path(filename).suffix.lower()
# Skip files that are too large or unwanted
unwanted_files = {
'package-lock.json', 'yarn.lock', 'poetry.lock',
'requirements.txt', '.gitignore', 'README.md',
'LICENSE', 'CHANGELOG.md'
}
if filename in unwanted_files:
return False
# Process code files
return ext in self.supported_extensions or ext in [
'.py', '.js', '.ts', '.jsx', '.tsx', '.java', '.cpp', '.c', '.h',
'.go', '.rs', '.php', '.rb', '.swift', '.kt', '.scala', '.cs'
]
def _process_file(self, file_path: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Process a single file and generate hierarchical chunks"""
chunks = []
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
except Exception as e:
logger.info(f"β Could not read {relative_path}: {e}")
return chunks
if not content.strip():
return chunks
file_ext = Path(file_path).suffix.lower()
# Level 1: File-level chunk
file_chunk = self._create_file_chunk(content, relative_path, repo_name)
chunks.append(file_chunk)
# Language-specific parsing for deeper levels
if file_ext in self.supported_extensions:
try:
deeper_chunks = self.supported_extensions[file_ext](content, relative_path, repo_name)
chunks.extend(deeper_chunks)
except Exception as e:
logger.info(f"β οΈ Advanced parsing failed for {relative_path}, using basic chunking: {e}")
# Fallback to basic function extraction
basic_chunks = self._basic_function_extraction(content, relative_path, repo_name)
chunks.extend(basic_chunks)
else:
# For unsupported languages, do basic function/class detection
basic_chunks = self._basic_function_extraction(content, relative_path, repo_name)
chunks.extend(basic_chunks)
return chunks
def _create_file_chunk(self, content: str, relative_path: str, repo_name: str) -> CodeChunk:
"""Create Level 1: File-level context chunk"""
# Extract file summary info
lines = content.split('\n')
total_lines = len(lines)
# Get imports/includes
imports = self._extract_imports(content, Path(relative_path).suffix)
# Create condensed file overview
file_summary = f"""File: {relative_path}
Lines: {total_lines}
Language: {Path(relative_path).suffix}
Imports/Dependencies:
{chr(10).join(imports[:10])} # Show first 10 imports
File Purpose: {self._infer_file_purpose(relative_path, content)}
Main Components:
{self._extract_main_components_summary(content, Path(relative_path).suffix)}
"""
chunk_id = self._generate_chunk_id(repo_name, relative_path, "file", "")
metadata = {
'repo_name': repo_name,
'file_path': relative_path,
'chunk_type': 'file',
'level': 1,
'language': Path(relative_path).suffix,
'total_lines': total_lines,
'imports': imports,
'timestamp': datetime.now().isoformat()
}
return CodeChunk(
id=chunk_id,
content=file_summary,
chunk_type='file',
metadata=metadata
)
def _parse_python(self, content: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Parse Python files for classes and functions"""
chunks = []
try:
tree = ast.parse(content)
except SyntaxError as e:
logger.info(f"β οΈ Python syntax error in {relative_path}: {e}")
return self._basic_function_extraction(content, relative_path, repo_name)
# Level 2: Class chunks
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef):
class_chunk = self._create_class_chunk(node, content, relative_path, repo_name)
chunks.append(class_chunk)
# Level 3: Method chunks within class
for method in [n for n in node.body if isinstance(n, ast.FunctionDef)]:
method_chunk = self._create_function_chunk(
method, content, relative_path, repo_name,
parent_class=node.name
)
chunks.append(method_chunk)
# Level 4: Complex method sub-chunks
if self._calculate_complexity(method) > self.complexity_threshold:
sub_chunks = self._create_sub_chunks(method, content, relative_path, repo_name)
chunks.extend(sub_chunks)
# Level 3: Standalone functions
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
# Skip if it's inside a class (already handled above)
parent_classes = [n for n in ast.walk(tree) if isinstance(n, ast.ClassDef)
if any(isinstance(child, ast.FunctionDef) and child.name == node.name
for child in ast.walk(n))]
if not parent_classes:
func_chunk = self._create_function_chunk(node, content, relative_path, repo_name)
chunks.append(func_chunk)
# Level 4: Complex function sub-chunks
if self._calculate_complexity(node) > self.complexity_threshold:
sub_chunks = self._create_sub_chunks(node, content, relative_path, repo_name)
chunks.extend(sub_chunks)
return chunks
def _create_class_chunk(self, class_node: ast.ClassDef, content: str, relative_path: str, repo_name: str) -> CodeChunk:
"""Create Level 2: Class-level chunk"""
lines = content.split('\n')
class_content = self._extract_node_content(class_node, lines)
# Get class methods summary
methods = [n.name for n in class_node.body if isinstance(n, ast.FunctionDef)]
# Get docstring
docstring = ast.get_docstring(class_node) or "No docstring available"
# Get inheritance info
bases = [self._get_node_name(base) for base in class_node.bases] if class_node.bases else []
class_summary = f"""Class: {class_node.name}
File: {relative_path}
Inheritance: {' -> '.join(bases) if bases else 'No inheritance'}
Docstring:
{docstring[:300]}...
Methods ({len(methods)}):
{', '.join(methods)}
Full Class Definition:
{class_content[:1000]}... # Truncated for embedding
"""
chunk_id = self._generate_chunk_id(repo_name, relative_path, "class", class_node.name)
metadata = {
'repo_name': repo_name,
'file_path': relative_path,
'chunk_type': 'class',
'level': 2,
'class_name': class_node.name,
'methods': methods,
'inheritance': bases,
'line_start': class_node.lineno,
'line_end': getattr(class_node, 'end_lineno', class_node.lineno),
'docstring': docstring,
'timestamp': datetime.now().isoformat()
}
return CodeChunk(
id=chunk_id,
content=class_summary,
chunk_type='class',
metadata=metadata
)
def _create_function_chunk(self, func_node: ast.FunctionDef, content: str, relative_path: str,
repo_name: str, parent_class: Optional[str] = None) -> CodeChunk:
"""Create Level 3: Function-level chunk"""
lines = content.split('\n')
func_content = self._extract_node_content(func_node, lines)
# Get function signature
args = [arg.arg for arg in func_node.args.args]
signature = f"{func_node.name}({', '.join(args)})"
# Get docstring
docstring = ast.get_docstring(func_node) or "No docstring available"
# Calculate complexity
complexity = self._calculate_complexity(func_node)
func_summary = f"""Function: {signature}
File: {relative_path}
Class: {parent_class or 'Standalone function'}
Complexity Score: {complexity}
Docstring:
{docstring[:200]}...
Function Implementation:
{func_content}
"""
chunk_id = self._generate_chunk_id(
repo_name, relative_path, "function",
f"{parent_class}.{func_node.name}" if parent_class else func_node.name
)
metadata = {
'repo_name': repo_name,
'file_path': relative_path,
'chunk_type': 'function',
'level': 3,
'function_name': func_node.name,
'class_name': parent_class,
'signature': signature,
'arguments': args,
'complexity': complexity,
'line_start': func_node.lineno,
'line_end': getattr(func_node, 'end_lineno', func_node.lineno),
'docstring': docstring,
'timestamp': datetime.now().isoformat()
}
return CodeChunk(
id=chunk_id,
content=func_summary,
chunk_type='function',
metadata=metadata
)
def _create_sub_chunks(self, func_node: ast.FunctionDef, content: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Create Level 4: Sub-chunks for complex functions"""
chunks = []
# For now, create logical blocks based on control structures
lines = content.split('\n')
func_lines = lines[func_node.lineno-1:getattr(func_node, 'end_lineno', func_node.lineno)]
# Simple block detection based on indentation and keywords
blocks = self._detect_code_blocks(func_lines, func_node.name)
for i, block in enumerate(blocks):
if len(block['content']) > 50: # Only create chunks for substantial blocks
chunk_id = self._generate_chunk_id(
repo_name, relative_path, "block", f"{func_node.name}_block_{i}"
)
block_summary = f"""Code Block {i+1} in {func_node.name}()
Type: {block['type']}
Purpose: {block['purpose']}
Code:
{block['content']}
"""
metadata = {
'repo_name': repo_name,
'file_path': relative_path,
'chunk_type': 'block',
'level': 4,
'function_name': func_node.name,
'block_index': i,
'block_type': block['type'],
'block_purpose': block['purpose'],
'timestamp': datetime.now().isoformat()
}
chunks.append(CodeChunk(
id=chunk_id,
content=block_summary,
chunk_type='block',
metadata=metadata
))
return chunks
# Helper methods
def _extract_imports(self, content: str, file_ext: str) -> List[str]:
"""Extract import statements based on language"""
imports = []
lines = content.split('\n')
if file_ext == '.py':
for line in lines[:50]: # Check first 50 lines
stripped = line.strip()
if stripped.startswith(('import ', 'from ')):
imports.append(stripped)
elif file_ext in ['.js', '.ts']:
for line in lines[:50]:
stripped = line.strip()
if stripped.startswith(('import ', 'const ', 'require(')):
imports.append(stripped)
return imports
def _infer_file_purpose(self, relative_path: str, content: str) -> str:
"""Infer the purpose of a file based on path and content"""
filename = os.path.basename(relative_path).lower()
if 'test' in filename:
return "Test file"
elif 'config' in filename:
return "Configuration file"
elif 'util' in filename or 'helper' in filename:
return "Utility/Helper functions"
elif '__init__' in filename:
return "Package initialization"
elif 'main' in filename:
return "Main entry point"
elif 'model' in filename:
return "Data model/schema definition"
elif 'view' in filename:
return "View/UI component"
elif 'controller' in filename:
return "Controller/Logic handler"
else:
# Analyze content for clues
if 'class ' in content and 'def __init__' in content:
return "Class definition file"
elif 'def ' in content:
return "Function definitions"
else:
return "Code file"
def _extract_main_components_summary(self, content: str, file_ext: str) -> str:
"""Extract summary of main components (classes, functions)"""
if file_ext != '.py':
return "Component analysis available for Python files only"
try:
tree = ast.parse(content)
classes = [node.name for node in ast.walk(tree) if isinstance(node, ast.ClassDef)]
functions = [node.name for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)]
summary = ""
if classes:
summary += f"Classes: {', '.join(classes[:5])}\n"
if functions:
summary += f"Functions: {', '.join(functions[:10])}\n"
return summary or "No major components detected"
except:
return "Could not analyze components"
def _basic_function_extraction(self, content: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Fallback function extraction using regex patterns"""
chunks = []
# This is a simplified fallback - you can enhance with regex patterns
# for different languages
return chunks
def _extract_node_content(self, node: ast.AST, lines: List[str]) -> str:
"""Extract the actual code content for an AST node"""
start_line = node.lineno - 1
end_line = getattr(node, 'end_lineno', node.lineno) - 1
if end_line >= len(lines):
end_line = len(lines) - 1
return '\n'.join(lines[start_line:end_line + 1])
def _get_node_name(self, node: ast.AST) -> str:
"""Get the name of an AST node"""
if hasattr(node, 'id'):
return node.id
elif hasattr(node, 'attr'):
return node.attr
else:
return str(node)
def _calculate_complexity(self, node: ast.FunctionDef) -> int:
"""Calculate cyclomatic complexity of a function"""
complexity = 1 # Base complexity
for child in ast.walk(node):
if isinstance(child, (ast.If, ast.While, ast.For, ast.With, ast.Try)):
complexity += 1
elif isinstance(child, ast.ExceptHandler):
complexity += 1
elif isinstance(child, (ast.And, ast.Or)):
complexity += 1
return complexity
def _detect_code_blocks(self, func_lines: List[str], func_name: str) -> List[Dict[str, str]]:
"""Detect logical code blocks within a function"""
blocks = []
current_block = []
block_type = "sequential"
for line in func_lines:
stripped = line.strip()
if any(keyword in stripped for keyword in ['if ', 'elif ', 'else:']):
if current_block:
blocks.append({
'content': '\n'.join(current_block),
'type': block_type,
'purpose': f"Logic block in {func_name}"
})
current_block = []
block_type = "conditional"
elif any(keyword in stripped for keyword in ['for ', 'while ']):
if current_block:
blocks.append({
'content': '\n'.join(current_block),
'type': block_type,
'purpose': f"Logic block in {func_name}"
})
current_block = []
block_type = "loop"
elif any(keyword in stripped for keyword in ['try:', 'except', 'finally:']):
if current_block:
blocks.append({
'content': '\n'.join(current_block),
'type': block_type,
'purpose': f"Logic block in {func_name}"
})
current_block = []
block_type = "exception_handling"
current_block.append(line)
if current_block:
blocks.append({
'content': '\n'.join(current_block),
'type': block_type,
'purpose': f"Final block in {func_name}"
})
return blocks
def _generate_chunk_id(self, repo_name: str, file_path: str, chunk_type: str, identifier: str) -> str:
"""Generate unique chunk ID"""
unique_string = f"{repo_name}:{file_path}:{chunk_type}:{identifier}"
return hashlib.md5(unique_string.encode()).hexdigest()
# Placeholder methods for other languages
def _parse_javascript(self, content: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Parse JavaScript files - placeholder for now"""
return []
def _parse_typescript(self, content: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Parse TypeScript files - placeholder for now"""
return []
def _parse_java(self, content: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Parse Java files - placeholder for now"""
return []
def _parse_cpp(self, content: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Parse C++ files - placeholder for now"""
return []
def _parse_c(self, content: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Parse C files - placeholder for now"""
return []
def _parse_go(self, content: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Parse Go files - placeholder for now"""
return []
def _parse_rust(self, content: str, relative_path: str, repo_name: str) -> List[CodeChunk]:
"""Parse Rust files - placeholder for now"""
return [] |