Files
Cortex-Inara/cortex/context_loader.py
Scott Idem 2f675ee4bf Initial commit — Cortex API + Inara identity
Cortex: FastAPI backend serving Inara via Claude/Gemini CLI backends.
Includes SSE streaming chat, session persistence, Google Chat webhook
handler, and Docker support.

Inara: Identity files (persona, soul, protocols, memory, context tiers)
mounted read-only into the container at runtime.

Features in initial cut:
- /chat endpoint with SSE keepalive + LLM fallback
- Session store with rolling history window
- Markdown rendering, copy-to-clipboard, links open in new tab
- Stacked right-column input controls (height selector, enter toggle,
  note mode with public/private) — semi-hidden until textarea grows
- /note endpoint for injecting public context into session history
- Docker Compose config (local dev runs natively; Docker for server)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-06 03:41:00 -05:00

53 lines
1.6 KiB
Python

from pathlib import Path
from config import settings
# Files loaded per tier — mirrors CONTEXT_TIERS.md
TIER_FILES: dict[int, list[str]] = {
1: ["SOUL.md", "IDENTITY.md"], # + USER.md summary only
2: ["SOUL.md", "IDENTITY.md", "USER.md", "MEMORY.md", "PROTOCOLS.md"],
3: ["SOUL.md", "IDENTITY.md", "USER.md", "MEMORY.md", "PROTOCOLS.md"],
4: ["SOUL.md", "IDENTITY.md", "USER.md", "MEMORY.md", "PROTOCOLS.md"],
}
# Lines of USER.md to include at Tier 1 (just identity + what he cares about)
TIER_1_USER_LINES = 30
def _read(path: Path) -> str:
if path.exists():
return path.read_text()
return f"[missing: {path.name}]"
def load_context(tier: int = 2) -> str:
inara_dir = settings.inara_path()
parts = []
files = TIER_FILES.get(tier, TIER_FILES[2])
for filename in files:
path = inara_dir / filename
if not path.exists():
continue
if filename == "USER.md" and tier == 1:
# Tier 1: include only the first N lines
lines = path.read_text().splitlines()[:TIER_1_USER_LINES]
content = "\n".join(lines)
else:
content = path.read_text()
parts.append(f"--- {filename} ---\n{content}")
if tier >= 3:
# Add recent session logs
sessions_dir = inara_dir / "sessions"
if sessions_dir.exists():
count = 2 if tier == 3 else 7
session_files = sorted(sessions_dir.glob("*.md"), reverse=True)[:count]
for sf in session_files:
parts.append(f"--- Session: {sf.name} ---\n{sf.read_text()}")
return "\n\n".join(parts)