context added, wired in. first attempt

This commit is contained in:
serversdwn
2025-11-28 19:29:41 -05:00
parent d9281a1816
commit b0f42ba86e
5 changed files with 802 additions and 22 deletions

View File

@@ -97,22 +97,106 @@ def push_to_neomem(summary: str, session_id: str):
except Exception as e:
print(f"NeoMem push failed: {e}")
# ─────────────────────────────
# Background summarizer
# ─────────────────────────────
# ───────────────────────────────────────────────
# Multilevel Summaries (L1, L5, L10, L20, L30)
# ───────────────────────────────────────────────
# History maps
L10_HISTORY = {} # session_id → list of L10 blocks
L20_HISTORY = {} # session_id → list of merged overviews
def summarize_L1(buf):
return summarize_simple(buf[-5:])
def summarize_L5(buf):
return summarize_simple(buf[-10:])
def summarize_L10(buf):
# “Reality Check” for last 10 exchanges
text = ""
for e in buf[-10:]:
text += f"User: {e['user_msg']}\nAssistant: {e['assistant_msg']}\n\n"
prompt = f"""
You are Lyra Intake performing a short 'Reality Check'.
Summarize the last block of conversation (up to 10 exchanges)
in one clear paragraph focusing on tone, intent, and direction.
{text}
Reality Check:
"""
return llm(prompt)
def summarize_L20(L10_list):
joined = "\n\n".join(L10_list)
prompt = f"""
You are Lyra Intake creating a 'Session Overview'.
Merge the following Reality Check paragraphs into one short summary
capturing progress, themes, and the direction of the conversation.
{joined}
Overview:
"""
return llm(prompt)
def summarize_L30(L20_list):
joined = "\n\n".join(L20_list)
prompt = f"""
You are Lyra Intake generating a 'Continuity Report'.
Condense these session overviews into one high-level reflection,
noting major themes, persistent goals, and shifts.
{joined}
Continuity Report:
"""
return llm(prompt)
def bg_summarize(session_id: str):
"""Runs all summary levels on every exchange."""
try:
hopper = SESSIONS.get(session_id)
if not hopper:
return
buf = list(hopper["buffer"])
summary = summarize_simple(buf)
push_to_neomem(summary, session_id)
if not buf:
return
# Ensure history lists exist
L10_HISTORY.setdefault(session_id, [])
L20_HISTORY.setdefault(session_id, [])
# L1, L5 (simple factual)
s_L1 = summarize_L1(buf)
s_L5 = summarize_L5(buf)
# L10 (append to history)
s_L10 = summarize_L10(buf)
L10_HISTORY[session_id].append(s_L10)
# L20 (merge all L10s)
s_L20 = summarize_L20(L10_HISTORY[session_id])
L20_HISTORY[session_id].append(s_L20)
# L30 (merge all L20s)
s_L30 = summarize_L30(L20_HISTORY[session_id])
# Push most important tier(s) to NeoMem
push_to_neomem(s_L10, session_id)
push_to_neomem(s_L20, session_id)
push_to_neomem(s_L30, session_id)
print(f"🧩 L1/L5/L10/L20/L30 updated for {session_id}")
print(f"🧩 Summary generated for {session_id}")
except Exception as e:
print(f"Summarizer error: {e}")
print(f"💥 Multilevel summarizer error for {session_id}: {e}")
# ─────────────────────────────
# Routes
@@ -155,6 +239,42 @@ def get_summary(session_id: str = Query(...)):
summary = summarize_simple(list(hopper["buffer"]))
return {"summary_text": summary, "session_id": session_id}
@app.get("/context")
def get_context(session_id: str = Query(...)):
"""Return full multilevel summary context for Cortex."""
if session_id not in SESSIONS:
return {
"session_id": session_id,
"exchange_count": 0,
"L1": "",
"L5": "",
"L10": "",
"L20": "",
"L30": "",
"last_updated": None
}
buffer = list(SESSIONS[session_id]["buffer"])
# Build levels on demand
L1 = summarize_L1(buffer)
L5 = summarize_L5(buffer)
L10 = summarize_L10(buffer)
L20 = summarize_L20(L10_HISTORY.get(session_id, []))
L30 = summarize_L30(L20_HISTORY.get(session_id, []))
return {
"session_id": session_id,
"exchange_count": len(buffer),
"L1": L1,
"L5": L5,
"L10": L10,
"L20": L20,
"L30": L30,
"last_updated": datetime.now().isoformat()
}
@app.get("/health")
def health():
return {"ok": True, "model": SUMMARY_MODEL, "url": SUMMARY_URL}