Spaces:
Sleeping
Sleeping
File size: 918 Bytes
b946ba0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# ai/code_memory.py
import os, json, datetime
MEMORY_PATH = "./user_data/code_memory.json"
def load_memory():
if not os.path.exists(MEMORY_PATH):
return []
try:
with open(MEMORY_PATH, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return []
def save_to_memory(query: str, code: str, emotion: str, skill: str):
"""Store each AI-generated code snippet and context."""
os.makedirs(os.path.dirname(MEMORY_PATH), exist_ok=True)
memory = load_memory()
record = {
"timestamp": datetime.datetime.now().isoformat(),
"query": query,
"code": code.strip(),
"emotion": emotion,
"skill": skill
}
memory.append(record)
with open(MEMORY_PATH, "w", encoding="utf-8") as f:
json.dump(memory[-100:], f, indent=2, ensure_ascii=False) # keep last 100 entries
|