DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(reading): resolve reflowed text selectors consistently
Bingxi Zhao (Frank)4cf053257c74
diff --git a/web/lib/reading-w3c-annotations.ts b/web/lib/reading-w3c-annotations.tsindex 09b339031..814c425d1 100644--- a/web/lib/reading-w3c-annotations.ts+++ b/web/lib/reading-w3c-annotations.ts@@ -58,32 +58,37 @@ export function resolveTextSelectors(positionSelector?.type === "TextPositionSelector" &&positionSelector.start >= 0 &&positionSelector.end <= text.length &&- text.slice(positionSelector.start, positionSelector.end) === exact+ normalise(text.slice(positionSelector.start, positionSelector.end)) ===+ normalise(exact)) {+ const canonicalExact = text.slice(+ positionSelector.start,+ positionSelector.end,+ );return [- quoteSelector?.type === "TextQuoteSelector"- ? quoteSelector- : quoteAt(text, exact, positionSelector.start),+ quoteAt(text, canonicalExact, positionSelector.start),positionSelector,];}const candidates = occurrences(text, exact);- const contextual = candidates.filter((start) => {+ const contextual = candidates.filter(({ start, end }) => {if (quoteSelector?.type !== "TextQuoteSelector") return true;const prefixMatches =- !quoteSelector.prefix || text.slice(0, start).endsWith(quoteSelector.prefix);+ !quoteSelector.prefix ||+ normalise(text.slice(0, start)).endsWith(normalise(quoteSelector.prefix));const suffixMatches =!quoteSelector.suffix ||- text.slice(start + exact.length).startsWith(quoteSelector.suffix);+ normalise(text.slice(end)).startsWith(normalise(quoteSelector.suffix));return prefixMatches && suffixMatches;});const matches = contextual.length ? contextual : candidates;if (matches.length !== 1) return null;- const start = matches[0];+ const { start, end } = matches[0];+ const canonicalExact = text.slice(start, end);return [- quoteAt(text, exact, start),- { type: "TextPositionSelector", start, end: start + exact.length },+ quoteAt(text, canonicalExact, start),+ { type: "TextPositionSelector", start, end },];}@@ -152,14 +157,23 @@ function quoteAt(text: string, exact: string, start: number): ReadingTextSelecto};}-function occurrences(text: string, quote: string): number[] {- const found: number[] = [];- let start = 0;- while (start <= text.length - quote.length) {- const match = text.indexOf(quote, start);- if (match < 0) break;- found.push(match);- start = match + Math.max(1, quote.length);- }- return found;+function normalise(value: string): string {+ return value.replace(/\s+/g, " ").trim();+}++function occurrences(+ text: string,+ quote: string,+): Array<{ start: number; end: number }> {+ const words = quote.match(/\S+/g);+ if (!words?.length) return [];+ const pattern = new RegExp(words.map(escapeRegExp).join("\\s+"), "g");+ return [...text.matchAll(pattern)].map((match) => ({+ start: match.index,+ end: match.index + match[0].length,+ }));+}++function escapeRegExp(value: string): string {+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");}diff --git a/web/tests/e2e/reading-w3c-annotations.audit.ts b/web/tests/e2e/reading-w3c-annotations.audit.tsindex bb80d944d..6c137738c 100644--- a/web/tests/e2e/reading-w3c-annotations.audit.ts+++ b/web/tests/e2e/reading-w3c-annotations.audit.ts@@ -101,6 +101,10 @@ test("a rich text annotation reflows and activates its sidebar entry", async ({await expect(highlight).toBeVisible();await expect(page.getByText("Light can behave like a wave")).toBeVisible();+ await page.setViewportSize({ width: 1100, height: 700 });+ await expect(highlight).toBeVisible();+ await expect(highlight).toHaveAttribute("data-annotation", "annotation-1");+const sidebarEntry = page.getByRole("button").filter({ hasText: "Wave behavior" });await expect(sidebarEntry).toBeVisible();const article = page.locator("article.r6o-annotatable");diff --git a/web/tests/reading-w3c-annotations.test.ts b/web/tests/reading-w3c-annotations.test.tsindex 7c506c603..7f03f6e99 100644--- a/web/tests/reading-w3c-annotations.test.ts+++ b/web/tests/reading-w3c-annotations.test.ts@@ -9,6 +9,7 @@ import {} from "@/lib/reading-w3c-annotations";function annotation(overrides: Partial<AnnotationItem> = {}): AnnotationItem {+ const { source_anchor = "", ...rest } = overrides;return {annotation_id: "mark-1",locator: 1,@@ -17,10 +18,11 @@ function annotation(overrides: Partial<AnnotationItem> = {}): AnnotationItem {quote: "portable quote",note: "",rects: [],+ source_anchor,author: "user",created_at: 1,updated_at: 1,- ...overrides,+ ...rest,};}@@ -83,6 +85,28 @@ test("prefix and suffix disambiguate repeated Chinese text", () => {});});+test("whitespace-normalised selectors resolve to the rendered text", () => {+ const selectors = resolveTextSelectors(+ "Start portable\n\nquote end",+ annotation({+ selectors: [+ { type: "TextQuoteSelector", exact: "portable quote" },+ { type: "TextPositionSelector", start: 6, end: 21 },+ ],+ }),+ );++ assert.deepEqual(selectors, [+ {+ type: "TextQuoteSelector",+ exact: "portable\n\nquote",+ prefix: "Start ",+ suffix: " end",+ },+ { type: "TextPositionSelector", start: 6, end: 21 },+ ]);+});+test("an ambiguous legacy quote stays unresolved instead of marking the wrong text", () => {assert.equal(resolveTextSelectors("portable quote then portable quote", annotation()),