Skip to content

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

19.5 KB
Raw Blame
import { apiFetch, apiUrl } from "@/lib/api";

// ── Notebooks (file-backed under data/user/workspace/notebook) ──
//
// Favorites live at /notebooks in the product and under /api/notebooks in the
// transport. They collect saved output from chat, research
// and Co-Writer (written by SaveToNotebookModal, read back by
// NotebookRecordPicker). They are a different feature from the Question
// Bank at /space/questions further down this file, which only tracks quiz
// entries — the two share the "notebook" word in their API paths for
// historical reasons, nothing else.

export type NotebookRecordType =
  | "solve"
  | "question"
  | "research"
  | "chat"
  | "co_writer"
  | "reading"
  | "tutorbot"
  | "video_learning";

export interface NotebookSummary {
  id: string;
  name: string;
  description?: string;
  color?: string;
  icon?: string;
  record_count?: number;
  created_at?: number;
  updated_at?: number;
  /** Set when the file is on disk but could not be parsed. */
  unreadable?: boolean;
}

export interface NotebookRecordItem {
  id: string;
  type: NotebookRecordType | string;
  title: string;
  summary?: string;
  user_query: string;
  output: string;
  metadata?: Record<string, unknown>;
  created_at?: number;
  kb_name?: string | null;
}

export interface NotebookDetail extends NotebookSummary {
  records: NotebookRecordItem[];
}

export async function listNotebooks(): Promise<NotebookSummary[]> {
  const response = await apiFetch(apiUrl("/api/notebooks"), {
    cache: "no-store",
  });
  if (!response.ok) throw new Error(`Request failed: ${response.status}`);
  const data = (await response.json()) as { notebooks: NotebookSummary[] };
  return data.notebooks ?? [];
}

export async function getNotebook(notebookId: string): Promise<NotebookDetail> {
  const response = await apiFetch(apiUrl(`/api/notebooks/${notebookId}`), {
    cache: "no-store",
  });
  if (!response.ok) throw new Error(`Request failed: ${response.status}`);
  return (await response.json()) as NotebookDetail;
}

export async function createNotebook(payload: {
  name: string;
  description?: string;
  color?: string;
  icon?: string;
}): Promise<NotebookSummary> {
  const response = await apiFetch(apiUrl("/api/notebooks"), {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      name: payload.name,
      description: payload.description ?? "",
      color: payload.color ?? "#6366F1",
      icon: payload.icon ?? "book",
    }),
  });
  if (!response.ok) throw new Error(`Request failed: ${response.status}`);
  const data = (await response.json()) as { notebook: NotebookSummary };
  return data.notebook;
}

export async function updateNotebook(
  notebookId: string,
  payload: {
    name?: string;
    description?: string;
    color?: string;
    icon?: string;
  },
): Promise<NotebookSummary> {
  const response = await apiFetch(apiUrl(`/api/notebooks/${notebookId}`), {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(payload),
  });
  if (!response.ok) throw new Error(`Request failed: ${response.status}`);
  const data = (await response.json()) as { notebook: NotebookSummary };
  return data.notebook;
}

export async function deleteNotebook(notebookId: string): Promise<void> {
  const response = await apiFetch(apiUrl(`/api/notebooks/${notebookId}`), {
    method: "DELETE",
  });
  if (!response.ok) throw new Error(`Request failed: ${response.status}`);
}

export async function deleteNotebookRecord(
  notebookId: string,
  recordId: string,
): Promise<void> {
  const response = await apiFetch(
    apiUrl(`/api/notebooks/${notebookId}/records/${recordId}`),
    {
      method: "DELETE",
    },
  );
  if (!response.ok) throw new Error(`Request failed: ${response.status}`);
}

/**
 * Edit one record in place.
 *
 * Send only the fields being changed: the backend distinguishes an omitted
 * field from an explicit `null`, so spreading a whole record in here would
 * rewrite values the user never touched.
 */
export async function updateNotebookRecord(
  notebookId: string,
  recordId: string,
  changes: {
    title?: string;
    summary?: string;
    user_query?: string;
    output?: string;
    metadata?: Record<string, unknown>;
    kb_name?: string | null;
  },
): Promise<NotebookRecordItem> {
  const response = await apiFetch(
    apiUrl(`/api/notebooks/${notebookId}/records/${recordId}`),
    {
      method: "PUT",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(changes),
    },
  );
  if (!response.ok) throw new Error(`Request failed: ${response.status}`);
  const data = (await response.json()) as { record: NotebookRecordItem };
  return data.record;
}

