#!/usr/bin/env python3 """Add an item to PA memory. Usage: memory-add.py memory-add.py preference "Always use dark mode" memory-add.py fact "Server IP is 192.168.1.100" memory-add.py auto "Some text to auto-categorize" """ import json import sys import uuid from datetime import datetime from pathlib import Path MEMORY_DIR = Path.home() / ".claude/state/personal-assistant/memory" CATEGORIES = { "preference": "preferences.json", "decision": "decisions.json", "project": "projects.json", "fact": "facts.json", } # Keywords for auto-categorization PREFERENCE_KEYWORDS = ["prefer", "always", "never", "like", "want", "should"] DECISION_KEYWORDS = ["decided", "chose", "chosen", "use", "using", "switched"] PROJECT_KEYWORDS = ["repo", "project", "codebase", "~/", "/home/", "github"] def auto_categorize(content: str) -> str: """Determine category based on content.""" content_lower = content.lower() if any(kw in content_lower for kw in PREFERENCE_KEYWORDS): return "preference" if any(kw in content_lower for kw in DECISION_KEYWORDS): return "decision" if any(kw in content_lower for kw in PROJECT_KEYWORDS): return "project" return "fact" def load_memory(category: str) -> list: """Load existing memory items.""" filepath = MEMORY_DIR / CATEGORIES[category] if filepath.exists(): with open(filepath) as f: data = json.load(f) return data.get("items", []) return [] def save_memory(category: str, items: list): """Save memory items.""" filepath = MEMORY_DIR / CATEGORIES[category] filepath.parent.mkdir(parents=True, exist_ok=True) data = { "version": "1.0", "category": category, "items": items } with open(filepath, "w") as f: json.dump(data, f, indent=2) def add_item(category: str, content: str) -> dict: """Add an item to memory.""" if category == "auto": category = auto_categorize(content) if category not in CATEGORIES: print(f"Error: Invalid category '{category}'") print(f"Valid categories: {', '.join(CATEGORIES.keys())}") sys.exit(1) # Create new item item = { "id": str(uuid.uuid4()), "date": datetime.now().strftime("%Y-%m-%d"), "content": content, "status": "active" } # Load, append, save items = load_memory(category) items.append(item) save_memory(category, items) return {"category": category, "item": item} def main(): if len(sys.argv) < 3: print(__doc__) sys.exit(1) category = sys.argv[1] content = " ".join(sys.argv[2:]) result = add_item(category, content) print(f"✓ Saved to {result['category']}s:") print(f" \"{result['item']['content']}\"") print(f" ID: {result['item']['id'][:8]}...") if __name__ == "__main__": main()