Spaces:
Sleeping
Sleeping
| """brain import — Inject the .brain/ runtime bridge into the current workspace.""" | |
| import shutil | |
| import typer | |
| from pathlib import Path | |
| from brain.config import get_active_brain | |
| from brain.paths import get_workspace_dir | |
| from brain.templates.workspace_runtime import generate_workspace_runtime | |
| from brain.utils.display import error, success, info | |
| # Directories/files to skip when doing a full import (avoid recursive bloat) | |
| _FULL_IMPORT_EXCLUDES = {"versions", ".git"} | |
| def import_brain( | |
| full: bool = typer.Option( | |
| False, | |
| "--full", | |
| help="Import the full Brain Instance structure into the workspace runtime.", | |
| ), | |
| ) -> None: | |
| """Create the .brain/ workspace runtime in the current directory.""" | |
| active = get_active_brain() | |
| if not active or not active.exists(): | |
| error("No active Brain selected.") | |
| error("Run: brain use <brain-name>") | |
| raise typer.Exit(code=1) | |
| workspace = get_workspace_dir() | |
| if workspace.exists(): | |
| info("Workspace runtime already exists. Regenerating...") | |
| generate_workspace_runtime( | |
| workspace_brain_dir=workspace, | |
| brain_name=active.name, | |
| brain_path=active, | |
| ) | |
| if full: | |
| _copy_brain_contents(active, workspace) | |
| success("Full Brain imported into workspace runtime.") | |
| else: | |
| success("Workspace runtime created.") | |
| info(f"Path: {workspace}") | |
| def _copy_brain_contents(brain_path: Path, workspace: Path) -> None: | |
| """Copy the full Brain Instance structure into .brain/ (excluding versions).""" | |
| for item in brain_path.iterdir(): | |
| if item.name in _FULL_IMPORT_EXCLUDES: | |
| continue | |
| dest = workspace / item.name | |
| # Skip files we already generate (AGENT.md, BRAIN.json, etc.) | |
| if dest.exists() and item.is_file(): | |
| continue | |
| if item.is_dir(): | |
| if dest.exists(): | |
| shutil.rmtree(dest) | |
| shutil.copytree(item, dest) | |
| else: | |
| shutil.copy2(item, dest) | |