UI reference

Global search

Global search with Saasie is the one search in Saaslivery. The navbar bar searches every app the member can open, from any tab. The tab they are in is context: its record types come first and its matches rank a little higher. It is never a limit. The platform owns the endpoint, the access checks, the matching and the ranking; each app owns only what it searches and who may see it.

JS: public/js/saasie.js (the bar and each tab's ai-screen) CSS: public/js/saasie.css Markup: templates/platform/partials/saasie_screen.html Backend: core/search.py (registry and matcher), handlers/search.py (dispatcher), handlers/searches.py (history)


How it works

  1. Clicking into the bar shows the tab's ai-screen, #{app}-ai-screen, a sibling of #{app}-screen. With nothing searched yet it lists the member's history for that tab.
  2. Enter sends POST /search with {q, context}. With the Ask Saasie toggle on, or with Ctrl or Cmd held, Enter asks Saasie instead.
  3. The dispatcher walks every registered member search, the context app first, one app after another on the request's one connection. It skips an app the member cannot open, loads the member's permissions for each app it keeps, and searches each type the member may see on its own.
  4. Each type answers its best 20 and the total that matched, which the badge shows.
  5. Each search or question is kept in the member's history for that tab (search_sessions). Results are never stored: restoring a session runs every entry again.

Esc clears the bar when it has text, and closes the ai-screen when the bar is empty. Nothing drops down or pops up from the bar.


Endpoints

POST /search                   {"q": "invoice", "context": "mail"}
GET  /search?app=files&q=...   one app, for pickers and embeds
GET  /searches?context=mail    the member's sessions in that tab, newest first
GET  /searches/:id             one of the member's own sessions
POST /searches                 {"context": "mail", "entry": {"kind": "search", "text": "invoice", "apps": [...]}}
POST /searches/:id/entries     {"entry": {...}}

POST /search answers with one():

Field Description
groups One per app and type that matched: app, app_name, type, label, icon, columns, total, more, context, and its best 20 hits.
best The best 20 hits across every group, for the All badge.
total Every match across every group.

total is exact when the statement counted it. A handler that drops rows after its statement ran (a per-row permission check) calls search.uncounted(), and the group then says more instead of a count.

Hit fields

Field Description
type, id, title, subtitle, url, score As the handler returns them.
event Instead of url: a window event to dispatch, {name, detail} (Calendar opens the calendar on the meeting's day).
any column field Whatever the type's columns name (status, date, amount with currency).
app, app_name, label, icon, rank Added by the dispatcher. rank is the score plus the context boost.

Matching

core.search.Match builds the WHERE clause and score for a set of columns:

  • Substrings and IDs: each query word must appear in the record (PL-41 finds PL-417).
  • Typos: a word that does not appear still matches a word in the record within 1 edit (4 or 5 letters) or 2 edits (6 or more), so fshes and ifshes both find fishes. Candidates come from the pg_trgm index, edit distance from fuzzystrmatch.
  • Ranking: a word found in a name column outranks one found in a detail column; an exact match on the first name column (an ID, a code) ranks highest, then a prefix. Every app scores with the same formula, so scores rank across apps.

search_rows() also counts every match with a window column and records it on the Search, which is where the badge counts come from.

Every searched column set has a trigram index built on exactly the expression core.search.document() produces, in the app's own migration.


Implementing search for an app

1. Write the handler

from core.search import Search, search_rows

async def api_search(req: Request, ctx: Context, search: Search) -> list:
    if not search.wants("document"):
        return []
    rows = await search_rows(
        ctx.db.raw, search, source="paper_documents", columns="id, title", names=["title"],
        scope="workspace_id = $1 AND NOT is_deleted AND (visibility = $2 OR id = ANY($3::text[]))",
        params=[ctx.db.workspace_id, VISIBILITY_WORKSPACE, list(await accessible_doc_ids(ctx))],
    )
    return [{"type": "document", "id": r["id"], "title": r["title"], "score": r["score"],
             "url": f"/app/papers/doc/{r['id']}"} for r in rows]

scope is the app's own visibility rule, exactly as its list endpoint applies it. For text that lives outside a table (a resolved name, a mailbox subject), score it with rank_texts(), the same matcher.

2. Register it, in badge order, with its label, icon and columns

from core.i18n import N_
from core.search import AMOUNT, DATE, PILL, Column, Searchable, register

register("papers", api_search, [Searchable("document", label=N_("Documents"), icon="description")])
register("finance", api_search, [
    Searchable("invoice", ("finance.entries.view",), label=N_("Invoices"), icon="receipt_long", columns=(
        Column(N_("Number"), "title"), Column(N_("Status"), "status", PILL),
        Column(N_("Date"), "date", DATE), Column(N_("Amount"), "amount", AMOUNT),
    )),
])

A type's keys are the ones its list endpoint requires; holding any one is enough. Without columns a type shows Result and Detail (title, subtitle). A platform search that checks its own caller passes gate= instead of being checked as an app; member=False keeps a staff search out of member search, and it answers alone in its own console.

3. Index what it searches

CREATE INDEX IF NOT EXISTS paper_documents_search_trgm ON paper_documents
    USING gin ((lower(coalesce(title, ''))) gin_trgm_ops);

That is all. The app needs no front-end code for search.


History

search_sessions is a tenant table: every workspace has search. A session belongs to one member and one tab (context) and holds its entries as JSON: the kind (search or ask), the text, the apps that answered, and for an insight the query and settings Saasie chose. History follows the plan's retention window through a platform pass in core/retention.py that runs in every workspace.