From c65c3eba0ada9334635e7f64f71b3e4855f41296 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Fri, 16 Oct 2026 00:47:29 +0000 Subject: [PATCH] feat(automations): feed_post.received activities for news feed posts Each post a news feed brings in is now a feed_post.received activity (a child of asset.added) instead of a generic file.added. Its object carries the post's address, site, feed title, author, published date and categories, read from what the feed listed and kept on the resource as `post`; all of the feed's own text is marked as outside text, so automations wrap it before a model sees it. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SWH8a26M9wkAPq2eRrfWZK --- CHANGELOG.md | 11 ++-- README.md | 26 ++++---- docs/automations.md | 1 + docs/processing.md | 3 + docs/requirements.md | 2 +- fastapi_backend/app/domain/events_content.py | 61 +++++++++++++++++-- fastapi_backend/app/domain/rss.py | 16 +++++ fastapi_backend/app/domain/sources.py | 13 ++++ .../tests/api/test_news_feed_source.py | 17 +++++- nextjs-frontend/components/timeline/model.ts | 1 + 10 files changed, 127 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b04425f9e..0e1e57367 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,13 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version Markdown or plain text and exports it, questions and code blocks have their own copy buttons, and the conversation's header has Export; without a PDF printer on the server, the browser prints it instead ([Copy and export](docs/assistant.md#copy-and-export)). +- **News feeds.** Subscribe to a blog's or a news site's RSS or Atom feed: add it on the Sources page (its address, + or just the site's) and watch it, and each new post comes into a namespace as text, with its link, author and date, + so answers cite it. Feeds are asked for again only when they've changed, and a private feed's address is kept like a + password ([News feeds](docs/processing.md#news-feeds-rss-and-atom)). +- **Automations on feed posts.** Each new post of a news feed is a `feed_post.received` activity with its address, + site, feed, author, date and categories, so an automation can start on posts from one feed, author or category + ([Kinds](docs/automations.md#kinds)). ## 0.14.0 October 17, 2026 {id="0.14.0"} @@ -72,10 +79,6 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version each run shows what its actions did. Try one on an activity without keeping or sending anything, or run it now; Settings → Automations switches them all off and sets how far they may go ([Running automations](docs/automations.md#running-automations)). -- **News feeds.** Subscribe to a blog's or a news site's RSS or Atom feed: add it on the Sources page (its address, - or just the site's) and watch it, and each new post comes into a namespace as text, with its link, author and date, - so answers cite it. Feeds are asked for again only when they've changed, and a private feed's address is kept like a - password ([News feeds](docs/processing.md#news-feeds-rss-and-atom)). - **Each answer says what produced it.** The *i* in an answer's header shows the model and the host it ran on, the tools it was offered and used, the scope, its limits and what it used, tokens, cost (an estimate from your prices), how long it took, how it ended and which tool results read like instructions to an AI, with the calls made for it. diff --git a/README.md b/README.md index 3532f9807..130d915ac 100644 --- a/README.md +++ b/README.md @@ -143,19 +143,19 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p [notes](docs/notes.md), [evidence PDFs](docs/api.md#evidence-pdfs); [checks](docs/requirements.md#learn-from-it)) - **Automate it.** Pipelines and workflows on a canvas, routines on a schedule, batch runs over a collection, and notifications to webhooks, Slack, Discord and Matterbridge. Everything that happens in Lens (new assets, emails, - calendar events, files, what people say to Lens, sensor readings, notes, graph changes, runs, budget warnings) is kept - as a stream of activities in W3C Activity Streams form, which agents read through the API, MCP and the assistant, and - people review on the Timeline page (and Latest on Home), marking what they've seen. Automations act on that stream, on - a schedule, at a time or when their own webhook is called: they test conditions, then tell you in your own chat app, - ask the model or the agent, use a tool, call a web address (its credentials kept sealed), run a workflow, pipeline or - routine, or change assets and notes. A Classify node in a workflow sorts an asset into labels, content types or - topics, acting only on a sure answer. Anything that reaches outside Lens or changes more than what set it off asks - first, until you let it run on its own: on the Approvals page or, for a change inside Lens, with a yes in the chat it - told you in; agents can decline an approval but never give one. Each automation has limits and budgets, and agents - make, try and run them over MCP and in the assistant, or draft one from a sentence; the studio to draw them in the web - app is planned. ([Processing](docs/processing.md#workflows-and-the-canvas), [notifications](docs/notifications.md), - [automations](docs/automations.md), [approvals](docs/automations.md#approvals), [timeline](docs/timeline.md); - [checks](docs/requirements.md#automate-it)) + calendar events, files, feed posts, what people say to Lens, sensor readings, notes, graph changes, runs, budget + warnings) is kept as a stream of activities in W3C Activity Streams form, which agents read through the API, MCP and + the assistant, and people review on the Timeline page (and Latest on Home), marking what they've seen. Automations act + on that stream, on a schedule, at a time or when their own webhook is called: they test conditions, then tell you in + your own chat app, ask the model or the agent, use a tool, call a web address (its credentials kept sealed), run a + workflow, pipeline or routine, or change assets and notes. A Classify node in a workflow sorts an asset into labels, + content types or topics, acting only on a sure answer. Anything that reaches outside Lens or changes more than what + set it off asks first, until you let it run on its own: on the Approvals page or, for a change inside Lens, with a yes + in the chat it told you in; agents can decline an approval but never give one. Each automation has limits and budgets, + and agents make, try and run them over MCP and in the assistant, or draft one from a sentence; the studio to draw them + in the web app is planned. ([Processing](docs/processing.md#workflows-and-the-canvas), + [notifications](docs/notifications.md), [automations](docs/automations.md), + [approvals](docs/automations.md#approvals), [timeline](docs/timeline.md); [checks](docs/requirements.md#automate-it)) - **See what it costs.** An activity ledger of every model call, with reports, charts and budgets that cap spending. Setup asks for one overall budget after a cloud model, with a soft limit that warns and a hard limit that stops paid calls. A meter in the top bar shows spend against the budget, and the Costs page breaks it down by day, model and diff --git a/docs/automations.md b/docs/automations.md index 40a810eb9..2e9fb09db 100644 --- a/docs/automations.md +++ b/docs/automations.md @@ -109,6 +109,7 @@ and the paths holding outside text are in `GET /api/v1/activities/kinds` (`event | `asset.added` | | `recordings` | `{type: Audio, Video, Image or Document, id: lens:recording/, name: its title, url: /resources/, media: audio, video, image, document or transcript, content_type}` | Lens added “Scan 12” | | `email.received` | `asset.added` | `recordings` | `from`, `to`, `cc` (addresses), `subject`, `list`, `mailbox`, `date` (sent), `snippet` (the start of its text), `attachments` (how many) | Work mail received “School trip on Friday” from office@stmarys.org | | `calendar_event.added` | `asset.added` | `recordings` | `startTime`, `endTime`, `location`, `organizer`, `attendees` (addresses), `calendar` | Family calendar added the event “Parents' evening” on 2026-10-20 | +| `feed_post.received` | `asset.added` | `recordings` | `href` (the post's address), `site`, `feed` (its title), `author`, `published`, `categories` | Harbour news received the post “Autumn regatta” from harbour.example.org | | `file.added` | `asset.added` | `recordings` | `path` (in its source), `mediaType`, `size` (bytes), `duration` (seconds) | Office share added “Minutes” | | `upload.added` | `asset.added` | `recordings` | `path` (the file's name), `mediaType`, `size`, `duration` | Sam uploaded “Call with the school” | | `web_page.saved` | `asset.added` | `recordings` | `href` (the page's address), `site` | Sam saved “Museum opening hours” from museum.example.org | diff --git a/docs/processing.md b/docs/processing.md index c34e2618a..548206785 100644 --- a/docs/processing.md +++ b/docs/processing.md @@ -588,6 +588,9 @@ it and its link. (never its address). It comes in as text: the title, its link, who wrote it, when, the feed, its categories and enclosures, then its text. Its content type is Feed post, so a namespace can give feed posts a pipeline of their own (Settings › Content types). +- **Automations**: each new post is a `feed_post.received` activity (a kind of `asset.added`) with the post's + address, site, feed, author, date and categories, so an automation can start on posts from one feed, author or + category ([Automations](automations.md#kinds)). What the feed wrote is marked as outside text. - **New posts only, unless you backfill**: as for any watch, a new watch on a feed takes the posts published after it starts; turn on backfill to bring in those the feed lists already. The newest 500 posts of a feed are listed. - **Light on the network**: a feed read is reused for a minute, then asked for again with its `ETag` and diff --git a/docs/requirements.md b/docs/requirements.md index 6b9e97727..76f5a4d23 100644 --- a/docs/requirements.md +++ b/docs/requirements.md @@ -175,7 +175,7 @@ request, to another check that proves the same thing; loosening a requirement or | Routines on a schedule | [test_routines.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_routines.py) `test_routines_are_checked_and_run_when_due`, `test_schedules`; [journeys.mjs](https://github.com/adeelahmad/lense/blob/main/ci/clicks/journeys.mjs) `routine-create`; [baseline.json](https://github.com/adeelahmad/lense/blob/main/ci/clicks/baseline.json) `routine-create` ≤ 2 clicks | | Batch runs over a collection | [test_batches.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_batches.py) `test_collections_and_batches` | | Notifications to webhooks, Slack, Discord and Matterbridge | [test_notifications.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_notifications.py) `test_owners_manage_targets_and_a_test_is_signed`, `test_runs_that_end_are_sent_once_to_matterbridge` | -| Everything that happens is kept as W3C Activity Streams activities, read by agents through the API, MCP and the assistant | [test_events.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_events.py) `test_activities_have_one_shape`; [test_activities.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_activities.py) `test_the_timeline_lists_new_assets_newest_first`, `test_agents_read_the_timeline_too` | +| Everything that happens is kept as W3C Activity Streams activities, read by agents through the API, MCP and the assistant | [test_events.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_events.py) `test_activities_have_one_shape`; [test_activities.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_activities.py) `test_the_timeline_lists_new_assets_newest_first`, `test_agents_read_the_timeline_too`; [test_news_feed_source.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_news_feed_source.py) `test_a_news_feed_as_a_source` | | The Timeline page and Latest on Home, marking what you've seen | [test_timeline_marks.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_timeline_marks.py) `test_marks_are_set_cleared_and_filter_the_timeline`; [timeline-ui.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-ui.test.tsx) `Home's Latest card lists what you haven't reviewed` | | Automations act on the stream, on a schedule, at a time or when their own webhook is called | [test_automation_runs.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_automation_runs.py) `test_each_new_activity_runs_each_automation_it_matches_once`, `test_a_schedule_runs_its_automation_when_its_timer_fires`, `test_a_call_to_a_webhook_runs_its_automation`; [test_timer_triggers.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_timer_triggers.py) `test_an_at_trigger_fires_once_then_reads_done` | | They test conditions, then tell you, ask the model or the agent, use a tool, call a web address with sealed credentials, run Lens work, or change assets and notes | [test_automation_runs.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_automation_runs.py) `test_the_if_limits_and_dry_runs`; [test_actions_messages.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_actions_messages.py) `test_notify_tells_me_in_my_chat_app`; [test_actions_ai.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_actions_ai.py) `test_ask_model_answers_in_text_or_as_the_fields_a_schema_names`, `test_ask_agent_works_on_what_happened_and_saves_the_conversation`, `test_call_tool_reads_as_the_person_and_changes_only_by_asking_them`; [test_actions_web.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_actions_web.py) `test_credentials_in_headers_are_sealed_to_the_automation_and_the_host_and_never_shown`; [test_actions_run.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_actions_run.py) `test_run_workflow_queues_it_on_the_asset_as_the_automation`, `test_run_pipeline_processes_the_asset_again`; [test_actions_lens.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_actions_lens.py) `test_set_field_is_kept_in_the_assets_history_by_the_automation`, `test_write_note_writes_a_page_adds_to_a_things_page_or_notes_an_asset` | diff --git a/fastapi_backend/app/domain/events_content.py b/fastapi_backend/app/domain/events_content.py index 4ab39e982..b69f27868 100644 --- a/fastapi_backend/app/domain/events_content.py +++ b/fastapi_backend/app/domain/events_content.py @@ -3,8 +3,8 @@ nothing is stored twice. Each new asset is one activity, `create-recording-`, the same every time it's read, of the most specific kind its -row tells: an email received from a mailbox (`email.received`), a calendar event (`calendar_event.added`), a file -from storage or Drive (`file.added`), an upload (`upload.added`), a page or video saved from the web +row tells: an email received from a mailbox (`email.received`), a calendar event (`calendar_event.added`), a news +feed's post (`feed_post.received`), a file from storage or Drive (`file.added`), an upload (`upload.added`), a page or video saved from the web (`web_page.saved`), something imported (pasted text, a transcript, an import hook's: `import.received`), else `asset.added`, their parent. The kind is read from the row's content type, where it came from and who brought it, never from its file. @@ -14,8 +14,8 @@ scan). Labels like `watch:`, a person's email or `routine:` are looked up a page at a time, a few queries for all of them, and their names kept for NAME_SECONDS (`actors`), so other sources name their actors the same way. -Mail addressing and an event's times are stored on the recording when it's made (`mail`, `event`: sources.py) and -read from there; an email's own details (`email`: subject, who, a snippet, its attachments) once it's been read. +Mail addressing, an event's times and a feed post's address and author are stored on the recording when it's made +(`mail`, `event`, `post`: sources.py) and read from there; an email's own details (`email`: subject, who, a snippet, its attachments) once it's been read. Older rows without them are read without those fields. """ @@ -36,7 +36,7 @@ R = store.R FIELDS = ( "id AS rid, record::id(id) AS id, space, title, source, media, remote, content_type, created_at, path, web, engine, " - "created_by, mail, event, email, size, duration_ms, attached_to, sensor, form" + "created_by, mail, event, post, email, size, duration_ms, attached_to, sensor, form" ) TYPES = {"audio": "Audio", "video": "Video", "image": "Image", "document": "Document", "transcript": "Document"} AUTOMATION_RX = re.compile(r"^automation:(\d+)$") @@ -46,6 +46,7 @@ UPLOAD_RX = re.compile(r"/uploads/[^/]+/[0-9a-f]{24}/[^/]+$") # uploads.py keeps each upload in // MAIL_SOURCES = {"imap", "gmail"} CALENDAR_SOURCES = {"ical", "gcal"} +FEED_SOURCES = {"rss"} NAME_SECONDS = 30 # how long a looked-up name is used before it's read again APPS = { "assistant": {"type": "Application", "id": "lens:assistant", "name": "Assistant"}, @@ -287,6 +288,23 @@ def _event(r): ) +def _feed_post(r): + """A feed post's fields: its address, site, feed, author, date and categories, as its feed listed them (`post`).""" + p = r.get("post") if isinstance(r.get("post"), dict) else {} + href = str(p.get("href") or "")[:2000] + site = urllib.parse.urlsplit(href or str(p.get("site") or "")).hostname + return store.clean( + { + "href": href or None, + "site": site or None, + "feed": _text(p.get("feed"), 300), + "author": _text(p.get("author"), 200), + "published": p.get("published"), + "categories": [str(c)[:100] for c in p.get("categories") or []][:10] or None, + } + ) + + def _file(r, kind): """A file's fields: its name in its source (or the name it was uploaded with), media type, size and length.""" remote = r.get("remote") or {} @@ -323,6 +341,8 @@ def _kind(r, ctype, source_type, actor): return "email.received" if ctype == "calendar_event" or source_type in CALENDAR_SOURCES: return "calendar_event.added" + if ctype == "feed_post" or source_type in FEED_SOURCES: + return "feed_post.received" if actor["id"].startswith("lens:import_hook"): return "import.received" if (r.get("web") or {}).get("url"): @@ -344,6 +364,8 @@ def _summary(kind, actor, obj): if kind == "calendar_event.added": start = str(obj.get("startTime") or "") return f"{who} added the event “{title}”" + (f" on {start[:10]}" if start else "") + if kind == "feed_post.received": + return f"{who} received the post “{title}”" + (f" from {obj['site']}" if obj.get("site") else "") if kind == "upload.added": return f"{who} uploaded “{title}”" if kind == "web_page.saved": @@ -415,6 +437,8 @@ def _activities(db, rows): obj.update(_mail(r)) elif kind == "calendar_event.added": obj.update(_event(r)) + elif kind == "feed_post.received": + obj.update(_feed_post(r)) elif kind in ("file.added", "upload.added"): obj.update(_file(r, kind)) elif kind == "web_page.saved": @@ -614,6 +638,33 @@ def _child(name, label, props, untrusted, sample_actor, sample_obj, summary): }, "Family calendar added the event “Parents' evening” on 2026-10-20", ) +_child( + "feed_post.received", + "Feed post received", + { + "href": _s("Post address"), + "site": _s("Site"), + "feed": _s("Feed"), + "author": _s("Author"), + "published": _s("Published", format="date-time"), + "categories": {"type": "array", "title": "Categories", "items": {"type": "string", "title": "Category"}}, + }, + ("object.name", "object.href", "object.site", "object.feed", "object.author", "object.categories"), + {"type": "Service", "id": "lens:source/15", "name": "Harbour news"}, + { + "type": "Document", + "name": "Autumn regatta", + "media": "transcript", + "content_type": "feed_post", + "href": "https://harbour.example.org/posts/regatta", + "site": "harbour.example.org", + "feed": "Harbour news", + "author": "Mara Keane", + "published": "2026-10-15T08:00:00+00:00", + "categories": ["Harbour"], + }, + "Harbour news received the post “Autumn regatta” from harbour.example.org", +) _child( "file.added", "File added", diff --git a/fastapi_backend/app/domain/rss.py b/fastapi_backend/app/domain/rss.py index a8c0ded65..93768f67e 100644 --- a/fastapi_backend/app/domain/rss.py +++ b/fastapi_backend/app/domain/rss.py @@ -228,11 +228,27 @@ def parse(raw, base, ctype=None): "title": p["title"] or None, "when": p["published"] or p["updated"], "settled": True, # a post is whole once its feed lists it: no waiting for it to settle + "post": _listed(p, title, site), } posts[rel] = (entry, body) return {"title": title, "site": site, "posts": posts} +def _listed(p, feed_title, site): + """What a listing says of a post beside its file, kept on its resource as `post` (sources._post_of) for automations + and the timeline: its address, the feed and site it came from, who wrote it, when, and its categories.""" + who = next((a["name"] or a["email"] for a in p["authors"] if a["name"] or a["email"]), None) + out = { + "href": p["page"], + "feed": feed_title, + "site": site, + "author": who, + "published": p["published"] or p["updated"], + "categories": p["categories"][:10], + } + return {k: v for k, v in out.items() if v} + + def _iso(t): try: return dt.datetime.fromtimestamp(calendar.timegm(t), dt.UTC).isoformat(timespec="seconds") if t else None diff --git a/fastapi_backend/app/domain/sources.py b/fastapi_backend/app/domain/sources.py index 8060f96d5..23d6b7784 100644 --- a/fastapi_backend/app/domain/sources.py +++ b/fastapi_backend/app/domain/sources.py @@ -618,6 +618,7 @@ def _ingest(db, cfg, src, f, kind, space, by, steps=None, pipeline=None, collect "recorded_at": f.get("when"), "mail": _mail_of(f), "event": event, + "post": _post_of(f), } ), ) @@ -674,6 +675,18 @@ def _mail_of(f): return {k: v for k, v in out.items() if v} or None +def _post_of(f): + """What a news feed said of a post as it was listed (rss._listed: its address, feed, site, author, date and + categories), kept small on its resource as `post` for automations and the timeline (events_content.py). None for + a file that isn't a feed's post.""" + p = f.get("post") + if not isinstance(p, dict): + return None + out = {k: str(p.get(k) or "")[: 2000 if k == "href" else 300] for k in ("href", "feed", "site", "author", "published")} + out["categories"] = [str(c)[:100] for c in p.get("categories") or []][:10] + return {k: v for k, v in out.items() if v} or None + + def _event_of(local, f, src): """A calendar event's times, place and people (calendars.details), kept on its resource as `event`; None for a file that isn't an event, or one that can't be read.""" diff --git a/fastapi_backend/tests/api/test_news_feed_source.py b/fastapi_backend/tests/api/test_news_feed_source.py index ead2c63f0..5f8f1cf25 100644 --- a/fastapi_backend/tests/api/test_news_feed_source.py +++ b/fastapi_backend/tests/api/test_news_feed_source.py @@ -10,7 +10,7 @@ import pytest -from app.domain import content_types, feeds, netguard, rss, sources, store +from app.domain import content_types, events, feeds, netguard, rss, sources, store from tests.helpers import drain, login, make_user R = store.R @@ -163,6 +163,21 @@ def test_a_news_feed_as_a_source(site, client, db, cfg, admin): # site first: i types = content_types.all_types(db) rec = db.one("SELECT source, path, title, media FROM $r", r=R("recording", one["id"])) assert content_types.recognise(db, rec, types)["key"] == "feed_post" + # an activity automations can start on: the post's address, feed and author, its text marked as outside text + a = events.get(db, cfg, f"create-recording-{one['id']}", events.viewer(1, None, admin=True)) + assert a["kind"] == "feed_post.received" and events.check_activity(a) + assert a["actor"] == {"type": "Service", "id": f"lens:source/{sid}", "name": "harbour"} + o = a["object"] + assert (o["href"], o["site"], o["feed"], o["author"], o["published"], o["categories"]) == ( + "https://harbour.example.org/posts/night", + "harbour.example.org", + "Harbour news", + "Mara Keane", + "2026-09-29T08:30:00+00:00", + ["Harbour"], + ) + assert a["summary"] == "harbour received the post “Night crossing” from harbour.example.org" + assert {"object.name", "object.href", "object.feed", "object.author"} <= set(a["lens"]["untrusted"]) # asked again once the copy read is old: it hasn't changed (304), so nothing is new and nothing is parsed _stale() diff --git a/nextjs-frontend/components/timeline/model.ts b/nextjs-frontend/components/timeline/model.ts index 4516416f7..ab8816349 100644 --- a/nextjs-frontend/components/timeline/model.ts +++ b/nextjs-frontend/components/timeline/model.ts @@ -210,6 +210,7 @@ const NOUNS: Record = { "email.received": ["received", "email", "emails"], "calendar_event.added": ["added", "calendar event", "calendar events"], "calendar_event.changed": ["changed", "calendar event", "calendar events"], + "feed_post.received": ["received", "post", "posts"], "file.added": ["added", "file", "files"], "upload.added": ["uploaded", "file", "files"], "web_page.saved": ["saved", "web page", "web pages"],