Skip to content

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

feat(learner): expose learner profile controls

XZH545c3610d886
diff --git a/web/app/(admin)/admin/users/page.tsx b/web/app/(admin)/admin/users/page.tsx
index 0a3901951..10eaf51cb 100644
--- a/web/app/(admin)/admin/users/page.tsx
+++ b/web/app/(admin)/admin/users/page.tsx
@@ -13,6 +13,7 @@ import {
} from "@/lib/admin-api";
import { GrantEditor } from "@/features/multi-user/components/GrantEditor";
import { BookPermissionEditor } from "@/features/multi-user/components/BookPermissionEditor";
+import { LearnerProfileEditor } from "@/features/multi-user/components/LearnerProfileEditor";
import { UserAvatar } from "@/components/UserAvatar";
import { ConfirmDialog } from "@/components/ui/ConfirmDialog";
import { filterUsersByQuery } from "@/lib/admin-users";
@@ -456,6 +457,7 @@ export default function AdminUsersPage() {
<td colSpan={4} className="p-0">
<GrantEditor key={user.id} userId={user.id} />
<BookPermissionEditor userId={user.id} />
+ <LearnerProfileEditor username={user.username} />
</td>
</tr>
)}
diff --git a/web/features/multi-user/components/LearnerProfileEditor.tsx b/web/features/multi-user/components/LearnerProfileEditor.tsx
new file mode 100644
index 000000000..81815689a
--- /dev/null
+++ b/web/features/multi-user/components/LearnerProfileEditor.tsx
@@ -0,0 +1,98 @@
+"use client";
+
+import { useEffect, useState } from "react";
+import { Check, Loader2 } from "lucide-react";
+import { useTranslation } from "react-i18next";
+import {
+ getLearnerProfile,
+ setLearnerProfile,
+ type LearnerProfile,
+} from "@/lib/admin-api";
+
+const fields: Array<keyof LearnerProfile> = [
+ "age",
+ "grade_level",
+ "curriculum",
+ "language",
+ "reading_level",
+ "explanation_style",
+];
+
+export function LearnerProfileEditor({ username }: { username: string }) {
+ const { t } = useTranslation();
+ const [profile, setProfile] = useState<LearnerProfile>({});
+ const [loading, setLoading] = useState(true);
+ const [saving, setSaving] = useState(false);
+ const [message, setMessage] = useState("");
+
+ useEffect(() => {
+ let active = true;
+ void getLearnerProfile(username)
+ .then((value) => {
+ if (active) setProfile(value ?? {});
+ })
+ .catch(() => {
+ if (active) setMessage(t("Failed to load learner profile"));
+ })
+ .finally(() => {
+ if (active) setLoading(false);
+ });
+ return () => {
+ active = false;
+ };
+ }, [t, username]);
+
+ async function save() {
+ setSaving(true);
+ setMessage("");
+ try {
+ await setLearnerProfile(username, profile);
+ setMessage(t("Saved"));
+ } catch (error) {
+ setMessage(error instanceof Error ? error.message : t("Failed to save learner profile"));
+ } finally {
+ setSaving(false);
+ }
+ }
+
+ return (
+ <div className="border-t border-[var(--border)] bg-[var(--background)]/40 px-5 py-4">
+ <div className="mb-3 flex items-center justify-between gap-3">
+ <div>
+ <h3 className="text-sm font-medium text-[var(--foreground)]">{t("Learner Profile")}</h3>
+ <p className="text-xs text-[var(--muted-foreground)]">{t("Adapt explanations to this learner")}</p>
+ </div>
+ <button
+ type="button"
+ onClick={() => void save()}
+ disabled={loading || saving}
+ className="inline-flex items-center gap-1.5 rounded-lg border border-[var(--border)] px-3 py-1.5 text-xs text-[var(--foreground)] disabled:opacity-50"
+ >
+ {saving ? <Loader2 size={13} className="animate-spin" /> : <Check size={13} />}
+ {t("Save")}
+ </button>
+ </div>
+ <div className="grid grid-cols-1 gap-2 sm:grid-cols-2 lg:grid-cols-3">
+ {fields.map((field) => (
+ <label key={field} className="text-xs text-[var(--muted-foreground)]">
+ {t(field === "grade_level" ? "Grade level" : field === "explanation_style" ? "Explanation style" : field.replaceAll("_", " "))}
+ <input
+ value={profile[field] ?? ""}
+ onChange={(event) =>
+ setProfile((current) => ({
+ ...current,
+ [field]: field === "age" ? (event.target.value ? Number(event.target.value) : undefined) : event.target.value,
+ }))
+ }
+ type={field === "age" ? "number" : "text"}
+ min={field === "age" ? 3 : undefined}
+ max={field === "age" ? 120 : undefined}
+ className="mt-1 w-full rounded-md border border-[var(--border)] bg-[var(--card)] px-2 py-1.5 text-sm text-[var(--foreground)]"
+ />
+ </label>
+ ))}
+ </div>
+ {message && <p className="mt-2 text-xs text-[var(--muted-foreground)]">{message}</p>}
+ </div>
+ );
+}
diff --git a/web/lib/admin-api.ts b/web/lib/admin-api.ts
index 193ea8296..65d95851f 100644
--- a/web/lib/admin-api.ts
+++ b/web/lib/admin-api.ts
@@ -15,6 +15,44 @@ export interface UserRecord {
};
}
+export interface LearnerProfile {
+ age?: number;
+ grade_level?: string;
+ curriculum?: string;
+ language?: string;
+ reading_level?: string;
+ explanation_style?: string;
+}
+
+export async function getLearnerProfile(username: string): Promise<LearnerProfile | null> {
+ const res = await apiFetch(
+ apiUrl(`/api/v1/auth/users/${encodeURIComponent(username)}/learner-profile`),
+ );
+ if (!res.ok) throw new Error("Failed to fetch learner profile");
+ const data = (await res.json()) as { learner_profile?: LearnerProfile | null };
+ return data.learner_profile ?? null;
+}
+
+export async function setLearnerProfile(
+ username: string,
+ profile: LearnerProfile,
+): Promise<LearnerProfile | null> {
+ const res = await apiFetch(
+ apiUrl(`/api/v1/auth/users/${encodeURIComponent(username)}/learner-profile`),
+ {
+ method: "PUT",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify(profile),
+ },
+ );
+ if (!res.ok) {
+ const data = await res.json().catch(() => ({}));
+ throw new Error(data.detail ?? "Failed to save learner profile");
+ }
+ const data = (await res.json()) as { learner_profile?: LearnerProfile | null };
+ return data.learner_profile ?? null;
+}
+
export async function listUsers(): Promise<UserRecord[]> {
const res = await apiFetch(apiUrl("/api/v1/auth/users"));
if (!res.ok) throw new Error("Failed to fetch users");
diff --git a/web/locales/en/app.json b/web/locales/en/app.json
index 125449c60..859561aab 100644
--- a/web/locales/en/app.json
+++ b/web/locales/en/app.json
@@ -193,6 +193,12 @@
"Loading conversation": "Loading conversation…",
"Still loading…": "Still loading…",
"Save": "Save",
+ "Learner Profile": "Learner Profile",
+ "Adapt explanations to this learner": "Adapt explanations to this learner",
+ "Failed to load learner profile": "Failed to load learner profile",
+ "Failed to save learner profile": "Failed to save learner profile",
+ "Grade level": "Grade level",
+ "Explanation style": "Explanation style",
"Cancel": "Cancel",
"Error": "Error",
"Success": "Success",
diff --git a/web/locales/zh/app.json b/web/locales/zh/app.json
index d3171771f..d08c870bd 100644
--- a/web/locales/zh/app.json
+++ b/web/locales/zh/app.json
@@ -193,6 +193,12 @@
"Loading conversation": "加载对话中…",
"Still loading…": "仍在加载中…",
"Save": "保存",
+ "Learner Profile": "学习者档案",
+ "Adapt explanations to this learner": "根据该学习者调整讲解",
+ "Failed to load learner profile": "加载学习者档案失败",
+ "Failed to save learner profile": "保存学习者档案失败",
+ "Grade level": "年级",
+ "Explanation style": "讲解风格",
"Cancel": "取消",
"Error": "错误",
"Success": "成功",