/** Move a record to another notebook, or copy it there under a new id. */
export async function relocateNotebookRecord(
  notebookId: string,
  recordId: string,
  targetNotebookId: string,
  mode: "move" | "copy",
): Promise<NotebookRecordItem> {
  const response = await apiFetch(
    apiUrl(`/api/notebooks/${notebookId}/records/${recordId}/actions/${mode}`),
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ target_notebook_id: targetNotebookId }),
    },
  );
  if (!response.ok) throw new Error(`Request failed: ${response.status}`);
  const data = (await response.json()) as { record: NotebookRecordItem };
  return data.record;
}

/** Fetch a whole notebook rendered as one Markdown document. */
export async function exportNotebookMarkdown(
  notebookId: string,
): Promise<string> {
  const response = await apiFetch(
    apiUrl(`/api/notebooks/${notebookId}/export`),
    {
      cache: "no-store",
    },
  );
  if (!response.ok) throw new Error(`Request failed: ${response.status}`);
  return await response.text();
}

// ── Question notebook (quiz entries + categories) ─────────────────

export interface NotebookAnswerImage {
  id: string;
  url: string;
  filename: string;
  mime_type: string;
}

export interface NotebookEntry {
  id: number;
  session_id: string;
  session_title: string;
  /** Present on servers with independent Question Bank provenance support. */
  origin_type?: QuestionOriginType;
  origin_ref?: string;
  turn_id: string;
  question_id: string;
  question: string;
  question_type: string;
  options: Record<string, string>;
  correct_answer: string;
  explanation: string;
  difficulty: string;
  user_answer: string;
  user_answer_images?: NotebookAnswerImage[];
  source: AssessmentSource;
  material_id: string;
  material_title: string;
  section_id: string;
  section_title: string;
  score_trend: ScoreTrend;
  assessment_type?: AssessmentType | "";
  result?: AssessmentResult;
  mastery_path_id?: string;
  knowledge_point_id?: string;
  attempt_count?: number;
  hints_used?: number;
  confidence?: number | null;
  response_time?: number | null;
  quality?: number | null;
  is_correct: boolean;
  resolved: boolean;
  bookmarked: boolean;
  followup_session_id: string;
  /** Latest AI-judge text for this entry; empty when never run. */
  ai_judgment?: string;
  created_at: number;
  updated_at: number;
  categories?: NotebookCategory[];
  practice?: {
    is_mistake: boolean | number;
    due_at: number;
    review_count: number;
    last_answer: string | null;
    last_rating: "again" | "hard" | "good" | "easy" | null;
  } | null;
}

export interface NotebookCategory {
  id: number;
  name: string;
  created_at: number;
  entry_count: number;
}

export type AssessmentSource =
  | "deep_question"
  | "mastery_path"
  | "immersive_reading"
  | "book"
  | "partner_chat"
  | "import";

export type QuestionOriginType =
  "conversation" | "external_import" | "document_analysis";

export type AssessmentType = "quiz" | "focus_check" | "qualitative" | "review";

export type AssessmentResult = "correct" | "incorrect" | "partial" | "ungraded";

export type ScoreTrend = "new" | "improved" | "declined" | "unchanged";

export interface QuestionBankMaterial {
  source: AssessmentSource;
  material_id: string;
  material_title: string;
  entry_count: number;
  unresolved_count: number;
}

export interface NotebookEntryListResponse {
  items: NotebookEntry[];
  total: number;
}

async function expectJson<T>(response: Response): Promise<T> {
  if (!response.ok) {
    // Surface FastAPI's `detail` when there is one: "A category named 'Math'
    // already exists" is actionable, "Request failed: 409" is not.
    let detail = "";
    try {
      const body = await response.json();
      if (typeof body?.detail === "string") detail = body.detail;
    } catch {
      /* non-JSON error body — fall back to the status line */
    }
    throw new Error(detail || `Request failed: ${response.status}`);
  }
  return response.json() as Promise<T>;
}

// ── Entries ──────────────────────────────────────────────────────

export interface NotebookEntryFilter {
  mistakes_only?: boolean;
  category_id?: number;
  /** Only entries in no category at all — the triage inbox. */
  uncategorized?: boolean;
  bookmarked?: boolean;
  is_correct?: boolean;
  source?: AssessmentSource;
  material_id?: string;
  section_id?: string;
  assessment_type?: AssessmentType;
  result?: AssessmentResult;
  mastery_path_id?: string;
  knowledge_point_id?: string;
  resolved?: boolean;
  score_trend?: ScoreTrend;
  search?: string;
  sort?: "recent" | "oldest";
  limit?: number;
  offset?: number;
}

