1  #!/usr/bin/env python3
   2  import sys
   3  import sqlite3
   4  import os
   5  import subprocess
   6  
   7  query = " ".join(sys.argv[1:]).strip()
   8  
   9  # Check local cache or remote Dell
  10  project_dir = os.path.dirname(os.path.abspath(__file__))
  11  cache_db = os.path.join(project_dir, ".memory_cache.db")
  12  dell_db = "/var/neural_memory/data/memory.db"
  13  
  14  if os.path.exists(dell_db):
  15      db_path = dell_db
  16  elif os.path.exists(cache_db):
  17      db_path = cache_db
  18  else:
  19      print("[!] Local memory cache not found. Run ~/project/sync_memory.sh first.")
  20      sys.exit(1)
  21  
  22  conn = sqlite3.connect(db_path)
  23  cur = conn.cursor()
  24  
  25  print(f"=== NEURAL RECALL: Query \"{query}\" (from {os.path.basename(db_path)}) ===\n")
  26  
  27  if not query:
  28      print("Listing recent facts and skills:")
  29      cur.execute("SELECT category, subject, content FROM facts ORDER BY id DESC LIMIT 5;")
  30      for cat, subj, cont in cur.fetchall():
  31          print(f"• [{cat.upper()}] {subj}: {cont}")
  32      conn.close()
  33      sys.exit(0)
  34  
  35  # Search facts
  36  cur.execute("""
  37      SELECT category, subject, content, timestamp FROM facts 
  38      WHERE subject LIKE ? OR content LIKE ? OR category LIKE ?
  39      ORDER BY id DESC LIMIT 10;
  40  """, (f"%{query}%", f"%{query}%", f"%{query}%"))
  41  facts = cur.fetchall()
  42  
  43  if facts:
  44      print("--- FACTS & HARDWARE ---")
  45      for cat, subj, cont, ts in facts:
  46          print(f"• [{cat}] {subj}: {cont} (logged: {ts[:10]})")
  47      print()
  48  
  49  # Search episodes
  50  cur.execute("""
  51      SELECT title, context, actions, outcome, timestamp FROM episodes
  52      WHERE title LIKE ? OR context LIKE ? OR actions LIKE ? OR outcome LIKE ?
  53      ORDER BY id DESC LIMIT 5;
  54  """, (f"%{query}%", f"%{query}%", f"%{query}%", f"%{query}%"))
  55  episodes = cur.fetchall()
  56  
  57  if episodes:
  58      print("--- RELEVANT EPISODES ---")
  59      for title, ctx, act, out, ts in episodes:
  60          print(f"• [{ts[:10]}] {title}")
  61          print(f"  Actions: {act}")
  62          print(f"  Outcome: {out}\n")
  63  
  64  # Search skills
  65  cur.execute("""
  66      SELECT name, trigger, command_template, description FROM skills
  67      WHERE name LIKE ? OR trigger LIKE ? OR description LIKE ?;
  68  """, (f"%{query}%", f"%{query}%", f"%{query}%"))
  69  skills = cur.fetchall()
  70  
  71  if skills:
  72      print("--- RELEVANT SKILLS / COMMANDS ---")
  73      for name, trig, cmd, desc in skills:
  74          print(f"• Skill: {name} ({desc})")
  75          print(f"  Command: {cmd}\n")
  76  
  77  conn.close()
  78