From 7dc69ca5615a3ce898f93de4159cb284b0bb93e1 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 17 Oct 2026 02:03:52 +0000 Subject: [PATCH] feat(analysis): read the dates assets give as days, for agents to find by day With Settings > Analysis > Read dates on (off by default), analysis reads each line for the dates it gives and keeps them in date_said: the words, the first and last day they mean, the period (day, weekend, week, month, year) and whether they were worked out from the asset's own date. "next Tuesday" in a recording made on a Wednesday is the Tuesday after, "last week" the Monday to Sunday before, "March 2024" all of March. Agents find what was said about a day or a range with GET /api/v1/dates (start and end as YYYY-MM-DD, YYYY-MM or YYYY), the MCP tool list_dates and the assistant's find_dates, each date with its line and a link to that moment, within what the person can read. English rules in Lens's own code, no model or package. A weekday alone, a date that reads two ways (10/11/2026), "may" in small letters and a bare number like "2000 people" are left out rather than guessed. Rows go with the recording when it's deleted, reset or moved, and are cleared when its lines are split or merged until analysis reads them again. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_014z3r72SB2quWGKbgGt46mZ --- CHANGELOG.md | 6 + DEVLOG.md | 11 + README.md | 14 +- docs/api.md | 12 + docs/assistant.md | 3 +- docs/mcp.md | 1 + docs/processing.md | 19 ++ docs/requirements.md | 5 + fastapi_backend/app/api/mcp_tools.py | 72 +++++ fastapi_backend/app/api/v1/router.py | 2 + fastapi_backend/app/api/v1/routes/dates.py | 37 +++ fastapi_backend/app/domain/ai_tools.py | 38 +++ fastapi_backend/app/domain/analyze.py | 7 +- fastapi_backend/app/domain/chat.py | 1 + fastapi_backend/app/domain/dates.py | 296 ++++++++++++++++++ fastapi_backend/app/domain/deletion.py | 1 + fastapi_backend/app/domain/moving.py | 1 + fastapi_backend/app/domain/store.py | 9 +- fastapi_backend/app/domain/transcript.py | 1 + fastapi_backend/app/schemas/dates.py | 31 ++ fastapi_backend/tests/api/test_dates.py | 117 +++++++ nextjs-frontend/app/openapi-client/index.ts | 8 + nextjs-frontend/app/openapi-client/sdk.gen.ts | 21 ++ .../app/openapi-client/types.gen.ts | 172 ++++++++++ .../components/settings/bodies.tsx | 1 + nextjs-frontend/components/settings/model.ts | 7 + nextjs-frontend/openapi.json | 290 +++++++++++++++++ 27 files changed, 1176 insertions(+), 7 deletions(-) create mode 100644 fastapi_backend/app/api/v1/routes/dates.py create mode 100644 fastapi_backend/app/domain/dates.py create mode 100644 fastapi_backend/app/schemas/dates.py create mode 100644 fastapi_backend/tests/api/test_dates.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 74f6ab9a5..76bdf3827 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version ## Unreleased +- **Dates read as days, for agents.** With Settings → Analysis → Read dates on (off by default), analysis reads the + dates each line gives as days on the calendar: "next Tuesday" in a recording made on a Wednesday is the Tuesday + after, "last week" Monday to Sunday before, "March 2024" all of March, worked out from the asset's own date. Agents + ask what was said about a day or a range with `GET /api/v1/dates`, the MCP tool `list_dates` and the assistant's + `find_dates`, each date with its line and a link to that moment. English rules, no model; a weekday alone or + "10/11/2026" is left out rather than guessed ([Dates](docs/processing.md#dates)). - **Choose the tools, a limit and the namespaces of an answer.** A conversation, or one question, can pick the tool groups or tools its answers may use (`tools`), cap their tool calls (`max_tool_calls`, up to the new Settings → AI assistant → Most tool calls a question may ask for, 50 by default) and narrow one answer to its own namespaces diff --git a/DEVLOG.md b/DEVLOG.md index 62f67c08f..c8cab4ee5 100644 --- a/DEVLOG.md +++ b/DEVLOG.md @@ -2,6 +2,17 @@ Plans and progress for work in flight. Newest first. +## 2026-10-17 · Dates read as days + +Adeel: dates extraction. `dates.py` reads the dates each line gives (written out, numeric, and relative to the day) +into `date_said` as first and last days, worked out from the asset's `recorded_at`, when Settings → Analysis → Read +dates is on. `GET /api/v1/dates`, MCP `list_dates` and the assistant's `find_dates` find them by day or range with +their lines. English rules in Lens's own code rather than dateparser: dateparser couldn't read "next Tuesday" or "this +Friday", and its own search over a line finds dates in ordinary words. + +Refine later: DATE entities named by their day, so "next Tuesday" and "20 October" in two assets meet in the graph; +other languages' month and day names; a Dates view in the web app (a calendar of what was said); times of day. + ## 2026-10-17 · Entities found by spaCy Adeel: NLP libraries for named entities. Settings → Analysis → Entity extraction → spaCy existed but needed spaCy diff --git a/README.md b/README.md index bd05eb16d..0ad7d5ff4 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,10 @@ measured numbers ([requirements and their checks](docs/requirements.md)). Work t listed under [In progress](#in-progress), and what comes next under [Planned](#planned). - **Connect your agents.** Lens is an MCP server (`/mcp`): agents search, find assets by face, voice, entity, topic, - type, content type or date in one call, classify an asset or a text with the decision model, read assets' text and - notes, cite exact moments, find similar content, explore the graph with Cypher or SPARQL or browse it as folders and - files, suggest topics and graph changes, edit and merge entities and topics (with undo), write notes and make + type, content type or date in one call, find what was said about a day, classify an asset or a text with the + decision model, read assets' text and notes, cite exact moments, find similar content, explore the graph with Cypher + or SPARQL or browse it as folders and files, suggest topics and graph changes, edit and merge entities and topics + (with undo), write notes and make podcasts. They sign in with OAuth and see only what their person may read; the account menu copies the MCP address in one click. A namespace can also have an MCP server of its own (`/mcp/`) and answer OpenAI's chat completions API for any app, cited from that namespace alone, each off until its owner turns it on, with API keys that @@ -121,10 +122,15 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p rules, without a model. 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. Pages and shots are described in words by the model provider's vision model or, off by default, by Florence-2 on your own server. + Transcripts can be cleaned up before analysis (stutters, "um"s, spacing and, with a local model, punctuation put + back), entities found by spaCy instead of the rules, and the dates people say read as days on the calendar, each off + by default. ([Graph](docs/graph.md), [entity pages](docs/notes.md#entity-pages), [similar content](docs/processing.md#similar-content), [graph history](docs/graph-history.md), [topics](docs/topics.md), [video](docs/video.md), [NSFW classification](docs/nsfw.md), - [long recordings](docs/speech-providers.md#long-recordings); [checks](docs/requirements.md#understand-it)) + [long recordings](docs/speech-providers.md#long-recordings), + [cleaning up transcripts](docs/processing.md#cleaning-up-transcripts), [dates](docs/processing.md#dates); + [checks](docs/requirements.md#understand-it)) - **Learn from it.** Make a two-host podcast episode about what you pick, every claim cited and checked against its source, read aloud in two voices. Keep notes beside every asset, entity and topic, written by you or the assistant, in a tree where notes are renamed, moved, copied and pinned (pins are your own) and each note shows the notes it's diff --git a/docs/api.md b/docs/api.md index f7865bac3..02e4b568f 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1258,6 +1258,18 @@ An entity's `page` is its page in the web app, by name: `/wiki// str: return f"ns:{namespace}" if namespace else "all" +@tool( + "list_dates", + "Find what was said about a day", + 'Dates said in assets, read as days on the calendar: "next Tuesday" in a recording made on a Wednesday is the ' + 'Tuesday after, "last week" the seven days before that Monday, "March 2024" all of March. Give start and end ' + "(YYYY-MM-DD, or YYYY-MM or YYYY) for what falls between them, the earliest first, each with the words said, the " + "line and a url to that moment. relative: worked out from the asset's own date. Only assets analysed while Settings " + "→ Analysis → Read dates was on have them (reading says whether it is).", + Arg("start", "string", "from this day on: YYYY-MM-DD, YYYY-MM or YYYY", max_length=10), + Arg("end", "string", "up to this day: YYYY-MM-DD, YYYY-MM or YYYY", max_length=10), + Arg("namespace", "string", "only this namespace"), + Arg("recording_id", "integer", "only this asset"), + Arg("query", "string", "only dates whose words or line have this text", max_length=200), + Arg("limit", "integer", "how many", default=25, minimum=1, maximum=100), + Arg("offset", "integer", "skip this many (for the next page)", default=0, minimum=0, maximum=10000), +) +def list_dates( + ctx: Context, + start: str | None, + end: str | None, + namespace: str | None, + recording_id: int | None, + query: str | None, + limit: int, + offset: int, +) -> dict[str, Any]: + res = date_routes.list_dates( + ctx.acl, + ctx.user, + ctx.db, + ctx.cfg, + start=start or "", + end=end or "", + ns=namespace, + recording=recording_id, + q=query or "", + limit=limit, + offset=offset, + ) + out: dict[str, Any] = { + "reading": res.reading, + "total": res.total, + "dates": [ + { + k: v + for k, v in { + "day": d.day, + "until": d.until if d.until != d.day else None, + "period": d.period, + "said": d.said, + "relative": d.relative or None, + "recording_id": d.recording_id, + "title": d.title, + "namespace": d.namespace, + "recorded_at": d.recorded_at, + "line": d.idx, + "at": d.time, + "speaker": d.speaker, + "text": _cut(d.text), + "url": ctx.link(d.recording_id, d.t0), + }.items() + if v is not None + } + for d in res.items + ], + } + if offset + len(res.items) < res.total: + out["next_offset"] = offset + len(res.items) + return out + + @tool( "explore_graph", "Explore the knowledge graph", diff --git a/fastapi_backend/app/api/v1/router.py b/fastapi_backend/app/api/v1/router.py index 8243c6574..e1c00c4b8 100644 --- a/fastapi_backend/app/api/v1/router.py +++ b/fastapi_backend/app/api/v1/router.py @@ -22,6 +22,7 @@ components, content_types, custom_nodes, + dates, entities, entity_setup, extensions, @@ -114,6 +115,7 @@ searches, speakers, entities, + dates, topics, graph, graph_history, diff --git a/fastapi_backend/app/api/v1/routes/dates.py b/fastapi_backend/app/api/v1/routes/dates.py new file mode 100644 index 000000000..ea391b6ee --- /dev/null +++ b/fastapi_backend/app/api/v1/routes/dates.py @@ -0,0 +1,37 @@ +"""Dates said in assets, as days on the calendar (dates.py): what was said about a day, a month or a range.""" + +from __future__ import annotations + +from fastapi import APIRouter, HTTPException, Query + +from app.api.deps import Acl, Cfg, CurrentUser, Db +from app.domain import dates +from app.schemas.dates import DateList + +router = APIRouter(tags=["dates"]) + + +@router.get("/dates") +def list_dates( + acl: Acl, + user: CurrentUser, + db: Db, + cfg: Cfg, + start: str = Query("", description="from this day on: YYYY-MM-DD, or YYYY-MM or YYYY for its first day"), + end: str = Query("", description="up to this day: YYYY-MM-DD, or YYYY-MM or YYYY for its last day"), + ns: str | None = Query(None, description="one namespace"), + recording: int | None = Query(None, description="one asset"), + q: str = Query("", description="only dates whose words or line have this text"), + limit: int = 50, + offset: int = 0, +) -> DateList: + """The dates the assets you can read give that fall between `start` and `end` (a date that spans days counts when + any of its days does), the earliest first, each with its line. Analysis reads them while Settings → Analysis → Read + dates is on.""" + try: + a, b = dates.day_bound(start), dates.day_bound(end, last=True) + except ValueError as e: + raise HTTPException(400, str(e)) from None + spaces, within = acl.scope(ns) + out = dates.said(db, spaces, within, a, b, recording, q.strip() or None, max(1, min(limit, 200)), max(0, offset)) + return DateList.model_validate({**out, "reading": bool((cfg.get("analysis") or {}).get("dates"))}) diff --git a/fastapi_backend/app/domain/ai_tools.py b/fastapi_backend/app/domain/ai_tools.py index a247730f4..1ba15e223 100644 --- a/fastapi_backend/app/domain/ai_tools.py +++ b/fastapi_backend/app/domain/ai_tools.py @@ -15,6 +15,7 @@ batches, classify as classifymod, cypher, + dates, decide, entities, event_marks, @@ -227,6 +228,14 @@ False, ), ("entity_timeline", "How often an entity was mentioned per month.", {"entity_id": _I}, ["entity_id"], False), + ( + "find_dates", + 'What was said about a day or a range: dates said in assets, read as days on the calendar ("next Tuesday" ' + "in a recording is worked out from its date), each with its line. start and end are YYYY-MM-DD, YYYY-MM or YYYY.", + {"start": _S, "end": _S, "namespace": _S, "recording_id": _I, "query": _S, "limit": _I}, + [], + False, + ), ( "find_topics", "Find topics: each namespace's controlled vocabulary of what assets are about, with other labels, a " @@ -574,6 +583,7 @@ def builtin_names(): "find_entities", "entity_mentions", "entity_timeline", + "find_dates", "find_topics", "topic_recordings", "graph_neighbours", @@ -1673,6 +1683,34 @@ def t_entity_mentions(self, entity_id, limit=8): def t_entity_timeline(self, entity_id): return entities.timeline(self.db, [int(entity_id)], self.readable), "Counted mentions by month" + def t_find_dates(self, start=None, end=None, namespace=None, recording_id=None, query=None, limit=10): + spaces = {s for s in self.readable if namespace is None or store.space_names(self.db).get(s) == namespace} + res = dates.said( + self.db, + spaces, + day_from=dates.day_bound(start), + day_to=dates.day_bound(end, last=True), + recording=recording_id, + q=(query or "").strip() or None, + limit=200, + ) + out = [ + { + "ref": self.ref(d["recording_id"], d.get("t0") or 0, d.get("text") or d["said"], d.get("speaker"), d.get("title")), + "day": d["day"] if d["until"] == d["day"] else f"{d['day']} to {d['until']}", + "said": d["said"], + **({"worked_out_from_the_asset_date": d.get("recorded_at")} if d.get("relative") else {}), + "title": d.get("title"), + "time": d.get("time"), + "text": d.get("text"), + } + for d in res["items"] + if self.allowed is None or d["recording_id"] in self.allowed + ][: min(int(limit), 30)] + if not out and not (self.cfg.get("analysis") or {}).get("dates"): + return {"dates": [], "note": "reading dates is off (Settings → Analysis → Read dates)"}, "Dates aren't read here" + return {"dates": out}, f"Found {len(out)} date(s)" + def t_find_topics(self, query=None, namespace=None, limit=20): spaces = self.readable if namespace: diff --git a/fastapi_backend/app/domain/analyze.py b/fastapi_backend/app/domain/analyze.py index ee0cad2f5..6f79629ee 100644 --- a/fastapi_backend/app/domain/analyze.py +++ b/fastapi_backend/app/domain/analyze.py @@ -449,7 +449,7 @@ def one_type(seg_ents): def analyze_recording(db, cfg, rid, seg_ents=None, say=None): """Entities, keywords, sections and talk statistics. `seg_ents` (per line, [(name, type)]) replaces the extractor: a workflow's save entities node passes what its nodes found.""" - rec = db.one("SELECT space, collection FROM $r", r=R("recording", rid)) + rec = db.one("SELECT space, collection, recorded_at FROM $r", r=R("recording", rid)) nid = rec["space"] setup = entity_setup.effective(db, nid, rec.get("collection")) listed = setup["mode"] != "self" # fixed or hybrid: a list of defined entities comes first @@ -544,6 +544,11 @@ def analyze_recording(db, cfg, rid, seg_ents=None, say=None): except Exception: # noqa: BLE001 - servers without INSERT RELATION: one edge at a time for m in ments: db.q("RELATE $a->mentions->$b CONTENT $d", a=m["in"], b=m["out"], d={k: v for k, v in m.items() if k not in ("in", "out")}) + from . import dates + + n = dates.index_recording(db, cfg, rid, segs, nid, rec.get("recorded_at")) # Settings → Analysis → Read dates + if n and say: + say(f"read {n} date(s) as days on the calendar") db.q("UPDATE $r SET stats = $st, status = 'analyzed', analyzed_at = $t", r=R("recording", rid), st=talk_stats(segs), t=store.now()) from . import topics # imported here: topics builds on this module diff --git a/fastapi_backend/app/domain/chat.py b/fastapi_backend/app/domain/chat.py index 4be4be36d..0b3fc95e3 100644 --- a/fastapi_backend/app/domain/chat.py +++ b/fastapi_backend/app/domain/chat.py @@ -834,6 +834,7 @@ def attached_note(files): "find_entities", "entity_mentions", "entity_timeline", + "find_dates", "find_topics", "topic_recordings", "graph_neighbours", diff --git a/fastapi_backend/app/domain/dates.py b/fastapi_backend/app/domain/dates.py new file mode 100644 index 000000000..93fbeed61 --- /dev/null +++ b/fastapi_backend/app/domain/dates.py @@ -0,0 +1,296 @@ +"""Dates said in assets, read as days on the calendar (analysis.dates, off by default; docs/processing.md#dates). + +Analysis reads each line for the dates it gives and keeps them in `date_said`, one row per date: the words said, the +first and last day they mean (`day`, `until`, as YYYY-MM-DD), how long that is (`period`: day, weekend, week, month or +year) and whether it was worked out from the asset's own date (`relative`). "next Tuesday" in a recording made on +Wednesday 14 October 2026 is 2026-10-20, "last week" is 5 to 11 October, "March 2024" all of March. Agents and the +API ask by day or range (`GET /api/v1/dates`, the MCP tool `list_dates`, the assistant's `find_dates`), each date with +its line and a link to the moment. + +The rules read English: day and month names, numbers ("3 March 2024", "March 3rd", "2026-10-20", "20/10/2026"), and +what's said relative to the day ("yesterday", "in three days", "two weeks ago", "this Friday", "next month"). A date +two ways ("10/11/2026") or a weekday on its own ("on Friday": the one before or the one after?) is left out rather +than guessed. "this Friday" is the coming one, "next Friday" the Friday of the week after, "last Friday" the one +before the asset's day. A year alone counts after a word that makes it one ("in 2024", "since 1998"), so "2000 +people" isn't a date. Without an asset date (`recorded_at`), only dates that name their year are kept. +""" + +from __future__ import annotations + +import calendar +import datetime as dt +import re + +from . import store + +R = store.R +PERIODS = ("day", "weekend", "week", "month", "year") +MONTHS = { + m: i + for i, names in enumerate( + ( + ("january", "jan"), + ("february", "feb"), + ("march", "mar"), + ("april", "apr"), + ("may",), + ("june", "jun"), + ("july", "jul"), + ("august", "aug"), + ("september", "sep", "sept"), + ("october", "oct"), + ("november", "nov"), + ("december", "dec"), + ), + start=1, + ) + for m in names +} +WEEKDAYS = ("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday") +NUMBERS = {"a": 1, "an": 1, "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, "ten": 10} # fmt: skip +_MON = "|".join(sorted(MONTHS, key=len, reverse=True)) +_DAY = "|".join(WEEKDAYS) +_N = r"\d{1,3}|" + "|".join(NUMBERS) +_UNIT = r"day|week|month|year" +_ORD = r"(?:st|nd|rd|th)?" +_YEAR = r"(?:19|20)\d{2}" +PATTERNS = [ + # absolute: a day of a month, perhaps with its year + ("dmy", rf"(?P\d{{1,2}}){_ORD}(?:\s+of)?\s+(?P{_MON})\.?(?:,?\s+(?P{_YEAR}))?"), + ("mdy", rf"(?P{_MON})\.?\s+(?:the\s+)?(?P\d{{1,2}}){_ORD}(?:,?\s+(?P{_YEAR}))?"), + ("my", rf"(?P{_MON})\.?,?\s+(?:of\s+)?(?P{_YEAR})"), + ("iso", rf"(?P{_YEAR})-(?P\d{{2}})-(?P\d{{2}})"), + ("num", rf"(?P\d{{1,2}})[/.](?P\d{{1,2}})[/.](?P{_YEAR})"), + ("year", rf"(?:in|since|until|till|by|from|during|before|after|of)\s+(?P{_YEAR})"), + # relative to the asset's day + ( + "near", + r"(?Pthe\s+day\s+after\s+tomorrow|the\s+day\s+before\s+yesterday|today|tonight|yesterday|tomorrow|this\s+(?:morning|afternoon|evening))", + ), # fmt: skip + ("wd", rf"(?Pthis|next|last|coming|past)\s+(?P{_DAY})"), + ("unit", r"(?Pthis|next|last|past|coming)\s+(?Pweekend|week|month|year)"), + ("ago", rf"(?P{_N})\s+(?P{_UNIT})s?\s+ago"), + ("in", rf"(?:in\s+(?P{_N})\s+(?P{_UNIT})s?(?!\s+ago)|(?P{_N})\s+(?P{_UNIT})s?\s+from\s+now)"), +] +# one pass over a line: each pattern as one named group, its own groups unnamed (_SUB reads them) +FIND = re.compile("|".join(f"(?P<{k}>(?', '(?:', p)})(?![\\w-]))" for k, p in PATTERNS), re.I) +_SUB = {k: re.compile(p, re.I) for k, p in PATTERNS} + + +def base_day(recorded_at): + """The day an asset was made, as recorded_at gives it (an ISO date and time), or None.""" + try: + return dt.datetime.fromisoformat(str(recorded_at)[:19]).date() if recorded_at else None + except ValueError: + return None + + +def day_bound(value, last=False): + """YYYY-MM-DD, YYYY-MM or YYYY as a day (YYYY-MM-DD): the first day it holds, or with `last` the last; None for + nothing. ValueError for anything else.""" + v = (value or "").strip() + if not v: + return None + try: + if re.fullmatch(r"\d{4}-\d{2}-\d{2}", v): + return dt.date.fromisoformat(v).isoformat() + if re.fullmatch(r"\d{4}-\d{2}", v): + y, m = int(v[:4]), int(v[5:]) + return (_month_end(y, m) if last else dt.date(y, m, 1)).isoformat() + if re.fullmatch(r"\d{4}", v): + return f"{v}-12-31" if last else f"{v}-01-01" + except ValueError: + pass + raise ValueError(f"{v} isn't a day: YYYY-MM-DD, YYYY-MM or YYYY") + + +def _n(x): + x = (x or "").lower() + return int(x) if x.isdigit() else NUMBERS.get(x) + + +def _month_end(y, m): + return dt.date(y, m, calendar.monthrange(y, m)[1]) + + +def _add(day, unit, n): + if unit == "day": + return day + dt.timedelta(days=n) + if unit == "week": + return day + dt.timedelta(weeks=n) + if unit == "month": + m = day.month - 1 + n + y, m = day.year + m // 12, m % 12 + 1 + return dt.date(y, m, min(day.day, calendar.monthrange(y, m)[1])) + return dt.date(day.year + n, day.month, min(day.day, calendar.monthrange(day.year + n, day.month)[1])) + + +def _span(day, unit): + """(first, last day, period) of the `unit` that holds `day`.""" + if unit == "week": + start = day - dt.timedelta(days=day.weekday()) + return start, start + dt.timedelta(days=6), "week" + if unit == "weekend": + start = day - dt.timedelta(days=day.weekday()) + dt.timedelta(days=5) + return start, start + dt.timedelta(days=1), "weekend" + if unit == "month": + return day.replace(day=1), _month_end(day.year, day.month), "month" + if unit == "year": + return dt.date(day.year, 1, 1), dt.date(day.year, 12, 31), "year" + return day, day, "day" + + +def _verb_like(month): + """A month's short name ("mar", "dec") or "may" in small letters is more likely a word than a month.""" + low = month.lower() + return not month[:1].isupper() and (low == "may" or (len(low) <= 4 and low not in ("june", "july"))) + + +def resolve(kind, m, base): + """(first, last, period, relative) for one match of PATTERNS[kind], or None when it isn't a day on the calendar.""" + g = {k: v for k, v in m.groupdict().items() if v} + if kind in ("dmy", "mdy", "my", "iso", "num"): + if kind == "num": + a, b = int(g["a"]), int(g["b"]) + if a <= 12 and b <= 12 and a != b: + return None # 10/11: October or November? + d, mo = (a, b) if a > 12 or a == b else (b, a) + else: + if kind != "iso" and _verb_like(g["m"]): + return None + mo = int(g["m"]) if kind == "iso" else MONTHS[g["m"].lower()] + d = int(g.get("d") or 0) + y = int(g["y"]) if g.get("y") else (base.year if base else None) + if y is None: + return None + try: + if not d: + return dt.date(y, mo, 1), _month_end(y, mo), "month", False + day = dt.date(y, mo, d) + except ValueError: + return None + return day, day, "day", not g.get("y") + if kind == "year": + return dt.date(int(g["y"]), 1, 1), dt.date(int(g["y"]), 12, 31), "year", False + if base is None: + return None + if kind == "near": + w = " ".join(g["w"].lower().split()) + shift = {"yesterday": -1, "tomorrow": 1, "the day after tomorrow": 2, "the day before yesterday": -2}.get(w, 0) + day = base + dt.timedelta(days=shift) + return day, day, "day", True + if kind == "wd": + q, want = g["q"].lower(), WEEKDAYS.index(g["wd"].lower()) + if q in ("this", "coming"): + day = base + dt.timedelta(days=(want - base.weekday()) % 7) + elif q == "next": + day = base - dt.timedelta(days=base.weekday()) + dt.timedelta(weeks=1, days=want) + else: # last, past: the one before the asset's day + day = base - dt.timedelta(days=(base.weekday() - want) % 7 or 7) + return day, day, "day", True + if kind == "unit": + q, unit = g["q"].lower(), g["u"].lower() + step = 0 if q == "this" else (1 if q in ("next", "coming") else -1) + day = _add(base, "week" if unit == "weekend" else unit, step) + return (*_span(day, unit), True) + n, unit = _n(g.get("n") or g.get("n2")), (g.get("u") or g.get("u2")).lower() + if not n: + return None + day = _add(base, unit, -n if kind == "ago" else n) + return (*_span(day, unit), True) + + +def find(text, base=None): + """The dates a line gives: [{text, day, until, period, relative}], in the order said.""" + out = [] + for m in FIND.finditer(text or ""): + kind = m.lastgroup + sub = _SUB[kind].fullmatch(m.group(kind)) + got = resolve(kind, sub, base) if sub else None + if got: + first, last, period, relative = got + said = sub.group("y") if kind == "year" else m.group(kind) # "in 2024": the year is the date + out.append({"text": said, "day": first.isoformat(), "until": last.isoformat(), "period": period, "relative": relative}) + return out + + +def index_recording(db, cfg, rid, segs, space, recorded_at): + """Keep the dates a recording's lines give (when Settings → Analysis reads dates); how many.""" + if not (cfg.get("analysis") or {}).get("dates"): + db.q("DELETE date_said WHERE recording = $r", r=rid) + return 0 + base = base_day(recorded_at) + rows = [ + {"recording": rid, "space": space, "segment": s["id"], "idx": s["idx"], "t0": s.get("t0"), **d} + for s in segs + for d in find(s.get("text"), base) + ] + db.run(["DELETE date_said WHERE recording = $r"] + (["INSERT INTO date_said $rows"] if rows else []), r=rid, rows=rows) + return len(rows) + + +def said(db, spaces, within=None, day_from=None, day_to=None, recording=None, q=None, limit=50, offset=0): + """Dates said in the assets of these namespaces (and the collections in `within`) that fall in [day_from, day_to] + (either may be left open), the earliest first: {total, items: [...]} with each date's line.""" + from . import library, render + + where, p = ["space IN $all"], {"all": sorted(set(spaces) | set(within or {}))} + if day_from: + where.append("until >= $a") + p["a"] = day_from + if day_to: + where.append("day <= $b") + p["b"] = day_to + if recording is not None: + where.append("recording = $rec") + p["rec"] = int(recording) + rows = db.rows( + f"SELECT recording, segment, idx, t0, text, day, until, period, relative FROM date_said WHERE {' AND '.join(where)}", **p + ) + if not rows: + return {"total": 0, "items": []} + cond, sp = library.in_scope(spaces, within) + recs = { + r["id"]: r + for r in db.rows( + f"SELECT record::id(id) AS id, title, recorded_at, space FROM recording WHERE id IN $ids AND {cond}", + ids=[R("recording", i) for i in {x["recording"] for x in rows}], + **sp, + ) + } + rows = [x for x in rows if x["recording"] in recs] + if not rows: + return {"total": 0, "items": []} + seg_ids = [R("segment", i) for i in {x["segment"] for x in rows}] + lines = {s["id"]: s for s in db.rows("SELECT record::id(id) AS id, text, speaker FROM segment WHERE id IN $ids", ids=seg_ids)} + if q: + needle = q.lower() + rows = [x for x in rows if needle in (lines.get(x["segment"], {}).get("text") or "").lower() or needle in x["text"].lower()] + rows.sort(key=lambda x: (x["day"], x["until"], recs[x["recording"]].get("recorded_at") or "", x["segment"])) + page = rows[offset : offset + limit] + names, spaces_n = render.speaker_names(db, [lines.get(x["segment"], {}).get("speaker") for x in page]), store.space_names(db) + items = [] + for x in page: + rec, line = recs[x["recording"]], lines.get(x["segment"], {}) + items.append( + store.clean( + { + "day": x["day"], + "until": x["until"], + "period": x["period"], + "said": x["text"], + "relative": bool(x.get("relative")), + "recording_id": x["recording"], + "title": rec.get("title"), + "namespace": spaces_n.get(rec.get("space")), + "recorded_at": rec.get("recorded_at"), + "segment": x["segment"], + "idx": x["idx"], + "t0": x.get("t0"), + "time": store.tc(x.get("t0")), + "speaker": names.get(line.get("speaker")), + "text": line.get("text"), + } + ) + ) + return {"total": len(rows), "items": items} diff --git a/fastapi_backend/app/domain/deletion.py b/fastapi_backend/app/domain/deletion.py index ea6020d80..1264304d1 100644 --- a/fastapi_backend/app/domain/deletion.py +++ b/fastapi_backend/app/domain/deletion.py @@ -30,6 +30,7 @@ "term", "topic_about", "section", + "date_said", "appearance", "segment", "output", diff --git a/fastapi_backend/app/domain/moving.py b/fastapi_backend/app/domain/moving.py index 8019f5041..a7da77210 100644 --- a/fastapi_backend/app/domain/moving.py +++ b/fastapi_backend/app/domain/moving.py @@ -43,6 +43,7 @@ "segment", "appearance", "section", + "date_said", "shot", "ocr_span", "face_track", diff --git a/fastapi_backend/app/domain/store.py b/fastapi_backend/app/domain/store.py index 250f8f3f0..8638a60a6 100644 --- a/fastapi_backend/app/domain/store.py +++ b/fastapi_backend/app/domain/store.py @@ -153,7 +153,8 @@ def labels(): "sample_seconds": 60, "cross_namespace": "suggest", }, - # anytopdf: the places and dates anytopdf (run here) finds, as hints for the extractor (enrich.py); cleanup: the + # anytopdf: the places and dates anytopdf (run here) finds, as hints for the extractor (enrich.py); dates: the dates + # each line gives kept as days on the calendar (dates.py); cleanup: the # clean step tidies transcripts before analysis (cleanup.py), cleanup_fillers taking out "um" and "uh" too, and # cleanup_punctuation (off, missing, always) putting marks back with a local model "analysis": { @@ -161,6 +162,7 @@ def labels(): "spacy_model": "en_core_web_sm", "gazetteer": [], "anytopdf": False, + "dates": False, "cleanup": False, "cleanup_fillers": True, "cleanup_punctuation": "off", @@ -1181,6 +1183,10 @@ def close(self): "DEFINE TABLE IF NOT EXISTS merge SCHEMALESS", "DEFINE TABLE IF NOT EXISTS section SCHEMALESS", "DEFINE INDEX IF NOT EXISTS section_rec ON section FIELDS recording", + # the dates a recording's lines give, as days on the calendar (dates.py) + "DEFINE TABLE IF NOT EXISTS date_said SCHEMALESS", + "DEFINE INDEX IF NOT EXISTS date_said_rec ON date_said FIELDS recording", + "DEFINE INDEX IF NOT EXISTS date_said_day ON date_said FIELDS space, day", "DEFINE TABLE IF NOT EXISTS entity SCHEMALESS", "DEFINE INDEX IF NOT EXISTS entity_space ON entity FIELDS space", "DEFINE INDEX IF NOT EXISTS entity_key ON entity FIELDS ekey UNIQUE", @@ -1776,6 +1782,7 @@ def space_names(db): "DELETE mentions WHERE recording = $rid", "DELETE term WHERE recording = $rid", "DELETE section WHERE recording = $rid", + "DELETE date_said WHERE recording = $rid", "DELETE appearance WHERE recording = $rid", "DELETE segment WHERE recording = $rid AND idx >= $keep", "DELETE passage WHERE recording = $rid", diff --git a/fastapi_backend/app/domain/transcript.py b/fastapi_backend/app/domain/transcript.py index d15f8eb4c..1ae941778 100644 --- a/fastapi_backend/app/domain/transcript.py +++ b/fastapi_backend/app/domain/transcript.py @@ -143,6 +143,7 @@ def _renumber(db, rid, segs, start, merged=None): db.run( [ "DELETE mentions WHERE recording = $rid", + "DELETE date_said WHERE recording = $rid", "FOR $s IN $rows { UPSERT $s.id CONTENT $s; }", "DELETE segment WHERE recording = $rid AND idx >= $keep", f"UPDATE segment_edit SET idx = idx {sign} 1 WHERE recording = $rid AND idx >= $later", diff --git a/fastapi_backend/app/schemas/dates.py b/fastapi_backend/app/schemas/dates.py new file mode 100644 index 000000000..8b0076dca --- /dev/null +++ b/fastapi_backend/app/schemas/dates.py @@ -0,0 +1,31 @@ +from __future__ import annotations + +from pydantic import Field + +from app.schemas.common import ResponseModel + + +class DateSaid(ResponseModel): + """A date an asset's line gives, as days on the calendar.""" + + day: str = Field(description="the first day it means, YYYY-MM-DD") + until: str = Field(description="the last day it means: the same day for a day, the Sunday for a week, the 31st for a month") + period: str = Field(description="day, weekend, week, month or year") + said: str = Field(description='the words that give it, like "next Tuesday" or "3 March 2024"') + relative: bool = Field(description="worked out from the asset's own date (recorded_at): a day said relative to it, or without its year") + recording_id: int + title: str | None = None + namespace: str | None = None + recorded_at: str | None = None + segment: int = Field(description="the line's id") + idx: int = Field(description="the line's place in the asset's text") + t0: int | None = Field(None, description="ms") + time: str | None = None + speaker: str | None = None + text: str | None = Field(None, description="the line") + + +class DateList(ResponseModel): + total: int + items: list[DateSaid] + reading: bool = Field(description="whether analysis reads dates (Settings → Analysis → Read dates); off, nothing new is read") diff --git a/fastapi_backend/tests/api/test_dates.py b/fastapi_backend/tests/api/test_dates.py new file mode 100644 index 000000000..5fcf36c37 --- /dev/null +++ b/fastapi_backend/tests/api/test_dates.py @@ -0,0 +1,117 @@ +"""Dates said in assets, read as days on the calendar (analysis.dates): the rules, the analyze step keeping them, and +agents finding them by day through the API, MCP and the assistant, only in what the person can read.""" + +from __future__ import annotations + +import datetime as dt +import json + +import pytest + +from app.domain import ai_tools, dates, ingest, jobs, store +from tests.api.test_mcp import tool +from tests.api.test_oauth import _bearer, _grant +from tests.helpers import drain, login, make_user + +R = store.R +WED = dt.date(2026, 10, 14) # a Wednesday + + +@pytest.mark.parametrize( + ("said", "day", "until", "period", "relative"), + [ + ("next Tuesday", "2026-10-20", None, "day", True), + ("this Friday", "2026-10-16", None, "day", True), + ("next Friday", "2026-10-23", None, "day", True), + ("last Friday", "2026-10-09", None, "day", True), + ("yesterday", "2026-10-13", None, "day", True), + ("the day after tomorrow", "2026-10-16", None, "day", True), + ("in three days", "2026-10-17", None, "day", True), + ("5 days from now", "2026-10-19", None, "day", True), + ("last week", "2026-10-05", "2026-10-11", "week", True), + ("two weeks ago", "2026-09-28", "2026-10-04", "week", True), + ("next weekend", "2026-10-24", "2026-10-25", "weekend", True), + ("next month", "2026-11-01", "2026-11-30", "month", True), + ("last year", "2025-01-01", "2025-12-31", "year", True), + ("3 March 2024", "2024-03-03", None, "day", False), + ("3rd of March", "2026-03-03", None, "day", True), + ("March the 5th, 2025", "2025-03-05", None, "day", False), + ("March 2024", "2024-03-01", "2024-03-31", "month", False), + ("2026-10-20", "2026-10-20", None, "day", False), + ("20/10/2026", "2026-10-20", None, "day", False), + ], +) +def test_dates_are_read_as_days(said, day, until, period, relative): + got = dates.find(f"We said {said}, I think.", WED) + assert [(d["text"], d["day"], d["until"], d["period"], d["relative"]) for d in got] == [(said, day, until or day, period, relative)] + + +def test_what_could_be_two_dates_is_left_out(): + for line in ("see you on Friday", "on 10/11/2026", "I may 3 times", "about 2000 people", "31 February 2024", "for three years"): + assert dates.find(line, WED) == [], line + assert [d["text"] for d in dates.find("in 2024, about 2000 people", WED)] == ["2024"] + # without the asset's date, only dates that give their year + assert [d["text"] for d in dates.find("next Tuesday, March 3rd or 5 May 2024", None)] == ["5 May 2024"] + assert dates.day_bound("2026-02", last=True) == "2026-02-28" and dates.day_bound("2024") == "2024-01-01" + with pytest.raises(ValueError): + dates.day_bound("next week") + + +PLAN = """Ann|N|Let's meet next Tuesday to go over the budget. +Ben|N|The report from 3 March 2024 still holds, and the launch is in March 2027. +Ann|N|Okay, nothing else.""" + + +def _plan(db, cfg): + rid = ingest.import_text(db, cfg, "pods", PLAN, "Planning", "auto") + db.q("UPDATE $r SET recorded_at = '2026-10-14T10:00:00'", r=R("recording", rid)) + jid = jobs.enqueue(db, rid, ["analyze"]) + drain(db, cfg) + return rid, jid + + +def test_off_by_default_reads_none(db, cfg): + rid, _ = _plan(db, cfg) + assert not db.rows("SELECT * FROM date_said WHERE recording = $r", r=rid) + + +def test_agents_find_what_was_said_about_a_day(client, db, cfg): + cfg["analysis"]["dates"] = True + rid, jid = _plan(db, cfg) + assert any("read 3 date(s) as days on the calendar" in line for line in db.one("SELECT log FROM $j", j=R("job", jid))["log"]) + make_user(db, "vi@x.io", "viewer password 1", roles={"pods": "viewer"}) + make_user(db, "ca@x.io", "calls password 1", roles={"calls": "viewer"}) + vi, ca = login(client, "vi@x.io", "viewer password 1"), login(client, "ca@x.io", "calls password 1") + + r = client.get("/api/v1/dates", headers=vi, params={"start": "2026-10", "end": "2026-10"}) + assert r.status_code == 200, r.text + got = r.json() + assert got["reading"] is True and got["total"] == 1 + d = got["items"][0] + assert (d["day"], d["said"], d["relative"], d["recording_id"], d["idx"]) == ("2026-10-20", "next Tuesday", True, rid, 0) + assert d["text"] == "Let's meet next Tuesday to go over the budget." and d["title"] == "Planning" + # a range takes a month that overlaps it; everything, the earliest first + assert [x["said"] for x in client.get("/api/v1/dates", headers=vi, params={"start": "2027-03-15"}).json()["items"]] == ["March 2027"] + assert [x["day"] for x in client.get("/api/v1/dates", headers=vi).json()["items"]] == ["2024-03-03", "2026-10-20", "2027-03-01"] + assert client.get("/api/v1/dates", headers=vi, params={"q": "launch"}).json()["total"] == 2 # the line's words count + assert client.get("/api/v1/dates", headers=vi, params={"start": "soon"}).status_code == 400 + assert client.get("/api/v1/dates", headers=ca).json()["total"] == 0 # not their namespace + + # over MCP, signed in as the viewer + _, tokens = _grant(client, vi, scope="read") + out = tool(client, _bearer(tokens), "list_dates", start="2026-10-20", end="2026-10-20") + assert out["reading"] is True and [x["said"] for x in out["dates"]] == ["next Tuesday"] + assert out["dates"][0]["url"].endswith(f"/resources/{rid}") and out["dates"][0]["relative"] is True + + # the assistant cites the line + box = ai_tools.Toolbox(db, cfg, {"id": 1, "email": "vi@x.io"}, {store.ns_id(db, "pods")}, set(), None, None) + res, note = box.call("find_dates", {"start": "2026-10-19", "end": "2026-10-25"}) + found = json.loads(res)["dates"] + assert note == "Found 1 date(s)" and found[0]["said"] == "next Tuesday" and found[0]["ref"] == 1 + assert box.refs[0]["text"] == "Let's meet next Tuesday to go over the budget." + + # turned off, analysis takes them away again + cfg["analysis"]["dates"] = False + jobs.enqueue(db, rid, ["analyze"]) + drain(db, cfg) + assert not db.rows("SELECT * FROM date_said WHERE recording = $r", r=rid) diff --git a/nextjs-frontend/app/openapi-client/index.ts b/nextjs-frontend/app/openapi-client/index.ts index f01abc91e..ceb919b93 100644 --- a/nextjs-frontend/app/openapi-client/index.ts +++ b/nextjs-frontend/app/openapi-client/index.ts @@ -15,6 +15,7 @@ export { Collections, Comments, ContentTypes, + Dates, Entities, Extensions, Fedora, @@ -657,6 +658,8 @@ export type { CustomNodeCreate, CustomNodeUpdate, CustomNodeVersionCreate, + DateList, + DateSaid, DaySpend, DecideApprovalData, DecideApprovalError, @@ -1787,6 +1790,11 @@ export type { ListCustomNodesErrors, ListCustomNodesResponse, ListCustomNodesResponses, + ListDatesData, + ListDatesError, + ListDatesErrors, + ListDatesResponse, + ListDatesResponses, ListEntitiesData, ListEntitiesError, ListEntitiesErrors, diff --git a/nextjs-frontend/app/openapi-client/sdk.gen.ts b/nextjs-frontend/app/openapi-client/sdk.gen.ts index b601680f3..0ca0d0840 100644 --- a/nextjs-frontend/app/openapi-client/sdk.gen.ts +++ b/nextjs-frontend/app/openapi-client/sdk.gen.ts @@ -872,6 +872,9 @@ import type { ListCustomNodesData, ListCustomNodesErrors, ListCustomNodesResponses, + ListDatesData, + ListDatesErrors, + ListDatesResponses, ListEntitiesData, ListEntitiesErrors, ListEntitiesResponses, @@ -7817,6 +7820,24 @@ export class Entities { } } +export class Dates { + /** + * List Dates + * + * The dates the assets you can read give that fall between `start` and `end` (a date that spans days counts when + * any of its days does), the earliest first, each with its line. Analysis reads them while Settings → Analysis → Read + * dates is on. + */ + public static listDates( + options?: Options, + ): RequestResult { + return (options?.client ?? client).get({ + url: "/api/v1/dates", + ...options, + }); + } +} + export class Topics { /** * List Topics diff --git a/nextjs-frontend/app/openapi-client/types.gen.ts b/nextjs-frontend/app/openapi-client/types.gen.ts index 29b5fd8c8..0d7e17951 100644 --- a/nextjs-frontend/app/openapi-client/types.gen.ts +++ b/nextjs-frontend/app/openapi-client/types.gen.ts @@ -5502,6 +5502,114 @@ export type CustomNodeVersionCreate = { notes?: string | null; }; +/** + * DateList + */ +export type DateList = { + /** + * Total + */ + total: number; + /** + * Items + */ + items: Array; + /** + * Reading + * + * whether analysis reads dates (Settings → Analysis → Read dates); off, nothing new is read + */ + reading: boolean; + [key: string]: unknown; +}; + +/** + * DateSaid + * + * A date an asset's line gives, as days on the calendar. + */ +export type DateSaid = { + /** + * Day + * + * the first day it means, YYYY-MM-DD + */ + day: string; + /** + * Until + * + * the last day it means: the same day for a day, the Sunday for a week, the 31st for a month + */ + until: string; + /** + * Period + * + * day, weekend, week, month or year + */ + period: string; + /** + * Said + * + * the words that give it, like "next Tuesday" or "3 March 2024" + */ + said: string; + /** + * Relative + * + * worked out from the asset's own date (recorded_at): a day said relative to it, or without its year + */ + relative: boolean; + /** + * Recording Id + */ + recording_id: number; + /** + * Title + */ + title?: string | null; + /** + * Namespace + */ + namespace?: string | null; + /** + * Recorded At + */ + recorded_at?: string | null; + /** + * Segment + * + * the line's id + */ + segment: number; + /** + * Idx + * + * the line's place in the asset's text + */ + idx: number; + /** + * T0 + * + * ms + */ + t0?: number | null; + /** + * Time + */ + time?: string | null; + /** + * Speaker + */ + speaker?: string | null; + /** + * Text + * + * the line + */ + text?: string | null; + [key: string]: unknown; +}; + /** * DaySpend */ @@ -35420,6 +35528,70 @@ export type FindGraphPathResponses = { export type FindGraphPathResponse = FindGraphPathResponses[keyof FindGraphPathResponses]; +export type ListDatesData = { + body?: never; + path?: never; + query?: { + /** + * Start + * + * from this day on: YYYY-MM-DD, or YYYY-MM or YYYY for its first day + */ + start?: string; + /** + * End + * + * up to this day: YYYY-MM-DD, or YYYY-MM or YYYY for its last day + */ + end?: string; + /** + * Ns + * + * one namespace + */ + ns?: string | null; + /** + * Recording + * + * one asset + */ + recording?: number | null; + /** + * Q + * + * only dates whose words or line have this text + */ + q?: string; + /** + * Limit + */ + limit?: number; + /** + * Offset + */ + offset?: number; + }; + url: "/api/v1/dates"; +}; + +export type ListDatesErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type ListDatesError = ListDatesErrors[keyof ListDatesErrors]; + +export type ListDatesResponses = { + /** + * Successful Response + */ + 200: DateList; +}; + +export type ListDatesResponse = ListDatesResponses[keyof ListDatesResponses]; + export type ListTopicsData = { body?: never; path?: never; diff --git a/nextjs-frontend/components/settings/bodies.tsx b/nextjs-frontend/components/settings/bodies.tsx index c99953a5b..eab5e9c80 100644 --- a/nextjs-frontend/components/settings/bodies.tsx +++ b/nextjs-frontend/components/settings/bodies.tsx @@ -253,6 +253,7 @@ export function SectionBody({ ctx }: { ctx: BodyCtx }) { {raw("analysis.entities") === "spacy" && } +