From 1cac4b25047bee5f616b310efe8bc25ba1169125 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Thu, 15 Oct 2026 11:15:23 +0000 Subject: [PATCH 1/2] feat(notes): find a note by its words, on the Notes page and in Jump to The Notes page gets a search box (title, summary and text, the pages of things too, with the words around each match marked; Enter opens the first), a Projects / Areas / Resources / Archive filter with counts, and the notes each note sits inside. Jump to (Ctrl+K) finds notes as well. GET /api/v1/notes/search serves both, and find_notes gives agents the words around each match and the note it's inside. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01RrY2byt5BHVeBXJUyfwWpb --- CHANGELOG.md | 6 + docs/api.md | 4 +- docs/mcp.md | 2 +- docs/notes.md | 13 + fastapi_backend/app/api/mcp_tools.py | 6 +- fastapi_backend/app/api/v1/routes/notebook.py | 25 ++ fastapi_backend/app/domain/ai_tools.py | 2 + fastapi_backend/app/domain/notebook.py | 31 ++- fastapi_backend/app/schemas/notebook.py | 18 ++ fastapi_backend/tests/api/test_mcp.py | 1 + fastapi_backend/tests/api/test_notebook.py | 34 +++ nextjs-frontend/__tests__/notes-find.test.tsx | 122 +++++++++ nextjs-frontend/app/openapi-client/index.ts | 7 + nextjs-frontend/app/openapi-client/sdk.gen.ts | 18 ++ .../app/openapi-client/types.gen.ts | 124 +++++++++ .../components/app-shell/command-palette.tsx | 38 ++- .../components/notes/notes-home.tsx | 216 +++++++++++++--- nextjs-frontend/lib/hooks/debounce.ts | 11 + nextjs-frontend/openapi.json | 244 ++++++++++++++++++ 19 files changed, 876 insertions(+), 46 deletions(-) create mode 100644 nextjs-frontend/__tests__/notes-find.test.tsx create mode 100644 nextjs-frontend/lib/hooks/debounce.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index c80f9ecb3..25d7e4d6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version ## Unreleased +- **Find a note.** The Notes page has a search box that finds notes by the words in their title, summary or text (the + pages of recordings and entities too), with the words around each match marked, and Enter opens the first. A filter + shows only Projects, Areas, Resources or Archive, and each note says which notes it's inside. Jump to (⌘K) finds notes + as well. `GET /api/v1/notes/search`; `find_notes` gives agents the words around each match + ([Finding notes](docs/notes.md#finding-notes)). + ## 0.10.0 October 15, 2026 {id="0.10.0"} - **The assistant always answers.** When a question used up its tool steps (`ai.max_steps`), the assistant used to stop diff --git a/docs/api.md b/docs/api.md index eab222305..1ec2dc8e6 100644 --- a/docs/api.md +++ b/docs/api.md @@ -628,6 +628,7 @@ deleted. ``` GET /api/v1/notes POST /api/v1/notes +GET /api/v1/notes/search GET /api/v1/notes/{pid} PATCH /api/v1/notes/{pid} DELETE /api/v1/notes/{pid} @@ -642,7 +643,8 @@ DELETE /api/v1/notes/{pid}/files ``` Note pages ([Notes](notes.md)), apart from the notes on a recording above. `GET /notes?ns=` is a namespace's page tree -(`all=true` adds the pages of things). `move` puts a free note inside `parent` before `before`, `history` lists what the +(`all=true` adds the pages of things). `search?q=` finds pages by the words in their title, summary or text, title +matches first, each with the words around the match ([Finding notes](notes.md#finding-notes)). `move` puts a free note inside `parent` before `before`, `history` lists what the page was before each change, and `restore` puts an earlier version back (what it was becomes a version too). `files` lists the files and images attached to a page; `blobs` uploads (in chunks) and reads them, and `DELETE …/files?key=` removes one. diff --git a/docs/mcp.md b/docs/mcp.md index 9b8e56ab7..01d347442 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -63,7 +63,7 @@ front of the web app also needs `TRUST_PROXY_HEADERS=true` ([Authentication](aut | `list_topics` | each namespace's vocabulary of topics ([Topics](topics.md)), by any label, top topics or the narrower ones of a topic | | `get_topic` | one topic: its labels, definition, broader, narrower and related topics, and the recordings about it, with links | | `suggest_topic` | suggest that recordings are about a topic; it waits for someone to accept it. Needs a write-scope token and editor access | -| `find_notes` | [notes](notes.md) by words in their title, summary or text, or by where they're filed (PARA), with links | +| `find_notes` | [notes](notes.md) by words in their title, summary or text, or by where they're filed (PARA), with the words around the match and links | | `read_note` | a note's Markdown with its links and backlinks, by id or as the page of a thing (`recording:12`) | | `write_note` | write a note or a thing's page, marked as written by an assistant. Needs a write-scope token and editor access | | `create_podcast` | make a two-host learning [episode](podcasts.md) about recordings or lines in them, with an angle, a length and a style; it's made in the background. Needs a write-scope token and editor access where it goes | diff --git a/docs/notes.md b/docs/notes.md index c385f796e..ab894df54 100644 --- a/docs/notes.md +++ b/docs/notes.md @@ -97,6 +97,19 @@ refining it, or someone else always starts a new one. **History** on a page list it; pick one to read it, and editors can restore it. Restoring keeps the page as it was too, so it can be undone. `GET /api/v1/notes/{id}/history`, `GET .../history/{version}` and `POST .../history/{version}/restore`. +## Finding notes + +The **Notes** page lists the free notes in view, the latest changed first, each with the notes it sits inside +("In Home lab › Parts"), where it's filed and when it changed. The box at the top finds notes by the words in their +title, one-line summary or text, the pages of recordings, entities and topics too: those with the words in the title +come first, each with the words around the match, and Enter opens the first. **Projects**, **Areas**, **Resources** and +**Archive** show only the notes filed there, with how many each has. **Jump to** (⌘K or Ctrl+K) finds notes the same +way, beside recordings, pages and settings. + +`GET /api/v1/notes/search?q=&ns=&place=&free=` is the same search, in every namespace you can read unless `ns` names one; +`free=true` leaves out the pages of things. The assistant's and MCP's `find_notes` give the same words around the match +(`snippet`) and the note each is inside (`inside`). + ## Access Pages belong to a namespace: anyone with a role there reads them, editors write, move and delete them. The pages of diff --git a/fastapi_backend/app/api/mcp_tools.py b/fastapi_backend/app/api/mcp_tools.py index 3b1386cf1..8a3629324 100644 --- a/fastapi_backend/app/api/mcp_tools.py +++ b/fastapi_backend/app/api/mcp_tools.py @@ -1452,8 +1452,8 @@ def _note_url(ctx: Context, pid: int) -> str: "Find notes", "Notes in the namespaces you can read: free notes and the pages of recordings, entities, topics, collections and " "speakers, found by words in their title, one-line summary or text (title matches first, then the latest). Each " - "has its id, one-line summary, where it is filed (PARA: project, area, resource, archive), what it is the page of, " - "and a url.", + "has its id, one-line summary, the words around the match in its text, where it is filed (PARA: project, area, " + "resource, archive), the note it is inside, what it is the page of, and a url.", Arg("query", "string", "words to look for", max_length=200), Arg("namespace", "string", "only this namespace"), Arg("place", "string", "only notes filed here", enum=notebook.PLACES), @@ -1474,6 +1474,8 @@ def find_notes(ctx: Context, query: str | None, namespace: str | None, place: st "place": r.get("place"), "page_of": r.get("about"), "namespace": names.get(r["space"]), + "inside": r.get("parent"), + "snippet": r.get("snippet"), "updated": r.get("updated_at"), "url": _note_url(ctx, r["id"]), } diff --git a/fastapi_backend/app/api/v1/routes/notebook.py b/fastapi_backend/app/api/v1/routes/notebook.py index ed181025b..e63ac981f 100644 --- a/fastapi_backend/app/api/v1/routes/notebook.py +++ b/fastapi_backend/app/api/v1/routes/notebook.py @@ -24,6 +24,7 @@ from app.schemas.notebook import ( NoteFile, NoteHistory, + NoteHit, NoteHomeSuggestion, NoteLinkSuggestion, NoteLinkTarget, @@ -33,9 +34,11 @@ NotePageItem, NotePageMove, NotePageUpdate, + NoteSearch, NoteTree, NoteVersion, NoteVersionItem, + Place, ) router = APIRouter(prefix="/notes", tags=["notes"]) @@ -118,6 +121,28 @@ def link_targets( return [NoteLinkTarget(**t) for t in notebook.search_targets(db, sid, q, sign, limit)] +@router.get("/search") +def search_pages( + user: CurrentUser, + acl: Acl, + db: Db, + q: str = Query("", max_length=200, description="words in the title, one-line summary or text"), + ns: str | None = Query(None, description="only this namespace (default: every one you can read)"), + place: Place | None = Query(None, description="only notes filed here"), + free: bool = Query(False, description="only free notes, not the pages of things"), + limit: int = Query(50, ge=1, le=200), +) -> NoteSearch: + """Pages found by their words, those with them in the title first, then the latest changed, each with the words + around the match: what the Notes page and Jump to show, and what the assistant's find_notes reads.""" + names = store.space_names(db) + spaces = [acl.namespace(ns)] if ns else (list(names) if user.admin else acl.spaces()) + total, hits = notebook.find(db, spaces, q, place, limit, free=free) + return NoteSearch( + total=total, + pages=[NoteHit(**{k: v for k, v in h.items() if k != "space"}, namespace=names.get(h["space"], "")) for h in hits], + ) + + @router.get("/about/{kind}/{key}") def page_about(kind: str, key: int, user: CurrentUser, acl: Acl, db: Db) -> NotePage | NotePageDraft: """The page of a recording, entity, topic, collection or speaker: its page, or a draft while nobody has written one.""" diff --git a/fastapi_backend/app/domain/ai_tools.py b/fastapi_backend/app/domain/ai_tools.py index 041c1dff8..51a276ce3 100644 --- a/fastapi_backend/app/domain/ai_tools.py +++ b/fastapi_backend/app/domain/ai_tools.py @@ -1138,6 +1138,8 @@ def t_find_notes(self, query=None, namespace=None, place=None, limit=10): "place": r.get("place"), "page_of": r.get("about"), "namespace": names.get(r["space"]), + "inside": r.get("parent"), + "snippet": r.get("snippet"), } ) for r in hits diff --git a/fastapi_backend/app/domain/notebook.py b/fastapi_backend/app/domain/notebook.py index 6a96ff379..fe77afa69 100644 --- a/fastapi_backend/app/domain/notebook.py +++ b/fastapi_backend/app/domain/notebook.py @@ -681,23 +681,46 @@ def labels(db, targets): return out -def find(db, spaces, query="", place=None, limit=10): +SNIPPET = 160 + + +def snippet(text, query, size=SNIPPET): + """The words around the first place the text has the query, on one line, with … where it was cut; None without.""" + text, q = " ".join(str(text or "").split()), " ".join(str(query or "").split()) + at = text.casefold().find(q.casefold()) if q else -1 + if at < 0: + return None + start = max(0, at - size // 3) + if start: + cut = text.find(" ", start) + start = cut + 1 if 0 <= cut < at else start + end = min(len(text), start + size) + if end < len(text): + cut = text.rfind(" ", at + len(q), end) + end = cut if cut > 0 else end + return ("…" if start else "") + text[start:end] + ("…" if end < len(text) else "") + + +def find(db, spaces, query="", place=None, limit=10, free=False): """Pages in these namespaces whose title, summary or text has the words, those with them in the title first, then - the latest changed. Returns (how many matched, the first `limit` without their bodies).""" + the latest changed; only free notes with `free`. Returns (how many matched, the first `limit` without their bodies, + each with the words around the match in its text as `snippet`).""" q = " ".join(str(query or "").split()).casefold() rows = db.rows( - "SELECT record::id(id) AS id, space, title, summary, place, about, updated_at, text FROM note_page WHERE space IN $s", + "SELECT record::id(id) AS id, space, title, summary, place, parent, about, author, updated_at, text FROM note_page " + "WHERE space IN $s", s=list(spaces), ) hits = [ r for r in rows if (not place or r.get("place") == place) + and (not free or not r.get("about")) and (not q or any(q in str(r.get(k) or "").casefold() for k in ("title", "summary", "text"))) ] hits.sort(key=lambda r: r.get("updated_at") or "", reverse=True) hits.sort(key=lambda r: q not in str(r["title"]).casefold()) - return len(hits), [{k: v for k, v in r.items() if k != "text"} for r in hits[:limit]] + return len(hits), [{**{k: v for k, v in r.items() if k != "text"}, "snippet": snippet(r.get("text"), q)} for r in hits[:limit]] def search_targets(db, sid, q="", sign="@", limit=20): diff --git a/fastapi_backend/app/schemas/notebook.py b/fastapi_backend/app/schemas/notebook.py index 19ddfb95b..e62e68eee 100644 --- a/fastapi_backend/app/schemas/notebook.py +++ b/fastapi_backend/app/schemas/notebook.py @@ -98,6 +98,24 @@ class NoteTree(ResponseModel): pages: list[NotePageItem] +class NoteHit(ResponseModel): + id: int + title: str + summary: str | None = None + place: Place | None = None + parent: int | None = Field(None, description="the page it's inside, in the tree") + about: str | None = Field(None, description='what this is the page of, like "recording:12"; null for a free note') + author: Literal["person", "assistant"] = "person" + updated_at: str | None = None + namespace: str + snippet: str | None = Field(None, description="the words around the match in its text, when the text has it") + + +class NoteSearch(ResponseModel): + total: int = Field(description="how many pages match; `pages` has the first `limit`") + pages: list[NoteHit] + + class NotePageCreate(RequestModel): ns: str = Field(description="the namespace it goes in") title: str = Field(min_length=1, max_length=200) diff --git a/fastapi_backend/tests/api/test_mcp.py b/fastapi_backend/tests/api/test_mcp.py index 206c1d60e..78c28eb90 100644 --- a/fastapi_backend/tests/api/test_mcp.py +++ b/fastapi_backend/tests/api/test_mcp.py @@ -472,6 +472,7 @@ def test_note_tools(client, db, env): found = tool(client, h, "find_notes", query="capsid") assert [(n["note_id"], n["summary"], n["place"]) for n in found["notes"]] == [(plan, "Shipping capsids", "project")] assert found["notes"][0]["url"].endswith(f"/notes/{plan}") + assert tool(client, h, "find_notes", query="friday")["notes"][0]["snippet"] == "Ship on Friday, see ep1." assert tool(client, h, "find_notes", place="area")["total"] == 0 got = tool(client, h, "read_note", note_id=plan) assert got["body"].startswith("Ship on Friday") and got["links"] == [{"target": f"recording:{a}", "name": got["links"][0]["name"]}] diff --git a/fastapi_backend/tests/api/test_notebook.py b/fastapi_backend/tests/api/test_notebook.py index f77f39455..247ac3ebe 100644 --- a/fastapi_backend/tests/api/test_notebook.py +++ b/fastapi_backend/tests/api/test_notebook.py @@ -167,6 +167,40 @@ def test_domain_helpers(): assert notebook.plain(body) == "Hi Ada and Caps, Ada twice, x\n\nsite" +def test_search(client, env, db): + he, hv, hx = env["he"], env["hv"], env["hx"] + plans = _new(client, he, title="Plans", place="project", body="Hire two people.") + _new(client, he, title="Hiring", parent=plans["id"], body="We talk to " + "lots of people " * 20 + "about capsids today.") + _new(client, he, title="Old trip", place="archive", body="Lisbon") + r = client.get("/api/v1/notes/search?q=capsids", headers=hv) + assert r.status_code == 200, r.text + out = r.json() + assert out["total"] == 1 + hit = out["pages"][0] + assert (hit["title"], hit["parent"], hit["namespace"], hit["author"]) == ("Hiring", plans["id"], "pods", "person") + assert hit["snippet"].startswith("…") and hit["snippet"].endswith("about capsids today.") + # title matches first; a place narrows it; no words: everything, the latest changed first + assert [p["title"] for p in client.get("/api/v1/notes/search?q=hir", headers=hv).json()["pages"]] == ["Hiring", "Plans"] + assert [p["title"] for p in client.get("/api/v1/notes/search?place=archive", headers=hv).json()["pages"]] == ["Old trip"] + assert client.get("/api/v1/notes/search?ns=pods", headers=hv).json()["total"] == 3 + # a thing's page is found too, unless only free notes are asked for + _new(client, he, title="Episode 1", about=f"recording:{env['a']}", body="capsids again") + assert client.get("/api/v1/notes/search?q=capsids", headers=hv).json()["total"] == 2 + assert client.get("/api/v1/notes/search?q=capsids&free=true", headers=hv).json()["total"] == 1 + # people outside the namespace find nothing in it + assert client.get("/api/v1/notes/search?q=capsids", headers=hx).json()["total"] == 0 + assert client.get("/api/v1/notes/search?ns=pods", headers=hx).status_code == 404 + + +def test_snippet(): + assert notebook.snippet("one two three", "four") is None + assert notebook.snippet("one two three", "") is None + assert notebook.snippet("a short\ntext", "short") == "a short text" + long = " ".join(f"w{i}" for i in range(200)) + s = notebook.snippet(long, "w100") + assert s.startswith("…w") and s.endswith("…") and "w100" in s and len(s) <= notebook.SNIPPET + 2 + + @pytest.fixture def llm(cfg): from tests import fake_llm diff --git a/nextjs-frontend/__tests__/notes-find.test.tsx b/nextjs-frontend/__tests__/notes-find.test.tsx new file mode 100644 index 000000000..86a380760 --- /dev/null +++ b/nextjs-frontend/__tests__/notes-find.test.tsx @@ -0,0 +1,122 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import { Notes } from "@/app/openapi-client"; +import { CommandPalette } from "@/components/app-shell/command-palette"; +import { NotesHome } from "@/components/notes/notes-home"; + +jest.mock("@/app/openapi-client", () => ({ + Notes: { listPages: jest.fn(), searchPages: jest.fn(), createPage: jest.fn() }, + Resources: { listRecordings: jest.fn(() => Promise.resolve({ data: [], response: { ok: true, status: 200 } })) }, +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +const push = jest.fn(); +jest.mock("next/navigation", () => ({ usePathname: () => "/notes", useRouter: () => ({ push }) })); +jest.mock("@/components/ui/toast", () => ({ useToast: () => jest.fn() })); +jest.mock("@/lib/hooks/session", () => ({ + useArchive: () => ({ + admin: false, + namespaces: [{ name: "home" }], + namespace: "home", + can: () => true, + }), +})); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const m = (f: unknown) => f as jest.Mock; +const show = (ui: React.ReactNode) => + render( + + {ui} + , + ); + +const page = (id: number, title: string, extra: Record = {}) => ({ + id, + title, + author: "person", + updated_at: `2026-10-${10 + id}T10:00:00Z`, + ...extra, +}); + +beforeEach(() => { + jest.clearAllMocks(); + m(Notes.listPages).mockImplementation(() => + ok({ + namespace: "home", + pages: [ + page(1, "Home lab", { place: "project" }), + page(2, "Pi 5 shopping list", { parent: 1 }), + page(3, "NVMe hat options", { parent: 2 }), + page(4, "Old trip", { place: "archive" }), + ], + }), + ); +}); + +describe("the Notes page", () => { + it("shows where each note sits in the tree, and one PARA place at a time", async () => { + show(); + const hat = await screen.findByRole("link", { name: /NVMe hat options/ }); + expect(hat).toHaveTextContent("In Home lab › Pi 5 shopping list"); + expect(hat).toHaveAttribute("href", "/notes/3"); + fireEvent.click(screen.getByRole("radio", { name: /Archive/ })); + expect(screen.getAllByRole("listitem").map((li) => li.textContent)).toEqual([expect.stringContaining("Old trip")]); + fireEvent.click(screen.getByRole("radio", { name: /Areas/ })); + expect(screen.getByText("No notes here")).toBeInTheDocument(); + }); + + it("finds notes by their words, with the words around the match, and Enter opens the first", async () => { + m(Notes.searchPages).mockImplementation(() => + ok({ + total: 1, + pages: [ + { + id: 9, + title: "Weekly call", + about: "entity:5", + namespace: "home", + author: "assistant", + snippet: "…we talked about capsids…", + }, + ], + }), + ); + show(); + const box = await screen.findByRole("searchbox", { name: "Search notes" }); + fireEvent.change(box, { target: { value: "capsids" } }); + const hit = await screen.findByRole("link", { name: /Weekly call/ }); + expect(hit).toHaveTextContent("Page of entity"); + expect(hit).toHaveTextContent("…we talked about capsids…"); + expect(hit).toHaveAttribute("href", "/notes/about/entity/5"); + expect(m(Notes.searchPages).mock.calls.at(-1)[0].query).toMatchObject({ q: "capsids", ns: "home", place: null }); + fireEvent.keyDown(box, { key: "Enter" }); + expect(push).toHaveBeenCalledWith("/notes/about/entity/5"); + }); + + it("says when no note has the words", async () => { + m(Notes.searchPages).mockImplementation(() => ok({ total: 0, pages: [] })); + show(); + fireEvent.change(await screen.findByRole("searchbox", { name: "Search notes" }), { target: { value: "zebra" } }); + expect(await screen.findByText("No notes have “zebra”")).toBeInTheDocument(); + }); +}); + +describe("⌘K", () => { + it("jumps to a note found by its words", async () => { + m(Notes.searchPages).mockImplementation(() => + ok({ total: 1, pages: [{ id: 2, title: "Pi 5 shopping list", namespace: "home", place: "project" }] }), + ); + show(); + act(() => { + fireEvent.keyDown(window, { key: "k", ctrlKey: true }); + }); + fireEvent.change(screen.getByRole("combobox"), { target: { value: "shopping" } }); + const option = await screen.findByRole("option", { name: /Pi 5 shopping list/ }); + expect(within(option).getByText("Project")).toBeInTheDocument(); + fireEvent.click(option); + expect(push).toHaveBeenCalledWith("/notes/2"); + await waitFor(() => expect(screen.queryByRole("combobox")).not.toBeInTheDocument()); + }); +}); diff --git a/nextjs-frontend/app/openapi-client/index.ts b/nextjs-frontend/app/openapi-client/index.ts index af490230d..c5089f60c 100644 --- a/nextjs-frontend/app/openapi-client/index.ts +++ b/nextjs-frontend/app/openapi-client/index.ts @@ -2013,6 +2013,7 @@ export type { NoteCreate, NoteFile, NoteHistory, + NoteHit, NoteHomeSuggestion, NoteLink, NoteLinkSuggestion, @@ -2023,6 +2024,7 @@ export type { NotePageItem, NotePageMove, NotePageUpdate, + NoteSearch, NoteTree, NoteUpdate, NoteVersion, @@ -2610,6 +2612,11 @@ export type { SearchCreate, SearchFacets, SearchHit, + SearchPagesData, + SearchPagesError, + SearchPagesErrors, + SearchPagesResponse, + SearchPagesResponses, SearchPhotosData, SearchPhotosError, SearchPhotosErrors, diff --git a/nextjs-frontend/app/openapi-client/sdk.gen.ts b/nextjs-frontend/app/openapi-client/sdk.gen.ts index e3bfc4787..4f8b95d2a 100644 --- a/nextjs-frontend/app/openapi-client/sdk.gen.ts +++ b/nextjs-frontend/app/openapi-client/sdk.gen.ts @@ -1277,6 +1277,9 @@ import type { SealVaultData, SealVaultErrors, SealVaultResponses, + SearchPagesData, + SearchPagesErrors, + SearchPagesResponses, SearchPhotosData, SearchPhotosErrors, SearchPhotosResponses, @@ -5113,6 +5116,21 @@ export class Notes { }); } + /** + * Search Pages + * + * Pages found by their words, those with them in the title first, then the latest changed, each with the words + * around the match: what the Notes page and Jump to show, and what the assistant's find_notes reads. + */ + public static searchPages( + options?: Options, + ): RequestResult { + return (options?.client ?? client).get({ + url: "/api/v1/notes/search", + ...options, + }); + } + /** * Page About * diff --git a/nextjs-frontend/app/openapi-client/types.gen.ts b/nextjs-frontend/app/openapi-client/types.gen.ts index b0d2a2f70..23c21877e 100644 --- a/nextjs-frontend/app/openapi-client/types.gen.ts +++ b/nextjs-frontend/app/openapi-client/types.gen.ts @@ -9815,6 +9815,59 @@ export type NoteHistory = { [key: string]: unknown; }; +/** + * NoteHit + */ +export type NoteHit = { + /** + * Id + */ + id: number; + /** + * Title + */ + title: string; + /** + * Summary + */ + summary?: string | null; + /** + * Place + */ + place?: "project" | "area" | "resource" | "archive" | null; + /** + * Parent + * + * the page it's inside, in the tree + */ + parent?: number | null; + /** + * About + * + * what this is the page of, like "recording:12"; null for a free note + */ + about?: string | null; + /** + * Author + */ + author?: "person" | "assistant"; + /** + * Updated At + */ + updated_at?: string | null; + /** + * Namespace + */ + namespace: string; + /** + * Snippet + * + * the words around the match in its text, when the text has it + */ + snippet?: string | null; + [key: string]: unknown; +}; + /** * NoteHomeSuggestion */ @@ -10284,6 +10337,23 @@ export type NotePageUpdate = { view?: "page" | "edgeless" | null; }; +/** + * NoteSearch + */ +export type NoteSearch = { + /** + * Total + * + * how many pages match; `pages` has the first `limit` + */ + total: number; + /** + * Pages + */ + pages: Array; + [key: string]: unknown; +}; + /** * NoteTree */ @@ -26585,6 +26655,60 @@ export type LinkTargetsResponses = { export type LinkTargetsResponse = LinkTargetsResponses[keyof LinkTargetsResponses]; +export type SearchPagesData = { + body?: never; + path?: never; + query?: { + /** + * Q + * + * words in the title, one-line summary or text + */ + q?: string; + /** + * Ns + * + * only this namespace (default: every one you can read) + */ + ns?: string | null; + /** + * Place + * + * only notes filed here + */ + place?: "project" | "area" | "resource" | "archive" | null; + /** + * Free + * + * only free notes, not the pages of things + */ + free?: boolean; + /** + * Limit + */ + limit?: number; + }; + url: "/api/v1/notes/search"; +}; + +export type SearchPagesErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type SearchPagesError = SearchPagesErrors[keyof SearchPagesErrors]; + +export type SearchPagesResponses = { + /** + * Successful Response + */ + 200: NoteSearch; +}; + +export type SearchPagesResponse = SearchPagesResponses[keyof SearchPagesResponses]; + export type PageAboutData = { body?: never; path: { diff --git a/nextjs-frontend/components/app-shell/command-palette.tsx b/nextjs-frontend/components/app-shell/command-palette.tsx index 64678e610..3bc35b747 100644 --- a/nextjs-frontend/components/app-shell/command-palette.tsx +++ b/nextjs-frontend/components/app-shell/command-palette.tsx @@ -2,12 +2,13 @@ import * as D from "@radix-ui/react-dialog"; import { useQuery } from "@tanstack/react-query"; -import { CornerDownLeft, FileAudio, Search, Settings, Zap } from "lucide-react"; +import { CornerDownLeft, FileAudio, FileText, Search, Settings, Sparkles, Zap } from "lucide-react"; import { useRouter } from "next/navigation"; import { useEffect, useMemo, useRef, useState } from "react"; -import { Resources } from "@/app/openapi-client"; +import { Notes, Resources } from "@/app/openapi-client"; import { navFor } from "@/components/app-shell/nav-config"; +import { PLACES, pageOf } from "@/components/notes/links"; import { findSettings, hitHref, settingHref } from "@/components/settings/find"; import { data, useApiClient } from "@/lib/api/browser"; import { tc } from "@/lib/format"; @@ -22,7 +23,7 @@ type Item = { icon: React.ReactNode; }; -/** ⌘K: jump to a recording, a page or a setting; ">" limits results to actions. */ +/** ⌘K: jump to a recording, a note, a page or a setting; ">" limits results to actions. */ export function CommandPalette() { const [open, setOpen] = useState(false); const [q, setQ] = useState(""); @@ -56,6 +57,16 @@ export function CommandPalette() { staleTime: 60_000, }); + // Notes by their title, summary or text, found by the server as they're typed + const words = q.trim(); + const notes = useQuery({ + queryKey: ["palette-notes", namespace, words], + queryFn: () => data(Notes.searchPages({ client, query: { q: words, ns: namespace, limit: 8 } })), + enabled: open && !!words && !words.startsWith(">"), + staleTime: 30_000, + placeholderData: (prev) => prev, + }); + const items = useMemo(() => { const term = q.trim().toLowerCase(); const actionsOnly = term.startsWith(">"); @@ -106,6 +117,17 @@ export function CommandPalette() { icon: , })) : []; + const found: Item[] = t + ? (notes.data?.pages ?? []).map((n) => ({ + group: "Notes", + label: n.title, + meta: [PLACES.find((p) => p.value === n.place)?.label, namespace ? null : n.namespace] + .filter(Boolean) + .join(" · "), + href: n.about ? pageOf(n.about) : `/notes/${n.id}`, + icon: n.author === "assistant" ? : , + })) + : []; // Settings by name, or by a field in them (the LLM's timeout): a field opens its section on it const settings: Item[] = t ? findSettings(t, admin) @@ -121,8 +143,8 @@ export function CommandPalette() { .slice(0, 6) .map((x) => ({ group: "Settings", icon: , ...x })) : []; - return [...recordings, ...pages, ...settings, ...actions]; - }, [q, recs.data, admin, can]); + return [...recordings, ...found, ...pages, ...settings, ...actions]; + }, [q, recs.data, notes.data, namespace, admin, can]); useEffect(() => setSel(0), [q]); const go = (it: Item | undefined) => { @@ -141,7 +163,7 @@ export function CommandPalette() { className="flex h-10 w-full max-w-[540px] items-center gap-2.5 rounded-sm bg-surface-neutral px-3.5 text-left text-[14px] text-fg-muted hover:bg-border/60" > - Jump to a recording, speaker or page… + Jump to a recording, note or page… ⌘ @@ -183,7 +205,7 @@ export function CommandPalette() { aria-expanded aria-controls="palette-list" aria-activedescendant={items[sel] ? `palette-${sel}` : undefined} - placeholder="Search recordings, pages and settings, or > for actions" + placeholder="Search recordings, notes, pages and settings, or > for actions" className="h-14 flex-1 bg-transparent text-[16px] text-fg outline-hidden placeholder:text-fg-muted" /> esc @@ -191,7 +213,7 @@ export function CommandPalette() {
    {items.length === 0 && (
  • - {recs.isLoading ? "Searching…" : "No matches"} + {recs.isLoading || notes.isFetching ? "Searching…" : "No matches"}
  • )} {items.map((it, i) => { diff --git a/nextjs-frontend/components/notes/notes-home.tsx b/nextjs-frontend/components/notes/notes-home.tsx index 8eb743ec9..a80ced6b6 100644 --- a/nextjs-frontend/components/notes/notes-home.tsx +++ b/nextjs-frontend/components/notes/notes-home.tsx @@ -1,20 +1,41 @@ "use client"; -import { useMutation, useQueries, useQueryClient } from "@tanstack/react-query"; -import { FileText, NotebookPen, Plus, Sparkles } from "lucide-react"; +import { keepPreviousData, useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query"; +import { FileText, NotebookPen, Plus, Search, Sparkles } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/navigation"; +import { useMemo, useState } from "react"; import { Notes } from "@/app/openapi-client"; -import { PLACES } from "@/components/notes/links"; +import type { NotePageItem } from "@/app/openapi-client/types.gen"; +import { PLACES, pageOf } from "@/components/notes/links"; import { Button } from "@/components/ui/button"; +import { SearchInput } from "@/components/ui/field"; import { EmptyState, PageHeader, SkeletonRows } from "@/components/ui/states"; +import { Segmented } from "@/components/ui/tabs"; import { useToast } from "@/components/ui/toast"; import { ApiError, data, useApiClient } from "@/lib/api/browser"; import { shortDate } from "@/lib/format"; +import { useDebounced } from "@/lib/hooks/debounce"; import { useArchive } from "@/lib/hooks/session"; -/** Every free note in view, the latest changed first, with where it's filed. */ +type Place = NotePageItem["place"]; +type Row = { + id: number; + ns: string; + title: string; + summary?: string | null; + snippet?: string | null; + place?: Place; + parent?: number | null; + about?: string | null; + author?: string | null; + updated_at?: string | null; +}; + +/** Every free note in view, the latest changed first, with where it sits in the tree and where it's filed. Words in + * the search box find notes by their title, summary or text (the pages of things too); the filter shows one PARA + * place. */ export function NotesHome() { const client = useApiClient(); const qc = useQueryClient(); @@ -22,15 +43,36 @@ export function NotesHome() { const toast = useToast(); const { namespace, namespaces, can } = useArchive(); const names = namespace ? [namespace] : namespaces.map((n) => n.name); + const [q, setQ] = useState(""); + const [place, setPlace] = useState<"" | NonNullable>(""); + const words = useDebounced(q.trim().replace(/\s+/g, " "), 200); const trees = useQueries({ queries: names.map((ns) => ({ queryKey: ["notes-tree", ns], queryFn: () => data(Notes.listPages({ client, query: { ns } })), })), }); - const pages = trees + const search = useQuery({ + queryKey: ["notes-search", namespace, words, place], + queryFn: () => + data(Notes.searchPages({ client, query: { q: words, ns: namespace, place: place || null, limit: 100 } })), + enabled: !!words, + placeholderData: keepPreviousData, + }); + // Each free note by namespace and id, to name the notes a note is inside + const byKey = useMemo(() => { + const m = new Map(); + for (const t of trees) for (const p of t.data?.pages ?? []) m.set(`${t.data!.namespace}:${p.id}`, p); + return m; + }, [trees]); + const all: Row[] = trees .flatMap((t) => (t.data ? t.data.pages.map((p) => ({ ...p, ns: t.data.namespace })) : [])) .sort((a, b) => (b.updated_at ?? "").localeCompare(a.updated_at ?? "")); + const rows: Row[] = words + ? (search.data?.pages ?? []).map((p) => ({ ...p, ns: p.namespace })) + : all.filter((p) => !place || p.place === place); + const count = (v: string) => all.filter((p) => p.place === v).length; + const target = namespace ?? names.find((n) => can("editor", n)) ?? null; const create = useMutation({ mutationFn: () => data(Notes.createPage({ client, body: { ns: target!, title: "Untitled" } })), @@ -51,42 +93,156 @@ export function NotesHome() { New note ) : null; - const place = (v?: string | null) => PLACES.find((p) => p.value === v)?.label; + const placeLabel = (v?: string | null) => PLACES.find((p) => p.value === v)?.label; + /** "Home lab › Parts": the notes this one is inside, outermost first. */ + const path = (r: Row) => { + const out: string[] = []; + const seen = new Set(); + for (let at = r.parent; at != null && !seen.has(at);) { + seen.add(at); + const p = byKey.get(`${r.ns}:${at}`); + if (!p) break; + out.unshift(p.title); + at = p.parent; + } + return out.join(" › "); + }; + const loading = trees.some((t) => t.isPending) || (!!words && search.isPending); return (
    - {trees.some((t) => t.isPending) ? ( + {(all.length > 0 || q) && ( +
    + setQ(e.target.value)} + onKeyDown={(e) => { + // Enter opens the first note found + if (e.key !== "Enter" || e.nativeEvent.isComposing || !rows[0]) return; + e.preventDefault(); + router.push(hrefOf(rows[0])); + }} + className="min-w-[200px] flex-1" + /> +
    + setPlace(v as typeof place)} + items={[ + { value: "", label: "All" }, + ...PLACES.map((p) => ({ + value: p.value, + label: ( + <> + {SHOWN[p.value]} + {count(p.value) || ""} + + ), + })), + ]} + /> +
    +
    + )} + {loading ? ( - ) : !pages.length ? ( + ) : !all.length && !q ? ( } title="No notes yet" actions={newNote}> The assistant writes notes as it learns, and you can write your own. Type @ in a note to link a recording, person or another note, and # for a topic. Every recording and entity also has a page of its own. + ) : !rows.length ? ( + } title={words ? `No notes have “${words}”` : "No notes here"}> + {place ? `Nothing filed under ${placeLabel(place)} matches. Pick All to look everywhere.` : null} + ) : ( -
      - {pages.map((p) => ( -
    • - - {p.author === "assistant" ? ( - - ) : ( - - )} - - {p.title} - {p.summary && {p.summary}} - - - {[place(p.place), !namespace ? p.ns : null, p.updated_at ? shortDate(p.updated_at) : null] - .filter(Boolean) - .join(" · ")} - - -
    • - ))} -
    + <> +
      + {rows.map((p) => { + const inside = path(p); + const line = (words && p.snippet) || p.summary; + const meta = [ + placeLabel(p.place), + !namespace ? p.ns : null, + p.updated_at ? shortDate(p.updated_at) : null, + ] + .filter(Boolean) + .join(" · "); + return ( +
    • + + {p.author === "assistant" ? ( + + ) : ( + + )} + + {(inside || p.about) && ( + + {p.about ? `Page of ${p.about.split(":")[0]}` : `In ${inside}`} + + )} + + + + {line && ( + + + + )} + {/* on a phone, under the title, which then keeps the width */} + {meta} + + {meta} + +
    • + ); + })} +
    + {words && search.data && search.data.total > rows.length && ( +

    + The first {rows.length} of {search.data.total}: add a word to narrow it down. +

    + )} + )}
    ); } + +/** The text with the words searched for marked, wherever they are (any case). */ +function Marked({ text, words }: { text: string; words: string }) { + if (!words) return <>{text}; + const parts = text.split(new RegExp(`(${words.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")})`, "gi")); + return ( + <> + {parts.map((part, i) => + i % 2 ? ( + + {part} + + ) : ( + part + ), + )} + + ); +} + +const SHOWN: Record, string> = { + project: "Projects", + area: "Areas", + resource: "Resources", + archive: "Archive", +}; + +function hrefOf(p: Row): string { + return p.about ? pageOf(p.about) : `/notes/${p.id}`; +} diff --git a/nextjs-frontend/lib/hooks/debounce.ts b/nextjs-frontend/lib/hooks/debounce.ts new file mode 100644 index 000000000..b0888c5b1 --- /dev/null +++ b/nextjs-frontend/lib/hooks/debounce.ts @@ -0,0 +1,11 @@ +import { useEffect, useState } from "react"; + +/** The value once it has stopped changing for `ms`. */ +export function useDebounced(value: T, ms: number): T { + const [v, setV] = useState(value); + useEffect(() => { + const t = setTimeout(() => setV(value), ms); + return () => clearTimeout(t); + }, [value, ms]); + return v; +} diff --git a/nextjs-frontend/openapi.json b/nextjs-frontend/openapi.json index 49f9e7ffe..cdea45fea 100644 --- a/nextjs-frontend/openapi.json +++ b/nextjs-frontend/openapi.json @@ -9723,6 +9723,119 @@ } } }, + "/api/v1/notes/search": { + "get": { + "tags": [ + "notes" + ], + "summary": "Search Pages", + "description": "Pages found by their words, those with them in the title first, then the latest changed, each with the words\naround the match: what the Notes page and Jump to show, and what the assistant's find_notes reads.", + "operationId": "search_pages", + "parameters": [ + { + "name": "q", + "in": "query", + "required": false, + "schema": { + "type": "string", + "maxLength": 200, + "description": "words in the title, one-line summary or text", + "default": "", + "title": "Q" + }, + "description": "words in the title, one-line summary or text" + }, + { + "name": "ns", + "in": "query", + "required": false, + "schema": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "description": "only this namespace (default: every one you can read)", + "title": "Ns" + }, + "description": "only this namespace (default: every one you can read)" + }, + { + "name": "place", + "in": "query", + "required": false, + "schema": { + "anyOf": [ + { + "enum": [ + "project", + "area", + "resource", + "archive" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "description": "only notes filed here", + "title": "Place" + }, + "description": "only notes filed here" + }, + { + "name": "free", + "in": "query", + "required": false, + "schema": { + "type": "boolean", + "description": "only free notes, not the pages of things", + "default": false, + "title": "Free" + }, + "description": "only free notes, not the pages of things" + }, + { + "name": "limit", + "in": "query", + "required": false, + "schema": { + "type": "integer", + "maximum": 200, + "minimum": 1, + "default": 50, + "title": "Limit" + } + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NoteSearch" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, "/api/v1/notes/about/{kind}/{key}": { "get": { "tags": [ @@ -44199,6 +44312,114 @@ ], "title": "NoteHistory" }, + "NoteHit": { + "properties": { + "id": { + "type": "integer", + "title": "Id" + }, + "title": { + "type": "string", + "title": "Title" + }, + "summary": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Summary" + }, + "place": { + "anyOf": [ + { + "type": "string", + "enum": [ + "project", + "area", + "resource", + "archive" + ] + }, + { + "type": "null" + } + ], + "title": "Place" + }, + "parent": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "title": "Parent", + "description": "the page it's inside, in the tree" + }, + "about": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "About", + "description": "what this is the page of, like \"recording:12\"; null for a free note" + }, + "author": { + "type": "string", + "enum": [ + "person", + "assistant" + ], + "title": "Author", + "default": "person" + }, + "updated_at": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Updated At" + }, + "namespace": { + "type": "string", + "title": "Namespace" + }, + "snippet": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Snippet", + "description": "the words around the match in its text, when the text has it" + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "id", + "title", + "namespace" + ], + "title": "NoteHit" + }, "NoteHomeSuggestion": { "properties": { "page": { @@ -45114,6 +45335,29 @@ "type": "object", "title": "NotePageUpdate" }, + "NoteSearch": { + "properties": { + "total": { + "type": "integer", + "title": "Total", + "description": "how many pages match; `pages` has the first `limit`" + }, + "pages": { + "items": { + "$ref": "#/components/schemas/NoteHit" + }, + "type": "array", + "title": "Pages" + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "total", + "pages" + ], + "title": "NoteSearch" + }, "NoteTree": { "properties": { "namespace": { From 7577603ec90d964e571591b0c999824260d11a0d Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Thu, 15 Oct 2026 11:32:55 +0000 Subject: [PATCH 2/2] test(clicks): graph-merge waits for the imports' jobs to finish The journey failed now and then (on main too) with a 20 s timeout: an entity is in the graph once something links it twice, which takes import-file's episode as well, and a slow runner can still be reading it. The same wait as in #396, not counted. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01RrY2byt5BHVeBXJUyfwWpb --- ci/clicks/journeys.mjs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ci/clicks/journeys.mjs b/ci/clicks/journeys.mjs index e42d83845..c8d181513 100644 --- a/ci/clicks/journeys.mjs +++ b/ci/clicks/journeys.mjs @@ -670,6 +670,10 @@ export const JOURNEYS = [ from: "/", async run(ctx) { const { page, meter } = ctx; + // An entity is in the graph once something links it twice (graph.min_edge_weight), which takes import-file's + // pi-episode as well; a slow machine can still be reading it here, so wait for the jobs to finish (not counted) + const idle = page.getByRole("button", { name: /^Activity: (Nothing running|\d+ failed)$/ }); + await madeInBackground(ctx, "/", idle, "/", "the imports' jobs finished"); await go(ctx, "Graph"); const find = page.getByRole("combobox", { name: "Find in graph" }); await page.getByRole("region", { name: "Graph" }).waitFor();