Bundler¶
codebrief.tools.bundler
¶
Bundle Generator for CodeBrief.
This module provides functions to create comprehensive context bundles by orchestrating calls to multiple CodeBrief tools. It aggregates outputs from tree generation, Git context, dependency listing, and file flattening into a single, well-structured Markdown document suitable for LLM consumption.
Core functionalities: - Orchestrate multiple tool outputs into a single bundle - Support configurable inclusion/exclusion of different context sections - Handle multiple flatten specifications for different parts of the project - Generate structured Markdown with clear sectioning and navigation - Graceful error handling when individual tools encounter issues - Support for both file output and console display
Functions¶
generate_tree_content(project_root, config_global_excludes)
¶
Generate directory tree content for the bundle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_root | Path | The root directory of the project | required |
config_global_excludes | List[str] | Global exclude patterns from config | required |
Returns:
Type | Description |
---|---|
str | Formatted tree content as string |
Source code in src/codebrief/tools/bundler.py
generate_git_content(project_root, log_count=5, full_diff=False, diff_options=None)
¶
Generate Git context content for the bundle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_root | Path | The root directory of the project | required |
log_count | int | Number of recent commits to include | 5 |
full_diff | bool | Whether to include full diff output | False |
diff_options | Optional[str] | Optional diff options string | None |
Returns:
Type | Description |
---|---|
str | Formatted Git context as string |
Source code in src/codebrief/tools/bundler.py
generate_deps_content(project_root)
¶
Generate dependency list content for the bundle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_root | Path | The root directory of the project | required |
Returns:
Type | Description |
---|---|
str | Formatted dependency content as string |
Source code in src/codebrief/tools/bundler.py
generate_flatten_content(project_root, flatten_path, config_global_excludes)
¶
Generate flattened file content for a specific path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_root | Path | The root directory of the project | required |
flatten_path | Path | The specific path to flatten | required |
config_global_excludes | List[str] | Global exclude patterns from config | required |
Returns:
Type | Description |
---|---|
str | Formatted flattened content as string |
Source code in src/codebrief/tools/bundler.py
create_bundle(project_root, output_file_path=None, include_tree=True, include_git=True, include_deps=True, flatten_paths=None, git_log_count=5, git_full_diff=False, git_diff_options=None)
¶
Create a comprehensive context bundle by aggregating multiple tool outputs.
This function orchestrates calls to various CodeBrief tools and combines their outputs into a single, well-structured Markdown document. The bundle includes a table of contents and clear sectioning for easy navigation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_root | Path | The root directory of the project to bundle | required |
output_file_path | Optional[Path] | Optional path to save the bundle. If None, returns string | None |
include_tree | bool | Whether to include directory tree (default: True) | True |
include_git | bool | Whether to include Git context (default: True) | True |
include_deps | bool | Whether to include dependency information (default: True) | True |
flatten_paths | Optional[List[Path]] | Optional list of paths to flatten and include | None |
git_log_count | int | Number of recent commits to include in Git context | 5 |
git_full_diff | bool | Whether to include full diff in Git context | False |
git_diff_options | Optional[str] | Optional Git diff options string | None |
Returns:
Type | Description |
---|---|
Optional[str] | Bundle content as string if no output file specified, None otherwise. |
Raises:
Type | Description |
---|---|
Exit | If project_root is invalid or critical errors occur |
Source code in src/codebrief/tools/bundler.py
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 |
|
Modules¶
Overview¶
The bundler
module provides comprehensive context aggregation functionality, combining multiple codebrief tools into structured, well-organized bundles. It orchestrates the execution of tree generation, Git context extraction, dependency analysis, and file flattening to create comprehensive project context documents.
Key Features¶
- Multi-Tool Integration: Combines tree, git-info, deps, and flatten tools
- Flexible Configuration: Selective inclusion/exclusion of context sections
- Structured Output: Well-organized Markdown with table of contents
- Path Flexibility: Support for multiple flatten paths and custom configurations
- Error Resilience: Graceful handling of tool failures and missing components
Functions¶
create_bundle¶
Create a comprehensive context bundle combining multiple tools.
Parameters: - project_root
(Path): Root directory of the project - output_file_path
(Optional[Path]): Output file path (None for stdout) - exclude_tree
(bool, optional): Skip directory tree section (default: False) - exclude_git
(bool, optional): Skip Git context section (default: False) - exclude_deps
(bool, optional): Skip dependencies section (default: False) - exclude_files
(bool, optional): Skip flattened files section (default: False) - flatten_paths
(Optional[List[Path]]): Specific paths to flatten (default: [project_root]) - git_log_count
(int, optional): Number of Git commits to include (default: 10) - git_full_diff
(bool, optional): Include full Git diff (default: False) - git_diff_options
(Optional[str]): Custom Git diff options (default: None)
Returns: - None (outputs to file or stdout)
Raises: - FileNotFoundError
: If project_root doesn't exist - PermissionError
: If output file cannot be written - Various exceptions from underlying tools (handled gracefully)
Helper Functions¶
generate_tree_content¶
Generate directory tree content for the bundle.
Parameters: - project_root
(Path): Root directory - config_global_excludes
(List[str]): Global exclude patterns
Returns: - str
: Formatted tree content
generate_git_content¶
Generate Git context content for the bundle.
Parameters: - project_root
(Path): Root directory - log_count
(int): Number of commits - full_diff
(bool): Include full diff - diff_options
(Optional[str]): Custom diff options
Returns: - str
: Formatted Git content
generate_deps_content¶
Generate dependencies content for the bundle.
Parameters: - project_root
(Path): Root directory
Returns: - str
: Formatted dependencies content
generate_flatten_content¶
Generate flattened files content for the bundle.
Parameters: - project_root
(Path): Root directory - flatten_paths
(List[Path]): Paths to flatten - config_global_excludes
(List[str]): Global exclude patterns
Returns: - str
: Formatted flattened content
Usage Examples¶
Basic Usage¶
from pathlib import Path
from codebrief.tools.bundler import create_bundle
# Create complete bundle
create_bundle(
project_root=Path("."),
output_file_path=Path("project-bundle.md")
)
Advanced Configuration¶
# Create selective bundle for code review
create_bundle(
project_root=Path("."),
output_file_path=Path("review-bundle.md"),
exclude_deps=True,
flatten_paths=[Path("src"), Path("tests")],
git_log_count=5,
git_full_diff=True
)
CLI Integration¶
# This is how the CLI command uses the function
from codebrief.tools.bundler import create_bundle
def bundle_command(
root_dir: Path,
output: Optional[Path] = None,
exclude_tree: bool = False,
exclude_git: bool = False,
exclude_deps: bool = False,
exclude_files: bool = False,
flatten: Optional[List[Path]] = None,
git_log_count: int = 10,
git_full_diff: bool = False,
git_diff_options: Optional[str] = None,
):
create_bundle(
project_root=root_dir,
output_file_path=output,
exclude_tree=exclude_tree,
exclude_git=exclude_git,
exclude_deps=exclude_deps,
exclude_files=exclude_files,
flatten_paths=flatten or [root_dir],
git_log_count=git_log_count,
git_full_diff=git_full_diff,
git_diff_options=git_diff_options,
)
Output Structure¶
The bundler creates well-organized Markdown documents:
# codebrief Bundle
## Table of Contents
- [Directory Tree](#directory-tree)
- [Git Context](#git-context)
- [Dependencies](#dependencies)
- [Files: src/codebrief/tools](#files-srccodebrieftools)
## Directory Tree
π my-project/
βββ π README.md
βββ π src/
β βββ π codebrief/
βββ π tests/
## Git Context
# Git Context
## Repository Information
- **Current Branch:** main
- **Repository Status:** Clean working directory
## Recent Commits (Last 10)
[Git commit history...]
## Dependencies
# Dependencies
## Python Dependencies (pyproject.toml)
- typer: ^0.9.0
- rich: ^13.0.0
## Files: src/codebrief/tools
# --- File: src/codebrief/tools/bundler.py ---
[File contents...]
# --- File: src/codebrief/tools/git_provider.py ---
[File contents...]
Bundle Sections¶
Directory Tree Section¶
- Uses the
tree_generator
module - Respects
.llmignore
and configuration patterns - Provides visual project structure overview
Git Context Section¶
- Uses the
git_provider
module - Includes branch info, status, and commit history
- Optional full diff and custom diff options
Dependencies Section¶
- Uses the
dependency_lister
module - Analyzes Python and Node.js dependencies
- Supports multiple dependency file formats
Files Section¶
- Uses the
flattener
module - Aggregates file contents from specified paths
- Intelligent file filtering and binary file handling
Error Handling¶
The bundler handles various error scenarios:
Missing Tools¶
# If a tool fails, the section is skipped with a note
create_bundle(Path("."))
# Output includes: "## Git Context\n*Git context unavailable*"
Invalid Paths¶
# Graceful handling of non-existent flatten paths
create_bundle(
project_root=Path("."),
flatten_paths=[Path("nonexistent")]
)
# Skips invalid paths, continues with valid ones
Permission Issues¶
# Clear error messages for file access issues
create_bundle(
project_root=Path("."),
output_file_path=Path("/etc/bundle.md")
)
# Raises PermissionError with helpful message
Configuration Integration¶
Works seamlessly with codebrief's configuration system:
[tool.codebrief]
default_output_filename_bundle = "project-bundle.md"
global_exclude_patterns = ["*.pyc", "__pycache__/", ".venv/"]
Performance Considerations¶
- Parallel Processing: Tools run independently where possible
- Memory Efficient: Streams output to files for large projects
- Configurable Scope: Selective inclusion reduces processing time
- Caching: Reuses configuration and ignore patterns across tools
Testing¶
The module includes comprehensive test coverage:
- 7 test cases covering core functionality
- Helper function testing for individual components
- Integration testing with real project structures
- Error scenario testing for robustness
- Configuration integration testing
Dependencies¶
- pathlib: For path handling
- typing: For type annotations
- io.StringIO: For output capture and manipulation
- codebrief.tools.tree_generator: Directory tree generation
- codebrief.tools.git_provider: Git context extraction
- codebrief.tools.dependency_lister: Dependency analysis
- codebrief.tools.flattener: File content aggregation
- codebrief.utils.config_manager: Configuration management
Related Modules¶
tree_generator
: Directory structure visualizationgit_provider
: Git context extractiondependency_lister
: Dependency analysisflattener
: File content aggregationconfig_manager
: Configuration managementmain
: CLI integration and command handling
Best Practices¶
Bundle Composition¶
# For code review
create_bundle(
exclude_deps=True,
flatten_paths=[Path("src"), Path("tests")],
git_log_count=5
)
# For documentation
create_bundle(
exclude_git=True,
flatten_paths=[Path("docs"), Path("README.md")]
)
# For debugging
create_bundle(
git_full_diff=True,
flatten_paths=[Path("src")]
)