- db/database.py: conversation_messages 테이블 + save/load/cleanup 헬퍼 - conversation.py: write-through (memory + DB) + lazy load on first access - 메모리 캐시 1시간 TTL, DB 7일 보관 - add/get/format_for_prompt가 async로 변경 - worker.py: 모든 conversation_store 호출에 await 추가 - main.py lifespan에 startup cleanup 호출 (7일 이상 정리) 서버 재시작 후 "방금 그거 더 자세히" 같은 후속 질문이 컨텍스트 유지 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
"""NanoClaude — 비동기 job 기반 AI Gateway (Phase 3: Synology Chat 연동)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from config import settings
|
|
from db.database import init_db
|
|
from routers import chat, synology
|
|
from services.backend_registry import backend_registry
|
|
from services import job_queue as jq_module
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s %(levelname)s %(name)s — %(message)s",
|
|
)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
await init_db()
|
|
# 7일 이상 오래된 대화 정리
|
|
try:
|
|
from db.database import cleanup_old_conversations
|
|
deleted = await cleanup_old_conversations(days=7)
|
|
if deleted:
|
|
import logging
|
|
logging.getLogger(__name__).info("Cleaned up %d old conversation messages", deleted)
|
|
except Exception:
|
|
pass
|
|
backend_registry.init_from_settings(settings)
|
|
backend_registry.start_health_loop(settings.health_check_interval)
|
|
jq_module.init_queue(settings.max_concurrent_jobs)
|
|
yield
|
|
backend_registry.stop_health_loop()
|
|
|
|
|
|
app = FastAPI(
|
|
title="NanoClaude",
|
|
version="0.2.0",
|
|
description="비동기 job 기반 AI Gateway — Phase 2 (EXAONE → Gemma 파이프라인)",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
@app.middleware("http")
|
|
async def check_api_key(request: Request, call_next):
|
|
"""Optional API key check. 설정이 비어있으면 통과."""
|
|
if settings.api_key:
|
|
auth = request.headers.get("Authorization", "")
|
|
if request.url.path not in ("/", "/health") and auth != f"Bearer {settings.api_key}":
|
|
return JSONResponse(status_code=401, content={"detail": "Invalid API key"})
|
|
return await call_next(request)
|
|
|
|
|
|
app.include_router(chat.router)
|
|
app.include_router(synology.router)
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"service": "NanoClaude", "version": "0.2.0", "phase": 2}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health():
|
|
return {
|
|
"status": "ok",
|
|
"backends": backend_registry.health_summary(),
|
|
"queue": jq_module.job_queue.stats if jq_module.job_queue else {},
|
|
}
|