File size: 2,031 Bytes
a02272f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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)