feat(aggregate): Support custom view mode with annotated slices

This commit is contained in:
2026-05-11 18:52:22 -04:00
parent d22c98c9ac
commit 1303fc1402
2 changed files with 55 additions and 0 deletions
+16
View File
@@ -214,6 +214,22 @@ def build_file_items(base_dir: Path, files: list[str | dict[str, Any]]) -> list[
content = summarize.summarise_file(path, content)
elif view_mode == "none":
content = "(context excluded)"
elif view_mode == "custom":
if custom_slices:
lines = content.splitlines()
slices_text = []
for s in custom_slices:
start = s.get("start_line", 1)
end = s.get("end_line", len(lines))
tag = s.get("tag", "unnamed")
comment = s.get("comment", "")
s_idx = max(0, start - 1)
e_idx = min(len(lines), end)
chunk = "\n".join(lines[s_idx:e_idx])
slices_text.append(f"---\n[Slice: {tag}] ({comment})\nLines {start}-{end}:\n{chunk}")
content = "\n\n".join(slices_text)
else:
content = summarize.summarise_file(path, content)
except FileNotFoundError:
content = f"ERROR: file not found: {path}"
mtime = 0.0
+39
View File
@@ -124,3 +124,42 @@ def test_files_section_rendering(tmp_path):
assert "```txt\n**Summary** content\n```" not in sections
assert "**Summary** content" in sections
def test_view_mode_custom(tmp_path):
base_dir = tmp_path / "project"
base_dir.mkdir()
test_file = base_dir / "test.py"
content = "line1\nline2\nline3\nline4\nline5\n"
test_file.write_text(content, encoding="utf-8")
slices = [
{"start_line": 1, "end_line": 2, "tag": "top", "comment": "header"},
{"start_line": 4, "end_line": 5, "tag": "bottom", "comment": "footer"}
]
files = [{"path": "test.py", "view_mode": "custom", "custom_slices": slices}]
items = aggregate.build_file_items(base_dir, files)
assert len(items) == 1
assert items[0]["view_mode"] == "custom"
# Check formatting
expected_1 = "---\n[Slice: top] (header)\nLines 1-2:\nline1\nline2"
expected_2 = "---\n[Slice: bottom] (footer)\nLines 4-5:\nline4\nline5"
assert expected_1 in items[0]["content"]
assert expected_2 in items[0]["content"]
assert "line3" not in items[0]["content"]
def test_view_mode_custom_empty_default_to_summary(tmp_path):
base_dir = tmp_path / "project"
base_dir.mkdir()
test_file = base_dir / "test.py"
test_file.write_text("def hello():\n print('world')\n", encoding="utf-8")
files = [{"path": "test.py", "view_mode": "custom", "custom_slices": []}]
items = aggregate.build_file_items(base_dir, files)
assert len(items) == 1
assert items[0]["view_mode"] == "custom"
# Should default to summary
assert "**Python**" in items[0]["content"]