Files
magpie/internal/plugin/testdata/searchauth/index.js
T
yetone 083821dc96 plugins: a search entry can use the plugin's own sign-in, with its own rests
Asked for by axelburks on Discord. A plugin's search hook entry can name
a provider its own auth hook signs in to, `auth: "<provider id>"`. It then
takes no key. The host picks an account, renews it under `fresh` (the wait
counts within the search's time), runs the hook in `inScope`, and gives it
`ctx.getAuth()`, which returns that account's sign-in. A token the hook saves
with client.auth.set goes to the same account.

The gateway (askSearchSignedIn) asks one account at a time. It takes the
accounts that are on, in the order the provider's model requests take them
(accountsOn, now shared with fallback.go), and puts an account resting from
a search refusal last. A refusal rests only that account's search
(restSearchKey "account <key>"), never its models' rest (restingUntil). Every
attempt gets its own request-log row. An entry without `auth`, or naming a
provider the plugin doesn't sign in to, is a keyed entry as before, with no
getAuth. In Settings → Search, a bound entry shows "Signs in with {name}" in
place of the key field, and the row says whether it is signed in and how
many accounts are on (every language).
Docs: reference.md (with an example), plugin-runtime, plugin-tools and
provider-plugins.

Run on mote at f44fa2ef6 + this change. After rebasing on acd789d45 (an
unrelated responses_ws change), go build ./... and -run "Search|Budget"
on plugin, gateway and provider are ok again at this tree.
- gofmt -l clean; go vet ./internal/... ok; Linux and Windows vet of
  plugin/gateway/provider/gui ok
- go test -count=1 -timeout 45m ./internal/plugin ./internal/provider
  ./internal/gateway ./internal/gui: plugin, provider and gateway
  ok; gui red only in TestMainWindowCorners/macOS_26_SDK, mote's
  macOS 27 title bar, already filed as #3949
- node --test on 15 GUI files: search-signs-in, search-api, search-keys,
  search-order, search-all, search-first, search-instead, searcher-pick,
  searcher-then, provider-search, settings-search, i18n-keys, gui-zh-tw,
  gui-ja, gui-de. 207 of 207 pass (chromium and webkit, 980/420px, en/zh).

Broken by hand. Each break made its test red:
- B1, no ctx.getAuth: plugin TestPluginSearchSignsIn "ctx.getAuth is not a
  function"
- B2, no renewal: "searched as due@seek acc-due", "the renewal never began"
- B3, gateway with no auth branch: gateway TestPluginSearchSignsIn "signed
  in: k9 undefined"
- B4, a refusal that also rests the model account: "a search's rate limit
  rested a@seek for its models"
- B5, getAuth handing out the provider's first account: "as the second
  account: a@seek acc-a"
- B6, the hook run outside inScope: "a token saved while searching as b: a
  rotated-b@seek"
- B7, getAuth given to every entry: "keyed: k9 function"
- settings.js showing the key field for a bound entry: search-signs-in red
  in every engine, width and language

Not tried: a real search vendor or Axel's own plugin; real accounts; runs
on the Windows or Linux boxes; the full GUI suite. The full go test ./...
runs after the push.
2026-11-04 20:55:42 +08:00

39 lines
2.0 KiB
JavaScript

// A plugin whose web search signs in as its own provider (search entries
// with auth): "seekco" is an auth hook with no models, renewed through
// magpie (auth.refresh: "r-slow…" takes 3 seconds). No network: the
// search answers with what ctx.getAuth() gave it; the query "rotate" saves
// a new token through client.auth.set, as a vendor that rotates it would.
export default async ({ client }) => ({
auth: {
provider: "seekco",
methods: [{ type: "api", label: "API key" }],
async refresh(auth) {
// "r-hold": says it began (SEARCHAUTH_HOLD.in) and waits for the test
// to let it go (SEARCHAUTH_HOLD)
const hold = process.env.SEARCHAUTH_HOLD
if (hold && auth.refresh.startsWith("r-hold")) {
const fs = await import("node:fs")
fs.writeFileSync(hold + ".in", "")
while (!fs.existsSync(hold)) await new Promise((ok) => setTimeout(ok, 20))
}
await new Promise((ok) => setTimeout(ok, auth.refresh.startsWith("r-slow") ? 3000 : 100))
return { access: "renewed-" + auth.refresh, refresh: auth.refresh + "+", expires: Date.now() + 3600e3 }
},
},
search: {
seek: {
name: "Seek",
auth: "seekco",
async execute({ query, key }, ctx) {
const a = await ctx.getAuth()
if (query === "rotate") await client.auth.set({ path: { id: "seekco" }, body: { ...a, access: "rotated-" + a.accountId } })
if (a.access === "spent") throw Object.assign(new Error("Seek: rate limited"), { status: 429, retryAfter: 60 })
return [{ title: a.accountId + " " + a.access, url: "https://seek.invalid/" + query, text: "key=" + key + " fields=" + Object.keys(a).sort().join(",") }]
},
},
// an id the plugin doesn't sign in to: a key's entry, as before
stray: { name: "Stray", auth: "elsewhere", execute: async ({ key }, ctx) => [{ title: key + " " + typeof ctx.getAuth, url: "https://x.invalid/" }] },
keyed: { name: "Keyed", execute: async ({ key }, ctx) => [{ title: key + " " + typeof ctx.getAuth, url: "https://x.invalid/" }] },
},
})