Spaces:
Running
Running
File size: 8,220 Bytes
a31f556 | 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | """
modules/deep_context.py β FRIDAY Deep Context Memory
Remembers everything about the user:
- Current projects and their status
- Friends/family/connections
- Enemies/threats
- Personal history
- Inside jokes
- User preferences
"""
import time
import os
import json
from config import DATA_DIR
CONTEXT_FILE = os.path.join(DATA_DIR, "deep_context.json")
def _load() -> dict:
if not os.path.exists(CONTEXT_FILE):
return {
"projects": {},
"people": {},
"relationships": {},
"jokes": [],
"preferences": {},
"history": [],
}
try:
with open(CONTEXT_FILE, "r") as f:
return json.load(f)
except Exception:
return {}
def _save(data: dict):
os.makedirs(DATA_DIR, exist_ok=True)
try:
with open(CONTEXT_FILE, "w") as f:
json.dump(data, f, indent=2)
except Exception:
pass
# ββ Projects βββββββββββββββββββββββββββββββββββββββββββββββββββ
def track_project(name: str, status: str = "active", description: str = ""):
"""Track a project."""
data = _load()
data.setdefault("projects", {})[name] = {
"name": name,
"status": status,
"description": description,
"updated": time.time(),
}
_save(data)
def get_project(name: str) -> dict | None:
"""Get project details."""
data = _load()
return data.get("projects", {}).get(name)
def get_active_projects() -> list[dict]:
"""Get all active projects."""
data = _load()
projects = data.get("projects", {})
return [p for p in projects.values() if p.get("status") == "active"]
def complete_project(name: str):
"""Mark project as complete."""
data = _load()
if name in data.get("projects", {}):
data["projects"][name]["status"] = "complete"
data["projects"][name]["completed"] = time.time()
_save(data)
# ββ People βββββββββββββββββββββββββββββββββββββββββββββββββββββ
def track_person(name: str, relationship: str = "", notes: str = ""):
"""Track a person in user's life."""
data = _load()
data.setdefault("people", {})[name] = {
"name": name,
"relationship": relationship,
"notes": notes,
"updated": time.time(),
}
_save(data)
def get_person(name: str) -> dict | None:
"""Get person details."""
data = _load()
return data.get("people", {}).get(name)
def get_people(relationship: str = "") -> list[dict]:
"""Get people by relationship."""
data = _load()
people = data.get("people", {})
if not relationship:
return list(people.values())
return [p for p in people.values() if p.get("relationship") == relationship]
# ββ Inside Jokes βββββββββββββββββββββββββββββββββββββββββββββββββββ
def add_joke(joke: str, response: str = ""):
"""Add inside joke/running bit."""
data = _load()
data.setdefault("jokes", []).append({
"joke": joke,
"response": response,
"time": time.time(),
})
# Keep last 30 jokes
jokes = data.get("jokes", [])
if len(jokes) > 30:
jokes = jokes[-30:]
data["jokes"] = jokes
_save(data)
def get_joke(keyword: str) -> str | None:
"""Get response to inside joke."""
data = _load()
jokes = data.get("jokes", [])
for j in reversed(jokes):
if keyword.lower() in j.get("joke", "").lower():
return j.get("response")
return None
# ββ Preferences βββββββββββββββββββββββββββββββββββββββββββββββββ
def set_preference(key: str, value):
"""Remember user preference."""
data = _load()
data.setdefault("preferences", {})[key] = {"value": value, "time": time.time()}
_save(data)
def get_preference(key: str):
"""Get user preference."""
data = _load()
return data.get("preferences", {}).get(key, {}).get("value")
# ββ History βββββββββββββββββββββββββββββββββββββββββββββββββββ
def add_to_history(event: str, category: str = "general"):
"""Add event to history."""
data = _load()
data.setdefault("history", []).append({
"event": event,
"category": category,
"time": time.time(),
})
# Keep last 100
history = data.get("history", [])
if len(history) > 100:
history = history[-100:]
data["history"] = history
_save(data)
def get_history(category: str = "", limit: int = 5) -> list[dict]:
"""Get history."""
data = _load()
history = data.get("history", [])
if category:
history = [h for h in history if h.get("category") == category]
return history[-limit:]
# ββ Query Engine βββββββββββββββββββββββββββββββββββββββββββββββ
def query(query_text: str) -> str:
"""Query deep context."""
q = query_text.lower()
# Check projects
if "project" in q:
active = get_active_projects()
if active:
names = [p.get("name") for p in active]
return f"Active projects: {', '.join(names)}"
return "No active projects."
# Check people
if "friend" in q or "people" in q:
friends = get_people("friend")
if friends:
names = [p.get("name") for p in friends]
return f"Your friends: {', '.join(names)}"
return "No friends tracked."
# Check what project I'm working on
if "working on" in q or "current project" in q:
active = get_active_projects()
if active:
return f"You're working on: {active[0].get('name')}"
return "Not tracking any projects."
return "I don't have that context yet. Teach me?"
# ββ Teach FRIDAY ββββββββββββββββββββββββββββββββββββββββββββββ
def learn(text: str) -> bool:
"""Learn from user text."""
text_lower = text.lower()
# Learn project
if "i'm working on" in text_lower or "working on" in text_lower:
# Extract project name
project = text.replace("i'm working on", "").replace("working on", "").strip()
if project and len(project) > 2:
track_project(project, "active", "user is working on this")
return True
# Learn friend
if "my friend" in text_lower or "friend named" in text_lower:
name = text.replace("my friend", "").replace("friend named", "").strip()
if name and len(name) > 1:
track_person(name, "friend", "from conversation")
return True
return False
# ββ Brain Integration βββββββββββββββββββββββββββββββββββββββββ
def get_context_block() -> str:
"""Get context block for brain."""
data = _load()
parts = []
# Active projects
active = get_active_projects()
if active:
parts.append(f"Projects: {', '.join([p['name'] for p in active])}")
# Key people
people = list(data.get("people", {}).values())[:3]
if people:
parts.append(f"People: {', '.join([p['name'] for p in people])}")
# Recent history
history = data.get("history", [])[-3:]
if history:
parts.append(f"Recent: {', '.join([h['event'] for h in history])}")
if parts:
return "[DEEP CONTEXT] " + " | ".join(parts)
return "" |