Skip to content

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

Merge pull request #1031 from Zuan0/reading-citation-capture

Bingxi Zhao (Frank)09b569803957
diff --cc deeptutor/api/routers/reading.py
index 5daf24e43,e4e86a095..80d612414
--- a/deeptutor/api/routers/reading.py
+++ b/deeptutor/api/routers/reading.py
@@@ -217,6 -166,6 +217,7 @@@ class AnnotationPayload(BaseModel)
class AnnotationInfo(BaseModel):
annotation_id: str
locator: int
++ material_revision: int = 1
kind: str
color: str
quote: str
diff --cc deeptutor/reading/models.py
index 654d1852e,8c21ac35f..76f70e4a3
--- a/deeptutor/reading/models.py
+++ b/deeptutor/reading/models.py
@@@ -21,11 -21,10 +21,11 @@@ from typing import Any, Litera
# What one locator addresses, per source format. Purely presentational for the
# model and the UI ("page 12" vs "chapter 3"); the addressing is identical.
-UnitKind = Literal["page", "chapter", "slide", "section"]
-RenderMode = Literal["text", "pdf", "epub"]
+UnitKind = Literal["page", "chapter", "slide", "section", "segment"]
+RenderMode = Literal["text", "pdf", "epub", "video", "audio"]
+ContentFormat = Literal["plain_text", "web_markdown"]
- AnnotationKind = Literal["highlight", "underline", "note"]
+ AnnotationKind = Literal["highlight", "underline", "note", "citation"]
TextSelectorType = Literal["TextQuoteSelector", "TextPositionSelector"]
MAX_TEXT_SELECTOR_CHARS = 2000
@@@ -344,6 -326,6 +344,9 @@@ class Annotation
annotation_id: str
locator: int
++ # Content revision the verified locator/selectors were captured against.
++ # Legacy rows predate revisioned web snapshots and therefore resolve to 1.
++ material_revision: int = 1
kind: AnnotationKind = "highlight"
color: str = DEFAULT_ANNOTATION_COLOR
quote: str = ""
@@@ -365,6 -347,6 +368,7 @@@
return {
"annotation_id": self.annotation_id,
"locator": self.locator,
++ "material_revision": self.material_revision,
"kind": self.kind,
"color": self.color,
"quote": self.quote,
@@@ -389,7 -371,7 +393,8 @@@
return cls(
annotation_id=str(data.get("annotation_id") or ""),
locator=max(1, int(data.get("locator") or 1)),
- kind=kind if kind in ("highlight", "underline", "note") else "highlight", # type: ignore[arg-type]
++ material_revision=max(1, int(data.get("material_revision") or 1)),
+ kind=(kind if kind in ("highlight", "underline", "note", "citation") else "highlight"), # type: ignore[arg-type]
color=color if color in ANNOTATION_COLORS else DEFAULT_ANNOTATION_COLOR,
quote=str(data.get("quote") or ""),
note=str(data.get("note") or ""),
diff --cc deeptutor/reading/store.py
index 252d93f45,122a55691..9a8c3cff1
--- a/deeptutor/reading/store.py
+++ b/deeptutor/reading/store.py
@@@ -911,6 -593,6 +911,7 @@@ class ReadingStore
if index is None:
stored = dataclass_replace(
stored,
++ material_revision=manifest.revision,
created_at=stored.created_at or now,
updated_at=now,
)
@@@ -918,6 -600,6 +919,7 @@@
else:
stored = dataclass_replace(
stored,
++ material_revision=existing[index].material_revision,
created_at=existing[index].created_at or now,
updated_at=now,
)
diff --cc tests/reading/test_engine.py
index 7b54bac2e,3794ed107..38809a9ca
--- a/tests/reading/test_engine.py
+++ b/tests/reading/test_engine.py
@@@ -439,6 -425,6 +439,7 @@@ def test_annotations_round_trip_with_ge
)
assert saved.annotation_id
++ assert saved.material_revision == manifest.revision
stored = store.annotations(manifest.material_id)
assert len(stored) == 1
assert stored[0].rects[0].to_list() == [0.1, 0.2, 0.5, 0.25]
diff --cc tests/reading/test_router.py
index 9183ffa81,b8a1f5dbf..904d09dec
--- a/tests/reading/test_router.py
+++ b/tests/reading/test_router.py
@@@ -425,6 -378,30 +425,31 @@@ def test_annotation_round_trips_w3c_tex
]
+ def test_citation_round_trips_through_the_existing_annotation_api(
+ client: TestClient,
+ ) -> None:
+ material = _upload(client)
+ base = f"/api/v1/reading/materials/{material['material_id']}/annotations"
+
+ created = client.put(
+ base,
+ json={
+ "locator": 1,
+ "kind": "citation",
+ "quote": "Sequence models",
+ "selectors": [
+ {"type": "TextQuoteSelector", "exact": "Sequence models"},
+ {"type": "TextPositionSelector", "start": 13, "end": 28},
+ ],
+ },
+ )
+
+ assert created.status_code == 200, created.text
+ assert created.json()["kind"] == "citation"
++ assert created.json()["material_revision"] == material["revision"]
+ assert client.get(base).json()[0]["kind"] == "citation"
+
+
def test_annotation_rejects_mismatched_quote_selector(client: TestClient) -> None:
material = _upload(client)
response = client.put(
diff --cc web/components/reading/AnnotationList.tsx
index d9f4af71d,dd4e8b5f3..450c959d9
--- a/web/components/reading/AnnotationList.tsx
+++ b/web/components/reading/AnnotationList.tsx
@@@ -1,16 -1,19 +1,16 @@@
"use client";
- import { useMemo } from "react";
- import { Bot, Sparkles, Trash2 } from "lucide-react";
+ import { useMemo, useState } from "react";
+ import { Bookmark, Bot, Highlighter, Sparkles, Trash2 } from "lucide-react";
import { useTranslation } from "react-i18next";
-import type { AnnotationItem, UnitKind } from "@/lib/reading-api";
+import {
+ ANNOTATION_SWATCH,
+ type AnnotationColor,
+ type AnnotationItem,
+ type UnitKind,
+} from "@/lib/reading-api";
import { unitLabel } from "./TextUnitView";
-const SWATCH: Record<string, string> = {
- yellow: "#facd5a",
- green: "#8cdb94",
- blue: "#7ac0fa",
- pink: "#faa1c7",
- purple: "#c7aefa",
-};
-
export interface AnnotationListProps {
annotations: AnnotationItem[];
unit: UnitKind;
@@@ -43,99 -57,127 +54,130 @@@ export function AnnotationList(
byLocator.set(annotation.locator, bucket);
}
return [...byLocator.entries()].sort((a, b) => a[0] - b[0]);
- }, [annotations]);
+ }, [visibleAnnotations]);
- if (!annotations.length) {
- return (
- <div className="flex h-full flex-col items-center justify-center gap-2 px-6 text-center">
- <Sparkles size={18} className="text-[var(--muted-foreground)]/60" />
- <p className="text-[12px] font-medium text-[var(--foreground)]">
- {t("No annotations yet")}
- </p>
- <p className="max-w-[220px] text-[11px] leading-relaxed text-[var(--muted-foreground)]">
- {t("Select text in the document to highlight it or attach a note.")}
- </p>
+ return (
+ <div className="flex h-full min-h-0 flex-col">
+ <div
+ role="tablist"
+ aria-label={t("Saved reading marks")}
+ className="grid shrink-0 grid-cols-2 gap-1 border-b border-[var(--border)] p-2"
+ >
+ <ViewTab
+ active={view === "annotations"}
+ icon={Highlighter}
+ label={t("Annotations")}
+ onClick={() => setView("annotations")}
+ />
+ <ViewTab
+ active={view === "citations"}
+ icon={Bookmark}
+ label={t("Citations")}
+ onClick={() => setView("citations")}
+ />
</div>
- );
- }
- return (
- <div className="dt-reader-scroll h-full overflow-y-auto px-2.5 py-2">
- {groups.map(([locator, rows]) => (
- <section key={locator} className="mb-3 last:mb-1">
- <h4 className="sticky top-0 z-10 mb-1 bg-[var(--background)]/95 px-1 py-1 font-mono text-[10px] uppercase tracking-[0.06em] text-[var(--muted-foreground)] backdrop-blur">
- {t(unitLabel(unit))} {locator}
- </h4>
- <ul className="space-y-1">
- {rows.map((annotation) => (
- <li key={annotation.annotation_id}>
- <div
- role="button"
- tabIndex={0}
- onClick={() => onSelect(annotation)}
- onKeyDown={(event) => {
- if (event.key === "Enter" || event.key === " ") {
- event.preventDefault();
- onSelect(annotation);
- }
- }}
- className={`group/anno relative w-full cursor-pointer rounded-lg border px-2.5 py-2 text-left transition ${
- annotation.annotation_id === activeId
- ? "border-[var(--ring)] bg-[var(--muted)]/60"
- : "border-transparent hover:border-[var(--border)] hover:bg-[var(--muted)]/40"
- }`}
- >
- <span
- aria-hidden
- className="absolute left-0 top-2 bottom-2 w-[3px] rounded-full"
- style={{
- background:
- ANNOTATION_SWATCH[
- annotation.color as AnnotationColor
- ] ?? ANNOTATION_SWATCH.yellow,
- }}
- />
- {annotation.quote && (
- <p className="line-clamp-3 pl-1.5 text-[12px] leading-[1.55] text-[var(--foreground)]">
- {annotation.quote}
- </p>
- )}
- {annotation.note && (
- <p className="mt-1 line-clamp-3 pl-1.5 text-[11px] leading-[1.5] text-[var(--muted-foreground)]">
- {annotation.note}
- </p>
- )}
- <div className="mt-1 flex items-center gap-1.5 pl-1.5">
- {annotation.author === "assistant" && (
- <span className="inline-flex items-center gap-1 rounded-full bg-[var(--primary)]/10 px-1.5 py-[1px] text-[10px] font-medium text-[var(--primary)]">
- <Bot size={9} />
- {t("AI")}
- </span>
- )}
- {annotation.kind === "underline" && (
- <span className="text-[10px] text-[var(--muted-foreground)]">
- {t("Underline")}
- </span>
- )}
- </div>
- <button
- type="button"
- title={t("Delete annotation")}
- aria-label={t("Delete annotation")}
- onClick={(event) => {
- event.stopPropagation();
- onDelete(annotation);
- }}
- className="absolute right-1.5 top-1.5 inline-flex h-6 w-6 items-center justify-center rounded-md text-[var(--muted-foreground)] opacity-0 transition hover:bg-[var(--destructive)]/10 hover:text-[var(--destructive)] focus-visible:opacity-100 group-hover/anno:opacity-100"
- >
- <Trash2 size={12} />
- </button>
- </div>
- </li>
- ))}
- </ul>
- </section>
- ))}
+ {!visibleAnnotations.length ? (
+ <div className="flex min-h-0 flex-1 flex-col items-center justify-center gap-2 px-6 text-center">
+ <Sparkles size={18} className="text-[var(--muted-foreground)]/60" />
+ <p className="text-[12px] font-medium text-[var(--foreground)]">
+ {t(
+ view === "citations" ? "No citations yet" : "No annotations yet",
+ )}
+ </p>
+ <p className="max-w-[220px] text-[11px] leading-relaxed text-[var(--muted-foreground)]">
+ {t(
+ view === "citations"
+ ? "Select text in the document to save a citation."
+ : "Select text in the document to highlight it or attach a note.",
+ )}
+ </p>
+ </div>
+ ) : (
+ <div className="dt-reader-scroll min-h-0 flex-1 overflow-y-auto px-2.5 py-2">
+ {groups.map(([locator, rows]) => (
+ <section key={locator} className="mb-3 last:mb-1">
+ <h4 className="sticky top-0 z-10 mb-1 bg-[var(--background)]/95 px-1 py-1 font-mono text-[10px] uppercase tracking-[0.06em] text-[var(--muted-foreground)] backdrop-blur">
+ {t(unitLabel(unit))} {locator}
+ </h4>
+ <ul className="space-y-1">
+ {rows.map((annotation) => (
+ <li key={annotation.annotation_id}>
+ <div
+ role="button"
+ tabIndex={0}
+ onClick={() => onSelect(annotation)}
+ onKeyDown={(event) => {
+ if (event.key === "Enter" || event.key === " ") {
+ event.preventDefault();
+ onSelect(annotation);
+ }
+ }}
+ className={`group/anno relative w-full cursor-pointer rounded-lg border px-2.5 py-2 text-left transition ${
+ annotation.annotation_id === activeId
+ ? "border-[var(--ring)] bg-[var(--muted)]/60"
+ : "border-transparent hover:border-[var(--border)] hover:bg-[var(--muted)]/40"
+ }`}
+ >
+ <span
+ aria-hidden
+ className="absolute left-0 top-2 bottom-2 w-[3px] rounded-full"
+ style={{
- background: SWATCH[annotation.color] ?? SWATCH.yellow,
++ background:
++ ANNOTATION_SWATCH[
++ annotation.color as AnnotationColor
++ ] ?? ANNOTATION_SWATCH.yellow,
+ }}
+ />
+ {annotation.quote && (
+ <p className="line-clamp-3 pl-1.5 text-[12px] leading-[1.55] text-[var(--foreground)]">
+ {annotation.quote}
+ </p>
+ )}
+ {annotation.note && (
+ <p className="mt-1 line-clamp-3 pl-1.5 text-[11px] leading-[1.5] text-[var(--muted-foreground)]">
+ {annotation.note}
+ </p>
+ )}
+ <div className="mt-1 flex items-center gap-1.5 pl-1.5">
+ {annotation.author === "assistant" && (
+ <span className="inline-flex items-center gap-1 rounded-full bg-[var(--primary)]/10 px-1.5 py-[1px] text-[10px] font-medium text-[var(--primary)]">
+ <Bot size={9} />
+ {t("AI")}
+ </span>
+ )}
+ {annotation.kind === "underline" && (
+ <span className="text-[10px] text-[var(--muted-foreground)]">
+ {t("Underline")}
+ </span>
+ )}
+ {annotation.kind === "citation" && (
+ <span className="inline-flex items-center gap-1 text-[10px] text-[var(--muted-foreground)]">
+ <Bookmark size={9} />
+ {t("Citation")}
+ </span>
+ )}
+ </div>
+ <button
+ type="button"
+ title={t("Delete annotation")}
+ aria-label={t("Delete annotation")}
+ onClick={(event) => {
+ event.stopPropagation();
+ onDelete(annotation);
+ }}
+ className="absolute right-1.5 top-1.5 inline-flex h-6 w-6 items-center justify-center rounded-md text-[var(--muted-foreground)] opacity-0 transition hover:bg-[var(--destructive)]/10 hover:text-[var(--destructive)] focus-visible:opacity-100 group-hover/anno:opacity-100"
+ >
+ <Trash2 size={12} />
+ </button>
+ </div>
+ </li>
+ ))}
+ </ul>
+ </section>
+ ))}
+ </div>
+ )}
</div>
);
}
diff --cc web/components/reading/ReaderPane.tsx
index 415361457,8ed5c4e70..544461fcd
--- a/web/components/reading/ReaderPane.tsx
+++ b/web/components/reading/ReaderPane.tsx
@@@ -293,6 -322,6 +293,7 @@@ export function ReaderPane(
{
annotation_id: temporaryId,
locator: selection.locator,
++ material_revision: material.revision ?? 1,
kind: kind === "note" ? "highlight" : kind,
color,
quote: selection.quote,
diff --cc web/lib/reading-api.ts
index 9118d0445,8777ac7ab..a713dfc68
--- a/web/lib/reading-api.ts
+++ b/web/lib/reading-api.ts
@@@ -8,11 -8,10 +8,11 @@@ import { apiFetch, apiUrl } from "@/lib
// unit word is carried on the material so the UI can say "page 12" or
// "chapter 3" without ever branching on the file type itself.
-export type UnitKind = "page" | "chapter" | "slide" | "section";
+export type UnitKind = "page" | "chapter" | "slide" | "section" | "segment";
- export type AnnotationKind = "highlight" | "underline" | "note";
+ export type AnnotationKind = "highlight" | "underline" | "note" | "citation";
export type ExportFormat = "auto" | "pdf" | "markdown";
-export type RenderMode = "text" | "pdf" | "epub";
+export type RenderMode = "text" | "pdf" | "epub" | "video" | "audio";
+export type ContentFormat = "plain_text" | "web_markdown";
/** Palette offered by the annotation toolbar; mirrored server-side. */
export const ANNOTATION_COLORS = [
@@@ -105,6 -84,6 +105,7 @@@ export type ReadingTextSelector
export interface AnnotationItem {
annotation_id: string;
locator: number;
++ material_revision?: number;
kind: AnnotationKind;
color: string;
quote: string;