Spaces:
Running
Running
File size: 27,307 Bytes
06b628f 95d1c6b 184a33b 95d1c6b 5eb296d 95d1c6b 06b628f cdcf600 184a33b 06b628f cdcf600 b1cc432 06b628f 95d1c6b 06b628f 95d1c6b 5eb296d 06b628f 5eb296d 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 6c52940 42d51e2 95d1c6b 06b628f 95d1c6b 6c52940 42d51e2 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 22fff46 42d51e2 22fff46 95d1c6b 06b628f 95d1c6b 22fff46 42d51e2 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 06b628f 184a33b 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 06b628f b8fda7c 06b628f 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 06b628f 95d1c6b 06b628f 2e9dd8e 95d1c6b 06b628f 95d1c6b 06b628f cdcf600 95d1c6b 06b628f 95d1c6b 86be60c 95d1c6b cdcf600 2e9dd8e cdcf600 2e9dd8e cdcf600 2e9dd8e cdcf600 2e9dd8e cdcf600 2e9dd8e cdcf600 2e9dd8e 95d1c6b 86be60c cdcf600 2e9dd8e 06b628f 86be60c 95d1c6b cdcf600 2e9dd8e cdcf600 2e9dd8e cdcf600 2e9dd8e 06b628f 95d1c6b cdcf600 2e9dd8e 06b628f 95d1c6b 06b628f cdcf600 2e9dd8e cdcf600 95d1c6b 9f79a8d cdcf600 b8fda7c cdcf600 9f79a8d cdcf600 9f79a8d 95d1c6b cdcf600 95d1c6b 06b628f cdcf600 2e9dd8e cdcf600 2e9dd8e 06b628f 5297cf5 06b628f 5297cf5 06b628f 5297cf5 df43d43 | 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | from fastapi import APIRouter, HTTPException, Depends
from fastapi.responses import StreamingResponse
import json
import asyncio
from pydantic import BaseModel
from typing import List, Dict, Optional
from app.services.orchestrator import orchestrator
from app.services.memory import ingest_text
from app.db.session import get_recent_messages, add_message, get_recent_sparks
from app.services.hippocampus import consolidate_memory
from app.services.neocortex import extract_and_store_knowledge
from app.services.sleep_cycle import run_sleep_cycle
from app.db.neo4j_driver import neo4j_db
from app.services.vitals import get_brain_vitals
from app.auth.auth import get_current_user
from app.services.brain_trace import build_brain_event, predict_intent, route_signal, score_attention
router = APIRouter()
def sse_event(event_type: str, payload: dict) -> str:
return f"event: {event_type}\ndata: {json.dumps(payload)}\n\n"
# ββ Brain Vitals βββββββββββββββββββββββββββββββββββββββββββββββββ
@router.get("/brain/vitals")
async def fetch_brain_vitals(current_user: str = Depends(get_current_user)):
return get_brain_vitals(current_user)
@router.get("/brain/sparks")
async def fetch_neural_sparks(limit: int = 5, current_user: str = Depends(get_current_user)):
return get_recent_sparks(user_id=current_user, limit=limit)
# ββ Knowledge Graph βββββββββββββββββββββββββββββββββββββββββββββββ
@router.get("/graph")
async def get_knowledge_graph(current_user: str = Depends(get_current_user)):
if not neo4j_db.driver:
return {"nodes": [], "edges": [], "status": "offline"}
try:
node_query = """
MATCH (n:Entity)
WHERE n.user_id = $user_id
OPTIONAL MATCH (n)-[r]-()
RETURN n.name AS id, count(r) AS connections
ORDER BY connections DESC
"""
node_results = neo4j_db.query(node_query, {"user_id": current_user}) or []
edge_query = """
MATCH (s:Entity)-[r]->(t:Entity)
WHERE (s.user_id = $user_id )
AND (t.user_id = $user_id )
RETURN s.name AS source, type(r) AS label, t.name AS target
"""
edge_results = neo4j_db.query(edge_query, {"user_id": current_user}) or []
nodes = [{"id": r["id"], "label": r["id"], "connections": r["connections"]} for r in node_results]
edges = [{"source": r["source"], "target": r["target"], "label": r["label"]} for r in edge_results]
return {"nodes": nodes, "edges": edges, "status": "online"}
except Exception as e:
return {"nodes": [], "edges": [], "status": "error", "detail": str(e)}
@router.get("/graph/stats")
async def get_graph_stats(current_user: str = Depends(get_current_user)):
if not neo4j_db.driver:
return {"node_count": 0, "edge_count": 0, "top_entities": [], "status": "offline"}
try:
count_query = """
MATCH (n:Entity)
WHERE n.user_id = $user_id
OPTIONAL MATCH (n)-[r]->()
RETURN count(DISTINCT n) AS nodes, count(DISTINCT r) AS edges
"""
counts = neo4j_db.query(count_query, {"user_id": current_user})
node_count = counts[0]["nodes"] if counts else 0
edge_count = counts[0]["edges"] if counts else 0
top_query = """
MATCH (n:Entity)-[r]-()
WHERE n.user_id = $user_id
RETURN n.name AS entity, count(r) AS connections
ORDER BY connections DESC
LIMIT 5
"""
top_results = neo4j_db.query(top_query, {"user_id": current_user}) or []
top_entities = [{"entity": r["entity"], "connections": r["connections"]} for r in top_results]
return {"node_count": node_count, "edge_count": edge_count, "top_entities": top_entities, "status": "online"}
except Exception as e:
return {"node_count": 0, "edge_count": 0, "top_entities": [], "status": "error", "detail": str(e)}
# ββ Request / Response Models βββββββββββββββββββββββββββββββββββββ
class QueryRequest(BaseModel):
text: str
class QueryResponse(BaseModel):
response: str
sources: List[str] = []
class IngestRequest(BaseModel):
text: str
metadata: Optional[Dict] = None
class IngestResponse(BaseModel):
message: str
chunks: int
class ConsolidateRequest(BaseModel):
pass # user_id now comes from token
# ββ Consolidate βββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.post("/consolidate", response_model=IngestResponse)
async def process_consolidation(current_user: str = Depends(get_current_user)):
try:
chunks, msg = consolidate_memory(current_user)
if chunks > 0:
history = get_recent_messages(current_user, exchanges=50)
user_msgs = [m['content'] for m in history if m['role'] == 'user']
doc = "\n".join(user_msgs)
triples = extract_and_store_knowledge(doc, current_user)
msg += f" Extracted {triples} graph relations."
return IngestResponse(message=msg, chunks=chunks)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ββ Sleep βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.post("/sleep")
async def process_sleep_cycle(current_user: str = Depends(get_current_user)):
try:
report = run_sleep_cycle(keep_recent=10)
return report
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ββ Ingest ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.post("/ingest", response_model=IngestResponse)
async def process_ingest(request: IngestRequest, current_user: str = Depends(get_current_user)):
try:
num_chunks = ingest_text(request.text, request.metadata, current_user)
triples = extract_and_store_knowledge(request.text, current_user)
return IngestResponse(
message=f"Sensory data ingested. Extracted {triples} graph relations.",
chunks=num_chunks
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ββ Stream Query ββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.post("/analyze")
async def analyze_text(request: QueryRequest, current_user: str = Depends(get_current_user)):
"""
Analyzes text to preview potential semantic links and cognitive metrics.
Checks existing graph to find potential overlaps.
"""
try:
from langchain_groq import ChatGroq
from langchain_core.messages import HumanMessage
from app.core.config import settings
api_key = settings.GROQ_API_KEY if settings.GROQ_API_KEY else "dummy_key"
llm = ChatGroq(model="llama-3.1-8b-instant", api_key=api_key)
# 1. Extract potential entities
prompt = f"Extract 5-8 key entities (names, concepts, places) from this text as a comma-separated list. Return ONLY the list: {request.text}"
response = await llm.ainvoke([HumanMessage(content=prompt)])
entities = [e.strip() for e in response.content.split(',') if e.strip()]
# 2. Check for existing overlaps in Neo4j
existing_links = []
if neo4j_db.driver:
# Look for entities that already exist for this user
check_query = """
MATCH (n:Entity)
WHERE n.user_id = $user_id AND toLower(n.name) IN $entities
RETURN n.name AS name, count{(n)--()} AS connections
"""
overlaps = neo4j_db.query(check_query, {
"user_id": current_user,
"entities": [e.lower() for e in entities]
}) or []
existing_links = [{"name": o["name"], "connections": o["connections"]} for o in overlaps]
# 3. Calculate metrics
char_count = len(request.text)
chunk_count = (char_count // 500) + 1
return {
"entities": entities,
"existing_links": existing_links,
"metrics": {
"density": min(char_count / 2000, 1.0),
"chunks": chunk_count,
"estimated_links": len(entities) * 1.5,
"reinforcement_index": len(existing_links) / max(len(entities), 1)
}
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/query/stream")
async def process_query_stream(request: QueryRequest, current_user: str = Depends(get_current_user)):
async def event_generator():
try:
history = get_recent_messages(current_user, exchanges=5)
attention = score_attention(request.text, len(history))
routing = route_signal(request.text, attention)
prediction = predict_intent(request.text, attention)
state_input = {
"input": request.text,
"user_id": current_user,
"chat_history": history,
"context": [],
"graph_context": [],
"reflection": "",
"response": ""
}
perception_msg = f"Processing query: {request.text[:50]}..."
yield sse_event("brain_trace", build_brain_event(
"perception",
58,
"Raw language input reached the sensory intake layer.",
next_regions=["thalamus"],
inputs_used=["user_input"],
data={"query": request.text}
))
yield sse_event("trace", {"phase": "perception", "message": perception_msg, "data": {"query": request.text}})
await asyncio.sleep(0.4)
yield sse_event("brain_trace", build_brain_event(
"attention",
attention["salience"],
"Attention scoring estimated urgency, emotion, memory relevance, complexity, and novelty.",
next_regions=["thalamus", "prefrontal_cortex"],
inputs_used=["user_input", "recent_history"],
data=attention
))
yield sse_event("trace", {
"phase": "attention",
"message": f"Attention salience computed at {attention['salience']}%.",
"data": attention
})
await asyncio.sleep(0.4)
if attention["emotional_intensity"] >= 70:
yield sse_event("brain_trace", build_brain_event(
"emotion",
attention["emotional_intensity"],
f"Detected elevated emotional salience associated with {attention['emotion_label']}.",
next_regions=["hippocampus", "prefrontal_cortex"],
inputs_used=["user_input"],
data={"emotion": attention["emotion_label"]}
))
yield sse_event("trace", {
"phase": "emotion",
"message": f"Amygdala analogue flagged {attention['emotion_label']} salience.",
"data": {"emotion": attention["emotion_label"]}
})
await asyncio.sleep(0.4)
yield sse_event("brain_trace", build_brain_event(
"routing",
66,
routing["reason"],
next_regions=routing["regions"],
inputs_used=["attention_scores", "user_input"],
data={"regions": routing["regions"]}
))
yield sse_event("trace", {
"phase": "routing",
"message": f"Routed cognition through {', '.join(routing['regions'])}.",
"data": {"regions": routing["regions"]}
})
await asyncio.sleep(0.4)
yield sse_event("brain_trace", build_brain_event(
"prediction",
prediction["confidence"],
f"Predicted intent: {prediction['intent']}",
next_regions=["working_memory", "prefrontal_cortex"],
inputs_used=["user_input", "attention_scores"],
data=prediction
))
yield sse_event("trace", {
"phase": "prediction",
"message": prediction["intent"],
"data": prediction
})
await asyncio.sleep(0.4)
yield sse_event("brain_trace", build_brain_event(
"working_memory",
52 + min(len(history) * 4, 24),
f"Loaded {len(history)} recent messages into working memory.",
next_regions=["hippocampus", "prefrontal_cortex"],
inputs_used=["recent_history"],
data={"history_count": len(history)}
))
yield sse_event("trace", {
"phase": "working_memory",
"message": f"Loaded {len(history)} recent messages into working memory.",
"data": {"history_count": len(history)}
})
await asyncio.sleep(0.4)
for output in orchestrator.stream(state_input):
for node_name, node_output in output.items():
if node_name == "reflect":
reflection = node_output.get("reflection", "")
yield sse_event("reflection", {"message": reflection})
yield sse_event("brain_trace", build_brain_event(
"reflection",
76,
"Prefrontal planning layer formed an internal intent map.",
next_regions=["hippocampus", "neocortex"],
inputs_used=["user_input", "working_memory"],
data={"reflection": reflection}
))
yield sse_event("trace", {"phase": "reflection", "message": "Intent map formed.", "data": {"reflection": reflection}})
await asyncio.sleep(0.4)
elif node_name == "retrieve":
trace_data = node_output.get("trace_data", {})
recall_msg = f"Found {trace_data.get('sensory_count')} sensory memories."
assoc_msg = f"Extracted {trace_data.get('graph_count')} graph relations."
yield sse_event("brain_trace", build_brain_event(
"recall",
72,
f"Hippocampal recall recovered {trace_data.get('sensory_count', 0)} sensory memories.",
next_regions=["neocortex", "prefrontal_cortex"],
inputs_used=["vector_memory", "working_memory"],
data={
"memories": node_output.get("context"),
"count": trace_data.get("sensory_count", 0),
}
))
yield sse_event("trace", {"phase": "recall", "message": recall_msg, "data": node_output.get("context")})
await asyncio.sleep(0.4)
suppressed_sensory = trace_data.get("suppressed_sensory", 0)
suppressed_graph = trace_data.get("suppressed_graph", 0)
yield sse_event("brain_trace", build_brain_event(
"inhibition",
61,
f"Suppressed {suppressed_sensory} weak sensory recalls and {suppressed_graph} weak graph associations.",
next_regions=["neocortex", "prefrontal_cortex"],
inputs_used=["retrieved_memories", "graph_candidates"],
data={
"suppressed_sensory": suppressed_sensory,
"suppressed_graph": suppressed_graph,
}
))
yield sse_event("trace", {
"phase": "inhibition",
"message": f"Suppressed {suppressed_sensory + suppressed_graph} low-salience recalls.",
"data": {
"suppressed_sensory": suppressed_sensory,
"suppressed_graph": suppressed_graph,
}
})
await asyncio.sleep(0.4)
yield sse_event("brain_trace", build_brain_event(
"association",
74,
f"Neocortical association found {trace_data.get('graph_count', 0)} semantic links.",
next_regions=["prefrontal_cortex", "language_cortex"],
inputs_used=["graph_memory", "retrieved_memories"],
data={
"graph_context": node_output.get("graph_context"),
"touched": trace_data.get("touched"),
}
))
yield sse_event("trace", {"phase": "association", "message": assoc_msg, "data": node_output.get("graph_context"), "touched": trace_data.get("touched")})
await asyncio.sleep(0.4)
elif node_name == "call_model":
reason_msg = "Synthesizing final response via Cortex Node..."
yield sse_event("brain_trace", build_brain_event(
"reasoning",
82,
"Prefrontal reasoning integrated memory, associations, and user intent into a response plan.",
next_regions=["language_cortex"],
inputs_used=["working_memory", "retrieved_memories", "graph_memory", "reflection"],
data={"prediction": prediction["intent"]}
))
yield sse_event("trace", {"phase": "reasoning", "message": reason_msg})
await asyncio.sleep(0.4)
final_response = node_output.get("response", "")
add_message(current_user, "user", request.text)
add_message(current_user, "assistant", final_response)
yield sse_event("brain_trace", build_brain_event(
"language",
88,
"Language generation layer converted the response plan into natural language.",
next_regions=["memory_consolidation"],
inputs_used=["response_plan"],
data={"response_preview": final_response[:120]}
))
yield sse_event("trace", {"phase": "language", "message": "Generating natural language output."})
yield sse_event("final_result", {"response": final_response})
# Build neural mesh AFTER streaming the response so
# the user sees the reply immediately, then the graph
# refreshes once knowledge extraction finishes.
exchange_text = f"User: {request.text}\nSoma: {final_response}"
try:
yield sse_event("brain_trace", build_brain_event(
"memory",
68,
"The completed exchange is being written into episodic and sensory memory.",
next_regions=["neocortex"],
inputs_used=["conversation_exchange"],
))
yield sse_event("trace", {
"phase": "memory",
"message": "Writing this exchange into episodic and sensory memory."
})
stored_chunks = await asyncio.to_thread(
ingest_text,
exchange_text,
{"type": "chat_exchange"},
current_user
)
yield sse_event("trace", {
"phase": "memory",
"message": f"Stored {stored_chunks} sensory chunks from this exchange.",
"data": {"chunks": stored_chunks}
})
yield sse_event("trace", {
"phase": "graph",
"message": "Extracting relationships for semantic memory."
})
triples = await asyncio.to_thread(extract_and_store_knowledge, request.text, current_user)
yield sse_event("brain_trace", build_brain_event(
"graph",
71,
f"Semantic cortex encoded {triples} new graph relations from the exchange.",
next_regions=[],
inputs_used=["conversation_exchange", "semantic_extraction"],
data={"triples": triples, "chunks": stored_chunks}
))
yield sse_event("trace", {
"phase": "graph",
"message": f"Updated the knowledge graph with {triples} new relations.",
"data": {"triples": triples}
})
yield sse_event("graph_updated", {"triples": triples, "chunks": stored_chunks})
except Exception as e:
print(f"Memory build error: {e}")
yield sse_event("trace", {
"phase": "graph",
"message": f"Memory writeback degraded: {str(e)}"
})
yield sse_event("graph_updated", {"triples": 0, "chunks": 0})
except Exception as e:
yield sse_event("error", {"detail": str(e)})
return StreamingResponse(event_generator(), media_type="text/event-stream")
# ββ Memory Explorer βββββββββββββββββββββββββββββββββββββββββββββ
@router.get("/memory/search")
async def process_memory_search(q: str, current_user: str = Depends(get_current_user)):
try:
from app.db.chroma import search_memories
results = search_memories(q, current_user)
memories = []
if results and "documents" in results and results["documents"]:
docs = results["documents"][0]
ids = results["ids"][0]
metadatas = results["metadatas"][0] if results["metadatas"] else []
distances = results["distances"][0] if results["distances"] else []
for i in range(len(docs)):
memories.append({
"id": ids[i],
"content": docs[i],
"metadata": metadatas[i] if i < len(metadatas) else {},
"similarity": round(1 - distances[i], 2) if i < len(distances) else 0
})
return {"memories": memories}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/memory/sensory")
async def get_sensory_memories(current_user: str = Depends(get_current_user)):
try:
from app.db.chroma import get_collection
collection = get_collection()
results = collection.get(where={"user_id": current_user})
memories = []
if results and "documents" in results:
for i in range(len(results["documents"])):
memories.append({
"id": results["ids"][i],
"content": results["documents"][i],
"metadata": results["metadatas"][i] if results["metadatas"] else {}
})
return {"memories": memories}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.delete("/memory/{memory_id}")
async def purge_memory_chunk(memory_id: str, current_user: str = Depends(get_current_user)):
try:
from app.db.chroma import delete_vector
success = delete_vector(memory_id, current_user)
if not success:
raise HTTPException(status_code=404, detail="Memory chunk not found or unauthorized.")
return {"message": "Memory chunk purged successfully."}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ββ History βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@router.get("/history")
async def fetch_chat_history(current_user: str = Depends(get_current_user)):
try:
history = get_recent_messages(current_user, exchanges=20)
return {"messages": history}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ββ Visitor Analytics βββββββββββββββββββββββββββββββββββββββββββββ
class HitRequest(BaseModel):
visitor_id: str
@router.post("/analytics/hit")
async def record_visitor_hit(request: HitRequest, current_user: str = Depends(get_current_user)):
try:
from app.services.analytics import analytics_manager
success = analytics_manager.record_hit(request.visitor_id)
return {"success": success, "message": "Hit recorded successfully" if success else "Hit recorded in local simulation fallback."}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/analytics/stats")
async def get_visitor_stats(current_user: str = Depends(get_current_user)):
try:
from app.services.analytics import analytics_manager
stats = analytics_manager.get_stats()
return stats
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
|