Files
DeepTutor/deeptutor/api/routers/sessions.py
xor_xe 6ca2ecd64f feat: add optional PocketBase integration for auth and session storage
Introduces PocketBase as an optional sidecar for authentication and
session/KB storage, activated only when POCKETBASE_URL is set in .env.
Falls back to the existing SQLite/JSON backend when not configured.

Backend:
- SessionStoreProtocol (typing.Protocol) + get_session_store() factory
- PocketBaseSessionStore: JSONL write-ahead buffer, batch-flush on turn end
- pocketbase_client.py: singleton admin client, 60s in-memory token cache
- auth.py: additive PocketBase path (email-based login/register)
- CORS: explicit origins instead of wildcard (wildcard+credentials fails)
- WebSocket: validate token at connect time when AUTH_ENABLED

Infrastructure:
- docker-compose: pocketbase service with healthcheck + depends_on
- scripts/pb_setup.py: idempotent PocketBase collection bootstrap
- requirements/server.txt: pocketbase>=0.12.0

Frontend:
- Register/login: username field -> email field for PocketBase mode
- auth.ts: normalise FastAPI 422 errors to plain strings
- agents page: guard bots.map() against non-array responses
- settings page: graceful fetch error handling
- *.env.local added to .gitignore

Docs: README PocketBase sidecar setup section added
2026-04-17 21:42:29 +04:00

137 lines
4.4 KiB
Python

"""
Unified session history API.
"""
from __future__ import annotations
import logging
from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel, Field, field_validator
from deeptutor.services.session import get_session_store
logger = logging.getLogger(__name__)
router = APIRouter()
class SessionRenameRequest(BaseModel):
title: str = Field(..., min_length=1, max_length=100)
class QuizResultItem(BaseModel):
question_id: str = ""
question: str = Field(..., min_length=1)
question_type: str = ""
options: dict[str, str] | None = None
user_answer: str = ""
correct_answer: str = ""
explanation: str | None = ""
difficulty: str | None = ""
is_correct: bool
@field_validator("options", mode="before")
@classmethod
def _coerce_options(cls, v):
return v if isinstance(v, dict) else {}
@field_validator("explanation", "difficulty", mode="before")
@classmethod
def _coerce_str(cls, v):
return v if isinstance(v, str) else ""
class QuizResultsRequest(BaseModel):
answers: list[QuizResultItem] = Field(default_factory=list)
def _format_quiz_results_message(answers: list[QuizResultItem]) -> str:
total = len(answers)
correct = sum(1 for item in answers if item.is_correct)
score_pct = round((correct / total) * 100) if total else 0
lines = ["[Quiz Performance]"]
for idx, item in enumerate(answers, 1):
question = item.question.strip().replace("\n", " ")
user_answer = (item.user_answer or "").strip() or "(blank)"
status = "Correct" if item.is_correct else "Incorrect"
suffix = f" ({status})"
if not item.is_correct and (item.correct_answer or "").strip():
suffix = f" ({status}, correct: {(item.correct_answer or '').strip()})"
qid = f"[{item.question_id}] " if item.question_id else ""
lines.append(f"{idx}. {qid}Q: {question} -> Answered: {user_answer}{suffix}")
lines.append(f"Score: {correct}/{total} ({score_pct}%)")
return "\n".join(lines)
@router.get("")
async def list_sessions(
limit: int = Query(default=50, ge=1, le=200),
offset: int = Query(default=0, ge=0),
):
store = get_session_store()
sessions = await store.list_sessions(limit=limit, offset=offset)
return {"sessions": sessions}
@router.get("/{session_id}")
async def get_session(session_id: str):
store = get_session_store()
session = await store.get_session_with_messages(session_id)
if session is None:
raise HTTPException(status_code=404, detail="Session not found")
return session
@router.patch("/{session_id}")
async def rename_session(session_id: str, payload: SessionRenameRequest):
store = get_session_store()
updated = await store.update_session_title(session_id, payload.title)
if not updated:
raise HTTPException(status_code=404, detail="Session not found")
session = await store.get_session(session_id)
return {"session": session}
@router.delete("/{session_id}")
async def delete_session(session_id: str):
store = get_session_store()
deleted = await store.delete_session(session_id)
if not deleted:
raise HTTPException(status_code=404, detail="Session not found")
return {"deleted": True, "session_id": session_id}
@router.post("/{session_id}/quiz-results")
async def record_quiz_results(session_id: str, payload: QuizResultsRequest):
if not payload.answers:
raise HTTPException(status_code=400, detail="Quiz results are required")
store = get_session_store()
session = await store.get_session(session_id)
if session is None:
raise HTTPException(status_code=404, detail="Session not found")
content = _format_quiz_results_message(payload.answers)
await store.add_message(
session_id=session_id,
role="user",
content=content,
capability="deep_question",
)
notebook_count = 0
try:
notebook_count = await store.upsert_notebook_entries(
session_id,
[item.model_dump() for item in payload.answers],
)
except Exception:
logger.warning(
"Failed to upsert notebook entries for session %s", session_id, exc_info=True
)
return {
"recorded": True,
"session_id": session_id,
"answer_count": len(payload.answers),
"notebook_count": notebook_count,
"content": content,
}