refactor: streamline workspace template sync

This commit is contained in:
Re-bin
2026-02-27 09:46:57 +00:00
parent e260219ce6
commit d5808bf586
2 changed files with 21 additions and 56 deletions

View File

@@ -20,6 +20,7 @@ from prompt_toolkit.patch_stdout import patch_stdout
from nanobot import __version__, __logo__ from nanobot import __version__, __logo__
from nanobot.config.schema import Config from nanobot.config.schema import Config
from nanobot.utils.helpers import sync_workspace_templates
app = typer.Typer( app = typer.Typer(
name="nanobot", name="nanobot",
@@ -185,8 +186,6 @@ def onboard():
workspace.mkdir(parents=True, exist_ok=True) workspace.mkdir(parents=True, exist_ok=True)
console.print(f"[green]✓[/green] Created workspace at {workspace}") 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) sync_workspace_templates(workspace)
console.print(f"\n{__logo__} nanobot is ready!") console.print(f"\n{__logo__} nanobot is ready!")
@@ -265,7 +264,6 @@ def gateway(
console.print(f"{__logo__} Starting nanobot gateway on port {port}...") console.print(f"{__logo__} Starting nanobot gateway on port {port}...")
config = load_config() config = load_config()
from nanobot.utils.helpers import sync_workspace_templates
sync_workspace_templates(config.workspace_path) sync_workspace_templates(config.workspace_path)
bus = MessageBus() bus = MessageBus()
provider = _make_provider(config) provider = _make_provider(config)
@@ -420,7 +418,6 @@ def agent(
from loguru import logger from loguru import logger
config = load_config() config = load_config()
from nanobot.utils.helpers import sync_workspace_templates
sync_workspace_templates(config.workspace_path) sync_workspace_templates(config.workspace_path)
bus = MessageBus() bus = MessageBus()
@@ -983,8 +980,6 @@ def status():
config_path = get_config_path() config_path = get_config_path()
config = load_config() config = load_config()
workspace = config.workspace_path workspace = config.workspace_path
from nanobot.utils.helpers import sync_workspace_templates
sync_workspace_templates(workspace)
console.print(f"{__logo__} nanobot Status\n") console.print(f"{__logo__} nanobot Status\n")

View File

@@ -79,64 +79,34 @@ def parse_session_key(key: str) -> tuple[str, str]:
return parts[0], parts[1] return parts[0], parts[1]
def sync_workspace_templates(workspace: Path, silent: bool = False) -> list[str]: def sync_workspace_templates(workspace: Path, silent: bool = False) -> list[str]:
""" """Sync bundled templates to workspace. Only creates missing files."""
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 importlib.resources import files as pkg_files
from rich.console import Console
console = Console()
added = []
try: try:
templates_dir = pkg_files("nanobot") / "templates" tpl = pkg_files("nanobot") / "templates"
except Exception: except Exception:
# Fallback for some environments where pkg_files might fail return []
if not tpl.is_dir():
return [] return []
if not templates_dir.is_dir(): added: list[str] = []
return []
# 1. Sync root templates def _write(src, dest: Path):
for item in templates_dir.iterdir(): """Write src content (or empty string if None) to dest if missing."""
if not item.name.endswith(".md"): if dest.exists():
continue return
dest = workspace / item.name dest.parent.mkdir(parents=True, exist_ok=True)
if not dest.exists(): dest.write_text(src.read_text(encoding="utf-8") if src else "", encoding="utf-8")
try: added.append(str(dest.relative_to(workspace)))
dest.write_text(item.read_text(encoding="utf-8"), encoding="utf-8")
added.append(item.name)
except Exception:
pass
# 2. Sync memory templates for item in tpl.iterdir():
memory_dir = workspace / "memory" if item.name.endswith(".md"):
memory_dir.mkdir(exist_ok=True) _write(item, workspace / item.name)
_write(tpl / "memory" / "MEMORY.md", workspace / "memory" / "MEMORY.md")
memory_src = templates_dir / "memory" / "MEMORY.md" _write(None, workspace / "memory" / "HISTORY.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) (workspace / "skills").mkdir(exist_ok=True)
# Print notices if files were added
if added and not silent: if added and not silent:
from rich.console import Console
for name in added: for name in added:
console.print(f" [dim]Created {name}[/dim]") Console().print(f" [dim]Created {name}[/dim]")
return added
return added