Files
ServerKit/backend/app/plugins_sdk/search_sdk.py
T
Juan DenisandClaude Opus 5 2a0b4ca029 feat(plugins): let extensions put their objects in global search
The omnisearch fan-out was a hardcoded sequence of core entity blocks, so an
extension's objects were absent from the command palette — the one place the
UI tells you to look. A plugin now registers a provider and its rows appear
alongside services, servers and domains, with no frontend change: the palette
already renders unknown types, bucketing them under "Results".

Scoping stays the provider's job, because search invents no ACL — every core
block scopes its own query and the provider is handed the user and workspace
to do the same. The shape, though, is not trusted:

- rows are always tagged with the registered type, so a plugin cannot mint a
  row that claims to be a 'service' or a 'vault' (which are trusted to have
  been scoped by core);
- the per-type cap is re-applied host-side rather than trusted from the
  provider, so one contributed type can't drown the core ones;
- a path must be an in-app router path. The palette hands it straight to
  navigate(), so a protocol-relative '//host' would walk the user off the
  panel entirely.

A malformed row is dropped on its own, a broken provider degrades to "no rows
of that type" — the same isolation each core block already has.

The lazy accessor in plugins_sdk now carries a table of surfaces rather than a
single special case for deploys, since the other SDK surfaces land the same way.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-25 13:40:00 -04:00

49 lines
1.8 KiB
Python

"""Plugin-facing SDK for global search.
from app.plugins_sdk import search
def find_worlds(query):
rows = World.query.filter(World.owner_id == query.user.id,
World.name.ilike(f'%{query.term}%'))
return [{'label': w.name, 'sublabel': 'Minecraft world',
'path': f'/minecraft/worlds/{w.id}'} for w in rows]
search.register('minecraft.world', find_worlds)
Registering makes a plugin's objects reachable from the command palette with no
frontend work: an unrecognised type renders under "Results" with the generic
icon rather than being dropped.
Your provider is handed a ``search.Query`` — ``term``, ``user``, ``workspace_id``
and ``limit`` — and **must scope its own rows to that user and workspace**. The
panel applies no post-filter; every core block scopes itself, and search exists
to surface what someone can already reach, never to widen it.
"""
from app.services import search_provider_registry
from app.services.search_provider_registry import SearchQuery
class SearchSdk:
"""Stable search surface for plugins."""
#: The object a provider receives. Exposed so plugins can build one in
#: their own tests without importing a host service path.
Query = SearchQuery
def register(self, entity_type, provider, replace=False):
"""Register ``provider(query) -> [{'label', 'path', 'sublabel'}, ...]``.
Namespace the type after your plugin (``minecraft.world``); bare names
are reserved for core entities. Rows are capped and normalised by the
host, and are always tagged with the type you registered.
"""
return search_provider_registry.register(entity_type, provider, replace=replace)
def types(self):
"""Entity types registered by plugins."""
return search_provider_registry.types()
search = SearchSdk()