From d5808bf586c09a16ea6ba5ba5d48674d7466a38c Mon Sep 17 00:00:00 2001 From: Re-bin Date: Fri, 27 Feb 2026 09:46:57 +0000 Subject: [PATCH] refactor: streamline workspace template sync --- nanobot/cli/commands.py | 7 +--- nanobot/utils/helpers.py | 70 ++++++++++++---------------------------- 2 files changed, 21 insertions(+), 56 deletions(-) diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index 9dee105..fc4c261 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -20,6 +20,7 @@ from prompt_toolkit.patch_stdout import patch_stdout from nanobot import __version__, __logo__ from nanobot.config.schema import Config +from nanobot.utils.helpers import sync_workspace_templates app = typer.Typer( name="nanobot", @@ -185,8 +186,6 @@ def onboard(): workspace.mkdir(parents=True, exist_ok=True) console.print(f"[green]✓[/green] Created workspace at {workspace}") - # Create default bootstrap files - from nanobot.utils.helpers import sync_workspace_templates sync_workspace_templates(workspace) console.print(f"\n{__logo__} nanobot is ready!") @@ -265,7 +264,6 @@ def gateway( console.print(f"{__logo__} Starting nanobot gateway on port {port}...") config = load_config() - from nanobot.utils.helpers import sync_workspace_templates sync_workspace_templates(config.workspace_path) bus = MessageBus() provider = _make_provider(config) @@ -420,7 +418,6 @@ def agent( from loguru import logger config = load_config() - from nanobot.utils.helpers import sync_workspace_templates sync_workspace_templates(config.workspace_path) bus = MessageBus() @@ -983,8 +980,6 @@ def status(): config_path = get_config_path() config = load_config() workspace = config.workspace_path - from nanobot.utils.helpers import sync_workspace_templates - sync_workspace_templates(workspace) console.print(f"{__logo__} nanobot Status\n") diff --git a/nanobot/utils/helpers.py b/nanobot/utils/helpers.py index 83653ac..8963138 100644 --- a/nanobot/utils/helpers.py +++ b/nanobot/utils/helpers.py @@ -79,64 +79,34 @@ def parse_session_key(key: str) -> tuple[str, str]: return parts[0], parts[1] def sync_workspace_templates(workspace: Path, silent: bool = False) -> list[str]: - """ - Synchronize default workspace template files from bundled templates. - Only creates files that do not exist. Returns list of added file names. - """ + """Sync bundled templates to workspace. Only creates missing files.""" from importlib.resources import files as pkg_files - from rich.console import Console - console = Console() - added = [] - try: - templates_dir = pkg_files("nanobot") / "templates" + tpl = pkg_files("nanobot") / "templates" except Exception: - # Fallback for some environments where pkg_files might fail + return [] + if not tpl.is_dir(): return [] - if not templates_dir.is_dir(): - return [] + added: list[str] = [] - # 1. Sync root templates - for item in templates_dir.iterdir(): - if not item.name.endswith(".md"): - continue - dest = workspace / item.name - if not dest.exists(): - try: - dest.write_text(item.read_text(encoding="utf-8"), encoding="utf-8") - added.append(item.name) - except Exception: - pass + def _write(src, dest: Path): + """Write src content (or empty string if None) to dest if missing.""" + if dest.exists(): + return + dest.parent.mkdir(parents=True, exist_ok=True) + dest.write_text(src.read_text(encoding="utf-8") if src else "", encoding="utf-8") + added.append(str(dest.relative_to(workspace))) - # 2. Sync memory templates - memory_dir = workspace / "memory" - memory_dir.mkdir(exist_ok=True) - - memory_src = templates_dir / "memory" / "MEMORY.md" - memory_dest = memory_dir / "MEMORY.md" - if memory_src.is_file() and not memory_dest.exists(): - try: - memory_dest.write_text(memory_src.read_text(encoding="utf-8"), encoding="utf-8") - added.append("memory/MEMORY.md") - except Exception: - pass - - # 3. History file (always ensure it exists) - history_file = memory_dir / "HISTORY.md" - if not history_file.exists(): - try: - history_file.write_text("", encoding="utf-8") - added.append("memory/HISTORY.md") - except Exception: - pass - - # 4. Ensure skills dir exists + for item in tpl.iterdir(): + if item.name.endswith(".md"): + _write(item, workspace / item.name) + _write(tpl / "memory" / "MEMORY.md", workspace / "memory" / "MEMORY.md") + _write(None, workspace / "memory" / "HISTORY.md") (workspace / "skills").mkdir(exist_ok=True) - # Print notices if files were added if added and not silent: + from rich.console import Console for name in added: - console.print(f" [dim]Created {name}[/dim]") - - return added \ No newline at end of file + Console().print(f" [dim]Created {name}[/dim]") + return added