export async function listNotebookEntries(
  filter: NotebookEntryFilter = {},
): Promise<NotebookEntryListResponse> {
  const params = new URLSearchParams();
  if (filter.mistakes_only) params.set("mistakes_only", "true");
  if (filter.category_id !== undefined)
    params.set("category_id", String(filter.category_id));
  if (filter.uncategorized) params.set("uncategorized", "true");
  if (filter.bookmarked !== undefined)
    params.set("bookmarked", String(filter.bookmarked));
  if (filter.is_correct !== undefined)
    params.set("is_correct", String(filter.is_correct));
  if (filter.source) params.set("source", filter.source);
  if (filter.material_id) params.set("material_id", filter.material_id);
  if (filter.section_id) params.set("section_id", filter.section_id);
  if (filter.assessment_type)
    params.set("assessment_type", filter.assessment_type);
  if (filter.result) params.set("result", filter.result);
  if (filter.mastery_path_id)
    params.set("mastery_path_id", filter.mastery_path_id);
  if (filter.knowledge_point_id)
    params.set("knowledge_point_id", filter.knowledge_point_id);
  if (filter.resolved !== undefined)
    params.set("resolved", String(filter.resolved));
  if (filter.score_trend) params.set("score_trend", filter.score_trend);
  if (filter.search) params.set("search", filter.search);
  if (filter.sort) params.set("sort", filter.sort);
  if (filter.limit !== undefined) params.set("limit", String(filter.limit));
  if (filter.offset !== undefined) params.set("offset", String(filter.offset));
  const query = params.toString();
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/entries${query ? `?${query}` : ""}`),
    { cache: "no-store" },
  );
  return expectJson<NotebookEntryListResponse>(response);
}

export async function getNotebookEntry(
  entryId: number,
): Promise<NotebookEntry> {
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/entries/${entryId}`),
    {
      cache: "no-store",
    },
  );
  return expectJson<NotebookEntry>(response);
}

export async function lookupNotebookEntry(
  sessionId: string,
  questionId: string,
  turnId?: string | null,
): Promise<NotebookEntry | null> {
  const params = new URLSearchParams({
    session_id: sessionId,
    question_id: questionId,
    // Probe quietly: a not-yet-saved question returns 204 instead of 404, so
    // it stays out of the server error log and the browser network console.
    missing_ok: "true",
  });
  if (turnId) params.set("turn_id", turnId);
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/entries/lookup/by-question?${params}`),
  );
  // 204 (missing_ok hit) and 404 (older servers) both mean "no entry yet".
  if (response.status === 204 || response.status === 404) return null;
  return expectJson<NotebookEntry>(response);
}

export async function lookupNotebookEntryByOrigin(
  originType: Exclude<QuestionOriginType, "conversation">,
  originRef: string,
  questionId: string,
  turnId?: string | null,
): Promise<NotebookEntry | null> {
  const params = new URLSearchParams({
    origin_type: originType,
    origin_ref: originRef,
    question_id: questionId,
    missing_ok: "true",
  });
  if (turnId) params.set("turn_id", turnId);
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/entries/lookup/by-question?${params}`),
  );
  if (response.status === 204 || response.status === 404) return null;
  return expectJson<NotebookEntry>(response);
}

export async function updateNotebookEntry(
  entryId: number,
  updates: {
    bookmarked?: boolean;
    followup_session_id?: string;
    ai_judgment?: string;
    resolved?: boolean;
  },
): Promise<void> {
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/entries/${entryId}`),
    {
      method: "PATCH",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(updates),
    },
  );
  await expectJson<{ updated: boolean }>(response);
}

export interface NotebookAnswerImageUpload {
  id?: string;
  /** Base64 (no ``data:`` prefix) for a freshly-picked image. */
  base64?: string;
  /** Existing AttachmentStore URL for an already-persisted image. */
  url?: string;
  filename: string;
  mime_type: string;
}

export async function upsertNotebookEntry(data: {
  session_id?: string;
  origin_type?: QuestionOriginType;
  origin_ref?: string;
  turn_id?: string;
  question_id: string;
  question: string;
  question_type?: string;
  options?: Record<string, string>;
  correct_answer?: string;
  explanation?: string;
  difficulty?: string;
  user_answer?: string;
  /**
   * Optional list of images attached to the learner's answer. Omit to
   * leave any stored images untouched; pass an empty array to clear them.
   */
  user_answer_images?: NotebookAnswerImageUpload[];
  is_correct?: boolean;
  source?: AssessmentSource;
  material_id?: string;
  material_title?: string;
  section_id?: string;
  section_title?: string;
  assessment_type?: AssessmentType | "";
  result?: AssessmentResult;
  mastery_path_id?: string;
  knowledge_point_id?: string;
  attempt_count?: number;
  hints_used?: number;
  confidence?: number | null;
  response_time?: number | null;
  quality?: number | null;
}): Promise<NotebookEntry> {
  const response = await apiFetch(
    apiUrl("/api/question-notebook/entries/upsert"),
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        ...data,
        options: data.options || {},
        explanation: data.explanation || "",
        difficulty: data.difficulty || "",
      }),
    },
  );
  return expectJson<NotebookEntry>(response);
}

export async function deleteNotebookEntry(entryId: number): Promise<void> {
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/entries/${entryId}`),
    {
      method: "DELETE",
    },
  );
  await expectJson<{ deleted: boolean }>(response);
}

