Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

Block 22: display module name on /learning cards instead of raw book_id

whf95f44989422e
diff --git a/.hermes_block22_spec.md b/.hermes_block22_spec.md
new file mode 100644
index 000000000..4e555256f
--- /dev/null
+++ b/.hermes_block22_spec.md
@@ -0,0 +1,80 @@
+---
+type: ["api", "ux"]
+skills: []
+negative_tests: 2
+---
+
+# Block 22: Display module name instead of raw book_id on /learning cards
+
+## Problem
+
+`GET /api/v1/learning/progress` returns `ProgressSummary` without a human-readable name field. The `/learning` page card heading shows `card.book_id` directly, resulting in auto-generated IDs like `unified_1778812096729_f4d265d6` displayed to users. Only manually-named books (e.g., "default", "init3") look readable.
+
+## Root cause
+
+`list_progress()` in `deeptutor/learning/service.py:139-146` builds a summary dict with 6 fields but no `name`. The `LearningProgress.modules[]` each have a `.name`, but the summary never surfaces it. The frontend `ProgressSummary` interface mirrors the backend exactly — no name field.
+
+## Files to modify (3)
+
+| File | Change |
+|------|--------|
+| `deeptutor/learning/service.py:139-146` | Add `"name"` to summaries dict |
+| `web/lib/learning-api.ts:45-52` | Add `name: string` to `ProgressSummary` |
+| `web/app/(workspace)/learning/page.tsx:132` | Change `{card.book_id}` to `{card.name \|\| card.book_id}` |
+
+## Implementation details
+
+### 1. Backend (`service.py`)
+
+In `list_progress()`, derive display name from modules:
+```python
+display_name = ""
+if progress.modules:
+ display_name = progress.modules[0].name
+summaries.append({
+ ...
+ "name": display_name or progress.book_id, # fallback to book_id
+ ...
+})
+```
+
+### 2. Frontend type (`learning-api.ts`)
+
+Add to `ProgressSummary`:
+```typescript
+name: string;
+```
+
+### 3. Frontend card (`page.tsx:132`)
+
+Change line 132 from:
+```tsx
+{card.book_id}
+```
+to:
+```tsx
+{card.name || card.book_id}
+```
+
+## Acceptance criteria
+
+1. `list_progress()` returns `"name"` field for all books
+2. Empty-module books show `name` = book_id (fallback)
+3. Non-empty books show `name` = first module name
+4. Frontend card heading uses `card.name` when available
+5. All existing tests pass (no breakage)
+6. Book IDs in URLs (router.push, delete/redo params, card keys) remain unchanged
+
+## Negative tests (2 required)
+
+1. Book with 0 modules — verify `name` falls back to `book_id`, card shows book_id text
+2. Book with modules but empty name in first module — verify no crash, graceful fallback
+
+## Verification
+
+```bash
+# Backend test
+python -m pytest deeptutor/learning/tests/ -v
+
+# Frontend: visit /learning, verify module cards show readable names
+```
diff --git a/deeptutor/learning/service.py b/deeptutor/learning/service.py
index a5712a1b1..90c269254 100644
--- a/deeptutor/learning/service.py
+++ b/deeptutor/learning/service.py
@@ -136,8 +136,13 @@ class LearningService:
1 for kp_id in current_kp_ids
if progress.mastery_levels.get(kp_id, 0) >= 0.7
)
+ # Derive display name from first module, fall back to book_id
+ display_name = ""
+ if progress.modules:
+ display_name = progress.modules[0].name or ""
summaries.append({
"book_id": progress.book_id,
+ "name": display_name or progress.book_id,
"modules_count": len(progress.modules),
"kp_count": total_kps,
"current_stage": progress.current_stage.value if progress.current_stage else "",
diff --git a/deeptutor/learning/tests/test_api_endpoints.py b/deeptutor/learning/tests/test_api_endpoints.py
index be8a5257d..12ba81d3c 100644
--- a/deeptutor/learning/tests/test_api_endpoints.py
+++ b/deeptutor/learning/tests/test_api_endpoints.py
@@ -39,12 +39,42 @@ class TestListProgress:
client.post("/api/v1/learning/progress/testbook/init-modules",
json={"modules": [{"id": "m1", "name": "M1", "order": 0,
"knowledge_points": [{"id": "kp1", "name": "KP1",
- "type": "concept", "module_id": "m1"}]}]})
+ "type": "concept", "module_id": "m1"}]}]})
resp = client.get("/api/v1/learning/progress")
assert resp.status_code == 200
book_ids = [p["book_id"] for p in resp.json()]
assert "testbook" in book_ids
+ def test_list_name_from_first_module(self, client):
+ """Book with modules: name = first module name."""
+ client.post("/api/v1/learning/progress/named/init-modules",
+ json={"modules": [
+ {"id": "m1", "name": "线性代数", "order": 0,
+ "knowledge_points": [{"id": "kp1", "name": "向量",
+ "type": "concept", "module_id": "m1"}]}
+ ]})
+ resp = client.get("/api/v1/learning/progress")
+ assert resp.status_code == 200
+ for p in resp.json():
+ if p["book_id"] == "named":
+ assert p["name"] == "线性代数"
+ break
+ else:
+ pytest.fail("named book not found in progress list")
+
+ def test_list_name_fallback_empty_modules(self, client):
+ """Book with 0 modules: name falls back to book_id."""
+ client.post("/api/v1/learning/progress/empty_mods/init-modules",
+ json={"modules": []})
+ resp = client.get("/api/v1/learning/progress")
+ assert resp.status_code == 200
+ for p in resp.json():
+ if p["book_id"] == "empty_mods":
+ assert p["name"] == "empty_mods", f"expected book_id fallback, got {p['name']}"
+ break
+ else:
+ pytest.fail("empty_mods book not found in progress list")
+
# -- POST /progress/{book_id}/init-modules --------------------------------
diff --git a/web/app/(workspace)/learning/page.tsx b/web/app/(workspace)/learning/page.tsx
index 41d268536..e85d000d2 100644
--- a/web/app/(workspace)/learning/page.tsx
+++ b/web/app/(workspace)/learning/page.tsx
@@ -129,7 +129,7 @@ export default function LearningPage() {
className="p-5 rounded-xl border border-[var(--border)] bg-[var(--card)]"
>
<h3 className="font-semibold text-[var(--foreground)] truncate">
- {card.book_id}
+ {card.name || card.book_id}
</h3>
<p className="text-sm text-[var(--muted-foreground)] mt-1">
{card.modules_count} {t("guidedLearning.modules")} ·{" "}
diff --git a/web/lib/learning-api.ts b/web/lib/learning-api.ts
index 3abd9dbb0..b293405a3 100644
--- a/web/lib/learning-api.ts
+++ b/web/lib/learning-api.ts
@@ -44,6 +44,7 @@ export async function initModules(bookId: string, modules: ModuleInit[]) {
export interface ProgressSummary {
book_id: string;
+ name: string;
modules_count: number;
kp_count: number;
current_stage: string;