From b7463da3efa080d6675d0113e3531bf73adfd485 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 10 Oct 2026 20:28:18 +0000 Subject: [PATCH 1/3] feat(resources): a Similar tab on every resource, and a page with everything like it Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01NSG4xbWi1TYHFmWSUh1CSt --- nextjs-frontend/__tests__/similar.test.tsx | 95 ++++++++ nextjs-frontend/app/(app)/similar/page.tsx | 15 ++ .../components/recording/context.tsx | 1 + .../recording/document/document-layout.tsx | 1 + .../components/recording/mobile-layout.tsx | 3 +- .../components/recording/side-panel.tsx | 6 +- .../components/recording/similar-tab.tsx | 18 ++ .../recording/video/video-layout.tsx | 1 + nextjs-frontend/components/similar/model.ts | 94 ++++++++ .../components/similar/similar-list.tsx | 216 ++++++++++++++++++ .../components/similar/similar-page.tsx | 109 +++++++++ 11 files changed, 557 insertions(+), 2 deletions(-) create mode 100644 nextjs-frontend/__tests__/similar.test.tsx create mode 100644 nextjs-frontend/app/(app)/similar/page.tsx create mode 100644 nextjs-frontend/components/recording/similar-tab.tsx create mode 100644 nextjs-frontend/components/similar/model.ts create mode 100644 nextjs-frontend/components/similar/similar-list.tsx create mode 100644 nextjs-frontend/components/similar/similar-page.tsx diff --git a/nextjs-frontend/__tests__/similar.test.tsx b/nextjs-frontend/__tests__/similar.test.tsx new file mode 100644 index 000000000..a540cbe93 --- /dev/null +++ b/nextjs-frontend/__tests__/similar.test.tsx @@ -0,0 +1,95 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { render, screen } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import { Search } from "@/app/openapi-client"; +import type { SimilarResults } from "@/app/openapi-client/types.gen"; +import { basisFromParams, basisQuery, citeHref, similarHref, TEXT_MAX } from "@/components/similar/model"; +import { SimilarResultsView } from "@/components/similar/similar-list"; +import { TooltipProvider } from "@/components/ui/tooltip"; + +jest.mock("@/app/openapi-client", () => ({ Search: { findSimilar: jest.fn() } })); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +const findSimilar = Search.findSimilar as jest.Mock; + +const RES: SimilarResults = { + basis: "recording", + label: "ep1", + text: null, + terms: ["capsid", "gene"], + mode: "words", + meaning: null, + semantic: false, + total: 12, + items: [ + { + recording_id: 2, + title: "ep2", + namespace: "pods", + recorded_at: "2026-10-01T10:00:00", + media_kind: "document", + score: 0.03, + match: ["words", "topics"], + similarity: null, + cites: [ + { match: "words", source: "page", snippet: "the capsid & more", idx: 4, t0: 2000, page: 2 }, + { match: "words", source: "said", snippet: "a gene", idx: 7, t0: 65000, page: null }, + ], + words: ["capsid", "gene"], + topics: [{ id: 9, label: "Gene therapy" }], + entities: [{ id: 5, name: "Dyno Therapeutics" }], + }, + ], +}; + +function show(ui: React.ReactNode) { + const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + return render( + + {ui} + , + ); +} + +beforeEach(() => jest.clearAllMocks()); + +describe("similar content", () => { + it("asks for one basis, and links to the full list and the moments", () => { + expect(basisQuery({ text: "x".repeat(TEXT_MAX + 50), exclude: 3 })).toEqual({ text: "x".repeat(TEXT_MAX), exclude: 3 }); + expect(basisQuery({ topic: 4 })).toEqual({ topic: 4 }); + expect(similarHref({ text: "gene therapy", exclude: 3 })).toBe("/similar?text=gene+therapy&exclude=3"); + expect(similarHref({ text: "gene therapy" })).toBe("/similar?text=gene+therapy"); + expect(similarHref({ entity: 5 })).toBe("/similar?entity=5"); + expect(basisFromParams(new URLSearchParams("text=gene+therapy&exclude=3"))).toEqual({ text: "gene therapy", exclude: 3 }); + expect(basisFromParams(new URLSearchParams("recording=12"))).toEqual({ recording: 12 }); + expect(basisFromParams(new URLSearchParams("topic=x"))).toBeNull(); + expect(basisFromParams(new URLSearchParams(""))).toBeNull(); + // a page of a document opens at that page; a moment at its second + expect(citeHref(2, { page: 2, t0: 2000 })).toBe("/resources/2?page=2"); + expect(citeHref(2, { page: null, t0: 65000 })).toBe("/resources/2?t=65"); + }); + + it("lists what's alike with why, the moments to open, and Find more", async () => { + findSimilar.mockResolvedValue({ data: RES, response: { ok: true, status: 200 } }); + show(); + expect(await screen.findByRole("link", { name: "ep2" })).toHaveAttribute("href", "/resources/2"); + expect(findSimilar.mock.calls[0][0].query).toEqual({ recording: 1, limit: 10 }); + expect(screen.getByText("Shares key words")).toBeInTheDocument(); + expect(screen.getByText("Same topics")).toBeInTheDocument(); + // the snippet is text, its matches marked, its entities decoded + expect(screen.getByText("capsid").tagName).toBe("MARK"); + expect(screen.getByText("& more")).toBeInTheDocument(); + expect(screen.getByText("p. 3").closest("a")).toHaveAttribute("href", "/resources/2?page=2"); + expect(screen.getByText("1:05").closest("a")).toHaveAttribute("href", "/resources/2?t=65"); + expect(screen.getByRole("link", { name: /Gene therapy/ })).toHaveAttribute("href", "/topics/9"); + expect(screen.getByRole("link", { name: /Dyno Therapeutics/ })).toHaveAttribute("href", "/entities/5"); + expect(screen.getByText("By key words, topics and entities.")).toBeInTheDocument(); + expect(screen.getByRole("link", { name: /Find more \(12\)/ })).toHaveAttribute("href", "/similar?recording=1"); + }); + + it("says when nothing is alike, and why it wasn't by meaning", async () => { + findSimilar.mockResolvedValue({ data: { ...RES, items: [], total: 0 }, response: { ok: true, status: 200 } }); + show(); + expect(await screen.findByText("Nothing similar found.")).toBeInTheDocument(); + }); +}); diff --git a/nextjs-frontend/app/(app)/similar/page.tsx b/nextjs-frontend/app/(app)/similar/page.tsx new file mode 100644 index 000000000..d05c87a6f --- /dev/null +++ b/nextjs-frontend/app/(app)/similar/page.tsx @@ -0,0 +1,15 @@ +import type { Metadata } from "next"; +import { Suspense } from "react"; + +import { SimilarPage } from "@/components/similar/similar-page"; + +export const metadata: Metadata = { title: "Similar" }; + +/** Everything like a selected text, a resource, a topic or an entity ("Find more"). */ +export default function Page() { + return ( + + + + ); +} diff --git a/nextjs-frontend/components/recording/context.tsx b/nextjs-frontend/components/recording/context.tsx index d4cd05d3c..331be6797 100644 --- a/nextjs-frontend/components/recording/context.tsx +++ b/nextjs-frontend/components/recording/context.tsx @@ -21,6 +21,7 @@ export type PanelTab = | "iiif" | "details" | "files" + | "similar" | "shots" | "text" | "people" diff --git a/nextjs-frontend/components/recording/document/document-layout.tsx b/nextjs-frontend/components/recording/document/document-layout.tsx index 2ebc2bce3..90e80ad65 100644 --- a/nextjs-frontend/components/recording/document/document-layout.tsx +++ b/nextjs-frontend/components/recording/document/document-layout.tsx @@ -762,6 +762,7 @@ export function DocumentLayout({ compact }: { compact: boolean }) { { value: "pages", label: "Text", count: model.segments.length || undefined }, { value: "summary", label: "Summary" }, { value: "entities", label: "Entities" }, + { value: "similar", label: "Similar" }, { value: "chat", label: "Chat" }, { value: "notes", label: "Notes", count: notes || undefined }, { value: "comments", label: "Comments", count: open || undefined }, diff --git a/nextjs-frontend/components/recording/mobile-layout.tsx b/nextjs-frontend/components/recording/mobile-layout.tsx index 9afea7982..7fbf34e9d 100644 --- a/nextjs-frontend/components/recording/mobile-layout.tsx +++ b/nextjs-frontend/components/recording/mobile-layout.tsx @@ -55,7 +55,8 @@ export function MobileLayout() { const [lastTab, setLastTab] = useState(tab); if (tab !== lastTab) { setLastTab(tab); - if (["chat", "notes", "comments", "highlights", "history", "details", "files"].includes(tab)) setSheet(true); + if (["chat", "notes", "comments", "highlights", "history", "details", "files", "similar"].includes(tab)) + setSheet(true); } return ( diff --git a/nextjs-frontend/components/recording/side-panel.tsx b/nextjs-frontend/components/recording/side-panel.tsx index d2b174e56..c7969f79b 100644 --- a/nextjs-frontend/components/recording/side-panel.tsx +++ b/nextjs-frontend/components/recording/side-panel.tsx @@ -15,6 +15,7 @@ import { HighlightsTab } from "@/components/recording/highlights-tab"; import { useComments, useNotes } from "@/components/recording/hooks"; import { IiifTab, MetadataTab } from "@/components/recording/iiif-tab"; import { NotesTab } from "@/components/recording/notes-tab"; +import { SimilarTab } from "@/components/recording/similar-tab"; import { SpeakersTab } from "@/components/recording/speakers-tab"; import { SummaryTab } from "@/components/recording/summary-tab"; import { fitTabs } from "@/components/recording/tab-fit"; @@ -33,6 +34,7 @@ export const AUDIO_TABS: TabDef[] = [ { value: "summary", label: "Summary" }, { value: "speakers", label: "Speakers" }, { value: "entities", label: "Entities" }, + { value: "similar", label: "Similar" }, { value: "chat", label: "Chat" }, { value: "notes", label: "Notes" }, { value: "comments", label: "Comments" }, @@ -264,6 +266,8 @@ export function PanelBody({ tab }: { tab: PanelTab }) { return ; case "files": return ; + case "similar": + return ; default: return null; } @@ -297,7 +301,7 @@ export function PanelScroll({ ); } -/** Desktop side panel (488px): Summary · Speakers · Entities · Chat · Notes · Comments · History, and More (…). */ +/** Desktop side panel (488px): Summary · Speakers · Entities · Similar · Chat · Notes · Comments · History, and More (…). */ export function SidePanel() { const { tab, setTab } = useRec(); const tabs = useAudioTabs(); diff --git a/nextjs-frontend/components/recording/similar-tab.tsx b/nextjs-frontend/components/recording/similar-tab.tsx new file mode 100644 index 000000000..d5c5d75d9 --- /dev/null +++ b/nextjs-frontend/components/recording/similar-tab.tsx @@ -0,0 +1,18 @@ +"use client"; + +import { useRec } from "@/components/recording/context"; +import { SimilarResultsView } from "@/components/similar/similar-list"; + +/** Similar tab: the resources most like this one (by meaning, key words, topics and entities), each with the moments + * that show it, and Find more for all of them. Selecting text in the page shows what's like that instead. */ +export function SimilarTab() { + const { id } = useRec(); + return ( + <> +

+ Resources like this one. Select any text to see what's like that. +

+ + + ); +} diff --git a/nextjs-frontend/components/recording/video/video-layout.tsx b/nextjs-frontend/components/recording/video/video-layout.tsx index cd90eee7b..e222636ba 100644 --- a/nextjs-frontend/components/recording/video/video-layout.tsx +++ b/nextjs-frontend/components/recording/video/video-layout.tsx @@ -100,6 +100,7 @@ export function VideoLayout({ }, { value: "objects", label: "Objects", count: model.objects.length }, { value: "summary", label: "Summary" }, + { value: "similar", label: "Similar" }, { value: "iiif", label: "IIIF" }, ]; const more: TabDef[] = [ diff --git a/nextjs-frontend/components/similar/model.ts b/nextjs-frontend/components/similar/model.ts new file mode 100644 index 000000000..633e08b16 --- /dev/null +++ b/nextjs-frontend/components/similar/model.ts @@ -0,0 +1,94 @@ +"use client"; + +import { useInfiniteQuery, useQuery } from "@tanstack/react-query"; + +import { Search } from "@/app/openapi-client"; +import type { SimilarCite, SimilarResults } from "@/app/openapi-client/types.gen"; +import { recordingHref } from "@/components/search/links"; +import { data, useApiClient } from "@/lib/api/browser"; + +/** What to find content like: selected text (and the resource it was selected in), a resource, a topic or an entity. */ +export type SimilarBasis = + { text: string; exclude?: number | null } | { recording: number } | { topic: number } | { entity: number }; + +/** The most of a selection that's compared (the API's limit). */ +export const TEXT_MAX = 2000; +/** The fewest characters a selection needs before it's worth comparing. */ +export const TEXT_MIN = 3; + +/** The basis as the API's query parameters. */ +export function basisQuery(b: SimilarBasis) { + if ("text" in b) return { text: b.text.slice(0, TEXT_MAX), exclude: b.exclude ?? undefined }; + if ("recording" in b) return { recording: b.recording }; + if ("topic" in b) return { topic: b.topic }; + return { entity: b.entity }; +} + +function basisKey(b: SimilarBasis): string { + return JSON.stringify(basisQuery(b)); +} + +/** The first `limit` resources like `basis`. */ +export function useSimilar(basis: SimilarBasis | null, { limit = 10, enabled = true } = {}) { + const client = useApiClient(); + return useQuery({ + queryKey: ["similar", basis ? basisKey(basis) : null, limit], + queryFn: () => data(Search.findSimilar({ client, query: { ...basisQuery(basis!), limit } })), + enabled: enabled && basis != null, + staleTime: 60_000, + }); +} + +/** Every resource like `basis`, a page at a time (the full Similar page). */ +export function useSimilarPages(basis: SimilarBasis | null, pageSize = 20) { + const client = useApiClient(); + return useInfiniteQuery({ + queryKey: ["similar-pages", basis ? basisKey(basis) : null, pageSize], + queryFn: ({ pageParam }) => + data(Search.findSimilar({ client, query: { ...basisQuery(basis!), limit: pageSize, offset: pageParam } })), + initialPageParam: 0, + getNextPageParam: (last: SimilarResults, pages: SimilarResults[]) => { + const seen = pages.reduce((n, p) => n + p.items.length, 0); + return seen < last.total && last.items.length ? seen : undefined; + }, + enabled: basis != null, + staleTime: 60_000, + }); +} + +/** The full Similar page for a basis ("Find more"). */ +export function similarHref(b: SimilarBasis): string { + const q = new URLSearchParams(); + for (const [k, v] of Object.entries(basisQuery(b))) if (v != null && v !== "") q.set(k, String(v)); + return `/similar?${q}`; +} + +/** The basis a Similar page's address asks for, or null. */ +export function basisFromParams(p: URLSearchParams): SimilarBasis | null { + const n = (k: string) => { + const v = p.get(k); + return v && /^\d+$/.test(v) ? Number(v) : null; + }; + const text = p.get("text")?.trim(); + if (text) return { text, exclude: n("exclude") }; + const recording = n("recording"); + if (recording != null) return { recording }; + const topic = n("topic"); + if (topic != null) return { topic }; + const entity = n("entity"); + if (entity != null) return { entity }; + return null; +} + +/** Where a cited moment opens: its page of a document, or its time. */ +export function citeHref(recordingId: number, c: Pick): string { + return c.page != null ? `/resources/${recordingId}?page=${c.page}` : recordingHref(recordingId, c.t0); +} + +/** Why a result is alike, in words. */ +export const MATCH_LABEL: Record = { + meaning: "Close in meaning", + words: "Shares key words", + topics: "Same topics", + entities: "Same entities", +}; diff --git a/nextjs-frontend/components/similar/similar-list.tsx b/nextjs-frontend/components/similar/similar-list.tsx new file mode 100644 index 000000000..edd1aee57 --- /dev/null +++ b/nextjs-frontend/components/similar/similar-list.tsx @@ -0,0 +1,216 @@ +"use client"; + +import { ArrowRight, Hash, Sparkle, Tag } from "lucide-react"; +import Link from "next/link"; + +import type { SimilarItem, SimilarResults } from "@/app/openapi-client/types.gen"; +import { MediaIcon } from "@/components/library/cells"; +import { Snippet } from "@/components/search/results"; +import { MATCH_LABEL, citeHref, similarHref, useSimilar, type SimilarBasis } from "@/components/similar/model"; +import { EmptyState, Skeleton } from "@/components/ui/states"; +import { Tooltip } from "@/components/ui/tooltip"; +import { shortDate, tc } from "@/lib/format"; +import { cn } from "@/lib/utils"; + +/** Why a result is alike: close in meaning (with how close), shared key words, topics, entities. */ +function Why({ item }: { item: SimilarItem }) { + const words = item.words ?? []; + const topics = item.topics ?? []; + const entities = item.entities ?? []; + return ( + + {item.match.map((m) => ( + t.label).join(", ")}` + : `Mentions ${entities.map((e) => e.name).join(", ")}` + } + > + + {m === "meaning" && } + {MATCH_LABEL[m] ?? m} + + + ))} + + ); +} + +/** One alike resource: its title, where it is, why it's alike, and the moments that show it (each opens there). */ +export function SimilarRow({ item, compact, onOpen }: { item: SimilarItem; compact?: boolean; onOpen?: () => void }) { + const all = item.cites ?? []; + const cites = compact ? all.slice(0, 1) : all; + const topics = item.topics ?? []; + const entities = item.entities ?? []; + const meta = [item.namespace, item.recorded_at ? shortDate(item.recorded_at) : null].filter(Boolean).join(" · "); + return ( +
  • +
    + + + {item.title || `Resource ${item.recording_id}`} + + {meta && !compact && {meta}} +
    + {!compact && } + {cites.length > 0 && ( +
      + {cites.map((c, i) => ( +
    • + + + {c.page != null ? `p. ${c.page + 1}` : c.t0 != null ? tc(c.t0) : ""} + + + +
    • + ))} +
    + )} + {!compact && (topics.length > 0 || entities.length > 0) && ( +
    + {topics.slice(0, 5).map((t) => ( + + + {t.label} + + ))} + {entities.slice(0, 5).map((e) => ( + + + {e.name} + + ))} +
    + )} +
  • + ); +} + +export function SimilarRows({ + items, + compact, + onOpen, +}: { + items: SimilarItem[]; + compact?: boolean; + onOpen?: () => void; +}) { + return ( +
      + {items.map((x) => ( + + ))} +
    + ); +} + +/** How the results were found, in a line: by meaning too, or by words, topics and entities. */ +export function SimilarNote({ res, className }: { res: SimilarResults; className?: string }) { + const how = + res.mode === "meaning" + ? "By meaning, key words, topics and entities." + : "By key words, topics and entities." + + (res.meaning ? ` ${res.meaning[0].toUpperCase()}${res.meaning.slice(1)}.` : ""); + return

    {how}

    ; +} + +/** Content like `basis`: loading, nothing found, or the first `limit` with a link to all of them. */ +export function SimilarResultsView({ + basis, + limit = 10, + compact, + enabled = true, + onOpen, + empty, +}: { + basis: SimilarBasis | null; + limit?: number; + compact?: boolean; + enabled?: boolean; + onOpen?: () => void; + empty?: string; +}) { + const q = useSimilar(basis, { limit, enabled }); + if (!basis) return null; + if (q.isLoading) + return ( +
    + {Array.from({ length: compact ? 3 : 4 }, (_, i) => ( +
    + + +
    + ))} +
    + ); + if (q.isError) + return ( +

    + Couldn't find similar content: {q.error instanceof Error ? q.error.message : "try again"}. +

    + ); + const res = q.data; + if (!res?.items.length) + return compact ? ( +

    {empty ?? "Nothing similar found."}

    + ) : ( + + {empty ?? + "Nothing else you can read shares its meaning, key words, topics or entities. Similar content shows up as more is added and analysed."} + + ); + return ( +
    + +
    + + + Find more{res.total > res.items.length ? ` (${res.total})` : ""} + + +
    +
    + ); +} diff --git a/nextjs-frontend/components/similar/similar-page.tsx b/nextjs-frontend/components/similar/similar-page.tsx new file mode 100644 index 000000000..e9e1f9300 --- /dev/null +++ b/nextjs-frontend/components/similar/similar-page.tsx @@ -0,0 +1,109 @@ +"use client"; + +import { Search as SearchIcon, Sparkles } from "lucide-react"; +import Link from "next/link"; +import { useSearchParams } from "next/navigation"; +import { useMemo } from "react"; + +import { SimilarNote, SimilarRows } from "@/components/similar/similar-list"; +import { basisFromParams, useSimilarPages, type SimilarBasis } from "@/components/similar/model"; +import { Button } from "@/components/ui/button"; +import { EmptyState, PageHeader, SkeletonRows } from "@/components/ui/states"; +import { plural } from "@/lib/format"; + +/** What the page is like, as a heading: the selected text, or a link to the resource, topic or entity. */ +function Like({ basis, label }: { basis: SimilarBasis; label: string | null | undefined }) { + if ("text" in basis) + return ( + + “{(label ?? basis.text).length > 110 ? `${(label ?? basis.text).slice(0, 107)}…` : (label ?? basis.text)}” + + ); + const [href, what] = + "recording" in basis + ? [`/resources/${basis.recording}`, ""] + : "topic" in basis + ? [`/topics/${basis.topic}`, "the topic "] + : [`/entities/${basis.entity}`, ""]; + return ( + <> + {what} + + {label ?? "…"} + + + ); +} + +/** Similar (/similar?text= | recording= | topic= | entity=): every resource like it, best first, with why and the + * moments that show it, a page at a time. */ +export function SimilarPage() { + const params = useSearchParams(); + const basis = useMemo(() => basisFromParams(new URLSearchParams(params.toString())), [params]); + const q = useSimilarPages(basis); + const first = q.data?.pages[0]; + const items = q.data?.pages.flatMap((p) => p.items) ?? []; + + if (!basis) + return ( +
    + + } title="Select something first"> + Select any text in Lens, or open a resource's Similar tab, then choose Find more to see everything like + it. + +
    + ); + + return ( +
    + + Like + + } + meta={first ? plural(first.total, "resource") : undefined} + actions={ + first?.terms?.length ? ( + + ) : undefined + } + > + {first && } + + {q.isLoading ? ( + + ) : q.isError ? ( + + {q.error instanceof Error ? q.error.message : "Try again."} + + ) : !items.length ? ( + } title="Nothing similar yet"> + Nothing you can read shares its meaning, key words, topics or entities. Similar content shows up as more is + added and analysed. + + ) : ( + <> + + {q.hasNextPage && ( + + )} + + )} +
    + ); +} From 79a3f675fbb94e40b17198183e4cb8af34655fd9 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 10 Oct 2026 20:32:29 +0000 Subject: [PATCH 2/3] feat(search): similar content when selecting text, and for topics, entities and graph nodes Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01NSG4xbWi1TYHFmWSUh1CSt --- nextjs-frontend/__tests__/similar.test.tsx | 62 +++++- .../components/app-shell/account-menu.tsx | 11 +- .../components/app-shell/app-shell.tsx | 2 + .../components/entities/entity-drawer.tsx | 2 + nextjs-frontend/components/graph/panel.tsx | 15 ++ nextjs-frontend/components/similar/prefs.ts | 42 ++++ .../components/similar/selection-popover.tsx | 184 ++++++++++++++++++ .../components/similar/similar-list.tsx | 11 ++ .../components/topics/topic-drawer.tsx | 2 + 9 files changed, 328 insertions(+), 3 deletions(-) create mode 100644 nextjs-frontend/components/similar/prefs.ts create mode 100644 nextjs-frontend/components/similar/selection-popover.tsx diff --git a/nextjs-frontend/__tests__/similar.test.tsx b/nextjs-frontend/__tests__/similar.test.tsx index a540cbe93..5c03fb2fc 100644 --- a/nextjs-frontend/__tests__/similar.test.tsx +++ b/nextjs-frontend/__tests__/similar.test.tsx @@ -5,6 +5,8 @@ import "@testing-library/jest-dom"; import { Search } from "@/app/openapi-client"; import type { SimilarResults } from "@/app/openapi-client/types.gen"; import { basisFromParams, basisQuery, citeHref, similarHref, TEXT_MAX } from "@/components/similar/model"; +import { nodeBasis } from "@/components/graph/panel"; +import { resourceOf, selectedText } from "@/components/similar/selection-popover"; import { SimilarResultsView } from "@/components/similar/similar-list"; import { TooltipProvider } from "@/components/ui/tooltip"; @@ -55,12 +57,18 @@ beforeEach(() => jest.clearAllMocks()); describe("similar content", () => { it("asks for one basis, and links to the full list and the moments", () => { - expect(basisQuery({ text: "x".repeat(TEXT_MAX + 50), exclude: 3 })).toEqual({ text: "x".repeat(TEXT_MAX), exclude: 3 }); + expect(basisQuery({ text: "x".repeat(TEXT_MAX + 50), exclude: 3 })).toEqual({ + text: "x".repeat(TEXT_MAX), + exclude: 3, + }); expect(basisQuery({ topic: 4 })).toEqual({ topic: 4 }); expect(similarHref({ text: "gene therapy", exclude: 3 })).toBe("/similar?text=gene+therapy&exclude=3"); expect(similarHref({ text: "gene therapy" })).toBe("/similar?text=gene+therapy"); expect(similarHref({ entity: 5 })).toBe("/similar?entity=5"); - expect(basisFromParams(new URLSearchParams("text=gene+therapy&exclude=3"))).toEqual({ text: "gene therapy", exclude: 3 }); + expect(basisFromParams(new URLSearchParams("text=gene+therapy&exclude=3"))).toEqual({ + text: "gene therapy", + exclude: 3, + }); expect(basisFromParams(new URLSearchParams("recording=12"))).toEqual({ recording: 12 }); expect(basisFromParams(new URLSearchParams("topic=x"))).toBeNull(); expect(basisFromParams(new URLSearchParams(""))).toBeNull(); @@ -93,3 +101,53 @@ describe("similar content", () => { expect(await screen.findByText("Nothing similar found.")).toBeInTheDocument(); }); }); + +describe("similar content on select", () => { + function select(el: Node) { + const r = document.createRange(); + r.selectNodeContents(el); + const sel = window.getSelection()!; + sel.removeAllRanges(); + sel.addRange(r); + return sel; + } + + it("compares text selected in the page's content, not in fields, menus or elsewhere", () => { + document.body.innerHTML = ` +
    Library
    +
    +

    Capsid design with + machine learning

    +

    ok

    12 34

    +

    being edited now

    +

    left alone here

    +
    `; + const main = document.getElementById("main"); + const at = (id: string) => selectedText(select(document.getElementById(id)!), main); + expect(at("title")).toBe("Capsid design with machine learning"); + expect(at("short")).toBeNull(); + expect(at("digits")).toBeNull(); + expect(at("editing")).toBeNull(); + expect(at("opted")).toBeNull(); + expect(at("nav")).toBeNull(); + window.getSelection()!.removeAllRanges(); + expect(selectedText(window.getSelection(), main)).toBeNull(); + }); + + it("leaves out the resource it was selected in", () => { + expect(resourceOf("/resources/12")).toBe(12); + expect(resourceOf("/recordings/7/edit")).toBe(7); + expect(resourceOf("/topics")).toBeNull(); + expect(resourceOf(null)).toBeNull(); + }); + + it("finds what's like a graph node: a recording, topic or entity, or a merged entity's name", () => { + const n = { label: "Dyno Therapeutics", refs: [5] }; + expect(nodeBasis({ ...n, kind: "entity" })).toEqual({ entity: 5 }); + expect(nodeBasis({ ...n, kind: "entity", refs: [5, 9] })).toEqual({ text: "Dyno Therapeutics" }); + expect(nodeBasis({ ...n, kind: "topic" })).toEqual({ topic: 5 }); + expect(nodeBasis({ ...n, kind: "recording" })).toEqual({ recording: 5 }); + expect(nodeBasis({ ...n, kind: "speaker" })).toBeNull(); + expect(nodeBasis({ ...n, kind: "collection" })).toBeNull(); + }); +}); diff --git a/nextjs-frontend/components/app-shell/account-menu.tsx b/nextjs-frontend/components/app-shell/account-menu.tsx index c9614778a..4c44837fc 100644 --- a/nextjs-frontend/components/app-shell/account-menu.tsx +++ b/nextjs-frontend/components/app-shell/account-menu.tsx @@ -1,13 +1,14 @@ "use client"; import { useQuery } from "@tanstack/react-query"; -import { KeyRound, Keyboard, LogOut, Signpost, SunMoon, UserRound, Users } from "lucide-react"; +import { KeyRound, Keyboard, LogOut, Signpost, Sparkles, SunMoon, UserRound, Users } from "lucide-react"; import Link from "next/link"; import { signOut } from "next-auth/react"; import { useState } from "react"; import { Tokens } from "@/app/openapi-client"; import { useTheme, type ThemeChoice } from "@/components/app-shell/theme"; +import { useSelectMode } from "@/components/similar/prefs"; import { Menu, MenuContent, MenuItem, MenuTrigger } from "@/components/ui/menu"; import { Avatar, Skeleton } from "@/components/ui/states"; import { data, useApiClient } from "@/lib/api/browser"; @@ -37,6 +38,7 @@ export function AccountMenu({ name, email }: { name?: string | null; email: stri const client = useApiClient(); const [open, setOpen] = useState(false); const [theme, setTheme] = useTheme(); + const [similar, setSimilar] = useSelectMode(); const tokens = useQuery({ queryKey: ["tokens"], queryFn: () => data(Tokens.listTokens({ client })), @@ -87,6 +89,13 @@ export function AccountMenu({ name, email }: { name?: string | null; email: stri } onSelect={() => setTheme(NEXT[theme])}> Appearance: {theme[0].toUpperCase() + theme.slice(1)} + } + onSelect={() => setSimilar(similar === "show" ? "button" : "show")} + > + Similar on select: {similar === "show" ? "Show" : "Button"} + } diff --git a/nextjs-frontend/components/app-shell/app-shell.tsx b/nextjs-frontend/components/app-shell/app-shell.tsx index 2ff978bfc..d9941ffe9 100644 --- a/nextjs-frontend/components/app-shell/app-shell.tsx +++ b/nextjs-frontend/components/app-shell/app-shell.tsx @@ -15,6 +15,7 @@ import { Nav } from "@/components/app-shell/nav"; import { Shortcuts } from "@/components/app-shell/shortcuts"; import { WelcomeTour } from "@/components/home/welcome-tour"; import { PageChat } from "@/components/page-chat/page-chat"; +import { SimilarOnSelect } from "@/components/similar/selection-popover"; import { Button, IconButton } from "@/components/ui/button"; import { EmptyState, Skeleton } from "@/components/ui/states"; import { data, useApiClient } from "@/lib/api/browser"; @@ -193,6 +194,7 @@ export function AppShell({ user, children }: { user: SessionUser; children: Reac + ); diff --git a/nextjs-frontend/components/entities/entity-drawer.tsx b/nextjs-frontend/components/entities/entity-drawer.tsx index e7ce2f30c..6a69e4066 100644 --- a/nextjs-frontend/components/entities/entity-drawer.tsx +++ b/nextjs-frontend/components/entities/entity-drawer.tsx @@ -9,6 +9,7 @@ import { useEffect, useState } from "react"; import { Entities, Topics } from "@/app/openapi-client"; import type { EntityType } from "@/app/openapi-client/types.gen"; import { recordingHref } from "@/components/search/links"; +import { SimilarSection } from "@/components/similar/similar-list"; import { Banner } from "@/components/ui/banner"; import { Button } from "@/components/ui/button"; import { Drawer } from "@/components/ui/dialog"; @@ -316,6 +317,7 @@ export function EntityDrawer({ ))} + )} diff --git a/nextjs-frontend/components/graph/panel.tsx b/nextjs-frontend/components/graph/panel.tsx index 55fa69e4d..9f2cdaa84 100644 --- a/nextjs-frontend/components/graph/panel.tsx +++ b/nextjs-frontend/components/graph/panel.tsx @@ -20,6 +20,8 @@ import { useRecordingIndex } from "@/components/search/data"; import { hasMedia, recordingHref } from "@/components/search/links"; import { InlinePlayerBar, useInlinePlayer, type InlinePlayer } from "@/components/search/player"; import { talkTime } from "@/components/speakers/format"; +import { SimilarSection } from "@/components/similar/similar-list"; +import type { SimilarBasis } from "@/components/similar/model"; import { useSpeakerRecordings } from "@/components/speakers/parts"; import { Banner } from "@/components/ui/banner"; import { Button, IconButton } from "@/components/ui/button"; @@ -153,6 +155,17 @@ function SpeakerRecordings({ n, player }: { n: GraphNode; player: InlinePlayer } ); } +/** What a node's similar content is like: a recording, a topic or an entity itself; an entity merged across namespaces + * by its name. Speakers, collections and namespaces have none. */ +export function nodeBasis(n: Pick): SimilarBasis | null { + const id = n.refs[0]; + if (id == null) return null; + if (n.kind === "recording") return { recording: id }; + if (n.kind === "topic") return n.refs.length === 1 ? { topic: id } : { text: n.label }; + if (n.kind === "entity") return n.refs.length === 1 ? { entity: id } : { text: n.label }; + return null; +} + /** The node panel: what it is, where it's mentioned (playable), what it's connected to, and what you can do with it. */ export function NodePanel({ node, @@ -212,6 +225,7 @@ function StructurePanel({

    Right-click it (or long-press, or press M) for its parents, children and paths.

    + {links.length > 0 && ( <>

    Connected here

    @@ -316,6 +330,7 @@ function ThingPanel({

    {isSpeaker ? "Recordings" : "Mentions"}

    {isSpeaker ? : } + {links.length > 0 && ( <>

    Connected

    diff --git a/nextjs-frontend/components/similar/prefs.ts b/nextjs-frontend/components/similar/prefs.ts new file mode 100644 index 000000000..c12b34dac --- /dev/null +++ b/nextjs-frontend/components/similar/prefs.ts @@ -0,0 +1,42 @@ +"use client"; + +import { useCallback, useEffect, useState } from "react"; + +/** What selecting text does: show similar content at once ("show"), or offer a Similar button to open it ("button"). */ +export type SelectMode = "show" | "button"; +const KEY = "lens.similar.select"; +const EVENT = "lens:similar-select"; + +function read(): SelectMode { + try { + return localStorage.getItem(KEY) === "button" ? "button" : "show"; + } catch { + return "show"; + } +} + +/** The person's choice for this browser (the account menu switches it), kept in step across the page. */ +export function useSelectMode(): [SelectMode, (m: SelectMode) => void] { + const [mode, setModeState] = useState("show"); + useEffect(() => { + setModeState(read()); + const on = () => setModeState(read()); + window.addEventListener(EVENT, on); + window.addEventListener("storage", on); + return () => { + window.removeEventListener(EVENT, on); + window.removeEventListener("storage", on); + }; + }, []); + const setMode = useCallback((m: SelectMode) => { + try { + if (m === "show") localStorage.removeItem(KEY); + else localStorage.setItem(KEY, m); + } catch { + /* storage unavailable: this page keeps the choice */ + } + setModeState(m); + window.dispatchEvent(new CustomEvent(EVENT)); + }, []); + return [mode, setMode]; +} diff --git a/nextjs-frontend/components/similar/selection-popover.tsx b/nextjs-frontend/components/similar/selection-popover.tsx new file mode 100644 index 000000000..16ae7f244 --- /dev/null +++ b/nextjs-frontend/components/similar/selection-popover.tsx @@ -0,0 +1,184 @@ +"use client"; + +import { Sparkles, X } from "lucide-react"; +import { usePathname } from "next/navigation"; +import { useEffect, useRef, useState } from "react"; + +import { TEXT_MAX, TEXT_MIN, type SimilarBasis } from "@/components/similar/model"; +import { useSelectMode } from "@/components/similar/prefs"; +import { SimilarResultsView } from "@/components/similar/similar-list"; +import { cn } from "@/lib/utils"; + +const WIDTH = 380; +const SETTLE_MS = 350; // a selection that stays put this long is what was meant + +type Picked = { text: string; top: number; bottom: number; x: number }; + +/** Selected text worth comparing: in the page's content (not the header, navigation or chat), not in a field, and with + * a word in it. Elements marked data-no-similar opt out. */ +export function selectedText(sel: Selection | null, root: Element | null): string | null { + if (!sel || sel.isCollapsed || !sel.rangeCount || !root) return null; + const node = sel.getRangeAt(0).commonAncestorContainer; + const el = node.nodeType === 1 ? (node as Element) : node.parentElement; + if (!el || !root.contains(el)) return null; + if (el.closest("input, textarea, select, [contenteditable=true], [data-no-similar], [role=dialog], [role=menu]")) + return null; + const text = sel.toString().replace(/\s+/g, " ").trim(); + if (text.length < TEXT_MIN || !/\p{L}{2}/u.test(text)) return null; + return text.slice(0, TEXT_MAX); +} + +/** The resource a page is about (/resources/12), so what's selected in it doesn't find itself. */ +export function resourceOf(pathname: string | null): number | null { + const m = /^\/(?:resources|recordings)\/(\d+)/.exec(pathname ?? ""); + return m ? Number(m[1]) : null; +} + +/** + * Selecting text anywhere in a page (a resource's text or title, a note, a topic or an entity) shows the content most + * like it in a popover under the selection, with Find more for all of it. On a resource's text the selection toolbar + * stays above and this goes below. Esc, a click elsewhere or scrolling closes it. In the account menu, "Similar on + * select: Button" offers a small Similar button instead of opening at once. + */ +export function SimilarOnSelect() { + const pathname = usePathname(); + const [mode] = useSelectMode(); + const [picked, setPicked] = useState(null); + const [open, setOpen] = useState(false); + const box = useRef(null); + const modeRef = useRef(mode); + modeRef.current = mode; + const shown = useRef(false); + shown.current = picked != null; + + useEffect(() => { + let timer: ReturnType | undefined; + let pressed = false; + let dismissed: string | null = null; // closed while still selected (a toolbar button keeps it): don't reopen + const main = () => document.getElementById("main"); + const inside = (t: EventTarget | null) => t instanceof Node && Boolean(box.current?.contains(t)); + const look = () => { + clearTimeout(timer); + timer = setTimeout(() => { + if (pressed) return; // still dragging: wait for the button to come up + const sel = window.getSelection(); + if (sel && sel.rangeCount && inside(sel.getRangeAt(0).commonAncestorContainer)) return; + const text = selectedText(sel, main()); + if (!text || !sel) { + dismissed = null; + return; + } + if (text === dismissed) return; + const r = sel.getRangeAt(0).getBoundingClientRect(); + setPicked((p) => (p?.text === text ? p : { text, top: r.top, bottom: r.bottom, x: r.left + r.width / 2 })); + setOpen(modeRef.current === "show"); + }, SETTLE_MS); + }; + const close = () => { + clearTimeout(timer); + dismissed = selectedText(window.getSelection(), main()); + setPicked(null); + setOpen(false); + }; + const onDown = (e: PointerEvent) => { + if (inside(e.target)) return; + pressed = true; + close(); + }; + const onUp = () => { + pressed = false; + look(); + }; + const onKey = (e: KeyboardEvent) => { + if (e.key === "Escape") close(); + }; + const onScroll = (e: Event) => { + if (shown.current && !inside(e.target)) close(); // it would float away from the words + }; + document.addEventListener("selectionchange", look); + document.addEventListener("pointerdown", onDown, true); + document.addEventListener("pointerup", onUp, true); + document.addEventListener("keydown", onKey); + window.addEventListener("scroll", onScroll, true); + const onResize = () => shown.current && close(); + window.addEventListener("resize", onResize); + return () => { + clearTimeout(timer); + document.removeEventListener("selectionchange", look); + document.removeEventListener("pointerdown", onDown, true); + document.removeEventListener("pointerup", onUp, true); + document.removeEventListener("keydown", onKey); + window.removeEventListener("scroll", onScroll, true); + window.removeEventListener("resize", onResize); + }; + }, []); + + // a new page: what was selected on the last one is gone + useEffect(() => { + setPicked(null); + setOpen(false); + }, [pathname]); + + if (!picked) return null; + const vw = window.innerWidth; + const vh = window.innerHeight; + const narrow = vw < 640; + const below = vh - picked.bottom > 260 || picked.top < 300; + const left = narrow ? 8 : Math.max(8, Math.min(picked.x - WIDTH / 2, vw - WIDTH - 8)); + const exclude = resourceOf(pathname); + const basis: SimilarBasis = { text: picked.text, exclude }; + const quote = picked.text.length > 70 ? `${picked.text.slice(0, 67)}…` : picked.text; + const place = below ? { top: Math.min(picked.bottom + 12, vh - 120) } : { bottom: vh - picked.top + 12 }; + + if (!open) + return ( +
    + +
    + ); + + return ( +
    +
    + +
    + Similar to “{quote}” +
    + +
    +
    + setPicked(null)} /> +
    +
    + ); +} diff --git a/nextjs-frontend/components/similar/similar-list.tsx b/nextjs-frontend/components/similar/similar-list.tsx index edd1aee57..52814778d 100644 --- a/nextjs-frontend/components/similar/similar-list.tsx +++ b/nextjs-frontend/components/similar/similar-list.tsx @@ -214,3 +214,14 @@ export function SimilarResultsView({ ); } + +/** "Similar content" in a drawer or panel: the five most alike, with Find more. */ +export function SimilarSection({ basis, title = "Similar content" }: { basis: SimilarBasis | null; title?: string }) { + if (!basis) return null; + return ( +
    +

    {title}

    + +
    + ); +} diff --git a/nextjs-frontend/components/topics/topic-drawer.tsx b/nextjs-frontend/components/topics/topic-drawer.tsx index 541212947..098e4d407 100644 --- a/nextjs-frontend/components/topics/topic-drawer.tsx +++ b/nextjs-frontend/components/topics/topic-drawer.tsx @@ -6,6 +6,7 @@ import Link from "next/link"; import { useEffect, useState } from "react"; import { Topics } from "@/app/openapi-client"; +import { SimilarSection } from "@/components/similar/similar-list"; import { broaderChoices, splitLabels, type TopicItem } from "@/components/topics/model"; import { TopicPicker } from "@/components/topics/topic-picker"; import { Badge } from "@/components/ui/badge"; @@ -308,6 +309,7 @@ export function TopicDrawer({ ))} + )} From 07e4ff0117603c75b5ce07dfe29756799ae84f5f Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 10 Oct 2026 20:35:47 +0000 Subject: [PATCH 3/3] docs(search): the Similar tab and the selection popover in the web app Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01NSG4xbWi1TYHFmWSUh1CSt --- CHANGELOG.md | 6 ++++++ DEVLOG.md | 6 ++++-- docs/processing.md | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10ea98810..e200878e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version ## Unreleased +- **Similar content.** Every resource has a **Similar** tab, and selecting text anywhere (a transcript, a page, a + title, a graph label) shows the resources most like it, each citing the passages, words, topics and entities they + share, with **Find more** for the full list. Topics, entities and graph nodes list similar content too, and the + assistant and the MCP server can ask for it (`find_similar`). It works without search by meaning, by key words, + topics and entities ([Processing](docs/processing.md#similar-content)). + - **Prometheus metrics.** Lens can serve `/metrics` for Prometheus and Grafana: jobs by status and pipeline, imports, model calls, tokens, cost and failures by model, budgets, workers and disk. Off by default; turned on in Settings → Telemetry (or `LENS_PROMETHEUS=on`), it answers only a scraper with the scrape token, and follows `LENS_BIND`. A diff --git a/DEVLOG.md b/DEVLOG.md index 301fbdb07..823456920 100644 --- a/DEVLOG.md +++ b/DEVLOG.md @@ -14,8 +14,8 @@ resource's passages: no model call for a resource), key words (the text's words cites up to three moments. `GET /similar`, assistant and MCP `find_similar`. Without embeddings (the default) the other three answer, with no model call. -PRs: API, assistant and MCP tool; the Similar tab; the selection popover and the full Similar page; topics, entities and -graph nodes. +PRs: #296 the API, assistant and MCP tool; then the web app (the Similar tab, the selection popover and the full +Similar page at `/similar`, and similar content for topics, entities and graph nodes). Refine later: - key words of a text are single words and two-word phrases; stems beyond plurals (designing/design) don't match; @@ -24,6 +24,8 @@ Refine later: sharper; - entities in another namespace with the same name count only through the words, not as the same entity; - no "not similar" feedback yet, and nothing is cached between requests beyond the query vectors. +- selecting text in an editor (notes, the document editor) opens nothing, since selecting there is for editing; a + "Similar" item in the editor's own menu would cover it; ## 2026-10-10 · More voices for spoken answers diff --git a/docs/processing.md b/docs/processing.md index 5ac1c611e..a191cff7c 100644 --- a/docs/processing.md +++ b/docs/processing.md @@ -218,8 +218,8 @@ saying why. ### Similar content -The API (`GET /similar`), the assistant and the MCP server (`find_similar`) list the resources most like a text (a -sentence, a passage, a title), a resource, a topic or an entity. +The web app, the API (`GET /similar`), the assistant and the MCP server (`find_similar`) list the resources most like +a text (a sentence, a passage, a title), a resource, a topic or an entity. Four lists are fused by reciprocal rank, so a resource alike in several ways comes first: @@ -235,6 +235,17 @@ the words, and the topics and entities in common. Without an embeddings server ( answer, and no language model is called; with one, a selected text costs one embedding request, logged in the activity ledger. +In the web app: + +* every resource has a **Similar** tab, after Entities, listing what is most like it; +* selecting text anywhere (a transcript, a page, a title, a summary, a graph label) opens a popover with the five + resources most like the selection, each citing why, and **Find more** opens the full list at `/similar`, with + paging and a search for the key words. The account menu's **Similar on select** switches between showing the + popover at once and showing a small **Similar** button to click first. Text typed in a field or an editor (notes) + opens nothing; +* a topic's and an entity's drawer, and a resource, topic or entity selected on the graph canvas, list similar + content too. + ## Background work Imports, pipeline runs and folder scans return at once and run as jobs stored in SurrealDB. The server runs