JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
feat(workspace): add legacy workspace migration
z2tng0a4c37ec7805
diff --git a/jiuwenclaw/app.py b/jiuwenclaw/app.pyindex 143e799cf7..09492442d1 100644--- a/jiuwenclaw/app.py+++ b/jiuwenclaw/app.py@@ -16,8 +16,13 @@ from dotenv import load_dotenvfrom jiuwenclaw.utils import get_user_workspace_dir, get_env_file, prepare_workspace-_config_file = get_user_workspace_dir() / "config" / "config.yaml"-if not _config_file.exists():+_workspace_dir = get_user_workspace_dir()+_config_file = _workspace_dir / "config" / "config.yaml"+_new_workspace = _workspace_dir / "agent" / "jiuwenclaw_workspace"+_old_workspace = _workspace_dir / "agent" / "workspace"++# Initialize if config doesn't exist, or if legacy workspace exists but new doesn't (migration)+if not _config_file.exists() or (_old_workspace.exists() and not _new_workspace.exists()):prepare_workspace(overwrite=False)load_dotenv(dotenv_path=get_env_file())diff --git a/jiuwenclaw/app_agentserver.py b/jiuwenclaw/app_agentserver.pyindex d3ba5006e4..2e574271a2 100644--- a/jiuwenclaw/app_agentserver.py+++ b/jiuwenclaw/app_agentserver.py@@ -25,8 +25,13 @@ from jiuwenclaw.utils import get_user_workspace_dir, get_env_file, prepare_worksapply_openai_model_client_patch()# Ensure workspace initialized-_config_file = get_user_workspace_dir() / "config" / "config.yaml"-if not _config_file.exists():+_workspace_dir = get_user_workspace_dir()+_config_file = _workspace_dir / "config" / "config.yaml"+_new_workspace = _workspace_dir / "agent" / "jiuwenclaw_workspace"+_old_workspace = _workspace_dir / "agent" / "workspace"++# Initialize if config doesn't exist, or if legacy workspace exists but new doesn't (migration)+if not _config_file.exists() or (_old_workspace.exists() and not _new_workspace.exists()):prepare_workspace(overwrite=False)for _lg in LogManager.get_all_loggers().values():diff --git a/jiuwenclaw/app_gateway.py b/jiuwenclaw/app_gateway.pyindex 3c2cfe5178..1ee317dc9c 100644--- a/jiuwenclaw/app_gateway.py+++ b/jiuwenclaw/app_gateway.py@@ -34,8 +34,13 @@ from jiuwenclaw.utils import get_user_workspace_dir, get_env_file, prepare_worksapply_openai_model_client_patch()# Ensure workspace initialized-_config_file = get_user_workspace_dir() / "config" / "config.yaml"-if not _config_file.exists():+_workspace_dir = get_user_workspace_dir()+_config_file = _workspace_dir / "config" / "config.yaml"+_new_workspace = _workspace_dir / "agent" / "jiuwenclaw_workspace"+_old_workspace = _workspace_dir / "agent" / "workspace"++# Initialize if config doesn't exist, or if legacy workspace exists but new doesn't (migration)+if not _config_file.exists() or (_old_workspace.exists() and not _new_workspace.exists()):prepare_workspace(overwrite=False)# Reduce openjiuwen internal logs (keep Gateway logs)diff --git a/jiuwenclaw/config.py b/jiuwenclaw/config.pyindex a5e12c97f2..76215211cb 100644--- a/jiuwenclaw/config.py+++ b/jiuwenclaw/config.py@@ -317,3 +317,69 @@ def update_memory_forbidden_in_config(updates: dict[str, Any]) -> None:else:section[k] = v_dump_yaml_round_trip(_CONFIG_YAML_PATH, data)+++def migrate_config_from_template(+ template_path: Path,+ user_config_path: Path,+) -> bool:+ """Simplified config migration: merge template and user config.++ Merge rules (for first two levels):+ 1. First level: keep user value if exists, otherwise use template value+ 2. Second level: keep user value if exists, otherwise add from template+ 3. Exceptions (overwrite directly):+ - react.context_engine_config+ - permissions.tools++ Args:+ template_path: Path to template config.yaml+ user_config_path: Path to user config.yaml++ Returns:+ True if migration was performed, False otherwise.+ """+ if not user_config_path.exists():+ return False++ if not template_path.exists():+ return False++ template_data = _load_yaml_round_trip(template_path)+ user_data = _load_yaml_round_trip(user_config_path)++ if template_data is None:+ return False++ if user_data is None:+ user_data = {}++ modified = False++ # First level traversal+ for key, value in template_data.items():+ if key not in user_data:+ # User doesn't have this key, use template value+ user_data[key] = value+ modified = True+ elif isinstance(value, dict) and isinstance(user_data[key], dict):+ # Traverse second level of template+ for sub_key, sub_value in value.items():+ # Check for overwrite exceptions+ is_overwrite = (+ key == "react" and sub_key == "context_engine_config"+ ) or (key == "permissions" and sub_key == "tools")++ if is_overwrite:+ # Overwrite directly from template+ user_data[key][sub_key] = sub_value+ modified = True+ elif sub_key not in user_data[key]:+ # User doesn't have this sub-key, add it+ user_data[key][sub_key] = sub_value+ modified = True++ if modified:+ _dump_yaml_round_trip(user_config_path, user_data)++ return modifieddiff --git a/jiuwenclaw/utils.py b/jiuwenclaw/utils.pyindex 4a514b926a..5cb3e96a9b 100644--- a/jiuwenclaw/utils.py+++ b/jiuwenclaw/utils.py@@ -356,6 +356,156 @@ def prompt_preferred_language() -> Optional[Literal["zh", "en"]]:return None+def _get_builtin_skill_names() -> set[str]:+ """Get the set of built-in skill names from package resources."""+ builtin_skills_dir = get_builtin_skills_dir()+ if not builtin_skills_dir.exists():+ return set()+ return {item.name for item in builtin_skills_dir.iterdir() if item.is_dir()}+++def _migrate_legacy_workspace(+ workspace_dir: Path,+ preferred_language: Optional[str] = None,+) -> None:+ """Migrate from legacy layout to new DeepAgent workspace layout.++ Migration:+ - Old: ~/.jiuwenclaw/agent/workspace/ (agent-data.json here)+ - Old: ~/.jiuwenclaw/agent/home/ (PRINCIPLE.md, TONE.md, HEARTBEAT.md)+ - Old: ~/.jiuwenclaw/agent/skills/+ - Old: ~/.jiuwenclaw/agent/memory/++ - New: ~/.jiuwenclaw/agent/jiuwenclaw_workspace/ (DeepAgent standard)++ Mapping:+ - agent/workspace/ -> agent/jiuwenclaw_workspace/ (main workspace)+ - agent/home/HEARTBEAT.md -> agent/jiuwenclaw_workspace/HEARTBEAT.md+ - agent/skills/ -> agent/jiuwenclaw_workspace/skills/+ - agent/memory/ -> agent/jiuwenclaw_workspace/memory/++ Args:+ workspace_dir: Path to workspace root (~/.jiuwenclaw).+ preferred_language: Preferred language for config (zh/en).+ """+ logger.info(f"Migrating from legacy layout: {workspace_dir}")++ old_workspace = workspace_dir / "agent" / "workspace"+ old_home = workspace_dir / "agent" / "home"+ old_skills = workspace_dir / "agent" / "skills"+ old_memory = workspace_dir / "agent" / "memory"++ new_workspace = workspace_dir / "agent" / "jiuwenclaw_workspace"+ new_workspace.mkdir(parents=True, exist_ok=True)++ # 1. Migrate old workspace contents+ if old_workspace.exists():+ for item in old_workspace.iterdir():+ dest = new_workspace / item.name+ if item.is_dir():+ if dest.exists():+ shutil.rmtree(dest)+ shutil.copytree(item, dest)+ else:+ shutil.copy2(item, dest)+ logger.info(f"Migrated workspace: {old_workspace} -> {new_workspace}")++ # 2. Migrate old home files+ if old_home.exists():+ # HEARTBEAT.md -> HEARTBEAT.md (if not exists in new location)+ old_heartbeat = old_home / "HEARTBEAT.md"+ new_heartbeat = new_workspace / "HEARTBEAT.md"+ if old_heartbeat.exists() and not new_heartbeat.exists():+ shutil.copy2(old_heartbeat, new_heartbeat)+ logger.info("Migrated HEARTBEAT.md from home")++ # Merge PRINCIPLE.md and TONE.md into SOUL.md+ old_principle = old_home / "PRINCIPLE.md"+ old_tone = old_home / "TONE.md"+ new_soul = new_workspace / "SOUL.md"+ if not new_soul.exists() and (old_principle.exists() or old_tone.exists()):+ soul_content = ["# Agent Soul\n\n"]+ if old_principle.exists():+ principle_text = old_principle.read_text(encoding="utf-8")+ soul_content.append("## Principles\n\n")+ soul_content.append(principle_text)+ soul_content.append("\n\n")+ if old_tone.exists():+ tone_text = old_tone.read_text(encoding="utf-8")+ soul_content.append("## Tone\n\n")+ soul_content.append(tone_text)+ soul_content.append("\n\n")+ new_soul.write_text("".join(soul_content), encoding="utf-8")+ logger.info("Merged PRINCIPLE.md and TONE.md into SOUL.md")++ new_skills = new_workspace / "skills"+ if old_skills.exists():+ if new_skills.exists():+ shutil.rmtree(new_skills)+ shutil.copytree(old_skills, new_skills)+ logger.info(f"Migrated skills: {old_skills} -> {new_skills}")++ builtin_skill_names = _get_builtin_skill_names()+ for skill_dir in new_skills.iterdir():+ if skill_dir.is_dir() and (skill_dir.name in builtin_skill_names \+ or skill_dir.name in ["daily-report", "skill-creation"]):+ shutil.rmtree(skill_dir)++ # 4. Migrate memory+ new_memory = new_workspace / "memory"+ if old_memory.exists():+ new_memory.mkdir(parents=True, exist_ok=True)++ # 4.1 Migrate USER.md to workspace root (not in memory/)+ old_user = old_memory / "USER.md"+ new_user = new_workspace / "USER.md"+ if old_user.exists() and not new_user.exists():+ shutil.copy2(old_user, new_user)+ logger.info("Migrated USER.md from memory/ to workspace root")++ # 4.2 Create daily_memory directory and migrate date-based .md files+ daily_memory = new_memory / "daily_memory"+ daily_memory.mkdir(parents=True, exist_ok=True)++ for item in old_memory.iterdir():+ if item.name == "USER.md":+ continue # Already handled above+ if item.name == "MEMORY.md":+ # Copy MEMORY.md to new location+ shutil.copy2(item, new_memory / "MEMORY.md")+ elif item.is_file() and item.suffix == ".md":+ # Date-based memory files (e.g., 2025-01-01.md) go to daily_memory/+ shutil.copy2(item, daily_memory / item.name)+ logger.info(f"Migrated daily memory: {item.name}")+ elif item.is_dir():+ # Copy other directories as-is+ dest = new_memory / item.name+ if dest.exists():+ shutil.rmtree(dest)+ shutil.copytree(item, dest)++ logger.info(f"Migrated memory: {old_memory} -> {new_memory}")++ # 5. Clean up old directories after successful migration+ try:+ if old_workspace.exists():+ shutil.rmtree(old_workspace)+ logger.info(f"Removed old workspace: {old_workspace}")+ if old_home.exists():+ shutil.rmtree(old_home)+ logger.info(f"Removed old home: {old_home}")+ if old_skills.exists():+ shutil.rmtree(old_skills)+ logger.info(f"Removed old skills: {old_skills}")+ if old_memory.exists():+ shutil.rmtree(old_memory)+ logger.info(f"Removed old memory: {old_memory}")+ except OSError as e:+ logger.warning(f"Failed to remove some old directories: {e}")++ logger.info(f"Migration completed: {new_workspace}")++def prepare_workspace(overwrite: bool = True, preferred_language: Optional[str] = None):package_root = _find_package_root()if not package_root:@@ -364,6 +514,34 @@ def prepare_workspace(overwrite: bool = True, preferred_language: Optional[str]workspace_dir = get_user_workspace_dir()workspace_dir.mkdir(parents=True, exist_ok=True)+ # Check for legacy workspace migration or cleanup+ old_workspace = workspace_dir / "agent" / "workspace"+ old_home = workspace_dir / "agent" / "home"+ old_skills = workspace_dir / "agent" / "skills"+ old_memory = workspace_dir / "agent" / "memory"+ new_workspace = workspace_dir / "agent" / "jiuwenclaw_workspace"++ # If old workspace exists and new workspace does not exist, migrate (for start command)+ if old_workspace.exists() and not new_workspace.exists() and not overwrite:+ _migrate_legacy_workspace(workspace_dir, preferred_language)+ # If overwrite (init command), clean up old legacy directories first+ elif overwrite:+ try:+ if old_workspace.exists():+ shutil.rmtree(old_workspace)+ logger.info(f"Removed old workspace: {old_workspace}")+ if old_home.exists():+ shutil.rmtree(old_home)+ logger.info(f"Removed old home: {old_home}")+ if old_skills.exists():+ shutil.rmtree(old_skills)+ logger.info(f"Removed old skills: {old_skills}")+ if old_memory.exists():+ shutil.rmtree(old_memory)+ logger.info(f"Removed old memory: {old_memory}")+ except OSError as e:+ logger.warning(f"Failed to remove some old directories: {e}")+# ----- config: copy config.yaml -----resources_dir = package_root / "resources"config_yaml_src_candidates = [@@ -427,7 +605,6 @@ def prepare_workspace(overwrite: bool = True, preferred_language: Optional[str]template_agent_workspace = template_agent_dir / "jiuwenclaw_workspace"template_agent_memory = template_agent_dir / "jiuwenclaw_workspace" / "memory"- template_agent_skills = template_agent_dir / "jiuwenclaw_workspace" / "skills"def _copy_dir(src_dir: Path,@@ -474,21 +651,24 @@ def prepare_workspace(overwrite: bool = True, preferred_language: Optional[str]]for src_name, dst_name in multilang_files:src_path = template_agent_workspace / src_name- if src_path.exists():- shutil.copy2(src_path, deepagent_workspace / dst_name)+ dst_path = deepagent_workspace / dst_name+ if src_path.exists() and not dst_path.exists():+ shutil.copy2(src_path, dst_path)# skills state: shipped under resources/skills_state_src = template_root / "skills_state.json"if skills_state_src.exists():agent_skills.mkdir(parents=True, exist_ok=True)- shutil.copy2(skills_state_src, agent_skills / "skills_state.json")+ dest_skill_state = agent_skills / "skills_state.json"+ if not dest_skill_state.exists():+ shutil.copy2(skills_state_src, agent_skills / "skills_state.json")# sessions is runtime-only (template may not include it)agent_sessions.mkdir(parents=True, exist_ok=True)- # 与 home 模板语言一致,写回顶层 preferred_language- from jiuwenclaw.config import set_preferred_language_in_config_file+ from jiuwenclaw.config import migrate_config_from_template, set_preferred_language_in_config_file+ migrate_config_from_template(config_yaml_src, config_yaml_dest)set_preferred_language_in_config_file(config_yaml_dest, resolved_lang)