# router.py from unittest import result from fastapi import APIRouter, HTTPException from pydantic import BaseModel from reasoning.reasoning import reason_check from reasoning.reflection import reflect_notes from reasoning.refine import refine_answer from persona.speak import speak from ingest.intake_client import IntakeClient # ----------------------------- # Router (NOT FastAPI app) # ----------------------------- cortex_router = APIRouter() # Initialize Intake client once intake_client = IntakeClient() # ----------------------------- # Pydantic models # ----------------------------- class ReasonRequest(BaseModel): session_id: str user_prompt: str temperature: float | None = None # ----------------------------- # /reason endpoint # ----------------------------- @cortex_router.post("/reason") async def run_reason(req: ReasonRequest): # 1. Pull context from Intake try: intake_summary = await intake_client.get_context(req.session_id) except Exception: intake_summary = "(no context available)" # 2. Reflection try: reflection = await reflect_notes(intake_summary, identity_block=None) reflection_notes = reflection.get("notes", []) except Exception: reflection_notes = [] # 3. First-pass reasoning draft draft = await reason_check( req.user_prompt, identity_block=None, rag_block=None, reflection_notes=reflection_notes ) # 4. Refinement result = await refine_answer( draft_output=draft, reflection_notes=reflection_notes, identity_block=None, rag_block=None, ) final_neutral = result["final_output"] # 5. Persona layer persona_answer = await speak(final_neutral) # 6. Return full bundle return { "draft": draft, "neutral": final_neutral, "persona": persona_answer, "reflection": reflection_notes, "session_id": req.session_id, } # ----------------------------- # Intake ingest passthrough # ----------------------------- @cortex_router.post("/ingest") async def ingest_stub(): return {"status": "ok"}