Files

34 lines
780 B
Python

#!/usr/bin/env python3
import sys
import json
import os
args = json.loads(sys.stdin.read())
topic = args.get("topic", "note")
content = args.get("content", "")
workspace = os.environ.get("WORKSPACE", "/workspace")
mem_dir = f"{workspace}/memory"
os.makedirs(mem_dir, exist_ok=True)
# Write the memory file
filepath = f"{mem_dir}/{topic}.md"
with open(filepath, "w") as f:
f.write(content + "\n")
# Update MEMORY.md index
index_path = f"{workspace}/MEMORY.md"
existing = ""
try:
with open(index_path) as f:
existing = f.read()
except FileNotFoundError:
existing = "# Agent Memory\n"
entry = f"- [{topic}](memory/{topic}.md)"
if topic not in existing:
with open(index_path, "a") as f:
f.write(f"\n{entry}")
print(f"Memory saved to {filepath}")