CyberCaius asked on Discord #help (10-30) for Volcengine's Doubao search under Settings › Models › Search APIs, or a custom API address. Volcengine's Doubao Search Custom (豆包搜索 Custom版, once 联网搜索 / 融合信息搜索, docs.volcengine.com/docs/networked-search/networked-search, the same page as volcengine.com/docs/87772/2272953) can now be added with its API key: - provider.SearchVendors: volcengine, "Doubao Search (Volcengine)", at https://open.feedcoopapi.com, keys at console.volcengine.com/search-infinity/api-key, after DeepSeek. The GUI takes its vendors from SearchVendors, so it shows in the Search API picker with its Get a key link and base URL field; no GUI file changes. - askSearchAPI: POST /search_api/web_search, Bearer key (the default, as the docs and volcengine/mcp-server's askecho_search_infinity send it), body {Query, SearchType: "web", Count: searchHits, Filter: {NeedUrl: true}}. The query is cut to the 100 characters the docs allow. NeedUrl leaves out its 火山如意 cards, which have no URL to cite. - readVolcSearch reads Result.WebResults: Summary as the page's text (the docs recommend it for models; Content is the whole page, Snippet ~200 chars "only for list display"), Snippet when there is no Summary, PublishTime as the page's age, a 1970-01-01 time (its image example's stand-in) as none, and a result with no Url left out. - volcSearchError: its errors are in ResponseMetadata.Error, a code the docs list, and the docs don't say the HTTP status, so the code decides: 10401/10403/10409 rest the key as turned away (403), 10406/10408/ 10410/10412 (free searches spent with none paid, no plan, plan spent) as out of credit (402), 700429 as rate limited (429). Any other code is a plain error, a 200 carrying one included. The key is masked in the error as for every vendor. - A custom address: every search API already takes a base URL (#1580, the field under the key in Settings, url= in the CLI), so an endpoint that answers as Tavily, SearXNG, Brave, Exa, Firecrawl, You.com, DeepSeek or now Doubao Search does can be set there. There is no common format across the vendors (each has its own path, body and reply), so no separate "custom" vendor was added. - Siblings: CLI usage (main.go, search_cli.go's api list and empty-state hint), README.md, README.zh-CN.md, gateway-routing.md's path list and refusal paragraph. The test's reply is the docs' own web-search response example, byte for byte (testdata/volcengine_web_search.json); its error replies are the docs' error example with its "// 该字段为错误码" note taken out (not JSON). Not tried against the real API: no Volcengine search key in this run. Run (worktree on origin/mainc941e0877, then rebased ond31710c71): - TestVolcengineSearchAPI (new): red on the old code (no such vendor). Broken by hand six ways, each red with its own message: Summary not read (Snippet's text), volcSearchError not called (200 10403 read as "no Result in it"), query not cut (150 characters went), NeedUrl dropped (body {"Filter":{}}), 1970 age kept, 10412 not mapped (plain error, not a refusal). Restored: ok. - go test -run Search ./internal/gateway/, go test ./internal/provider/ ./internal/cli/: ok, before and after the rebase. - gofmt -l internal: nothing. go vet -tags nogui ./... on darwin, GOOS=linux, GOOS=windows: ok. - Not run before the push: the full go test ./... . mote had three other sessions' full suites running at load 140-230; it runs after the push and its result goes on the report.
211 lines
7.3 KiB
Go
211 lines
7.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"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>] [pick=turns|order]
|
|
add one, or give one a new key
|
|
(several keys as k1,k2: searches take them in turn,
|
|
or with pick=order the first until it 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 off <api> keep one, key and all, but never ask it
|
|
magpie search on <api> ask it again
|
|
magpie search move <api> <n> put one at place n in the order they are asked
|
|
magpie search rm <api> take one away
|
|
|
|
apis: tavily, brave, exa, firecrawl, you, deepseek, volcengine, 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.
|
|
|
|
With several keys, a key the API refused lately (turned away, out of credit, rate
|
|
limited) rests and is asked only after the others: a rate limit for as long as the API
|
|
says (30s to 10m), a key turned away for 10 minutes, one out of credit for an hour.
|
|
For Tavily, magpie search shows what each key has used of its limit (GET /usage).
|
|
|
|
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 their order (the order
|
|
they were added, until one is moved), skipping one switched off: 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|deepseek|volcengine|searxng> <key>"))
|
|
return nil
|
|
}
|
|
for i, a := range as {
|
|
what := a.MaskedKey()
|
|
if a.Key == "" {
|
|
what = "no key"
|
|
}
|
|
if a.URL != "" {
|
|
what += " " + a.URL
|
|
}
|
|
if len(a.Keys()) > 1 {
|
|
what += map[bool]string{true: " in order", false: " in turn"}[a.Pick == provider.SearchPickOrder]
|
|
}
|
|
line := fmt.Sprintf(" %d. %s %s", i+1, pad(a.Name(), 14), muted.Render(what))
|
|
if !a.Ready() {
|
|
line += " " + faint.Render("needs its key")
|
|
}
|
|
if a.Off {
|
|
line += " " + amber.Render("off")
|
|
}
|
|
fmt.Println(line)
|
|
if gateway.CanReadSearchQuota(a) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
for _, q := range gateway.SearchQuotas(ctx, a, true) {
|
|
fmt.Println(" " + pad(q.Key, 14) + " " + quotaLine(q))
|
|
}
|
|
cancel()
|
|
}
|
|
}
|
|
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 p, ok := strings.CutPrefix(v, "pick="); ok {
|
|
if a.Pick = p; p == "" {
|
|
a.Pick = "turns"
|
|
}
|
|
} else if a.Key == "" {
|
|
a.Key = v
|
|
} else {
|
|
return fmt.Errorf("magpie search add <api> <key> [url=<address>] [pick=turns|order]")
|
|
}
|
|
}
|
|
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 "on", "off":
|
|
if len(args) != 2 {
|
|
return fmt.Errorf("magpie search %s <api>", args[0])
|
|
}
|
|
if err := provider.SwitchSearchAPI(strings.ToLower(args[1]), args[0] == "on"); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), strings.ToLower(args[1]), args[0])
|
|
return nil
|
|
case "move", "mv":
|
|
n := 0
|
|
if len(args) == 3 {
|
|
fmt.Sscan(args[2], &n)
|
|
}
|
|
as := provider.StoredSearchAPIs()
|
|
i := slices.IndexFunc(as, func(x provider.SearchAPI) bool { return len(args) > 1 && x.Vendor == strings.ToLower(args[1]) })
|
|
if len(args) != 3 || n < 1 || n > len(as) {
|
|
return fmt.Errorf("magpie search move <api> <n>, n from 1 to %d", len(as))
|
|
}
|
|
if i < 0 {
|
|
return fmt.Errorf("no %s search API is set up", strings.ToLower(args[1]))
|
|
}
|
|
order := []string{}
|
|
for _, a := range as {
|
|
order = append(order, a.Vendor)
|
|
}
|
|
v := order[i]
|
|
order = slices.Insert(slices.Delete(order, i, i+1), n-1, v)
|
|
if err := provider.ArrangeSearchAPIs(order); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), as[i].Name(), muted.Render(fmt.Sprintf("search API %d of %d", n, 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|on|off|move|rm] (magpie search help)")
|
|
}
|
|
|
|
// quotaLine is what one key has used of its limit, as magpie search lists
|
|
// it: unknown, with why, when it couldn't be read, and never 0.
|
|
func quotaLine(q gateway.SearchKeyQuota) string {
|
|
var line string
|
|
switch {
|
|
case !q.Known:
|
|
line = amber.Render("unknown") + muted.Render(" · "+q.Err)
|
|
case q.Unlimited:
|
|
line = fmt.Sprintf("%s used · no limit", thousands(q.Used))
|
|
default:
|
|
line = fmt.Sprintf("%s / %s used", thousands(q.Used), thousands(q.Limit))
|
|
if q.Of == "plan" && q.Plan != "" {
|
|
line += muted.Render(" (" + q.Plan + " plan)")
|
|
} else if q.Of == "plan" {
|
|
line += muted.Render(" (plan)")
|
|
}
|
|
if q.PaygoLeft > 0 {
|
|
line += muted.Render(fmt.Sprintf(" · %s pay-as-you-go left", thousands(q.PaygoLeft)))
|
|
}
|
|
if q.UsedUp {
|
|
line += " " + amber.Render("used up")
|
|
}
|
|
}
|
|
if q.Resting != "" {
|
|
line += faint.Render(fmt.Sprintf(" · %s, asked last until %s", q.Resting, q.RestUntil.Local().Format("15:04")))
|
|
}
|
|
return line
|
|
}
|
|
|
|
// thousands is n with its thousands split by commas.
|
|
func thousands(n int64) string {
|
|
s := fmt.Sprint(n)
|
|
neg := strings.HasPrefix(s, "-")
|
|
s = strings.TrimPrefix(s, "-")
|
|
for i := len(s) - 3; i > 0; i -= 3 {
|
|
s = s[:i] + "," + s[i:]
|
|
}
|
|
if neg {
|
|
s = "-" + s
|
|
}
|
|
return s
|
|
}
|