Skip to content

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

Block 23: loading feedback, module picker, git cleanup

whf97d6976f34ff
diff --git a/.hermes_block23_spec.md b/.hermes_block23_spec.md
new file mode 100644
index 000000000..cdb9a91b5
--- /dev/null
+++ b/.hermes_block23_spec.md
@@ -0,0 +1,109 @@
+---
+type: ["ux", "git"]
+skills: []
+negative_tests: 2
+---
+
+# Block 23: Final polish — loading feedback, module selector, git cleanup
+
+## Issue 1: Loading UX — no feedback while waiting for LLM
+
+**File**: `web/app/(workspace)/learning/[bookId]/page.tsx`
+
+**Problem**: Lines 344-349 — after WS connects (`connecting=false`), content area shows "Ready to start learning" even though the LLM hasn't responded yet. User sees static text for 5-15s with no progress indicator. The Waiting state is invisible.
+
+**Fix**: Add `waitingForLLM` state. Set `true` in `ws.onopen` after sending `start_turn`. Set `false` when first `content` or `stage_start` event arrives. Show spinner + "Generating diagnostic questions..." text instead of "Ready".
+
+**Changes** (in order):
+1. Add state: `const [waitingForLLM, setWaitingForLLM] = useState(false);` after line 32
+2. In `ws.onopen` (line 101-108), after `ws.send(...)` add `setWaitingForLLM(true);`
+3. In `handleStreamEvent`, in `stage_start` handler (line 150), add `setWaitingForLLM(false);` at top
+4. In `handleStreamEvent`, in `content` handler (line 179), add `setWaitingForLLM(false);` at top
+5. Change lines 346-349 from:
+ ```
+ {connecting ? <Loader2 ... /> : t("guidedLearning.ready")}
+ ```
+ to:
+ ```
+ {connecting ? <Loader2 ... /> : waitingForLLM ? (
+ <div className="flex flex-col items-center gap-2">
+ <Loader2 className="w-8 h-8 animate-spin" />
+ <p className="text-sm">Generating diagnostic questions...</p>
+ </div>
+ ) : t("guidedLearning.ready")}
+ ```
+
+## Issue 2: Module selector — initial module selection before auto-start
+
+**File**: `web/app/(workspace)/learning/[bookId]/page.tsx`
+
+**Problem**: Page auto-connects WS and starts `guided_learning` immediately. With 3 modules (线性代数/微积分/概率论), user never gets to choose. ModuleTree is clickable but only AFTER connection.
+
+**Fix**: When `modules.length > 1` AND no `currentModuleId`, show a module picker screen BEFORE connecting WS. User picks a module → `handleModuleClick` → connect starts.
+
+**Changes**:
+1. Add `const [moduleSelected, setModuleSelected] = useState(false);` (~line 51)
+2. In `connect()`, guard: if `modules.length > 1 && !moduleSelected && !currentModuleId`, skip WS connect
+3. In `handleModuleClick`, add `setModuleSelected(true);` after sending change_module
+4. In `connect()` useEffect (line 270), add dependency on `moduleSelected` (or call connect inside handleModuleClick)
+5. Add module picker UI before connect: show module cards when `!moduleSelected && modules.length > 1 && !connecting`
+
+**Module picker UI** (insert before line 269 `useEffect` or in JSX before connecting state):
+```tsx
+// Show module picker when multiple modules + none selected
+{!moduleSelected && modules.length > 1 && !connecting && (
+ <div className="flex items-center justify-center h-full">
+ <div className="text-center max-w-md">
+ <h2 className="text-xl font-bold mb-2">Select a Module</h2>
+ <p className="text-sm text-[var(--muted-foreground)] mb-6">Choose a module to start guided learning</p>
+ <div className="flex flex-col gap-3">
+ {modules.map(m => (
+ <button
+ key={m.id}
+ onClick={() => { handleModuleClick(m.id); setModuleSelected(true); }}
+ className="px-6 py-4 rounded-xl border border-[var(--border)] hover:border-[var(--primary)] text-left transition-colors"
+ >
+ <div className="font-medium">{m.name}</div>
+ <div className="text-xs text-[var(--muted-foreground)] mt-1">{m.knowledge_points.length} knowledge points</div>
+ </button>
+ ))}
+ </div>
+ </div>
+ </div>
+)}
+```
+
+## Issue 3: Git cleanup — proxy.ts + middleware.ts
+
+**Files**: git index only
+
+**Problem**:
+- `web/proxy.ts` is untracked (needed by Next.js 16)
+- `web/middleware.ts` deleted from disk but still tracked by git
+
+**Fix**:
+```bash
+git rm web/middleware.ts
+git add web/proxy.ts
+```
+
+## Negative tests (2)
+
+1. Load `/learning/[bookId]` with 1 module → auto-connects (no picker), shows spinner during LLM wait
+2. Load `/learning/[bookId]` with 0 modules → shows "Ready to start learning" (no picker, no endless spinner)
+
+## Acceptance criteria
+
+1. Loading state shows spinner with descriptive text while waiting for LLM
+2. Module picker appears when 2+ modules exist and none selected
+3. Single-module books auto-start (no picker)
+4. `proxy.ts` committed, `middleware.ts` removed from git
+5. 92/92 tests still pass
+6. Webbridge verify: visit /learning/default → see loading spinner → see modules/stages
+
+## Verification
+
+```bash
+python -m pytest deeptutor/learning/tests/ -v
+git status web/proxy.ts web/middleware.ts
+```
diff --git a/web/app/(workspace)/learning/[bookId]/page.tsx b/web/app/(workspace)/learning/[bookId]/page.tsx
index cd132f0d1..da8e1cf75 100644
--- a/web/app/(workspace)/learning/[bookId]/page.tsx
+++ b/web/app/(workspace)/learning/[bookId]/page.tsx
@@ -30,6 +30,7 @@ export default function LearningBookPage() {
const [currentStage, setCurrentStage] = useState<string>("");
const currentStageRef = useRef<string>("");
const [connecting, setConnecting] = useState(true);
+ const [waitingForLLM, setWaitingForLLM] = useState(false);
const [error, setError] = useState<string | null>(null);
const errorRef = useRef<boolean>(false);
const activeTurnRetryRef = useRef<boolean>(false);
@@ -49,6 +50,7 @@ export default function LearningBookPage() {
const [modules, setModules] = useState<ModuleData[]>([]);
const [masteryLevels, setMasteryLevels] = useState<Record<string, number>>({});
const [currentModuleId, setCurrentModuleId] = useState<string>("");
+ const [moduleSelected, setModuleSelected] = useState(false);
const [waitingForInput, setWaitingForInput] = useState(false);
const [inputPrompt, setInputPrompt] = useState("");
const [userInput, setUserInput] = useState("");
@@ -84,6 +86,8 @@ export default function LearningBookPage() {
const handleStreamEventRef = useRef<(evt: StreamEvent) => void>(() => {});
const connect = useCallback(() => {
+ // Skip auto-connect when multiple modules exist and none selected yet
+ if (modules.length > 1 && !moduleSelected && !currentModuleId) return;
// Clear any pending reconnect timer
if (reconnectTimerRef.current) {
clearTimeout(reconnectTimerRef.current);
@@ -106,6 +110,7 @@ export default function LearningBookPage() {
book_references: [{ book_id: params.bookId, page_ids: [] }],
config: {},
}));
+ setWaitingForLLM(true);
};
ws.onmessage = (event) => {
@@ -148,6 +153,7 @@ export default function LearningBookPage() {
return;
}
if (evt.type === "stage_start") {
+ setWaitingForLLM(false);
currentStageRef.current = evt.stage;
setCurrentStage(evt.stage);
// Clear errors on new stage — previous turn's error should not block the next turn
@@ -278,6 +284,32 @@ export default function LearningBookPage() {
};
}, [connect]);
+ // Module picker: show when multiple modules exist and none selected
+ if (!connecting && !moduleSelected && modules.length > 1) {
+ return (
+ <div className="flex items-center justify-center h-full">
+ <div className="text-center max-w-md">
+ <h2 className="text-xl font-bold mb-2">Select a Module</h2>
+ <p className="text-sm text-[var(--muted-foreground)] mb-6">Choose a module to start guided learning</p>
+ <div className="flex flex-col gap-3">
+ {modules.map(m => (
+ <button
+ key={m.id}
+ onClick={() => { handleModuleClick(m.id); setModuleSelected(true); }}
+ className="px-6 py-4 rounded-xl border border-[var(--border)] hover:border-[var(--primary)] text-left transition-colors"
+ >
+ <div className="font-medium">{m.name}</div>
+ <div className="text-xs text-[var(--muted-foreground)] mt-1">
+ {m.knowledge_points.length} knowledge points
+ </div>
+ </button>
+ ))}
+ </div>
+ </div>
+ </div>
+ );
+ }
+
return (
<div className="flex h-full relative">
{toast && (
@@ -344,7 +376,16 @@ export default function LearningBookPage() {
) : (
!waitingForInput && (
<div className="flex items-center justify-center h-full text-[var(--muted-foreground)]">
- {connecting ? <Loader2 className="w-8 h-8 animate-spin" /> : t("guidedLearning.ready")}
+ {connecting ? (
+ <Loader2 className="w-8 h-8 animate-spin" />
+ ) : waitingForLLM ? (
+ <div className="flex flex-col items-center gap-2">
+ <Loader2 className="w-8 h-8 animate-spin" />
+ <p className="text-sm">Generating diagnostic questions...</p>
+ </div>
+ ) : (
+ t("guidedLearning.ready")
+ )}
</div>
)
)}
diff --git a/web/middleware.ts b/web/middleware.ts
deleted file mode 100644
index 7b4e0094d..000000000
--- a/web/middleware.ts
+++ /dev/null
@@ -1 +0,0 @@
-export { proxy as middleware, config } from "./proxy";
diff --git a/web/proxy.ts b/web/proxy.ts
new file mode 100644
index 000000000..f6f51b569
--- /dev/null
+++ b/web/proxy.ts
@@ -0,0 +1,73 @@
+import { NextRequest, NextResponse } from "next/server";
+
+const AUTH_ENABLED = process.env.NEXT_PUBLIC_AUTH_ENABLED === "true";
+const LOGIN_PATH = "/login";
+const COOKIE_NAME = "dt_token";
+
+export function proxy(req: NextRequest) {
+ // Auth is disabled (default) — let everything through
+ if (!AUTH_ENABLED) return NextResponse.next();
+
+ const { pathname } = req.nextUrl;
+
+ // Always allow auth pages and Next.js internals
+ if (
+ pathname.startsWith(LOGIN_PATH) ||
+ pathname.startsWith("/register") ||
+ pathname.startsWith("/_next") ||
+ pathname.startsWith("/favicon")
+ ) {
+ return NextResponse.next();
+ }
+
+ const token = req.cookies.get(COOKIE_NAME)?.value;
+
+ // No token — redirect to login, preserving the intended destination
+ if (!token) {
+ const loginUrl = req.nextUrl.clone();
+ loginUrl.pathname = LOGIN_PATH;
+ loginUrl.searchParams.set("next", pathname);
+ return NextResponse.redirect(loginUrl);
+ }
+
+ // Verify token structure (JWT: header.payload.signature)
+ const parts = token.split(".");
+ if (parts.length !== 3) {
+ const loginUrl = req.nextUrl.clone();
+ loginUrl.pathname = LOGIN_PATH;
+ loginUrl.searchParams.set("next", pathname);
+ const response = NextResponse.redirect(loginUrl);
+ response.cookies.delete(COOKIE_NAME);
+ return response;
+ }
+
+ // Check expiry from payload (base64url-decoded JSON)
+ try {
+ const payload = JSON.parse(
+ Buffer.from(parts[1], "base64url").toString("utf-8")
+ );
+ if (payload.exp && Date.now() >= payload.exp * 1000) {
+ const loginUrl = req.nextUrl.clone();
+ loginUrl.pathname = LOGIN_PATH;
+ loginUrl.searchParams.set("next", pathname);
+ const response = NextResponse.redirect(loginUrl);
+ response.cookies.delete(COOKIE_NAME);
+ return response;
+ }
+ } catch {
+ // Malformed payload — treat as invalid
+ const loginUrl = req.nextUrl.clone();
+ loginUrl.pathname = LOGIN_PATH;
+ loginUrl.searchParams.set("next", pathname);
+ const response = NextResponse.redirect(loginUrl);
+ response.cookies.delete(COOKIE_NAME);
+ return response;
+ }
+
+ return NextResponse.next();
+}
+
+export const config = {
+ // Run on all page routes, skip API and static assets
+ matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
+};