mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
feat(plugins): let a plugin answer place searches from its own index
TREK searches its own place index and OpenStreetMap. Neither carries a rating, so "the best rated hotel around here" has never been answerable, and an operator with a better index for their region had no way to reach the search box at all. The searchProvider hook is that way in: a plugin is handed the query, a limit, the language and the coordinate the search is biased toward, and answers with places in the same shape the core search returns, plus a rating. It sits beside the core search rather than inside it. The plugin runtime already imports PlacesModule, which imports MapsModule, so asking the providers from the maps service would close a module cycle — the same reason the other fourteen contribution controllers stand next to what they contribute to. The client asks both at once and merges, at the single point every caller already goes through, so all nine search surfaces get plugin hits without knowing the hook exists. A plugin index may never hold up the list: the client stops waiting at two and a half seconds and the host gives a provider two, which is also why the budget is the shortest of any hook here. With no such plugin installed the route answers without reaching the runtime at all. Every hit is normalized host-side and shared by the REST route and the MCP tool, so a cap that holds for the browser holds for the assistant: coordinates are range-checked, strings capped, the rating clamped to 0..5, a website must be http or https, and ids are namespaced so they can never collide with an OSM one. search_places_via_plugins is the MCP half, because what the user can see the assistant has to be able to see; search_place now points at it.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { Controller, Get, Query, Req, UseGuards } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import { JwtAuthGuard } from '../../auth/jwt-auth.guard';
|
||||
import { pluginsEnabled } from '../kill-switch';
|
||||
import { PluginHooks } from '../plugin-hooks.service';
|
||||
import {
|
||||
interleave,
|
||||
limitFrom,
|
||||
MAX_QUERY,
|
||||
nearFrom,
|
||||
normalizeSearchHits,
|
||||
type SearchHit,
|
||||
} from './plugin-search.helpers';
|
||||
|
||||
/**
|
||||
* GET /api/plugin-search — places found by plugins implementing `searchProvider` (#2221).
|
||||
*
|
||||
* A search index TREK does not ship. The core search asks the TREK index and
|
||||
* OpenStreetMap; this asks whatever else the operator installed, and the client draws
|
||||
* one list out of both. Additive and fail-safe like the other provider-hook
|
||||
* controllers: a provider that errors or times out contributes nothing and the search
|
||||
* is drawn without it.
|
||||
*
|
||||
* NOT trip-scoped, unlike its neighbours here. Searching for a place is not a read of
|
||||
* anybody's trip: the query is the caller's own words and the answer is a public index,
|
||||
* so the gate is being signed in. Nothing about a trip is passed to the plugin, which
|
||||
* is also what keeps a search provider from becoming a way to enumerate one.
|
||||
*
|
||||
* A route of its own beside `POST /api/maps/search` rather than a branch inside it,
|
||||
* for the reason `trip-warnings.mcp.ts` documents next door: the plugin runtime already
|
||||
* imports PlacesModule, which imports MapsModule, so asking the providers from inside
|
||||
* the maps service would close a module cycle. The client calls both and merges.
|
||||
*/
|
||||
@Controller('api/plugin-search')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class PluginSearchController {
|
||||
constructor(private readonly hooks: PluginHooks) {}
|
||||
|
||||
@Get()
|
||||
async search(
|
||||
@Query('q') q: string | undefined,
|
||||
@Query('lat') lat: string | undefined,
|
||||
@Query('lng') lng: string | undefined,
|
||||
@Query('lang') lang: string | undefined,
|
||||
@Query('limit') limit: string | undefined,
|
||||
@Req() req: Request & { user?: { id: number } },
|
||||
): Promise<{ places: SearchHit[] }> {
|
||||
if (!pluginsEnabled()) return { places: [] };
|
||||
const userId = req.user?.id;
|
||||
const query = String(q ?? '').trim().slice(0, MAX_QUERY);
|
||||
// An empty query is not an error here: the client fires this beside every search,
|
||||
// and a blank box simply has nothing to ask a provider about.
|
||||
if (!query || userId == null) return { places: [] };
|
||||
|
||||
const ids = this.hooks.providersOf('searchProvider');
|
||||
if (ids.length === 0) return { places: [] };
|
||||
|
||||
const request = {
|
||||
query,
|
||||
limit: limitFrom(limit),
|
||||
lang: lang ? String(lang).slice(0, 20) : undefined,
|
||||
near: nearFrom(lat, lng),
|
||||
};
|
||||
const results = await Promise.all(
|
||||
ids.map(async (id): Promise<SearchHit[]> => {
|
||||
try {
|
||||
return normalizeSearchHits(id, await this.hooks.searchPlaces(id, request, userId));
|
||||
} catch {
|
||||
return []; // a slow / failing provider is skipped, never fatal
|
||||
}
|
||||
}),
|
||||
);
|
||||
return { places: interleave(results) };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user