Files
magpie/search_cli.go
T
yetone cfa280a6c2 provider, gateway, gui: You.com can be added as a search API, with no key for its free search or with a key (#1626, brainsparker)
A model's web search, when no provider can search, goes to the search
APIs the user added; each of them needed a key except a SearXNG of the
user's own. You.com answers with no key too, so it is one a user can
add in one step.

- provider: SearchVendor has KeyOptional (You.com, SearXNG); Ready and
  check let a vendor that has it go with no key. You.com is listed
  between Firecrawl and SearXNG, its keys at you.com/platform.
- gateway: with no key, GET https://api.you.com/v1/agents/search?query=
  &count=6 (YouFreeSearch); with a key, POST https://ydc-index.io/v1/search
  {"query","count"} with X-API-Key, as its API reference says; with a
  base URL of the user's own and no key, GET <url>/v1/agents/search.
  Both replies are read from results.web then results.news: the
  snippets, else the description, and page_age. You.com's errors are a
  code beside a message, so the message is what the user is told (a
  402 "prepaid credit balance has been depleted" is a refused key, the
  next key or API is asked). Its requests carry the User-Agent tag You.com
  asked for, "youdotcom-integration/yetone-magpie", after magpie's own.
- gui: Settings › Web search lets You.com be added with the key empty,
  its placeholder saying so (zh, zh-TW, ja, de).
- cli: magpie search add you [<key>]; the help lists it.

New network addresses: api.you.com (keyless) and ydc-index.io (keyed),
asked only when the user adds You.com, with the search query only.

Checked against the real service on 10-11: a keyless GET to
api.you.com/v1/agents/search answered 200 (testdata/you/free_search.json
is that reply, cut to 3 hits), and a real run through askSearchAPIKeys
returned 6 pages. ydc-index.io/v1/search answered 402 with no key
(testdata/you/keyed_402.json, real bytes) and 403 to a made-up key. The
keyless endpoint isn't in the API reference; You.com's own docs pages for
third-party tools describe it (free, 100 a day per address, no account),
as LiteLLM, ragflow and AnythingLLM use it. you.com/terms bars automated
use of its website and apps, not the API. The keyed request shape is
from its API reference only: no key with credit was tried.

Run: go test -tags nogui . ./internal/gui/ ./internal/provider/
./internal/backup/ green; ./internal/gateway/ green but
TestTurnDoneSaidAtTheTurnsEnd (#1629, fixed in its own commit);
search-api.test.cjs in Chromium and WebKit, en/zh, 980/420px green;
gui-ja, gui-de and i18n-keys green; gofmt -l; go vet -tags nogui for
linux, windows and darwin. The tests make no network request (a stub
RoundTripper). Without the change, TestYouSearch, TestYouKeyOutOfCredit,
TestYouNeedsNoKey and TestSearchCLIYouWithoutKey fail, and with the GUI's
keyOptional check taken out search-api.test.cjs fails in all four
Chromium runs (Add sends nothing).
2026-10-11 22:45:11 +08:00

104 lines
3.6 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 add you [<key>] You.com: free with no key (100 searches a day)
magpie search rm <api> take one away
apis: tavily, brave, exa, firecrawl, you, searxng
url= is the API's base URL, for an endpoint of your own that answers as that API
does (e.g. url=https://search.example.com/tavily). Without it the vendor's own is
used; a new key alone keeps the url set before, and url= with nothing after it puts
the vendor's own back. The key is sent only to that url.
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
magpie search add you`
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|you|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])}
// a new key alone keeps the address set before (#1580); url= with
// nothing after it puts the vendor's own back
urlGiven := false
for _, v := range args[2:] {
if u, ok := strings.CutPrefix(v, "url="); ok {
a.URL, urlGiven = u, true
} else if a.Key == "" {
a.Key = v
} else {
return fmt.Errorf("magpie search add <api> <key> [url=<address>]")
}
}
if i := slices.IndexFunc(provider.StoredSearchAPIs(), func(x provider.SearchAPI) bool { return x.Vendor == a.Vendor }); i >= 0 && !urlGiven {
a.URL = provider.StoredSearchAPIs()[i].URL
}
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)")
}