#!/usr/bin/env python3
|
"""
|
创建/追加L2层 (journal/)
|
"""
|
|
import sys
|
import argparse
|
from datetime import datetime
|
from pathlib import Path
|
|
|
def main():
|
parser = argparse.ArgumentParser(description="写入L2层记忆")
|
parser.add_argument("--date", default=datetime.now().strftime("%Y-%m-%d"), help="日期 (YYYY-MM-DD)")
|
parser.add_argument("--title", required=True, help="事件标题")
|
parser.add_argument("--content", help="内容(或从stdin读取)")
|
parser.add_argument("--file", help="从文件读取内容")
|
args = parser.parse_args()
|
|
workspace = Path.home() / ".openclaw" / "workspace"
|
journal_dir = workspace / "memory" / "journal"
|
journal_dir.mkdir(parents=True, exist_ok=True)
|
|
journal_file = journal_dir / f"{args.date}.md"
|
|
# 获取内容
|
content = ""
|
if args.file:
|
with open(args.file, 'r', encoding='utf-8') as f:
|
content = f.read()
|
elif args.content:
|
content = args.content
|
elif not sys.stdin.isatty():
|
content = sys.stdin.read()
|
|
now = datetime.now().strftime("%H:%M")
|
|
# 构建记录
|
entry = f"\n## [{now}] {args.title}\n\n{content}\n"
|
|
# 写入文件
|
if journal_file.exists():
|
with open(journal_file, 'a', encoding='utf-8') as f:
|
f.write(entry)
|
print(f"✅ 已追加到 {journal_file}")
|
else:
|
header = f"# {args.date}\n"
|
with open(journal_file, 'w', encoding='utf-8') as f:
|
f.write(header + entry)
|
print(f"✅ 已创建 {journal_file}")
|
|
return 0
|
|
|
if __name__ == "__main__":
|
sys.exit(main())
|