context added, wired in. first attempt
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# reasoning.py
|
||||
import os
|
||||
import json
|
||||
from llm.llm_router import call_llm
|
||||
|
||||
|
||||
@@ -14,11 +15,19 @@ async def reason_check(
|
||||
user_prompt: str,
|
||||
identity_block: dict | None,
|
||||
rag_block: dict | None,
|
||||
reflection_notes: list[str]
|
||||
reflection_notes: list[str],
|
||||
context: dict | None = None
|
||||
) -> str:
|
||||
"""
|
||||
Build the *draft answer* for Lyra Cortex.
|
||||
This is the first-pass reasoning stage (no refinement yet).
|
||||
|
||||
Args:
|
||||
user_prompt: Current user message
|
||||
identity_block: Lyra's identity/persona configuration
|
||||
rag_block: Relevant long-term memories from NeoMem
|
||||
reflection_notes: Meta-awareness notes from reflection stage
|
||||
context: Unified context state from context.py (session state, intake, rag, etc.)
|
||||
"""
|
||||
|
||||
# --------------------------------------------------------
|
||||
@@ -47,21 +56,92 @@ async def reason_check(
|
||||
rag_txt = ""
|
||||
if rag_block:
|
||||
try:
|
||||
rag_txt = f"Relevant Info (RAG):\n{rag_block}\n\n"
|
||||
# Format NeoMem results with full structure
|
||||
if isinstance(rag_block, list) and rag_block:
|
||||
rag_txt = "Relevant Long-Term Memories (NeoMem):\n"
|
||||
for idx, mem in enumerate(rag_block, 1):
|
||||
score = mem.get("score", 0.0)
|
||||
payload = mem.get("payload", {})
|
||||
data = payload.get("data", "")
|
||||
metadata = payload.get("metadata", {})
|
||||
|
||||
rag_txt += f"\n[Memory {idx}] (relevance: {score:.2f})\n"
|
||||
rag_txt += f"Content: {data}\n"
|
||||
if metadata:
|
||||
rag_txt += f"Metadata: {json.dumps(metadata, indent=2)}\n"
|
||||
rag_txt += "\n"
|
||||
else:
|
||||
rag_txt = f"Relevant Info (RAG):\n{str(rag_block)}\n\n"
|
||||
except Exception:
|
||||
rag_txt = f"Relevant Info (RAG):\n{str(rag_block)}\n\n"
|
||||
|
||||
# --------------------------------------------------------
|
||||
# Context State (session continuity, timing, mode/mood)
|
||||
# --------------------------------------------------------
|
||||
context_txt = ""
|
||||
if context:
|
||||
try:
|
||||
# Build human-readable context summary
|
||||
context_txt = "=== CONTEXT STATE ===\n"
|
||||
context_txt += f"Session: {context.get('session_id', 'unknown')}\n"
|
||||
context_txt += f"Time since last message: {context.get('minutes_since_last_msg', 0):.1f} minutes\n"
|
||||
context_txt += f"Message count: {context.get('message_count', 0)}\n"
|
||||
context_txt += f"Mode: {context.get('mode', 'default')}\n"
|
||||
context_txt += f"Mood: {context.get('mood', 'neutral')}\n"
|
||||
|
||||
if context.get('active_project'):
|
||||
context_txt += f"Active project: {context['active_project']}\n"
|
||||
|
||||
# Include Intake multilevel summaries
|
||||
intake = context.get('intake', {})
|
||||
if intake:
|
||||
context_txt += "\nShort-Term Memory (Intake):\n"
|
||||
|
||||
# L1 - Recent exchanges
|
||||
if intake.get('L1'):
|
||||
l1_data = intake['L1']
|
||||
if isinstance(l1_data, list):
|
||||
context_txt += f" L1 (recent): {len(l1_data)} exchanges\n"
|
||||
elif isinstance(l1_data, str):
|
||||
context_txt += f" L1: {l1_data[:200]}...\n"
|
||||
|
||||
# L20 - Session overview (most important for continuity)
|
||||
if intake.get('L20'):
|
||||
l20_data = intake['L20']
|
||||
if isinstance(l20_data, dict):
|
||||
summary = l20_data.get('summary', '')
|
||||
context_txt += f" L20 (session overview): {summary}\n"
|
||||
elif isinstance(l20_data, str):
|
||||
context_txt += f" L20: {l20_data}\n"
|
||||
|
||||
# L30 - Continuity report
|
||||
if intake.get('L30'):
|
||||
l30_data = intake['L30']
|
||||
if isinstance(l30_data, dict):
|
||||
summary = l30_data.get('summary', '')
|
||||
context_txt += f" L30 (continuity): {summary}\n"
|
||||
elif isinstance(l30_data, str):
|
||||
context_txt += f" L30: {l30_data}\n"
|
||||
|
||||
context_txt += "\n"
|
||||
|
||||
except Exception as e:
|
||||
# Fallback to JSON dump if formatting fails
|
||||
context_txt = f"=== CONTEXT STATE ===\n{json.dumps(context, indent=2)}\n\n"
|
||||
|
||||
# --------------------------------------------------------
|
||||
# Final assembled prompt
|
||||
# --------------------------------------------------------
|
||||
prompt = (
|
||||
f"{notes_section}"
|
||||
f"{identity_txt}"
|
||||
f"{context_txt}" # Context BEFORE RAG for better coherence
|
||||
f"{rag_txt}"
|
||||
f"User message:\n{user_prompt}\n\n"
|
||||
"Write the best possible *internal draft answer*.\n"
|
||||
"This draft is NOT shown to the user.\n"
|
||||
"Be factual, concise, and focused.\n"
|
||||
"Use the context state to maintain continuity and reference past interactions naturally.\n"
|
||||
)
|
||||
|
||||
# --------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user