Skip to content

Your lightweight open-source personal agent.

feat: implement automatic workspace template synchronization

Hon Jia Xuancb999ae82600
diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py
index 1c20b50fd..9dee1054f 100644
--- a/nanobot/cli/commands.py
+++ b/nanobot/cli/commands.py
@@ -186,7 +186,8 @@ def onboard():
console.print(f"[green]✓[/green] Created workspace at {workspace}")
# Create default bootstrap files
- _create_workspace_templates(workspace)
+ from nanobot.utils.helpers import sync_workspace_templates
+ sync_workspace_templates(workspace)
console.print(f"\n{__logo__} nanobot is ready!")
console.print("\nNext steps:")
@@ -198,36 +199,6 @@ def onboard():
-def _create_workspace_templates(workspace: Path):
- """Create default workspace template files from bundled templates."""
- from importlib.resources import files as pkg_files
-
- templates_dir = pkg_files("nanobot") / "templates"
-
- for item in templates_dir.iterdir():
- if not item.name.endswith(".md"):
- continue
- dest = workspace / item.name
- if not dest.exists():
- dest.write_text(item.read_text(encoding="utf-8"), encoding="utf-8")
- console.print(f" [dim]Created {item.name}[/dim]")
-
- memory_dir = workspace / "memory"
- memory_dir.mkdir(exist_ok=True)
-
- memory_template = templates_dir / "memory" / "MEMORY.md"
- memory_file = memory_dir / "MEMORY.md"
- if not memory_file.exists():
- memory_file.write_text(memory_template.read_text(encoding="utf-8"), encoding="utf-8")
- console.print(" [dim]Created memory/MEMORY.md[/dim]")
-
- history_file = memory_dir / "HISTORY.md"
- if not history_file.exists():
- history_file.write_text("", encoding="utf-8")
- console.print(" [dim]Created memory/HISTORY.md[/dim]")
-
- (workspace / "skills").mkdir(exist_ok=True)
-
def _make_provider(config: Config):
"""Create the appropriate LLM provider from config."""
@@ -294,6 +265,8 @@ 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)
session_manager = SessionManager(config.workspace_path)
@@ -447,6 +420,8 @@ 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()
provider = _make_provider(config)
@@ -1008,6 +983,8 @@ 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 06d8fd515..83653ac75 100644
--- a/nanobot/utils/helpers.py
+++ b/nanobot/utils/helpers.py
@@ -76,4 +76,67 @@ def parse_session_key(key: str) -> tuple[str, str]:
parts = key.split(":", 1)
if len(parts) != 2:
raise ValueError(f"Invalid session key: {key}")
- return parts[0], parts[1]
\ No newline at end of file
+ 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.
+ """
+ from importlib.resources import files as pkg_files
+ from rich.console import Console
+ console = Console()
+ added = []
+
+ try:
+ templates_dir = pkg_files("nanobot") / "templates"
+ except Exception:
+ # Fallback for some environments where pkg_files might fail
+ return []
+
+ if not templates_dir.is_dir():
+ return []
+
+ # 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
+
+ # 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
+ (workspace / "skills").mkdir(exist_ok=True)
+
+ # Print notices if files were added
+ if added and not silent:
+ for name in added:
+ console.print(f" [dim]Created {name}[/dim]")
+
+ return added
\ No newline at end of file