// ── Entry ↔ Category ────────────────────────────────────────────

export async function addEntryToCategory(
  entryId: number,
  categoryId: number,
): Promise<void> {
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/entries/${entryId}/categories`),
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ category_id: categoryId }),
    },
  );
  await expectJson<{ added: boolean }>(response);
}

export interface BulkCategoryResult {
  changed: number;
  requested: number;
  category_id: number;
  link: boolean;
}

/**
 * File (or unfile) many entries in one request.
 *
 * One round-trip instead of N: the list refreshes once against a settled
 * server state rather than racing a burst of per-entry writes.
 */
export async function bulkLinkEntriesToCategory(
  entryIds: number[],
  categoryId: number,
  link = true,
): Promise<BulkCategoryResult> {
  const response = await apiFetch(
    apiUrl("/api/question-notebook/entries/categories/bulk"),
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        entry_ids: entryIds,
        category_id: categoryId,
        link,
      }),
    },
  );
  return expectJson<BulkCategoryResult>(response);
}

export interface QuestionBankStats {
  total: number;
  wrong: number;
  unresolved: number;
  bookmarked: number;
  uncategorized: number;
}

/** Counts behind the scope rail. */
export async function getQuestionBankStats(
): Promise<QuestionBankStats> {
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/stats`),
    {
      cache: "no-store",
    },
  );
  return expectJson<QuestionBankStats>(response);
}

export async function listQuestionBankMaterials(
): Promise<QuestionBankMaterial[]> {
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/materials`),
    {
      cache: "no-store",
    },
  );
  return expectJson<QuestionBankMaterial[]>(response);
}

export async function removeEntryFromCategory(
  entryId: number,
  categoryId: number,
): Promise<void> {
  const response = await apiFetch(
    apiUrl(
      `/api/question-notebook/entries/${entryId}/categories/${categoryId}`,
    ),
    { method: "DELETE" },
  );
  await expectJson<{ removed: boolean }>(response);
}

// ── Categories ──────────────────────────────────────────────────

export async function listCategories(
): Promise<NotebookCategory[]> {
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/categories`),
    {
      cache: "no-store",
    },
  );
  return expectJson<NotebookCategory[]>(response);
}

export async function createCategory(name: string): Promise<NotebookCategory> {
  const response = await apiFetch(apiUrl("/api/question-notebook/categories"), {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ name }),
  });
  return expectJson<NotebookCategory>(response);
}

export async function renameCategory(
  categoryId: number,
  name: string,
): Promise<void> {
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/categories/${categoryId}`),
    {
      method: "PATCH",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ name }),
    },
  );
  await expectJson<{ updated: boolean }>(response);
}

export async function deleteCategory(categoryId: number): Promise<void> {
  const response = await apiFetch(
    apiUrl(`/api/question-notebook/categories/${categoryId}`),
    {
      method: "DELETE",
    },
  );
  await expectJson<{ deleted: boolean }>(response);
}

/** Resolve a favorite after it has moved to another collection. */
export async function locateFavoriteRecord(
  recordId: string,
  collectionId: string,
): Promise<string | null> {
  const response = await apiFetch(
    apiUrl(
      `/api/notebooks/records/${encodeURIComponent(recordId)}/location?notebook_id=${encodeURIComponent(collectionId)}`,
    ),
    { cache: "no-store" },
  );
  if (response.status === 404) return null;
  if (!response.ok) throw new Error(`Request failed: ${response.status}`);
  const data = (await response.json()) as { notebook_id: string };
  return data.notebook_id;
}