- Split backend/app/core into weather/, discord/, memory_kb/, search/ subpackages by responsibility, keeping config.py and llm.py as the shared foundation; updated all import sites accordingly. - Add local Markdown knowledge base support (local_knowledge.py, knowledge_base/fgo.md, LOCAL_KB_* settings) that embeds and injects relevant docs into the system prompt by topic similarity. - Add ruff and pyright as backend dev dependencies with basic config in pyproject.toml for linting and type checking. - Document local machine quirks in CLAUDE.md (Postgres on port 8080, SSLKEYLOGFILE breaking litellm/pyright via OPENSSL_Uplink crashes). - Add backend/run_dev.ps1 dev launcher and docs/discord-bot.md.
60 lines
1.4 KiB
Python
60 lines
1.4 KiB
Python
import asyncio
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.core.config import CORS_ORIGINS
|
|
from app.core.discord.discord_bot import start_discord_bot
|
|
from app.core.discord.scheduler import start_scheduler
|
|
from app.core.memory_kb.word_memory import seed_from_history
|
|
from app.db.database import SessionLocal, init_db
|
|
from app.db.models import Message
|
|
from app.api import chat, memory, logs, system
|
|
|
|
|
|
def _seed_word_memory_from_db() -> None:
|
|
db = SessionLocal()
|
|
try:
|
|
rows = (
|
|
db.query(Message.content)
|
|
.filter(Message.role.in_(["user", "assistant"]))
|
|
.order_by(Message.created_at.desc())
|
|
.limit(200)
|
|
.all()
|
|
)
|
|
seed_from_history([content for (content,) in rows])
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
init_db()
|
|
_seed_word_memory_from_db()
|
|
start_scheduler()
|
|
asyncio.create_task(start_discord_bot())
|
|
yield
|
|
|
|
|
|
app = FastAPI(title="PlayChat API", version="0.1.0", lifespan=lifespan)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
app.include_router(chat.router)
|
|
app.include_router(memory.router)
|
|
app.include_router(logs.router)
|
|
app.include_router(system.router)
|
|
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
return {"status": "ok"}
|