RemanenetSpy
feat: add /chat/demo endpoint to power KaalChat widget with memory
02937b7
Raw
History Blame Contribute Delete
5.17 kB
"""
KAAL β€” FastAPI Application
==================================
The API gateway for the Chronos Temporal AI Agent Ecosystem.
Backend: Neon PostgreSQL + pgvector (persistent cloud storage).
Endpoints:
POST /ingest β€” Universal event ingestion
POST /query β€” Hybrid temporal + semantic retrieval
POST /connect β€” Register SaaS tools
GET /connectors β€” List connected tools
POST /agent/run β€” Execute agent with temporal memory
POST /billing/* β€” Razorpay checkout + usage
"""
from __future__ import annotations
import logging
import os
from contextlib import asynccontextmanager
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from chronos_core.memory_store import MemoryStore
from chronos_core.vector_store import VectorStore
from chronos_core.svo_parser import SVOParser
from api.deps import set_stores
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(name)s | %(levelname)s | %(message)s",
)
logger = logging.getLogger("chronos.api")
# ---------------------------------------------------------------------------
# Application Lifespan
# ---------------------------------------------------------------------------
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Initialize and tear down core services."""
try:
logger.info("πŸ•°οΈ KAAL starting up...")
# Initialize Memory Store
memory_store = MemoryStore()
await memory_store.initialize()
# Initialize Vector Store
vector_store = VectorStore()
await vector_store.initialize(pool=memory_store.pool)
# Initialize SVO Parser
svo_parser = SVOParser()
# Register singletons
set_stores(memory_store, vector_store, svo_parser)
logger.info("βœ… KAAL ready β€” Systems online")
yield # App is running
except Exception as e:
import traceback
import asyncio
error_msg = f"❌ CRITICAL STARTUP FAILURE: {str(e)}"
logger.critical(error_msg)
print(f"\n\n!!! CRITICAL STARTUP FAILURE !!!\n{error_msg}\n\n", flush=True)
traceback.print_exc()
await asyncio.sleep(2)
raise e
# Shutdown
logger.info("πŸ”’ KAAL shutting down...")
await memory_store.close()
logger.info("πŸ‘‹ Goodbye from KAAL")
# ---------------------------------------------------------------------------
# FastAPI App
# ---------------------------------------------------------------------------
app = FastAPI(
title="KAAL",
description=(
"The Temporal AI Agent Ecosystem. "
"Structured long-term memory for every agent and SaaS product. "
"Powered by SVO event tuples + dual calendar architecture."
),
version="0.2.0",
lifespan=lifespan,
docs_url="/docs",
redoc_url="/redoc",
)
# CORS β€” allow all origins for development / Streamlit
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------
from api.routes.ingest import router as ingest_router
from api.routes.query import router as query_router
from api.routes.connectors import router as connectors_router
from api.routes.agent import router as agent_router
from api.routes.billing import router as billing_router
from api.routes.voice import router as voice_router
from api.routes.chat import router as chat_router
app.include_router(ingest_router)
app.include_router(query_router)
app.include_router(connectors_router)
app.include_router(agent_router)
app.include_router(billing_router)
app.include_router(voice_router)
app.include_router(chat_router)
# ---------------------------------------------------------------------------
# Health Check
# ---------------------------------------------------------------------------
@app.get("/", tags=["Health"])
async def health_check():
"""Health check β€” verify KAAL is alive."""
return {
"service": "KAAL",
"version": "0.2.0",
"status": "operational",
"storage": "Neon PostgreSQL + pgvector",
"tagline": "Letters to the Future β€” for agents.",
"docs": "/docs",
}
@app.get("/health", tags=["Health"])
async def detailed_health():
"""Detailed health check with system stats."""
from api.deps import get_memory_store, get_vector_store
try:
memory = get_memory_store()
vector = get_vector_store()
event_count = await memory.count_events()
vector_count = await vector.count()
return {
"status": "healthy",
"stores": {
"postgres_events": event_count,
"pgvector_embeddings": vector_count,
},
}
except Exception as e:
return {
"status": "degraded",
"error": str(e),
}