search: a plugin can give a web search API (its search hook)

treeman on Discord #help asked for web search to be pluggable so users can
add search vendors. A plugin's `search` hook, {search: {<id>: {name, keysURL?,
keyOptional?, url?, execute({query, count, key, url}, ctx)}}}, adds an API to
Settings > Models > Web search as `<plugin>:<id>`. It is added, keyed, ordered,
switched off and removed like Tavily or Brave. The built-in APIs stay built in.

How: host.js collects the hook's entries (id [a-z0-9-], an execute) into
Loaded.Searches and runs one with the `search` op (timeout from ctx, an error
with a numeric status comes back as a refusal). The host notes them in
plugin-searches.json (internal/plugin/search.go); provider.AllSearchVendors
adds the noted ones of plugins that are on to SearchVendors, so Ready, check,
the CLI and the GUI list see them. askSearchAPI hands a plugin vendor to
askPluginSearch below askSearchAPIKeys, so keys, rest on 401/402/403/429,
order, the masked query and the request-log row are the built-ins' own.

Run (mote, on this tree, whose base df7c94bdf was just fetched):
- go test -timeout 45m ./internal/plugin ./internal/provider ./internal/gateway
  ./internal/cli: ok.
- go vet native; GOOS=windows go vet plugin, provider, gateway, cli: ok;
  GOOS=linux go vet plugin, provider, gateway: ok. gofmt -l: empty.
- internal/gui: TestCoreScriptBudget and TestMainWindowCorners fail in a
  full run. This change touches no script (only api.go's loop), so the
  budget red is the base's (re-checked after the rebase, below);
  TestMainWindowCorners is AppKit on mote (macOS 27).
Broken by hand: skipping off plugins in NotedSearches turns TestPluginSearch
red; the refusal mapping removed turns TestPluginSearchAPI red.
Not tried: a real plugin against a real vendor's API; the GUI JS suite (no
GUI script changed); a Linux or Windows window; site/public/docs (en, zh, ja)
do not describe the hook yet. A plugin just added is listed once the host has
loaded it; RestoreSearchAPIs drops a plugin's entry on a machine without it.
The community README has the template (magpie-community/plugins 3ec42d4).

Reported by treeman.
This commit is contained in:
yetone
2026-11-04 08:37:21 +08:00
parent df7c94bdf5
commit 90bf96a23d
14 changed files with 684 additions and 7 deletions
+37
View File
@@ -1157,6 +1157,43 @@ export const LemonPlugin = async () => ({
In TypeScript, `icon` and `placeholder` aren't in OpenCode's types: build
the hook as a variable (or cast it), or put the icon in `package.json`.
#### Web search APIs from a plugin
A plugin can add a web search API to Settings › Models › Web search with a
`search` hook (magpie's own; OpenCode ignores it). It is listed after
Tavily, Brave and the rest under its own name, and added, keyed, switched
off, reordered and removed like them (`magpie search add <plugin>:<id> <key>`
on the command line). magpie keeps its keys (several, tried in turn, a refused
one resting), its order and its place in the request log, masks the query
as it does for every search API, and asks the plugin only while the
plugin is on and the Web search plugin is.
```js
export const MojeekSearch = async () => ({
search: {
mojeek: { // [a-z0-9-], at most 32; the API is `<plugin>:mojeek`
name: "Mojeek", // its name in the list
keysURL: "https://www.mojeek.com/services/api/", // "Get a key" link, optional
keyOptional: false, // true: it answers with no key
url: false, // true: the user gives its address (a SearXNG's)
async execute({ query, count, key, url }, ctx) {
const r = await fetch(`https://api.mojeek.com/search?fmt=json&q=${encodeURIComponent(query)}&t=${count}&api_key=${key}`, { signal: ctx.abort })
if (!r.ok) throw Object.assign(new Error(await r.text()), { status: r.status }) // 401, 402, 403, 429 rest the key
const j = await r.json()
return j.response.results.map((x) => ({ title: x.title, url: x.url, text: x.desc }))
},
},
},
})
```
`execute` returns `[{ title, url, text?, age? }]` (or `{ results: [...] }`);
magpie strips tags and cuts each page's text. A throw with a numeric
`status` (and `retryAfter`, its Retry-After) is the API refusing the key,
treated as a built-in's 401/402/403/429 is; any other throw is a failure
and the next search API is asked. It has the time a search API has
(a quarter of a search's limit); `ctx.abort` fires then.
#### Gateway middleware
A plugin can also be gateway middleware: `onRequest(body, ctx)` sees each