mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 05:49:44 +08:00
A course held the right model — the syllabus that gives progress a
denominator, resources as references, conventions apart from the
assistant's own notes — but almost nothing crossed the edges between it
and the surfaces it was supposed to contain.
Getting in. Course Study was offered in the composer's mode menu while
the only way to bind a course was a link on the course page, so choosing
it there produced a conversation identical to an inert one, and the mode
advised pressing a button that did not exist. The composer now carries
the binding next to the mode: shown whenever there is one, and offered
as an unfinished field in Course Study when there is not. `_course_id`
rides every turn, so detaching detaches and the pill can never drift
from what the turn actually saw.
What a conversation inherits. `default_capability` and `default_persona`
were stored, validated and returned by the API while changing nothing:
`_apply_course_defaults` only fills fields a payload *omits*, and this
composer always sent all of them. They are applied client-side now,
along with the course's knowledge bases, where the learner can watch the
mode and material become the course's before they type. The dialog grows
the editor they never had.
Conventions. "Every conversation in this course starts knowing these" was
true only if the model happened to call `course_overview`. They ride in
the per-turn summary now, and — because the promise is not scoped to one
mode — in the system prompt of any course-bound turn.
Coming back. Mastery Path and Immersive Reading now read `?course=`,
scope themselves to it, and attach whatever is built there; the tiles and
hand-offs pass it; the picker offers to start one. A course could only
ever reference what already existed, so filling a new one meant leaving,
building, and remembering to return.
Hand-offs. A target with no ref stored an opening line for an index that
never consumes it, which then surfaced weeks later in an unrelated path.
An unresolvable ref was passed straight through: observed live, a
syllabus unit id ("u2") sent as a mastery path, producing a card pointing
at a page that does not exist. Refs are checked against the subsystem
that owns them — existence, not membership, since a path built outside
this course routes perfectly well.
Also: the sidebar groups conversations by course, so filing them is
finally readable; a course-scoped notebook console no longer shows
another course's notes beside a list saying there are none (the clear
raced an in-flight detail fetch that landed after it); a hand-off the
model also printed as prose is not rendered twice; a provider error is no
longer stored as a conversation's permanent title; `/space/courses` goes,
having been an unreferenced copy of `/courses`; and the Visualize mode
description gets its missing Chinese.
332 lines
9.5 KiB
TypeScript
332 lines
9.5 KiB
TypeScript
import { apiFetch, apiUrl } from "@/lib/api";
|
|
import { invalidateClientCache, withClientCache } from "@/lib/client-cache";
|
|
|
|
/**
|
|
* Kinds of resource a course may reference.
|
|
*
|
|
* A course stores a pointer, never the thing itself — one textbook can be the
|
|
* reading for two courses, and one partner can assist all of them. Mirrors
|
|
* `COURSE_RESOURCE_KINDS` in `deeptutor/services/courses.py`.
|
|
*/
|
|
export const COURSE_RESOURCE_KINDS = [
|
|
"knowledge_base",
|
|
"book",
|
|
"notebook",
|
|
"mastery_path",
|
|
"reading_workspace",
|
|
"partner",
|
|
"partner_group",
|
|
] as const;
|
|
|
|
export type CourseResourceKind = (typeof COURSE_RESOURCE_KINDS)[number];
|
|
|
|
export interface CourseResource {
|
|
id: string;
|
|
kind: CourseResourceKind;
|
|
ref_id: string;
|
|
/** Snapshot taken at attach time, so the row survives the target being renamed. */
|
|
label: string;
|
|
position: number;
|
|
added_at: number;
|
|
}
|
|
|
|
/**
|
|
* One ordered unit of what a course is supposed to cover.
|
|
*
|
|
* The syllabus is what gives progress a denominator — "6 questions done" says
|
|
* nothing without "out of twelve weeks". `covered` is the learner's own call:
|
|
* inferring it from mastered knowledge points would be a guess rendered as a
|
|
* progress bar, so the evidence sits next to the checkbox instead.
|
|
*/
|
|
export interface SyllabusUnit {
|
|
id: string;
|
|
position: number;
|
|
title: string;
|
|
topics: string[];
|
|
covered: boolean;
|
|
}
|
|
|
|
export type CourseStatus = "active" | "archived";
|
|
|
|
export interface StudyCourse {
|
|
id: string;
|
|
name: string;
|
|
description: string;
|
|
color: string;
|
|
created_at: number;
|
|
updated_at: number;
|
|
/** Learner-authored conventions for this subject. Reaches every turn. */
|
|
instructions: string;
|
|
/** The assistant's accumulating read on the learner. Never learner-edited. */
|
|
agent_notes: string;
|
|
default_capability: string;
|
|
default_persona: string;
|
|
resources: CourseResource[];
|
|
syllabus: SyllabusUnit[];
|
|
/** Archiving is reversible and never touches what the course references. */
|
|
status: CourseStatus;
|
|
archived_at: number;
|
|
}
|
|
|
|
/** One resource plus whether its target still resolves, and a per-kind summary. */
|
|
export interface CourseResourceState extends CourseResource {
|
|
available: boolean;
|
|
detail: Record<string, unknown>;
|
|
}
|
|
|
|
/**
|
|
* The course's whole picture, from one endpoint.
|
|
*
|
|
* Deliberately the same payload the `course_study` tools read: a course page
|
|
* and the assistant must never disagree about how far along the learner is,
|
|
* and two independent aggregations would drift the moment either side changed.
|
|
*/
|
|
export interface CourseState {
|
|
course: StudyCourse;
|
|
resources: CourseResourceState[];
|
|
sessions: {
|
|
active: number;
|
|
archived: number;
|
|
recent: { session_id: string; title: string; updated_at: number }[];
|
|
};
|
|
mastery: {
|
|
paths: {
|
|
path_id: string;
|
|
name: string;
|
|
objectives_total: number;
|
|
objectives_mastered: number;
|
|
stage: string;
|
|
weak_points: string[];
|
|
}[];
|
|
};
|
|
question_bank: {
|
|
total: number;
|
|
wrong: number;
|
|
weak_categories: { name: string; wrong: number }[];
|
|
};
|
|
reading: {
|
|
workspaces: { workspace_id: string; title: string; materials: number }[];
|
|
};
|
|
syllabus: {
|
|
total: number;
|
|
covered: number;
|
|
next: { id: string; title: string; position: number } | null;
|
|
units: (SyllabusUnit & {
|
|
/** Evidence, not a verdict: wrong questions matching this unit's topics. */
|
|
wrong_questions: number;
|
|
})[];
|
|
};
|
|
}
|
|
|
|
export const DEFAULT_COURSE_COLORS = [
|
|
"#C65D2E",
|
|
"#3F6F8F",
|
|
"#4F7655",
|
|
"#8A6543",
|
|
"#705B8E",
|
|
"#A04F5F",
|
|
];
|
|
|
|
async function expectJson<T>(response: Response): Promise<T> {
|
|
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
|
|
return response.json() as Promise<T>;
|
|
}
|
|
|
|
/** Fill in fields absent from courses created before the container existed. */
|
|
function normalizeCourse(raw: Partial<StudyCourse>): StudyCourse {
|
|
return {
|
|
id: String(raw.id ?? ""),
|
|
name: String(raw.name ?? ""),
|
|
description: String(raw.description ?? ""),
|
|
color: String(raw.color ?? DEFAULT_COURSE_COLORS[0]),
|
|
created_at: Number(raw.created_at ?? 0),
|
|
updated_at: Number(raw.updated_at ?? 0),
|
|
instructions: String(raw.instructions ?? ""),
|
|
agent_notes: String(raw.agent_notes ?? ""),
|
|
default_capability: String(raw.default_capability ?? ""),
|
|
default_persona: String(raw.default_persona ?? ""),
|
|
resources: Array.isArray(raw.resources) ? raw.resources : [],
|
|
syllabus: Array.isArray(raw.syllabus) ? raw.syllabus : [],
|
|
status: raw.status === "archived" ? "archived" : "active",
|
|
archived_at: Number(raw.archived_at ?? 0),
|
|
};
|
|
}
|
|
|
|
export async function listCourses(options?: {
|
|
force?: boolean;
|
|
}): Promise<StudyCourse[]> {
|
|
return withClientCache<StudyCourse[]>(
|
|
"courses:list",
|
|
async () => {
|
|
const response = await apiFetch(apiUrl("/api/v1/courses"), {
|
|
cache: "no-store",
|
|
});
|
|
const courses =
|
|
(await expectJson<{ courses: Partial<StudyCourse>[] }>(response))
|
|
.courses ?? [];
|
|
return courses.map(normalizeCourse);
|
|
},
|
|
{ force: options?.force, ttlMs: 15_000 },
|
|
);
|
|
}
|
|
|
|
export async function createCourse(input: {
|
|
name: string;
|
|
description?: string;
|
|
color?: string;
|
|
instructions?: string;
|
|
default_capability?: string;
|
|
default_persona?: string;
|
|
}): Promise<StudyCourse> {
|
|
const response = await apiFetch(apiUrl("/api/v1/courses"), {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(input),
|
|
});
|
|
const course = (await expectJson<{ course: Partial<StudyCourse> }>(response))
|
|
.course;
|
|
invalidateClientCache("courses:");
|
|
return normalizeCourse(course);
|
|
}
|
|
|
|
export async function updateCourse(
|
|
courseId: string,
|
|
input: Partial<
|
|
Pick<
|
|
StudyCourse,
|
|
| "name"
|
|
| "description"
|
|
| "color"
|
|
| "instructions"
|
|
| "default_capability"
|
|
| "default_persona"
|
|
| "status"
|
|
>
|
|
>,
|
|
): Promise<StudyCourse> {
|
|
const response = await apiFetch(apiUrl(`/api/v1/courses/${courseId}`), {
|
|
method: "PATCH",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(input),
|
|
});
|
|
const course = (await expectJson<{ course: Partial<StudyCourse> }>(response))
|
|
.course;
|
|
invalidateClientCache("courses:");
|
|
return normalizeCourse(course);
|
|
}
|
|
|
|
export async function deleteCourse(courseId: string): Promise<void> {
|
|
const response = await apiFetch(apiUrl(`/api/v1/courses/${courseId}`), {
|
|
method: "DELETE",
|
|
});
|
|
await expectJson<{ deleted: boolean }>(response);
|
|
invalidateClientCache("courses:");
|
|
}
|
|
|
|
export async function attachCourseResource(
|
|
courseId: string,
|
|
input: { kind: CourseResourceKind; ref_id: string; label?: string },
|
|
): Promise<CourseResource> {
|
|
const response = await apiFetch(
|
|
apiUrl(`/api/v1/courses/${courseId}/resources`),
|
|
{
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(input),
|
|
},
|
|
);
|
|
const resource = (await expectJson<{ resource: CourseResource }>(response))
|
|
.resource;
|
|
invalidateClientCache("courses:");
|
|
return resource;
|
|
}
|
|
|
|
export async function detachCourseResource(
|
|
courseId: string,
|
|
resourceId: string,
|
|
): Promise<void> {
|
|
const response = await apiFetch(
|
|
apiUrl(`/api/v1/courses/${courseId}/resources/${resourceId}`),
|
|
{ method: "DELETE" },
|
|
);
|
|
await expectJson<{ deleted: boolean }>(response);
|
|
invalidateClientCache("courses:");
|
|
}
|
|
|
|
export async function getCourseState(
|
|
courseId: string,
|
|
options?: { force?: boolean },
|
|
): Promise<CourseState> {
|
|
return withClientCache<CourseState>(
|
|
`courses:state:${courseId}`,
|
|
async () => {
|
|
const response = await apiFetch(
|
|
apiUrl(`/api/v1/courses/${courseId}/state`),
|
|
{ cache: "no-store" },
|
|
);
|
|
const state = await expectJson<CourseState>(response);
|
|
return { ...state, course: normalizeCourse(state.course ?? {}) };
|
|
},
|
|
{ force: options?.force, ttlMs: 10_000 },
|
|
);
|
|
}
|
|
|
|
export type CourseResourceCandidates = Partial<
|
|
Record<CourseResourceKind, { ref_id: string; label: string }[]>
|
|
>;
|
|
|
|
export async function listCourseResourceCandidates(options?: {
|
|
force?: boolean;
|
|
}): Promise<CourseResourceCandidates> {
|
|
return withClientCache<CourseResourceCandidates>(
|
|
"courses:candidates",
|
|
async () => {
|
|
const response = await apiFetch(
|
|
apiUrl("/api/v1/courses/resource-candidates"),
|
|
{ cache: "no-store" },
|
|
);
|
|
return (
|
|
(await expectJson<{ candidates: CourseResourceCandidates }>(response))
|
|
.candidates ?? {}
|
|
);
|
|
},
|
|
{ force: options?.force, ttlMs: 30_000 },
|
|
);
|
|
}
|
|
|
|
export async function setCourseSyllabus(
|
|
courseId: string,
|
|
units: { id?: string; title: string; topics?: string[]; covered?: boolean }[],
|
|
): Promise<StudyCourse> {
|
|
const response = await apiFetch(
|
|
apiUrl(`/api/v1/courses/${courseId}/syllabus`),
|
|
{
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ units }),
|
|
},
|
|
);
|
|
const course = (await expectJson<{ course: Partial<StudyCourse> }>(response))
|
|
.course;
|
|
invalidateClientCache("courses:");
|
|
return normalizeCourse(course);
|
|
}
|
|
|
|
export async function setSyllabusUnitCovered(
|
|
courseId: string,
|
|
unitId: string,
|
|
covered: boolean,
|
|
): Promise<SyllabusUnit> {
|
|
const response = await apiFetch(
|
|
apiUrl(`/api/v1/courses/${courseId}/syllabus/${unitId}`),
|
|
{
|
|
method: "PATCH",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ covered }),
|
|
},
|
|
);
|
|
const unit = (await expectJson<{ unit: SyllabusUnit }>(response)).unit;
|
|
invalidateClientCache("courses:");
|
|
return unit;
|
|
}
|