Files
magpie/search_cli.go
T
yetone 067a0b7552 gateway: a search API takes several keys, and one out of credit hands the search to the next (#1477, bluesky4485)
Tavily's free credit runs out, and changing the key by hand each time was
the reporter's chore. A search API's key can now be several, split by
commas (or spaces, semicolons, lines): Settings › Search's key field says
so, and `magpie search add tavily k1,k2` takes them. Each search starts
from the key that answered last (kept per vendor in memory) and goes on to
the next only when the answer is about the key: 401/403 turned away,
402/432/433 out of credit (Tavily's plan and pay-as-you-go limits), 429
rate limited. Anything else, such as a 500 or no answer in time, isn't the
key's, and the other keys aren't spent on it. Keys are stored as one
comma-joined string in the same field, so nothing is migrated; the list,
CLI and Settings show each key masked.

Run: TestSearchAPIKeysTakeTurns (httptest Tavily answering 432 with
Tavily's own body, 401 and 500) passes and goes red with the hand-off
removed; go test -tags nogui -run Search ./internal/gateway
./internal/provider ./internal/gui . passes; search-api, searcher-pick,
gui-ja, gui-de, gui-zh-tw .test.cjs pass in Chromium and WebKit. Not tried
against the real Tavily with a spent key.
2026-10-10 18:31:32 +08:00

91 lines
2.8 KiB
Go

package main
import (
"fmt"
"slices"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// The web search APIs, from the terminal: magpie search (#419).
const searchUsage = `usage:
magpie search the search APIs a model's web search goes to
magpie search add <api> <key> [url=<address>] add one, or give one a new key
(several keys as k1,k2: the next is tried when one is refused)
magpie search add searxng url=<address> [<key>]
magpie search rm <api> take one away
apis: tavily, brave, exa, firecrawl, searxng
A client's web search (Claude Code's WebSearch, Codex's web_search) is done by the
model's provider when it can search, then by a provider that can (a Claude or ChatGPT
account, OpenAI, Anthropic, OpenRouter…), and then by these, in the order they were
added: what they find goes to the model as it is, and no model is asked to read it.
Without any of them, the client's search is left out.
e.g. magpie search add tavily tvly-…
magpie search add searxng url=https://searx.example.com`
func searchCmd(args []string) error {
if len(args) == 0 {
as := provider.StoredSearchAPIs()
if len(as) == 0 {
fmt.Println(muted.Render(" no search API · magpie search add <tavily|brave|exa|firecrawl|searxng> <key>"))
return nil
}
for i, a := range as {
what := a.MaskedKey()
if a.Key == "" {
what = "no key"
}
if a.URL != "" {
what += " " + a.URL
}
line := fmt.Sprintf(" %d. %s %s", i+1, pad(a.Name(), 14), muted.Render(what))
if !a.Ready() {
line += " " + faint.Render("needs its key")
}
fmt.Println(line)
}
return nil
}
switch args[0] {
case "help", "-h", "--help":
fmt.Println(searchUsage)
return nil
case "add", "set":
if len(args) < 2 {
return fmt.Errorf("magpie search add <api> <key> [url=<address>]")
}
a := provider.SearchAPI{Vendor: strings.ToLower(args[1])}
for _, v := range args[2:] {
if u, ok := strings.CutPrefix(v, "url="); ok {
a.URL = u
} else if a.Key == "" {
a.Key = v
} else {
return fmt.Errorf("magpie search add <api> <key> [url=<address>]")
}
}
if err := provider.SetSearchAPI(a); err != nil {
return err
}
as := provider.StoredSearchAPIs()
i := slices.IndexFunc(as, func(x provider.SearchAPI) bool { return x.Vendor == a.Vendor })
fmt.Println(green.Render("✓"), as[i].Name(), muted.Render(fmt.Sprintf("search API %d of %d", i+1, len(as))))
return nil
case "rm", "remove":
if len(args) != 2 {
return fmt.Errorf("magpie search rm <api>")
}
if err := provider.RemoveSearchAPI(strings.ToLower(args[1])); err != nil {
return err
}
fmt.Println(green.Render("✓"), "removed")
return nil
}
return fmt.Errorf("magpie search [add|rm] (magpie search help)")
}