feat(context): Implement directory grouping and file stats in context composition panel

This commit is contained in:
2026-05-11 11:37:15 -04:00
parent daf887eed4
commit 5112debe14
3 changed files with 151 additions and 52 deletions
+34
View File
@@ -15,6 +15,7 @@ to use the MCP tools to fetch only what it needs.
import tomllib
import re
import glob
import os
from pathlib import Path, PureWindowsPath
from typing import Any, cast
from src import summarize
@@ -59,6 +60,39 @@ def resolve_paths(base_dir: Path, entry: str) -> list[Path]:
filtered.append(p)
return sorted(filtered)
def group_files_by_dir(files: list[Any]) -> dict[str, list[Any]]:
"""Groups FileItem objects by their relative directory path."""
grouped = {}
for f in files:
path_str = f.path if hasattr(f, 'path') else str(f)
# Normalize path separators
path_str = path_str.replace('\\', '/')
dir_name = os.path.dirname(path_str)
if not dir_name:
dir_name = "."
if dir_name not in grouped:
grouped[dir_name] = []
grouped[dir_name].append(f)
return grouped
def compute_file_stats(abs_path: str) -> dict[str, int]:
"""Computes lines and basic AST stats for a file."""
stats = {"lines": 0, "ast_elements": 0}
try:
with open(abs_path, 'r', encoding='utf-8') as f:
content = f.read()
stats["lines"] = len(content.splitlines())
if abs_path.endswith('.py'):
import ast
try:
tree = ast.parse(content)
stats["ast_elements"] = sum(1 for node in ast.walk(tree) if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)))
except Exception:
pass
except Exception:
pass
return stats
def build_discussion_section(history: list[Any]) -> str:
"""