From 1cf40220e1b7b8155e5b9013f1e859a7a8ebc018 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sun, 18 Oct 2026 07:30:20 +0000 Subject: [PATCH] feat(search): open a result's asset with the words searched for found Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QFUNzy1QZy5HhKAyJxTAYJ --- README.md | 2 +- changelog.d/search-find.md | 4 ++ docs/api.md | 4 ++ docs/requirements.md | 1 + nextjs-frontend/__tests__/search-find.test.ts | 37 +++++++++++++++++++ .../app/(app)/resources/[id]/page.tsx | 7 +++- nextjs-frontend/components/recording/model.ts | 20 ++++++++++ .../components/recording/recording-page.tsx | 28 +++++++++++++- nextjs-frontend/components/search/links.ts | 7 ++++ nextjs-frontend/components/search/query.ts | 16 ++++++++ nextjs-frontend/components/search/results.tsx | 21 ++++++++--- .../components/search/search-page.tsx | 2 +- 12 files changed, 138 insertions(+), 11 deletions(-) create mode 100644 changelog.d/search-find.md create mode 100644 nextjs-frontend/__tests__/search-find.test.ts diff --git a/README.md b/README.md index 9602f5d60..4fcb24c2f 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p [checks](docs/requirements.md#connect-your-agents)) - **Ask, and get cited answers.** A chat assistant on every page, and by voice, searches your archive by words (with "phrases", OR, and -word to leave lines out) and by meaning, once turned on, reads it, cites it and acts on it with - tools: changes wait for your approval. Ask it about Lens itself ("what's the MCP address?", "is Telegram on?") and it + tools: changes wait for your approval. A search result opens the asset with Find showing the words. Ask it about Lens itself ("what's the MCP address?", "is Telegram on?") and it answers from Lens's docs and this server, citing both. A Tools chip picks which tools an answer may use, how many calls it may make and which namespaces it reads. Once an admin turns them on, it also searches the web and reads pages, cited like the archive, and runs Python and bash in a sandbox of each conversation's own (on a Docker install, diff --git a/changelog.d/search-find.md b/changelog.d/search-find.md new file mode 100644 index 000000000..34624f447 --- /dev/null +++ b/changelog.d/search-find.md @@ -0,0 +1,4 @@ +- **A search result opens the asset with its words found.** Results link with `?q=`, the words searched for (not + filters or left-out words), so the asset opens with Find in its text showing them, on the match at the moment the + result was, or on the longest of the words when the text doesn't have them together + ([Search](docs/api.md#search)). diff --git a/docs/api.md b/docs/api.md index cab04134a..6c1518617 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1253,6 +1253,10 @@ asset (`facets`: up to 50 values each, most first, and `moments`), and lists the assets they're in, with how many of those assets each is in (`objects`); past 20,000 moments the counts cover 20,000 of them (`partial`). +In the web app, a result links to the asset with the words searched for (`/resources/?t=&q=`, +without filters, left-out words or `OR`): it opens with Find in its text showing them, on the match at that moment, +or on the longest of the words when the text doesn't have them together. + Each hit's `source` says where it was found: `said` (a line of the asset's text or transcript), `screen` (text on screen), `page` (a document's or an image's text: its `page`, from 0, and its `box` on it), `object` (a kind of object, where it's first seen: its time, or on a document's pages its `page`, with the `frame` and the `box` it's best seen in), `described` (what a shot or a page diff --git a/docs/requirements.md b/docs/requirements.md index 17a9f2bf1..3199e24d5 100644 --- a/docs/requirements.md +++ b/docs/requirements.md @@ -78,6 +78,7 @@ request, to another check that proves the same thing; loosening a requirement or | Questions about Lens itself answered from Lens's docs and this server's facts, both cited, for people and agents over MCP | [test_lens_help.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_lens_help.py) `test_the_docs_are_found_by_words`, `test_the_assistant_answers_about_lens_itself`, `test_agents_ask_lens_over_mcp`, `test_without_a_question_lens_help_still_gives_the_server` | | A chat assistant on every page | [test_chats.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_chats.py) `test_asking_from_a_page`; [page-chat.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/page-chat.test.tsx); [ask-place.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/ask-place.test.tsx) | | By voice | [assistant-mode.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/assistant-mode.test.tsx) `starts a voice chat from the mic`; [test_voice.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_voice.py) `test_what_is_said_comes_back_as_text` | +| A search result opens the asset with Find showing the words searched for, at the moment it linked to | [search-find.test.ts](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/search-find.test.ts) `carries them on the links from the results`, `finds the words together, else the longest word there, on the match at the linked moment` | | It searches by words, and by meaning once search by meaning is set up | [test_chats.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_chats.py) `test_tools_approvals_and_checking`; [test_semantic.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_semantic.py) `test_chat_retrieval_by_meaning`, `test_off_or_unconfigured` | | Word search takes "phrases", OR, and NOT or -word to leave lines out | [test_search_index.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_search_index.py) `test_not_and_minus_leave_lines_out`, `test_the_queries_parts` | | Answers cite their sources, and only what the person may read | [test_chats.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_chats.py) `test_chat_cites_only_what_you_can_read`; [chat.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/chat.test.tsx) `splits [n], [n, m] and [n][m]` | diff --git a/nextjs-frontend/__tests__/search-find.test.ts b/nextjs-frontend/__tests__/search-find.test.ts new file mode 100644 index 000000000..ff2ad4934 --- /dev/null +++ b/nextjs-frontend/__tests__/search-find.test.ts @@ -0,0 +1,37 @@ +import { hitHref } from "@/components/recording/files-model"; +import { arrivalFind, type Segment } from "@/components/recording/model"; +import { recordingHref, withFind } from "@/components/search/links"; +import { findText } from "@/components/search/query"; + +const seg = (idx: number, t0: number, text: string): Segment => ({ + idx, + t0, + t1: t0 + 1000, + speaker: null, + text, + emotion: null, + event: null, +}); + +describe("a search's words open the asset with them found", () => { + it("keeps only the words to find", () => { + expect(findText('budget speaker:"Host B" "next year" -draft NOT memo OR plan*')).toBe("budget next year plan"); + expect(findText("ns:pods -only")).toBe(""); + }); + + it("carries them on the links from the results", () => { + expect(withFind(recordingHref(4, 61_000), "budget -draft")).toBe("/resources/4?t=61&q=budget"); + expect(withFind(recordingHref(4), "café au lait")).toBe("/resources/4?q=caf%C3%A9%20au%20lait"); + const hit = { recording_id: 4, t0: null, file: null, line: null, page: 2 }; + expect(withFind(hitHref(hit), "speaker:Mara")).toBe("/resources/4?page=3"); + }); + + it("finds the words together, else the longest word there, on the match at the linked moment", () => { + const segs = [seg(0, 0, "The budget is late."), seg(1, 5000, "Next year's budget."), seg(2, 9000, "Budget again")]; + expect(arrivalFind(segs, "budget", 5000)).toEqual({ query: "budget", index: 1 }); + expect(arrivalFind(segs, "budget", null)).toEqual({ query: "budget", index: 0 }); + expect(arrivalFind(segs, "next year", null)).toEqual({ query: "next year", index: 0 }); + expect(arrivalFind(segs, "budget meeting", 9000)).toEqual({ query: "budget", index: 2 }); + expect(arrivalFind(segs, "hiring plan", null)).toBeNull(); + }); +}); diff --git a/nextjs-frontend/app/(app)/resources/[id]/page.tsx b/nextjs-frontend/app/(app)/resources/[id]/page.tsx index c4b2ebe82..09a2ca820 100644 --- a/nextjs-frontend/app/(app)/resources/[id]/page.tsx +++ b/nextjs-frontend/app/(app)/resources/[id]/page.tsx @@ -15,23 +15,26 @@ type Props = { file?: string | string[]; line?: string | string[]; page?: string | string[]; + q?: string | string[]; }>; }; /** A resource: a recording (R1–R9, VR1–VR3), a document or an image. `?t=` opens it at that moment, * `?page=` a document on its nth page, `?file=&line=` on one of its files (search links lines of files - * without times that way). /recordings/ redirects here. */ + * without times that way), `?q=` with those words found (search results link that way). /recordings/ + * redirects here. */ export default async function RecordingRoute({ params, searchParams }: Props) { const { id } = await params; const rid = Number(id); if (!Number.isInteger(rid) || rid <= 0) notFound(); - const { t, file, line, page } = await searchParams; + const { t, file, line, page, q } = await searchParams; return ( ); } diff --git a/nextjs-frontend/components/recording/model.ts b/nextjs-frontend/components/recording/model.ts index 7d2884780..85896be6e 100644 --- a/nextjs-frontend/components/recording/model.ts +++ b/nextjs-frontend/components/recording/model.ts @@ -652,6 +652,26 @@ export function findInSegments(segments: Segment[], query: string): FindHit[] { return out; } +/** What Find opens with on arriving from a search (?q=): its words together when the text has them, else the longest + * of its words the text has; on the first match at or after `startMs` (the moment the search linked to). Null when + * the text has none of them. */ +export function arrivalFind( + segments: Segment[], + q: string, + startMs: number | null, +): { query: string; index: number } | null { + const words = q.split(/\s+/).filter((w) => w.length >= 2); + const tries = [q.trim(), ...[...words].sort((a, b) => b.length - a.length)]; + for (const query of tries) { + const hits = findInSegments(segments, query); + if (!hits.length) continue; + const t0 = new Map(segments.map((s) => [s.idx, s.t0])); + const index = startMs == null ? 0 : hits.findIndex((h) => (t0.get(h.seg) ?? 0) >= startMs - 1000); + return { query, index: Math.max(0, index) }; + } + return null; +} + /** Lowercase and strip accents without changing the string's length (so offsets map back). */ export function fold(s: string): string { let out = ""; diff --git a/nextjs-frontend/components/recording/recording-page.tsx b/nextjs-frontend/components/recording/recording-page.tsx index 7200581c6..1da4315ce 100644 --- a/nextjs-frontend/components/recording/recording-page.tsx +++ b/nextjs-frontend/components/recording/recording-page.tsx @@ -14,7 +14,14 @@ import { pageAt, pageRef, parsePage } from "@/components/recording/document/mode import { usePlayer, useRecording, useRecordingJobs } from "@/components/recording/hooks"; import { pageState } from "@/components/recording/jobs"; import { MobileLayout } from "@/components/recording/mobile-layout"; -import { adjacentTurnStart, findInSegments, groupTurns, segmentAt, type EntityRef } from "@/components/recording/model"; +import { + adjacentTurnStart, + arrivalFind, + findInSegments, + groupTurns, + segmentAt, + type EntityRef, +} from "@/components/recording/model"; import type { NoteDraft } from "@/components/recording/notes-model"; import { RecordingSkeleton } from "@/components/recording/skeleton"; import { RecordingDialogs, type DialogState } from "@/components/recording/dialogs"; @@ -35,6 +42,7 @@ export function RecordingPage({ start, focus = null, page = null, + find = null, }: { id: number; start: number | null; @@ -42,6 +50,8 @@ export function RecordingPage({ focus?: { file: number; line: number | null } | null; /** A document's page to open on (?page=, from 1). */ page?: string | null; + /** Words to open with found (?q=, from a search). */ + find?: string | null; }) { const rec = useRecording(id); const jobs = useRecordingJobs(id); @@ -98,6 +108,7 @@ export function RecordingPage({ start={start} focus={focus} page={page} + find={find} rec={rec.data} model={player.data} state={state} @@ -145,6 +156,7 @@ type InnerProps = { start: number | null; focus: RecordingCtx["fileFocus"]; page: string | null; + find: string | null; rec: RecordingCtx["rec"]; model: RecordingCtx["model"]; state: RecordingCtx["state"]; @@ -153,7 +165,7 @@ type InnerProps = { speakers: RecordingCtx["speakers"]; }; -function Inner({ id, start, focus, page, rec, model, state, jobs, turns, speakers }: InnerProps) { +function Inner({ id, start, focus, page, find, rec, model, state, jobs, turns, speakers }: InnerProps) { const api = usePlayerApi(); const { hasMedia } = usePlayerState(); const { roleIn, can, admin } = useArchive(); @@ -195,6 +207,18 @@ function Inner({ id, start, focus, page, rec, model, state, jobs, turns, speaker api.seek(start * 1000, { manual: true }); }, [api, start]); + // ?q= (a search's words) opens Find with them found, on the match at or after ?t= when there is one. + const found = useRef(false); + useEffect(() => { + if (found.current || !find) return; + found.current = true; + const at = arrivalFind(model.segments, find, start == null ? null : start * 1000); + if (!at) return; + setQuery(at.query); + setHitIndex(at.index); + setFindOpen(true); + }, [find, start, model.segments]); + // #access (Home's "Needs attention" and access request emails link that way) opens the access settings. useEffect(() => { if (window.location.hash === "#access") setDialog({ kind: "access" }); diff --git a/nextjs-frontend/components/search/links.ts b/nextjs-frontend/components/search/links.ts index be9dea3fe..1cc1a0377 100644 --- a/nextjs-frontend/components/search/links.ts +++ b/nextjs-frontend/components/search/links.ts @@ -1,10 +1,17 @@ import type { RecordingSummary, SearchHit } from "@/app/openapi-client/types.gen"; +import { findText } from "@/components/search/query"; /** A link to a moment: the recording page, seeking to `ms` (as whole seconds, `?t=`). */ export function recordingHref(id: number, ms?: number | null): string { return ms && ms > 0 ? `/resources/${id}?t=${Math.floor(ms / 1000)}` : `/resources/${id}`; } +/** A link from a search to an asset, carrying what was searched for (`?q=`) so the asset opens with it found. */ +export function withFind(href: string, q?: string | null): string { + const text = findText(q ?? ""); + return text ? `${href}${href.includes("?") ? "&" : "?"}q=${encodeURIComponent(text)}` : href; +} + /** True when a recording has media to play (imported transcripts have none); undefined when unknown. */ export function hasMedia(r: Pick | undefined): boolean | undefined { if (!r) return undefined; diff --git a/nextjs-frontend/components/search/query.ts b/nextjs-frontend/components/search/query.ts index 89f6d8bbe..7faafd405 100644 --- a/nextjs-frontend/components/search/query.ts +++ b/nextjs-frontend/components/search/query.ts @@ -84,6 +84,22 @@ export function tokens(text: string): string[] { return text.match(/-?"[^"]+"|\S+/g) ?? []; } +/** The words a search looks for, as text to find in an asset (?q= on its page): without typed filters, left-out + * words (-word, NOT word), OR, quotes or a trailing *. Empty when nothing is left. */ +export function findText(input: string): string { + const ts = tokens(parseQuery(input).text); + const out: string[] = []; + for (let i = 0; i < ts.length; i++) { + const up = ts[i].toUpperCase(); + if (up === "NOT") i++; + else if (up !== "OR" && up !== "AND" && !ts[i].startsWith("-")) { + const w = ts[i].replace(/^"|"$/g, "").replace(/\*+$/, "").trim(); + if (/[\p{L}\p{N}]/u.test(w)) out.push(w); + } + } + return out.join(" "); +} + /** True when there is anything to search for (not just OR or punctuation). */ export function hasTerms(text: string): boolean { let negate = false; diff --git a/nextjs-frontend/components/search/results.tsx b/nextjs-frontend/components/search/results.tsx index 324c8cff7..b642c6dd6 100644 --- a/nextjs-frontend/components/search/results.tsx +++ b/nextjs-frontend/components/search/results.tsx @@ -8,7 +8,7 @@ import { forwardRef, type KeyboardEvent } from "react"; import type { SearchHit } from "@/app/openapi-client/types.gen"; import type { HitGroup } from "@/components/search/facets"; import { ROLE_LABEL, hitHref, type FileRole } from "@/components/recording/files-model"; -import { foundAs, onPage, recordingHref } from "@/components/search/links"; +import { foundAs, onPage, recordingHref, withFind } from "@/components/search/links"; import type { InlinePlayer } from "@/components/search/player"; import { splitSnippet } from "@/components/search/snippet"; import { speakerTone } from "@/components/speakers/format"; @@ -114,14 +114,16 @@ function HitRow({ first, player, audio, + q, }: { hit: SearchHit; first: boolean; player: InlinePlayer; audio: boolean | undefined; + q?: string; }) { const router = useRouter(); - const href = hitHref(hit); + const href = withFind(hitHref(hit), q); const paged = onPage(hit); const untimed = hit.t0 == null || paged; const at = paged ? `Page ${(hit.page ?? 0) + 1}` : hit.t0 == null ? `Line ${(hit.line ?? 0) + 1}` : tc(hit.t0); @@ -206,9 +208,11 @@ export const ResultGroups = forwardRef< groups: HitGroup[]; player: InlinePlayer; audioOf: (recordingId: number) => boolean | undefined; + /** What was searched for: each link opens the asset with it found. */ + q?: string; className?: string; } ->(function ResultGroups({ groups, player, audioOf, className }, ref) { +>(function ResultGroups({ groups, player, audioOf, q, className }, ref) { let first = true; return (
@@ -221,7 +225,7 @@ export const ResultGroups = forwardRef< >
{g.title} @@ -235,7 +239,14 @@ export const ResultGroups = forwardRef<
{g.hits.map((h) => { const row = ( - + ); first = false; return row; diff --git a/nextjs-frontend/components/search/search-page.tsx b/nextjs-frontend/components/search/search-page.tsx index 24b132de1..9ca5e37cc 100644 --- a/nextjs-frontend/components/search/search-page.tsx +++ b/nextjs-frontend/components/search/search-page.tsx @@ -459,7 +459,7 @@ export function SearchPage() { /> )} {groups.length > 0 && ( - + )} {results.hasNextPage && (