55"""
66import json , os , pathlib , datetime , logging
77from file_lock import atomic_json_write
8+ from utils import get_openclaw_home
89
910log = logging .getLogger ('sync_agent_config' )
1011logging .basicConfig (level = logging .INFO , format = '%(asctime)s [%(name)s] %(message)s' , datefmt = '%H:%M:%S' )
1112
1213# Auto-detect project root (parent of scripts/)
1314BASE = pathlib .Path (__file__ ).parent .parent
1415DATA = BASE / 'data'
15- OPENCLAW_CFG = pathlib .Path .home () / '.openclaw' / 'openclaw.json'
16+ OPENCLAW_HOME = get_openclaw_home ()
17+ OPENCLAW_CFG = OPENCLAW_HOME / 'openclaw.json'
1618
1719ID_LABEL = {
1820 'taizi' : {'label' : '太子' , 'role' : '太子' , 'duty' : '飞书消息分拣与回奏' , 'emoji' : '🤴' },
@@ -137,7 +139,7 @@ def main():
137139 if ag_id not in ID_LABEL :
138140 continue
139141 meta = ID_LABEL [ag_id ]
140- workspace = ag .get ('workspace' , str (pathlib . Path . home () / f'.openclaw/ workspace-{ ag_id } ' ))
142+ workspace = ag .get ('workspace' , str (OPENCLAW_HOME / f'workspace-{ ag_id } ' ))
141143 if 'allowAgents' in ag :
142144 allow_agents = ag .get ('allowAgents' , []) or []
143145 else :
@@ -155,13 +157,13 @@ def main():
155157
156158 # 补充不在 openclaw.json agents list 中的 agent(兼容旧版 main)
157159 EXTRA_AGENTS = {
158- 'taizi' : {'model' : default_model , 'workspace' : str (pathlib . Path . home () / '.openclaw/ workspace-taizi' ),
160+ 'taizi' : {'model' : default_model , 'workspace' : str (OPENCLAW_HOME / 'workspace-taizi' ),
159161 'allowAgents' : ['zhongshu' ]},
160- 'main' : {'model' : default_model , 'workspace' : str (pathlib . Path . home () / '.openclaw/ workspace-main' ),
162+ 'main' : {'model' : default_model , 'workspace' : str (OPENCLAW_HOME / 'workspace-main' ),
161163 'allowAgents' : ['zhongshu' ,'menxia' ,'shangshu' ,'hubu' ,'libu' ,'bingbu' ,'xingbu' ,'gongbu' ,'libu_hr' ]},
162- 'zaochao' : {'model' : default_model , 'workspace' : str (pathlib . Path . home () / '.openclaw/ workspace-zaochao' ),
164+ 'zaochao' : {'model' : default_model , 'workspace' : str (OPENCLAW_HOME / 'workspace-zaochao' ),
163165 'allowAgents' : []},
164- 'libu_hr' : {'model' : default_model , 'workspace' : str (pathlib . Path . home () / '.openclaw/ workspace-libu_hr' ),
166+ 'libu_hr' : {'model' : default_model , 'workspace' : str (OPENCLAW_HOME / 'workspace-libu_hr' ),
165167 'allowAgents' : ['shangshu' ]},
166168 }
167169 for ag_id , extra in EXTRA_AGENTS .items ():
@@ -265,7 +267,7 @@ def sync_scripts_to_workspaces():
265267 return
266268 synced = 0
267269 for proj_name , runtime_id in _SOUL_DEPLOY_MAP .items ():
268- ws_scripts = pathlib . Path . home () / f'.openclaw/ workspace-{ runtime_id } ' / 'scripts'
270+ ws_scripts = OPENCLAW_HOME / f'workspace-{ runtime_id } ' / 'scripts'
269271 ws_scripts .mkdir (parents = True , exist_ok = True )
270272 for src_file in scripts_src .iterdir ():
271273 if src_file .suffix not in ('.py' , '.sh' ) or src_file .stem .startswith ('__' ):
@@ -277,7 +279,7 @@ def sync_scripts_to_workspaces():
277279 except Exception :
278280 continue
279281 # also sync to workspace-main for legacy compatibility
280- ws_main_scripts = pathlib . Path . home () / '.openclaw/ workspace-main/ scripts'
282+ ws_main_scripts = OPENCLAW_HOME / 'workspace-main' / ' scripts'
281283 ws_main_scripts .mkdir (parents = True , exist_ok = True )
282284 for src_file in scripts_src .iterdir ():
283285 if src_file .suffix not in ('.py' , '.sh' ) or src_file .stem .startswith ('__' ):
@@ -300,7 +302,7 @@ def deploy_soul_files():
300302 src = agents_dir / proj_name / 'SOUL.md'
301303 if not src .exists ():
302304 continue
303- ws_dst = pathlib . Path . home () / f'.openclaw/ workspace-{ runtime_id } ' / 'soul.md'
305+ ws_dst = OPENCLAW_HOME / f'workspace-{ runtime_id } ' / 'soul.md'
304306 ws_dst .parent .mkdir (parents = True , exist_ok = True )
305307 # 只在内容不同时更新(避免不必要的写入)
306308 src_text = src .read_text (encoding = 'utf-8' , errors = 'ignore' )
@@ -313,7 +315,7 @@ def deploy_soul_files():
313315 deployed += 1
314316 # 太子兼容:同步一份到 legacy main agent 目录
315317 if runtime_id == 'taizi' :
316- ag_dst = pathlib . Path . home () / '.openclaw/ agents/ main/ SOUL.md'
318+ ag_dst = OPENCLAW_HOME / 'agents' / ' main' / ' SOUL.md'
317319 ag_dst .parent .mkdir (parents = True , exist_ok = True )
318320 try :
319321 ag_text = ag_dst .read_text (encoding = 'utf-8' , errors = 'ignore' )
@@ -322,7 +324,7 @@ def deploy_soul_files():
322324 if src_text != ag_text :
323325 ag_dst .write_text (src_text , encoding = 'utf-8' )
324326 # 确保 sessions 目录存在
325- sess_dir = pathlib . Path . home () / f'.openclaw/ agents/ { runtime_id } / sessions'
327+ sess_dir = OPENCLAW_HOME / ' agents' / runtime_id / ' sessions'
326328 sess_dir .mkdir (parents = True , exist_ok = True )
327329 if deployed :
328330 log .info (f'{ deployed } SOUL.md files deployed' )
0 commit comments