From abbd651167b685b3a41566c20e48c70e981008cc Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Mon, 19 Oct 2026 14:48:46 +0000 Subject: [PATCH] feat(search): filter by date, tag, origin and language GET /search takes from/to, tag, origin and language (the Library's filters, via library.where) and keeps every kind of hit to the assets that have them; the MCP search tool takes date_from/date_to (day, month or year), tags, origins and languages. The search page gets compact Assets controls at the top of the filter panel, empty until picked, typed filters in the box, chips, the URL and saved searches carry them. A recording-set restriction (such as the object filter) now applies to screen, file, object and description hits too, not only to what was said. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QFUNzy1QZy5HhKAyJxTAYJ --- README.md | 3 +- changelog.d/search-filters.md | 8 + docs/api.md | 17 +- docs/mcp.md | 2 +- docs/requirements.md | 1 + fastapi_backend/app/api/mcp_tools.py | 23 +- fastapi_backend/app/api/v1/routes/search.py | 39 ++- fastapi_backend/app/api/v1/routes/searches.py | 8 +- fastapi_backend/app/domain/search.py | 10 +- fastapi_backend/app/schemas/views.py | 14 +- fastapi_backend/tests/api/test_search.py | 66 +++++ .../__tests__/search-asset-filters.test.tsx | 139 +++++++++++ nextjs-frontend/__tests__/search.test.ts | 19 ++ nextjs-frontend/app/openapi-client/sdk.gen.ts | 7 +- .../app/openapi-client/types.gen.ts | 88 +++++++ .../components/search/asset-filters.tsx | 131 ++++++++++ .../components/search/no-results.tsx | 40 ++- nextjs-frontend/components/search/query.ts | 81 ++++++- .../components/search/save-search.tsx | 37 ++- .../components/search/search-page.tsx | 106 ++++++-- nextjs-frontend/openapi.json | 229 +++++++++++++++++- 21 files changed, 1009 insertions(+), 59 deletions(-) create mode 100644 changelog.d/search-filters.md create mode 100644 nextjs-frontend/__tests__/search-asset-filters.test.tsx create mode 100644 nextjs-frontend/components/search/asset-filters.tsx diff --git a/README.md b/README.md index 2ef73bc4..15b5fdd7 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,8 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p rules, without a model, and a decision model types the ones the rules can't. Find what is most like a passage, an asset, a topic or an entity, in a Similar tab on every asset or by selecting text anywhere, and, with anytopdf, photos by what they show. Search filters moments by person, emotion or the sound heard in them (laughter, applause, - music), with a count for each. Pages and shots are described in words by the model provider's vision model or, off by + music), with a count for each, and keeps to the assets recorded between two days, with a tag, from a source or in a + language, in the link and for agents too. Pages and shots are described in words by the model provider's vision model or, off by default, by Florence-2 or Moondream on your own server. A recording's sound can be cleaned up before it's transcribed (noise, volume, echo, and silence left out, with the transcript's times kept), and transcripts cleaned up before analysis (stutters, "um"s, spacing and, with a local model, punctuation put back, and grammar by a LanguageTool server diff --git a/changelog.d/search-filters.md b/changelog.d/search-filters.md new file mode 100644 index 00000000..910052aa --- /dev/null +++ b/changelog.d/search-filters.md @@ -0,0 +1,8 @@ +- **Search by when, tag, source and language.** `GET /api/v1/search` takes `from` and `to` (days: when the asset was + recorded, or added when its file doesn't say), `tag`, `origin` and `language`, the Library's filters of the same + names, and keeps every kind of hit to the assets that have them; the MCP `search` tool takes `date_from` and + `date_to` (a day, a month or a year), `tags`, `origins` and `languages`. On the search page they're compact controls + at the top of the filter panel, empty until picked, typed in the box too (`tag:interview`, `from:2026-01-01`), shown + as chips, kept in the link and in saved searches. A search's object filter now also keeps text on screen, file lines + and descriptions to the assets the object is seen in, not only what was said + ([search](docs/api.md#search)). diff --git a/docs/api.md b/docs/api.md index 4ce54d24..ec0d80a4 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1351,7 +1351,12 @@ namespaces you can read, best first: every word (English stemming), "quoted phrases" as written, `OR` between alternatives, `NOT` or `-` before a word or phrase to leave out the lines with it; `ns`, `speaker`, `emotion`, `event` (a sound heard, such as laughter, applause or music, typed any way; music is stored as `BGM`) and `recording` narrow it, and `object` keeps to the assets a kind of object is seen in -([Objects](#objects)); +([Objects](#objects)); `from` and `to` (days, both included: when the asset was recorded, or added when its file +doesn't say), `tag`, `origin` (`upload`, `paste`, `iiif`, `folder`, `file` or `source:`, as `GET +/recordings/origins` lists them) and `language` (a code, or `none` for not known) keep to the assets that have them, +exactly as the Library's filters of the same names do, on every kind of hit (said, on screen, in files …); repeat +`tag`, `origin` or `language` for several (any of them). None of them is set unless asked for, so a search without +them is as it was; `limit`/`offset` page through it. `total` counts the moments ranked so far (`capped` when there may be more). With `facets=true` it also counts all the matching moments, whatever the page, by namespace, person, emotion, sound (`events`; plain speech isn't one) and asset (`facets`: up to 50 values each, most first, and `moments`), and lists the kinds of object seen in the @@ -1360,7 +1365,11 @@ assets they're in, with how many of those assets each is in (`objects`); past 20 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. +or on the longest of the words when the text doesn't have them together. The search page's filter panel starts with +Assets: From and To days, and a tag, a source and a language to pick (a list is left out when there's nothing to +choose between). Each is empty until picked, one pick searches again, and each can be typed in the box too +(`from:2026-01-01`, `to:2026-03-31`, `tag:interview`, `origin:upload` or a source's name, `language:de` or +`language:German`). They're kept in the address, so a link carries them, and in a saved search. 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 @@ -1942,7 +1951,9 @@ DELETE /api/v1/searches/{sid} ``` Saved searches: `POST` with a `name` (unique among yours), the search's `q`, and any of `namespace`, `speaker`, -`emotion`, `event` and `recording`, as `GET /search` takes them. They follow the rules of saved views (above): yours, or +`emotion`, `event`, `recording`, `object`, `date_from` and `date_to` (`YYYY-MM-DD`), `tag`, `origin` and `language`, +as `GET /search` takes them (one value each; `date_from` and `date_to` are its `from` and `to`). They follow the rules +of saved views (above): yours, or `shared` with the namespace they search (editors there; a search of every namespace can't be shared); only their maker renames or shares them (`PATCH` with `name` or `shared`); their maker, or an owner of the namespace for a shared one, deletes them. `GET` lists yours, then the shared ones, with `speaker_name` and `recording_title` where you can read diff --git a/docs/mcp.md b/docs/mcp.md index 03ee8900..374f861d 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -70,7 +70,7 @@ For an app that speaks OpenAI's API rather than MCP, the namespace can also answ | Tool | | |---|---| | `lens_help` | Lens itself rather than the archive: the sections of Lens's docs that match a question (or one page read whole), each with its link, and this server's own facts: version, addresses (web app, MCP, API), what is turned on and your access ([Questions about Lens itself](assistant.md#questions-about-lens-itself)) | -| `search` | moments where words appear (written, spoken or shown on screen), best first: the asset, person, time, the matching text (matches in **bold**) and a link to that moment. Words, `"phrases"`, `OR`; by namespace, person, asset or sound heard (laughter, applause, music …) | +| `search` | moments where words appear (written, spoken or shown on screen), best first: the asset, person, time, the matching text (matches in **bold**) and a link to that moment. Words, `"phrases"`, `OR`; by namespace, person, asset or sound heard (laughter, applause, music …), and by when the assets were recorded (a day, month or year), their tags, where they came from and their language | | `find_similar` | the assets (recordings, documents and images) most like a text, an asset, a topic or an entity: why each is alike (meaning, key words, topics, entities in common) and the moments that show it, with links | | `find` | assets by any attribute: faces seen, people heard, entities, topics, kind, content type, dates (a day, month or year), tags, language, objects, emotions, length, collection and words in them; ids or names. Each says why it matched, with moments and links to cite ([Finding by any attribute](processing.md#finding-by-any-attribute)) | | `classify` | which of some labels fits an asset or a text, how sure, and every label ranked: labels you give, or Lens's content types or the namespace's topics; the decision model answers, else the language model. Read only: it changes nothing ([Classifying](processing.md#classifying)) | diff --git a/docs/requirements.md b/docs/requirements.md index de011c29..d1ff2776 100644 --- a/docs/requirements.md +++ b/docs/requirements.md @@ -225,6 +225,7 @@ request, to another check that proves the same thing; loosening a requirement or | A decision model (Jev or Laya) types the names the rules leave as Terms, in one call, keeping only types it is sure of; Settings › Analysis can leave them Terms | [test_name_types.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_name_types.py) `test_the_decision_model_types_what_the_rules_cant`, `test_a_type_the_model_isnt_sure_of_stays_a_term`, `test_by_default_the_language_model_isnt_asked`, `test_with_typing_rules_nothing_is_asked` | | What is most like a passage, an asset, a topic or an entity | [test_similar.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_similar.py) `test_like_a_text_by_its_key_words`, `test_like_a_resource_by_words_topics_and_entities`, `test_like_a_topic_and_an_entity`; [similar.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/similar.test.tsx) | | Search finds and filters moments by the sound heard in them (laughter, applause, music), typed any way, and counts each | [test_search.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_search.py) `test_sound_events_filter_and_count`; [test_semantic.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_semantic.py) `test_search_by_meaning_keeps_to_a_sound`; [search.test.ts](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/search.test.ts) `reads sound events typed any way` | +| Search keeps to the assets recorded between two days, with a tag, from a source or in a language, by the API, MCP and the search page (empty until picked, kept in the link and in saved searches) | [test_search.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_search.py) `test_dates_tags_origins_and_languages_narrow_search`; [search-asset-filters.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/search-asset-filters.test.tsx) `starts with none set, so the search is as it was, and picking one puts it in the address`, `asks with the ones in the address, shows each as a chip, and says which to clear when nothing matches`; [search.test.ts](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/search.test.ts) `reads dates, tags, sources and languages, and keeps them in the address` | | With anytopdf, photos found by what they show | [test_photos.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_photos.py) `test_photos_found_by_what_they_show`, `test_off_by_default` | | Pages and shots described by the provider's vision model or, off by default, by Florence-2 on this server, fetched pinned and checked when picked | [test_florence.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_florence.py) `test_florence_describes_pictures_without_a_model_provider`, `test_florence_is_fetched_pinned_and_checked`, `test_admins_and_agents_choose_what_describes_pictures`, `test_a_picture_is_read_as_the_model_was_trained` | | Or by Moondream on this server, off by default, fetched at a pinned revision only where the chosen model can run, and saying why where it can't | [test_moondream.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_moondream.py) `test_moondream_describes_pictures_without_a_model_provider`, `test_where_each_moondream_can_run`, `test_on_a_cpu_moondream_3_is_never_fetched_and_the_step_says_why`, `test_moondream_is_fetched_pinned`, `test_admins_and_agents_choose_moondream` | diff --git a/fastapi_backend/app/api/mcp_tools.py b/fastapi_backend/app/api/mcp_tools.py index 60ef186e..9d34e639 100644 --- a/fastapi_backend/app/api/mcp_tools.py +++ b/fastapi_backend/app/api/mcp_tools.py @@ -464,12 +464,23 @@ def _recording(ctx: Context, rid: int) -> dict[str, Any]: "pipelines' model steps wrote about assets (found: generated, with the step in output). Words are all required " '(matched after stemming), "quoted phrases" must appear as written, OR separates alternatives, NOT or a leading - leaves out lines with a word or phrase. Results are the best ' "moments first, each with the asset, the person (who said or wrote it), where in it, the matching text (matches in **bold**) " - "and a url that opens the asset at that moment. Use get_transcript for the lines around a moment.", + "and a url that opens the asset at that moment. A date range, tags, origins or languages keep to the assets that have " + "them. Use get_transcript for the lines around a moment.", Arg("query", "string", 'what to look for: words, "a phrase", or alternatives separated by OR', required=True, max_length=500), Arg("namespace", "string", "only this namespace (see list_namespaces)"), Arg("speaker_id", "integer", "only lines from this person (ids from list_speakers or get_recording)"), Arg("recording_id", "integer", "only within this asset"), Arg("event", "string", "only lines with this sound: laughter, applause, music, cough …", max_length=40), + Arg("date_from", "string", "only assets recorded (or added) on or after this day, month or year (2026-09-30, 2026-09, 2026)"), + Arg("date_to", "string", "only assets recorded (or added) on or before this day, month or year"), + Arg("tags", "string[]", "only assets with any of these tags (ignoring case)"), + Arg( + "origins", + "string[]", + "only assets that came from here: source: (a connected source), upload, paste, iiif, folder (the archive's own " + "folders) or file", + ), + Arg("languages", "string[]", "only assets in these languages (en, de …); none for those whose language isn't known"), Arg("limit", "integer", "how many moments", default=10, minimum=1, maximum=50), Arg("offset", "integer", "skip this many (for the next page)", default=0, minimum=0, maximum=10000), ) @@ -480,6 +491,11 @@ def search( speaker_id: int | None, recording_id: int | None, event: str | None, + date_from: str | None, + date_to: str | None, + tags: list[str] | None, + origins: list[str] | None, + languages: list[str] | None, limit: int, offset: int, ) -> dict[str, Any]: @@ -497,6 +513,11 @@ def search( event=event, recording=recording_id, object=None, + date_from=findmod.day(date_from, name="date_from"), + date_to=findmod.day(date_to, end=True, name="date_to"), + tag=tags or None, + origin=origins or None, + language=languages or None, limit=limit, offset=offset, facets=False, diff --git a/fastapi_backend/app/api/v1/routes/search.py b/fastapi_backend/app/api/v1/routes/search.py index 85af1bb2..6fc4793e 100644 --- a/fastapi_backend/app/api/v1/routes/search.py +++ b/fastapi_backend/app/api/v1/routes/search.py @@ -2,14 +2,15 @@ from __future__ import annotations +from datetime import date from typing import Literal from fastapi import APIRouter, HTTPException, Query, Request -from app.api.deps import Acl, Cfg, CurrentUser, Db +from app.api.deps import Acl, Cfg, CurrentUser, Db, domain_errors from app.api.media import sign_urls from app.domain import graph as graphmod -from app.domain import graph_history, hierarchy, nsfw, photos, render, similar, store +from app.domain import graph_history, hierarchy, library, nsfw, photos, render, similar, store from app.domain import search as searchmod from app.schemas.search import Graph, Mention, PhotoResults, SearchResults, SimilarResults, TermSuggestion @@ -31,6 +32,19 @@ def search_transcripts( event: str | None = Query(None, max_length=40, description="only lines with this sound (laughter, applause, music …), ignoring case"), recording: int | None = None, object: str | None = Query(None, max_length=60, description="only assets this kind of object is seen in (person, car …)"), + date_from: date | None = Query( + None, alias="from", description="only assets recorded on or after this day (added, when the file doesn't say when)" + ), + date_to: date | None = Query(None, alias="to", description="only assets recorded on or before this day"), + tag: list[str] | None = Query(None, description="only assets with these tags (ignoring case); repeat for several (any of them)"), + origin: list[str] | None = Query( + None, + description="only assets that came from here: source: (a connected source), upload, paste, iiif, folder (the " + "archive's own folders) or file (GET /recordings/origins); repeat for several", + ), + language: list[str] | None = Query( + None, description="only assets in these languages (codes, ignoring case; none: not known); repeat for several" + ), limit: int = Query(50, ge=1, le=200), offset: int = Query(0, ge=0), facets: bool = Query( @@ -48,10 +62,28 @@ def search_transcripts( a resource's supplementary transcripts, captions, translations and indexes, the kinds of object seen in videos, documents and images, what a model that can see said they show, or what a pipeline's model step wrote about them), best first, in the namespaces you can read and the collections you were given a role on. A person, emotion or - sound filter keeps to their lines. By meaning, a moment is a passage about what was asked, though its words may differ.""" + sound filter keeps to their lines. A date, tag, origin or language keeps to the assets that have it, as the Library + filters them. By meaning, a moment is a passage about what was asked, though its words may differ.""" if ns: acl.scope(ns) # 404 unless they see some of it also = acl.partial_recordings() + kept = None + if date_from or date_to or tag or origin or language: + spaces, within = acl.scope(ns) + with domain_errors(): + cond, p = library.where( + db, + spaces, + within=within, + cfg=cfg, + sensitive="include", # search blurs what's flagged rather than leaving it out + date_from=date_from, + date_to=date_to, + tags=tag, + origins=origin, + languages=language, + ) + kept = set(db.values(f"SELECT VALUE record::id(id) FROM recording WHERE {cond}", **p)) res = searchmod.search( db, q, @@ -71,6 +103,7 @@ def search_transcripts( cfg=cfg, mode=mode, event=event, + recordings=kept, ) blur = nsfw.blurred(db, cfg, [h.get("recording_id") for h in res.get("hits") or []]) for h in res.get("hits") or []: diff --git a/fastapi_backend/app/api/v1/routes/searches.py b/fastapi_backend/app/api/v1/routes/searches.py index 57b69fd5..5cccf55a 100644 --- a/fastapi_backend/app/api/v1/routes/searches.py +++ b/fastapi_backend/app/api/v1/routes/searches.py @@ -14,7 +14,7 @@ from app.domain import auth, store, views from app.domain.store import DB from app.schemas.common import Ok -from app.schemas.views import SavedSearch, SearchCreate, SearchUpdate +from app.schemas.views import SavedSearch, SearchCreate, SearchState, SearchUpdate router = APIRouter(prefix="/searches", tags=["searches"]) @@ -67,14 +67,14 @@ def list_searches(user: CurrentUser, acl: Acl, db: Db) -> list[SavedSearch]: @router.post("") def create_search(body: SearchCreate, user: Writer, acl: Acl, db: Db) -> SavedSearch: - """Save a search (its words, namespace, person, emotion, sound, asset and object) under a name unique among yours. Sharing it - with its namespace needs editor access there.""" + """Save a search (its words, namespace, person, emotion, sound, asset, object, dates, tag, origin and language) under a + name unique among yours. Sharing it with its namespace needs editor access there.""" sid = acl.namespace(body.namespace) if body.namespace else None if body.recording is not None: acl.recording(body.recording) if body.shared: check_share(acl, sid, "saved search") - state = body.model_dump(include={"q", "speaker", "emotion", "event", "recording", "object"}) + state = body.model_dump(include=set(SearchState.model_fields)) with domain_errors(): vid = views.create(db, user.id, body.name, sid, state, body.shared, kind="search") if body.shared: diff --git a/fastapi_backend/app/domain/search.py b/fastapi_backend/app/domain/search.py index 1e0ccb36..f1a1cf8d 100644 --- a/fastapi_backend/app/domain/search.py +++ b/fastapi_backend/app/domain/search.py @@ -115,8 +115,8 @@ def search( transcripts, captions, translations and indexes; with objects, the kinds of object seen in videos, documents and images; with described, what a model that can see said their pages and shots show, and what a pipeline's model step wrote about them) matching q. spaces limits the search to namespaces someone may read, and `also` adds recordings they may - read beyond those (in collections they were given a role on); recordings limits it to a set of recordings (such as - the transcripts a visitor may read), and obj to those a kind of object is seen in. event keeps to lines with that + read beyond those (in collections they were given a role on); recordings limits it, on every layer, to a set of recordings + (such as the transcripts a visitor may read, or those a date, tag, origin or language keeps), and obj to those a kind of object is seen in. event keeps to lines with that sound (Laughter, Applause, BGM …). With facets, also how many of all the matching moments (up to FACET_CAP) are in each namespace, speaker, emotion, sound and recording, and which kinds of object the recordings they're in have. @@ -340,8 +340,6 @@ def _meaning(db, cfg, q, groups, kinds, need, base, ns, spaces, recording, speak Passages don't record their sounds, so a sound is matched on their lines.""" params = {k: v for k, v in base.items() if k in ("sp", "allowed", "also", "rec", "recs", "spk", "emo")} where = space_filter(ns, spaces, recording, params) - if "recs" in params: - where += " AND recording IN $recs" if "spk" in params: where += " AND $spk IN speakers" if "emo" in params: @@ -590,6 +588,8 @@ def space_filter(ns, spaces, recording, params): parts.append("(space IN $allowed OR recording IN $also)" if "also" in params else "space IN $allowed") if recording not in (None, ""): parts.append("recording = $rec") + if "recs" in params: # a set of recordings (a kind of object seen, a date, tag, origin or language), on every layer + parts.append("recording IN $recs") return (" AND " + " AND ".join(parts)) if parts else "" @@ -620,7 +620,7 @@ def _objects(db, groups, where_f, cap, base): def _layer(db, groups, where_f, cap, base, table, extra, source): """Hits in another table of text with times (`extra` names the fields it adds), marked as from `source`.""" - params = {k: v for k, v in base.items() if k in ("m0", "m1", "sp", "allowed", "also", "rec") or k.startswith("q")} + params = {k: v for k, v in base.items() if k in ("m0", "m1", "sp", "allowed", "also", "rec", "recs") or k.startswith("q")} fields = f"record::id(id) AS id, recording, t0, t1, space, text, {extra}" rows = textindex.rows(db, table, groups, fields, where_f, params, cap, (M0, M1)) if rows is None and db.ready_fulltext(): diff --git a/fastapi_backend/app/schemas/views.py b/fastapi_backend/app/schemas/views.py index 2c802dbf..3ebbb17c 100644 --- a/fastapi_backend/app/schemas/views.py +++ b/fastapi_backend/app/schemas/views.py @@ -11,6 +11,8 @@ LibraryTab = Literal["all", "attention", "processing", "mine"] Tag = Annotated[str, Field(min_length=1, max_length=40)] +DAY = r"^\d{4}-\d{2}-\d{2}$" +ORIGIN = r"^(upload|paste|iiif|folder|file|source:\d+)$" class ViewState(RequestModel): @@ -26,7 +28,7 @@ class ViewState(RequestModel): ) media: Literal["any", "audio", "video", "transcript", "document", "image"] = "any" tags: list[Tag] = Field(default_factory=list, max_length=20, description="any of these") - origins: list[Annotated[str, Field(pattern=r"^(upload|paste|iiif|folder|file|source:\d+)$")]] = Field( + origins: list[Annotated[str, Field(pattern=ORIGIN)]] = Field( default_factory=list, max_length=20, description="where they came from (GET /recordings `origin`)" ) languages: list[Annotated[str, Field(min_length=1, max_length=20)]] = Field( @@ -78,6 +80,11 @@ class SearchState(RequestModel): event: str | None = Field(None, max_length=40, description="a sound heard (laughter, applause, music …)") recording: int | None = None object: str | None = Field(None, max_length=60, description="a kind of object the assets have (person, car …)") + date_from: str | None = Field(None, pattern=DAY, description="assets recorded on or after this day (/search `from`)") + date_to: str | None = Field(None, pattern=DAY, description="assets recorded on or before this day (/search `to`)") + tag: str | None = Field(None, max_length=100, description="assets with this tag") + origin: str | None = Field(None, pattern=ORIGIN, description="assets that came from here (upload, folder, source: …)") + language: str | None = Field(None, max_length=20, description="assets in this language (a code; none: not known)") class SavedSearch(ResponseModel): @@ -93,6 +100,11 @@ class SavedSearch(ResponseModel): recording: int | None = None recording_title: str | None = Field(None, description="the asset's title, when you can read it") object: str | None = Field(None, description="a kind of object the assets have") + date_from: str | None = Field(None, description="assets recorded on or after this day") + date_to: str | None = Field(None, description="assets recorded on or before this day") + tag: str | None = None + origin: str | None = Field(None, description="where the assets came from (upload, folder, source: …)") + language: str | None = Field(None, description="the assets' language") created_by: str | None = Field(None, description="its maker's email") created_at: str | None = None updated_at: str | None = None diff --git a/fastapi_backend/tests/api/test_search.py b/fastapi_backend/tests/api/test_search.py index 881cb535..4dcfefe6 100644 --- a/fastapi_backend/tests/api/test_search.py +++ b/fastapi_backend/tests/api/test_search.py @@ -148,3 +148,69 @@ def test_word_suggestions_for_a_prefix(client, db, cfg, folder): assert client.get("/api/v1/search/terms", params={"prefix": "c"}, headers=hv).status_code == 422 assert client.get("/api/v1/search/terms", params={"prefix": "caps", "ns": "calls"}, headers=hv).status_code == 404 assert client.get("/api/v1/search/terms", params={"prefix": "caps"}).status_code == 401 + + +def test_dates_tags_origins_and_languages_narrow_search(client, db, cfg, folder): + import pathlib + + from app.domain import library + from tests.api.test_mcp import _bearer, _grant, tool + + ep1, ep2, call = seed(db, cfg, folder) + R = store.R + for rid, day in ((ep1, "2026-09-30T09:00:00"), (ep2, "2026-03-02T10:00:00"), (call, "2025-11-20T08:00:00")): + db.q("UPDATE $r SET recorded_at = $d", r=R("recording", rid), d=day) + db.q("UPDATE $r SET language = 'en', path = $p", r=R("recording", ep1), p=str(pathlib.Path(cfg["data_dir"]) / "uploads" / "ep1.txt")) + db.q("UPDATE $r SET language = 'de'", r=R("recording", call)) + library.set_tags(db, call, ["Interview"]) + # on screen in ep2, so the filters must keep to their assets on every layer, not only in what's said + db.q( + "CREATE ocr_span CONTENT {recording: $r, space: $s, t0: 1000, t1: 2000, text: 'CAPSID ON SCREEN'}", r=ep2, s=store.ns_id(db, "pods") + ) + make_user(db, "root@x.io", "root password 1", admin=True) + make_user(db, "vi@x.io", "viewer password 1", roles={"pods": "viewer"}) + hr, hv = login(client, "root@x.io", "root password 1"), login(client, "vi@x.io", "viewer password 1") + + def assets(h=hr, **p): + r = client.get("/api/v1/search", params={"q": "capsid", **p}, headers=h) + assert r.status_code == 200, r.text + return {x["recording_id"] for x in r.json()["hits"]} + + assert assets() == {ep1, ep2, call} # nothing set changes nothing + assert assets(**{"from": "2026-01-01"}) == {ep1, ep2} + assert assets(**{"from": "2026-03-01", "to": "2026-03-02"}) == {ep2} # both days included + assert assets(to="2025-12-31") == {call} + assert assets(tag="interview") == {call} # ignoring case + assert assets(language="DE") == {call} + assert assets(language=["en", "de"]) == {ep1, call} # any of them + assert assets(origin="upload") == {ep1} + assert assets(origin="upload", language="de") == set() # filters combine with AND + assert assets(hv, tag="interview") == set() # still only what you can read + assert client.get("/api/v1/search", params={"q": "capsid", "origin": "nowhere"}, headers=hr).status_code == 400 + assert client.get("/api/v1/search", params={"q": "capsid", "from": "soon"}, headers=hr).status_code == 422 + with_facets = client.get("/api/v1/search", params={"q": "capsid", "tag": "interview", "facets": True}, headers=hr).json() + assert [x["id"] for x in with_facets["facets"]["recordings"]] == [call] + + # an agent narrows the same way, a day, a month or a year + _, tokens = _grant(client, hv, scope="read") + found = lambda **a: {r["recording_id"] for r in tool(client, _bearer(tokens), "search", query="capsid", **a)["results"]} # noqa: E731 + assert found() == {ep1, ep2} + assert found(date_from="2026-09") == {ep1} + assert found(date_to="2026-03") == {ep2} + assert found(languages=["en"], origins=["upload"]) == {ep1} + assert found(tags=["interview"]) == set() + + # a saved search keeps them + saved = client.post( + "/api/v1/searches", + headers=hv, + json={"name": "Spring", "q": "capsid", "date_from": "2026-03-01", "date_to": "2026-03-31", "tag": "x", "language": "en"}, + ).json() + assert (saved["date_from"], saved["date_to"], saved["tag"], saved["language"], saved["origin"]) == ( + "2026-03-01", + "2026-03-31", + "x", + "en", + None, + ) + assert client.post("/api/v1/searches", headers=hv, json={"name": "Bad", "q": "capsid", "date_from": "March"}).status_code == 422 diff --git a/nextjs-frontend/__tests__/search-asset-filters.test.tsx b/nextjs-frontend/__tests__/search-asset-filters.test.tsx new file mode 100644 index 00000000..ffb0a69a --- /dev/null +++ b/nextjs-frontend/__tests__/search-asset-filters.test.tsx @@ -0,0 +1,139 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; +import type { ReactNode } from "react"; + +import { Resources, Search } from "@/app/openapi-client"; +import type { SearchResults } from "@/app/openapi-client/types.gen"; +import { SearchPage } from "@/components/search/search-page"; +import { ToastProvider } from "@/components/ui/toast"; +import { TooltipProvider } from "@/components/ui/tooltip"; + +jest.mock("@/app/openapi-client", () => ({ + Search: { searchTranscripts: jest.fn(), searchPhotos: jest.fn(), suggestTerms: jest.fn() }, + Searches: { listSearches: jest.fn(), deleteSearch: jest.fn(), createSearch: jest.fn() }, + Speakers: { listSpeakers: jest.fn() }, + Resources: { listRecordings: jest.fn(), listTags: jest.fn(), listOrigins: jest.fn(), listLanguages: jest.fn() }, + Sources: {}, + Collections: {}, +})); +let mockSearch = ""; +const mockPush = jest.fn(); +jest.mock("next/navigation", () => ({ + useRouter: () => ({ push: mockPush, replace: jest.fn() }), + usePathname: () => "/search", + useSearchParams: () => new URLSearchParams(mockSearch), +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +jest.mock("@/lib/hooks/session", () => ({ + ...jest.requireActual("@/lib/hooks/session"), + useArchive: () => ({ namespaces: [], can: () => true }), +})); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const m = (f: unknown) => f as jest.Mock; +const NONE: SearchResults = { + q: "capsid", + query: "capsid", + total: 0, + capped: false, + hits: [], + mode: "keyword", + meaning: null, + semantic: false, +}; + +function wrap(ui: ReactNode) { + const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + return render( + + + {ui} + + , + ); +} + +beforeEach(() => { + jest.clearAllMocks(); + m(Search.searchTranscripts).mockImplementation(() => ok(NONE)); + m(Search.searchPhotos).mockImplementation(() => ok({ hits: [] })); + m(Resources.listRecordings).mockImplementation(() => ok([])); + m(Resources.listTags).mockImplementation(() => ok([{ tag: "interview", recordings: 3 }])); + m(Resources.listOrigins).mockImplementation(() => + ok([ + { origin: "upload", name: "Uploads", recordings: 4 }, + { origin: "source:2", name: "Team drive", recordings: 2 }, + ]), + ); + m(Resources.listLanguages).mockImplementation(() => + ok([ + { language: "en", recordings: 5 }, + { language: "de", recordings: 1 }, + ]), + ); + const { Searches } = jest.requireMock("@/app/openapi-client"); + m(Searches.listSearches).mockImplementation(() => ok([])); +}); + +const asked = () => m(Search.searchTranscripts).mock.calls.map(([o]) => o.query); + +describe("search by date, tag, source and language", () => { + it("starts with none set, so the search is as it was, and picking one puts it in the address", async () => { + mockSearch = "q=capsid"; + wrap(); + await waitFor(() => expect(asked().length).toBeGreaterThan(0)); + expect(asked()[0]).toMatchObject({ + q: "capsid", + from: undefined, + to: undefined, + tag: undefined, + origin: undefined, + }); + expect(asked()[0].language).toBeUndefined(); + expect(screen.getByLabelText("From")).toHaveValue(""); + expect(screen.getByLabelText("To")).toHaveValue(""); + expect(await screen.findByRole("option", { name: "interview (3)" })).toBeInTheDocument(); + expect(screen.getByLabelText("Tag")).toHaveValue(""); + + // one pick each: the list searches again at once, a date once it's a whole day + fireEvent.change(screen.getByLabelText("Tag"), { target: { value: "interview" } }); + expect(mockPush).toHaveBeenLastCalledWith("/search?q=capsid&tag=interview"); + fireEvent.change(screen.getByLabelText("From"), { target: { value: "2026-01-01" } }); + expect(mockPush).toHaveBeenLastCalledWith("/search?q=capsid&from=2026-01-01"); + await screen.findByRole("option", { name: "Team drive (2)" }); + fireEvent.change(screen.getByLabelText("Source"), { target: { value: "source:2" } }); + expect(mockPush).toHaveBeenLastCalledWith("/search?q=capsid&origin=source%3A2"); + fireEvent.change(screen.getByLabelText("Language"), { target: { value: "de" } }); + expect(mockPush).toHaveBeenLastCalledWith("/search?q=capsid&language=de"); + }); + + it("asks with the ones in the address, shows each as a chip, and says which to clear when nothing matches", async () => { + mockSearch = "q=capsid&from=2026-01-01&to=2026-03-31&tag=interview&origin=upload&language=de"; + wrap(); + await waitFor(() => expect(asked().some((q) => q.tag)).toBe(true)); + expect(asked().find((q) => q.tag)).toMatchObject({ + q: "capsid", + from: "2026-01-01", + to: "2026-03-31", + tag: ["interview"], + origin: ["upload"], + language: ["de"], + facets: false, + }); + // the facets and "without filters" count come from the words alone + expect(asked().some((q) => q.facets && !q.tag && !q.from)).toBe(true); + expect(screen.getByLabelText("From")).toHaveValue("2026-01-01"); + expect((await screen.findAllByText("date: 2026-01-01 – 2026-03-31")).length).toBeGreaterThan(0); + expect((await screen.findAllByText("source: Uploads")).length).toBeGreaterThan(0); + expect(screen.getAllByText("language: German").length).toBeGreaterThan(0); + expect(screen.getAllByText("tag: interview").length).toBeGreaterThan(0); + + fireEvent.click(await screen.findByRole("button", { name: "Any time" })); + expect(mockPush).toHaveBeenLastCalledWith("/search?q=capsid&tag=interview&origin=upload&language=de"); + fireEvent.click(screen.getByRole("button", { name: "Any tag" })); + expect(mockPush).toHaveBeenLastCalledWith( + "/search?q=capsid&from=2026-01-01&to=2026-03-31&origin=upload&language=de", + ); + }); +}); diff --git a/nextjs-frontend/__tests__/search.test.ts b/nextjs-frontend/__tests__/search.test.ts index cfa3319a..efd103ed 100644 --- a/nextjs-frontend/__tests__/search.test.ts +++ b/nextjs-frontend/__tests__/search.test.ts @@ -57,6 +57,25 @@ describe("parseQuery", () => { expect(savedSearchHref({ q: "capsid", event: "Laughter" })).toBe("/search?q=capsid&event=Laughter"); }); + it("reads dates, tags, sources and languages, and keeps them in the address", () => { + expect(parseQuery("tag:interview lang:de from:2026-01-01 to:2026-03-31 origin:upload capsid")).toEqual({ + text: "capsid", + typed: { tag: "interview", language: "de", from: "2026-01-01", to: "2026-03-31", origin: "upload" }, + }); + expect(parseQuery("mail from:alice").text).toBe("mail from:alice"); // not a day: words + const f = { from: "2026-01-01", to: "2026-03-31", tag: "interview", origin: "source:2", language: "de" }; + const qs = toParams("capsid", f); + expect(qs).toBe("q=capsid&from=2026-01-01&to=2026-03-31&tag=interview&origin=source%3A2&language=de"); + expect(fromParams(new URLSearchParams(qs)).filters).toMatchObject(f); + expect(fromParams(new URLSearchParams("q=capsid&from=soon")).filters.from).toBeUndefined(); + expect(activeFilterCount(f)).toBe(5); + expect(activeFilterCount({})).toBe(0); + const saved = { q: "capsid", date_from: "2026-01-01", date_to: "2026-03-31", tag: "interview", language: "de" }; + expect(savedSearchHref(saved)).toBe("/search?q=capsid&from=2026-01-01&to=2026-03-31&tag=interview&language=de"); + expect(savedSearchFilters(saved)).toBe("2026-01-01 – 2026-03-31 · #interview · in de"); + expect(savedSearchFilters({ q: "x", date_from: "2026-01-01" })).toBe("since 2026-01-01"); + }); + it("leaves words with colons that aren't filters alone", () => { expect(parseQuery("time: 14:29 notes").text).toBe("time: 14:29 notes"); }); diff --git a/nextjs-frontend/app/openapi-client/sdk.gen.ts b/nextjs-frontend/app/openapi-client/sdk.gen.ts index ba22e00f..3fe58460 100644 --- a/nextjs-frontend/app/openapi-client/sdk.gen.ts +++ b/nextjs-frontend/app/openapi-client/sdk.gen.ts @@ -7959,7 +7959,8 @@ export class Search { * a resource's supplementary transcripts, captions, translations and indexes, the kinds of object seen in videos, * documents and images, what a model that can see said they show, or what a pipeline's model step wrote about them), * best first, in the namespaces you can read and the collections you were given a role on. A person, emotion or - * sound filter keeps to their lines. By meaning, a moment is a passage about what was asked, though its words may differ. + * sound filter keeps to their lines. A date, tag, origin or language keeps to the assets that have it, as the Library + * filters them. By meaning, a moment is a passage about what was asked, though its words may differ. */ public static searchTranscripts( options: Options, @@ -8110,8 +8111,8 @@ export class Searches { /** * Create Search * - * Save a search (its words, namespace, person, emotion, sound, asset and object) under a name unique among yours. Sharing it - * with its namespace needs editor access there. + * Save a search (its words, namespace, person, emotion, sound, asset, object, dates, tag, origin and language) under a + * name unique among yours. Sharing it with its namespace needs editor access there. */ public static createSearch( options: Options, diff --git a/nextjs-frontend/app/openapi-client/types.gen.ts b/nextjs-frontend/app/openapi-client/types.gen.ts index d835614c..9804fa9f 100644 --- a/nextjs-frontend/app/openapi-client/types.gen.ts +++ b/nextjs-frontend/app/openapi-client/types.gen.ts @@ -24343,6 +24343,34 @@ export type SavedSearch = { * a kind of object the assets have */ object?: string | null; + /** + * Date From + * + * assets recorded on or after this day + */ + date_from?: string | null; + /** + * Date To + * + * assets recorded on or before this day + */ + date_to?: string | null; + /** + * Tag + */ + tag?: string | null; + /** + * Origin + * + * where the assets came from (upload, folder, source: …) + */ + origin?: string | null; + /** + * Language + * + * the assets' language + */ + language?: string | null; /** * Created By * @@ -24674,6 +24702,36 @@ export type SearchCreate = { * a kind of object the assets have (person, car …) */ object?: string | null; + /** + * Date From + * + * assets recorded on or after this day (/search `from`) + */ + date_from?: string | null; + /** + * Date To + * + * assets recorded on or before this day (/search `to`) + */ + date_to?: string | null; + /** + * Tag + * + * assets with this tag + */ + tag?: string | null; + /** + * Origin + * + * assets that came from here (upload, folder, source: …) + */ + origin?: string | null; + /** + * Language + * + * assets in this language (a code; none: not known) + */ + language?: string | null; /** * Name */ @@ -41919,6 +41977,36 @@ export type SearchTranscriptsData = { * only assets this kind of object is seen in (person, car …) */ object?: string | null; + /** + * From + * + * only assets recorded on or after this day (added, when the file doesn't say when) + */ + from?: string | null; + /** + * To + * + * only assets recorded on or before this day + */ + to?: string | null; + /** + * Tag + * + * only assets with these tags (ignoring case); repeat for several (any of them) + */ + tag?: Array | null; + /** + * Origin + * + * only assets that came from here: source: (a connected source), upload, paste, iiif, folder (the archive's own folders) or file (GET /recordings/origins); repeat for several + */ + origin?: Array | null; + /** + * Language + * + * only assets in these languages (codes, ignoring case; none: not known); repeat for several + */ + language?: Array | null; /** * Limit */ diff --git a/nextjs-frontend/components/search/asset-filters.tsx b/nextjs-frontend/components/search/asset-filters.tsx new file mode 100644 index 00000000..4fbab493 --- /dev/null +++ b/nextjs-frontend/components/search/asset-filters.tsx @@ -0,0 +1,131 @@ +"use client"; + +import { useId, useMemo } from "react"; + +import { languageName } from "@/components/library/model"; +import { useLanguages, useOrigins, useTagCounts } from "@/components/library/use-library"; +import { DAY_RX, type SearchFilters } from "@/components/search/query"; +import { Input, Select, type Option } from "@/components/ui/field"; +import { count } from "@/lib/format"; + +type Key = "from" | "to" | "tag" | "origin" | "language"; + +/** The names the asset filters' chips show: a source's name, a language's name. */ +export function useAssetFilterLabels(filters: SearchFilters): Partial> { + const ns = filters.namespace ?? null; + const origins = useOrigins(ns); + const { from, to, tag, origin, language } = filters; + return useMemo( + () => ({ + ...(from ? { from } : {}), + ...(to ? { to } : {}), + ...(tag ? { tag } : {}), + ...(origin ? { origin: origins.data?.find((o) => o.origin === origin)?.name ?? origin } : {}), + ...(language ? { language: languageName(language) } : {}), + }), + [from, to, tag, origin, language, origins.data], + ); +} + +/** Options with "Any …" first, the counted values, and the chosen one even when it's no longer among them. */ +function options(any: string, values: { value: string; label: string; n: number }[], chosen?: string): Option[] { + const out: Option[] = [ + { value: "", label: any }, + ...values.map((v) => ({ value: v.value, label: `${v.label} (${count(v.n)})` })), + ]; + if (chosen && !values.some((v) => v.value === chosen)) out.push({ value: chosen, label: chosen }); + return out; +} + +/** + * When the assets were recorded, their tag, where they came from and their language: filters on the assets the moments + * are in, as the Library has them. Each is empty until picked, and picking one searches again; a list with nothing to + * choose between isn't shown. + */ +export function AssetFilters({ + filters, + onChange, +}: { + filters: SearchFilters; + onChange: (key: Key, value: string | undefined) => void; +}) { + const id = useId(); + const ns = filters.namespace ?? null; + const tags = useTagCounts(ns); + const origins = useOrigins(ns); + const languages = useLanguages(ns); + const tagValues = (tags.data ?? []).map((t) => ({ value: t.tag, label: t.tag, n: t.recordings })); + const originValues = (origins.data ?? []).map((o) => ({ value: o.origin, label: o.name, n: o.recordings })); + const languageValues = (languages.data ?? []).map((l) => ({ + value: l.language ?? "none", + label: languageName(l.language), + n: l.recordings, + })); + const pick = (key: Key) => (e: React.ChangeEvent) => { + const v = e.target.value; + if (!v || (key !== "from" && key !== "to") || DAY_RX.test(v)) onChange(key, v || undefined); + }; + const date = "h-8 px-2 text-[13px]"; + return ( +
+

+ Assets +

+
+ + + + +
+ {(tagValues.length > 0 || filters.tag) && ( +
+ +
+ )} + {(languageValues.length > 1 || filters.language) && ( +
+