#!/usr/bin/env python3 """ 快速写入L0层 (MEMORY.md) """ import sys import argparse from datetime import datetime from pathlib import Path def main(): parser = argparse.ArgumentParser(description="写入L0层记忆") parser.add_argument("content", help="记录内容") parser.add_argument("--link", help="关联的L2文件路径") parser.add_argument("--type", default="活动", help="记录类型") args = parser.parse_args() workspace = Path.home() / ".openclaw" / "workspace" memory_file = workspace / "MEMORY.md" # 确保文件存在 if not memory_file.exists(): print(f"❌ {memory_file} 不存在") return 1 today = datetime.now().strftime("%Y-%m-%d") # 构建记录行 line = f"- **[{args.type}]** {args.content}" if args.link: line += f" → 详见 [{args.link}](./{args.link})" # 读取现有内容 with open(memory_file, 'r', encoding='utf-8') as f: content = f.read() # 在"最近活动"部分添加 if "### 最近活动" in content: parts = content.split("### 最近活动") if len(parts) == 2: # 在第一行后插入 lines = parts[1].split('\n') insert_idx = 0 for i, l in enumerate(lines): if l.strip() and not l.startswith('#'): insert_idx = i break lines.insert(insert_idx, f"- {today}: {args.content}") new_content = parts[0] + "### 最近活动" + '\n'.join(lines) with open(memory_file, 'w', encoding='utf-8') as f: f.write(new_content) print(f"✅ 已写入L0: {args.content}") return 0 print("⚠️ 未找到'最近活动'区块,请手动添加") return 1 if __name__ == "__main__": sys.exit(main())