Fix #3+#5: add init-modules endpoint + DEBUG_MODE (LEARNING_DEBUG=1 for second-level intervals)

This commit is contained in:
whf
2026-05-07 21:54:04 +08:00
parent 4f30e0fa1a
commit 44a77b4afd
2 changed files with 29 additions and 2 deletions

View File

@@ -39,6 +39,10 @@ class AnswerRequest(BaseModel):
mastery_estimate: float = 0.0
class InitModulesRequest(BaseModel):
modules: list[dict] # list of LearningModule-compatible dicts
# ── Endpoints ────────────────────────────────────────────────────────────────
@@ -100,3 +104,18 @@ async def get_reviews(book_id: str):
progress = service.get_or_create(book_id)
tasks = scheduler.get_due_tasks(progress)
return {"tasks": [t.model_dump() for t in tasks]}
@router.post("/progress/{book_id}/init-modules")
async def init_modules(book_id: str, body: InitModulesRequest):
from deeptutor.learning.models import KnowledgePoint, LearningModule
service = get_learning_service()
progress = service.get_or_create(book_id)
modules = []
for m in body.modules:
kps = [KnowledgePoint(**kp) for kp in m.pop("knowledge_points", [])]
modules.append(LearningModule(knowledge_points=kps, **m))
service.init_modules(progress, modules)
service.save(progress)
return {"status": "ok", "module_count": len(modules)}

View File

@@ -1,5 +1,6 @@
from __future__ import annotations
import os
import time
from deeptutor.learning.models import (
@@ -25,13 +26,20 @@ _TYPE_PRIORITY: dict[KnowledgeType, int] = {
class SpacedRepetitionScheduler:
def __init__(self) -> None:
# When True, intervals are in seconds instead of days (for testing)
self.DEBUG_MODE: bool = os.environ.get("LEARNING_DEBUG", "").lower() in ("1", "true", "yes")
def _seconds_per_unit(self) -> float:
return 1.0 if self.DEBUG_MODE else 86400.0
def get_initial_state(self, knowledge_type: KnowledgeType) -> RepetitionState:
intervals = INTERVAL_SEQUENCES[knowledge_type]
return RepetitionState(
interval_index=0,
consecutive_correct=0,
consecutive_wrong=0,
next_review_at=time.time() + intervals[0] * 86400,
next_review_at=time.time() + intervals[0] * self._seconds_per_unit(),
)
def schedule_next(
@@ -56,7 +64,7 @@ class SpacedRepetitionScheduler:
state.consecutive_wrong = 0
state.interval_index = max(0, min(state.interval_index, max_index))
state.next_review_at = time.time() + intervals[state.interval_index] * 86400
state.next_review_at = time.time() + intervals[state.interval_index] * self._seconds_per_unit()
return state
def get_due_tasks(