Files
magpie/internal/gateway/search_plugin_test.go
T
yetone 5666cb0490 search MCP: web_search takes a count, 6 by default, 1 to 20 (treeman)
The web search MCP server's web_search always gave 6 results: searchHits
was both what a search API (a plugin's included) was asked for and where
its pages were cut. It now takes an optional count, described in its
input schema (integer, minimum 1, maximum 20, default 6). A number or a
number in a string is taken, floored; below 1 is held to 1, above 20 to
20 (Brave and Tavily take no more than 20); null or a non-number is the
default 6. The tool's description is unchanged, and the web_search magpie
offers models on vendor requests keeps its schema (no count).

The count rides the search's context (withSearchCount/searchCount), so
every backend reads it: plugin search APIs by key and signed-in (the
count handed to the hook and the pages cut), Tavily max_results, Brave
count, Exa numResults, Firecrawl limit, DeepSeek count, Volcengine
Count, You.com count (all three routes), Yuanbao Search's Cnt (the
fewest of 10/20/... that covers it), SearXNG (cut only), Kimi Code's
limit, GLM's search MCP and Codex's backend search (cut only, neither
takes a count), WorkBuddy (its client's 5 unless more than 6 is asked).
A searcher model's search is prose and has no count.

Run (mote): go test -run 'Search|Codex|Kimi|GLM|WorkBuddy|Plugin'
./internal/gateway ok; -race of the new tests and TestSearchMCP* ok;
go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet of the
package ok; gofmt -l empty.
Broken by hand: searchCount ignoring the count went red in both new
tests (plugin asked for 6, said 6, every vendor asked 6); withSearchCount
without the clamp went red for 50, 0 and -3.
Not tried: a real search API or plugin with count > 6; the full suite
runs after the push.
2026-11-06 00:31:14 +08:00

161 lines
5.2 KiB
Go

package gateway
import (
"context"
"errors"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/gwtool"
"github.com/yetone/magpie/internal/provider"
)
// The tool point's web search with plugins/search on and off, for a user
// with a search API and a Kimi Code plan that searches by itself. On,
// magpie offers its web_search to a model that can't search and answers
// it with the API or the plan's own search service. Off, it offers none,
// the request goes to the vendor without it, and a search asked of magpie
// anyway (the search MCP server, Codex's in ChatGPT's place) says the
// plugin is off without asking the API or the plan. A vendor that
// searches by itself is sent its own web_search either way.
func TestSearchOnAndOff(t *testing.T) {
fresh(t)
relay, seen := relayOfEvery(t)
kimi := newKimiPlan(t)
plan := provider.Provider{ID: "kimi", Name: "Kimi Code", Preset: "kimi-code-cn", Key: "sk-kimi",
Chat: kimi.URL + "/coding/v1", Models: []string{"kimi-for-coding"}}
if err := provider.Save(plan); err != nil {
t.Fatal(err)
}
f := newSearchAPIs(t)
if err := provider.SetSearchAPI(f.api("tavily", "tvly-k")); err != nil {
t.Fatal(err)
}
s := New()
ask := func(native bool) string {
t.Helper()
p := provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"custom-model"}, Searches: native}
if native {
p.Anthropic = relay.URL
} else {
p.Chat = relay.URL + "/v1"
}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
_, before := seen()
body := `{"model":"relay/custom-model","max_tokens":100,"messages":[{"role":"user","content":"news?"}],"tools":[{"name":"Read","input_schema":{"type":"object"}},{"type":"web_search_20250305","name":"web_search"}]}`
if code, reply := post(t, "/v1/messages", body); code != 200 {
t.Fatalf("native %v: %d %s", native, code, reply)
}
paths, bodies := seen()
if len(bodies) != len(before)+1 {
t.Fatalf("native %v: upstream calls %v", native, paths[len(before):])
}
sent := bodies[len(bodies)-1]
if !strings.Contains(sent, "Read") {
t.Fatalf("native %v: the agent's own tool was dropped: %s", native, sent)
}
return sent
}
mcp := func() (string, error) {
return s.searchMCPRun(httptest.NewRequest("POST", "/mcp/search", nil), "latest go", nil)
}
// on
if !CanSearch() || !canSearchFor(provider.Provider{}) || !searchesOwn(plan) {
t.Fatal("on: magpie's search not offered")
}
if !strings.Contains(ask(false), "web_search") {
t.Error("on: a model that can't search wasn't given magpie's web_search")
}
if !strings.Contains(ask(true), "web_search_20250305") {
t.Error("on: a vendor that searches by itself wasn't sent its own web_search")
}
if _, err := mcp(); err != nil {
t.Errorf("on: MCP search: %v", err)
}
if _, _, err := s.webSearch(searchingOn(context.Background(), plan), "latest go"); err != nil {
t.Errorf("on: the plan's own search: %v", err)
}
f.mu.Lock()
asked := len(f.asked)
f.mu.Unlock()
kimi.mu.Lock()
searched := len(kimi.searched)
kimi.mu.Unlock()
if asked == 0 || searched == 0 {
t.Fatalf("on: the API was asked %d times, the plan %d", asked, searched)
}
// off
withoutSearch(t)
if CanSearch() || canSearchFor(provider.Provider{}) || canSearchFor(plan) || searchesOwn(plan) {
t.Error("off: magpie's search still offered")
}
if sent := ask(false); strings.Contains(sent, "web_search") {
t.Errorf("off: magpie's web_search offered: %s", sent)
}
if !strings.Contains(ask(true), "web_search_20250305") {
t.Error("off: a vendor that searches by itself lost its own web_search")
}
if _, err := mcp(); err == nil || !strings.Contains(err.Error(), "Web search plugin") {
t.Errorf("off: MCP search said %v, not that the plugin is off", err)
}
for _, ctx := range []context.Context{context.Background(), searchingOn(context.Background(), plan)} {
if _, _, err := s.webSearch(ctx, "latest go"); !errors.Is(err, errSearchOff) {
t.Errorf("off: search said %v", err)
}
}
f.mu.Lock()
defer f.mu.Unlock()
kimi.mu.Lock()
defer kimi.mu.Unlock()
if len(f.asked) != asked || len(kimi.searched) != searched {
t.Errorf("off: the API was asked %v, the plan %v", f.asked[asked:], kimi.searched[searched:])
}
}
// The magpie serving the gateway, and only while it serves, offers
// magpie's tools only as their plugins serve them (gwtool.Gate): with
// plugins/search off, not its web search.
func TestServingGatesTools(t *testing.T) {
fresh(t)
t.Setenv("MAGPIE_ADDR", "127.0.0.1:0") // never where a magpie runs
searchOn()
defer func() { searchOn = gwtool.Serve(gwtool.WebSearch) }()
if !gwtool.Served(gwtool.WebSearch) {
t.Fatal("gated before serving")
}
s := New()
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
done := make(chan error, 1)
go func() { done <- s.ListenAndServe(ctx) }()
deadline := time.Now().Add(3 * time.Second)
for {
s.lnMu.Lock()
ln := s.ln
s.lnMu.Unlock()
if ln != nil {
break
}
if time.Now().After(deadline) {
t.Fatal("gateway did not bind")
}
time.Sleep(10 * time.Millisecond)
}
if gwtool.Served(gwtool.WebSearch) {
t.Error("serving, with the plugin off: web search still served")
}
cancel()
if err := <-done; err != nil {
t.Fatal(err)
}
if !gwtool.Served(gwtool.WebSearch) {
t.Error("gate held after serving ended")
}
}