okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-021-hostA' into f-021-int74e25e165

main
1.7 KiB25 linesraw
1/**
2 * Registers the app's web search with the Harness's web seam (`ctx.web`), in place of the Harness's own backends that all need a service key.
3 *
4 *   config.provider   'duckduckgo' (default, no key), 'exa' or 'perplexity' (the person's own key, read from the environment variable
5 *                     named by config.keyEnv, which the host sets; the key is never in the profile file).
6 *
7 * The profile pins `web.searchProvider` to the id registered here, so a second usable backend cannot make the choice ambiguous.
8 * Plain ESM, loaded by absolute path like strata-gate.mjs; its only imports are the app's own search modules next to the host.
9 */
10import { createDuckDuckGoSearch } from '../../web-search-ddg.mjs';
11import { createExaSearch, createPerplexitySearch } from '../../web-search-keyed.mjs';
12
13export const name = 'strata-web-search';
14export const inject = ['web'];
15
16export function apply(ctx, config = {}) {
17  const keyOf = () => (config.keyEnv ? process.env[config.keyEnv] ?? '' : '');
18  // Tests only (the bridge option `searchMock`): the same search code on a pretend web, so nothing leaves the machine.
19  const pretend = config.mock === 'offline' ? { fetch: async () => { throw new TypeError('fetch failed'); } } : config.mock === 'limited' ? { fetch: async url => new Response('<html>please verify you are human: captcha</html>', { status: /duckduckgo/.test(String(url)) ? 202 : 429 }) } : null;
20  const provider = pretend ? createDuckDuckGoSearch(pretend)
21    : config.provider === 'exa' ? createExaSearch({ key: keyOf })
22    : config.provider === 'perplexity' ? createPerplexitySearch({ key: keyOf })
23    : createDuckDuckGoSearch();
24  ctx.web.registerSearchProvider(provider);
25}