Claude Code's WebSearch and Codex's web_search never name a count, so magpie answered every such search with searchHits, 6 pages (#5545, mdwsk88: more pages for research-heavy work, fewer to save context). settings.SearchCount holds the count. 0 means the default, 6 (SearchCountDefault), and the most is 20 (SearchCountMost), the most Brave and Tavily return. Save refuses a value out of range, so nothing is written. Settings.SearchPages reads it. The gateway's searchCount falls back to it when a search names no count. The web_search MCP tool still answers a named count with that count, and its schema's default says the count set. It reaches every search API (Brave, Tavily, Exa, You, the plugins' searchers), as a named count already did. - Settings › Models › Web search: "Results per search", segments 3, 6, 10, 15 and 20, plus a count set by hand in settings.json. POST /api/settings/search-count sets it alone. settingsapi.Apply keeps it as it keeps search-all, so a save of the Settings form doesn't put it back. - CLI: magpie search count says the count; magpie search count <n> sets it; magpie search count default puts 6 back. A bad value is refused and nothing is written. - docs/subsystems/gateway-routing.md says it. - New strings are in zh, zh-TW, ja and de. Run at this tree (mote-run, macOS): - go test -count=1: internal/settings, settingsapi, cli, gateway (TestSearchMCPCount, TestSearchCountReachesEveryAPI, TestSearchCountSetting), and gui (TestSettingsSearchCountSetOnItsOwn, and TestSettingsSaveKeepsWhatItDoesNotSend with SearchCount set): ok. internal/gateway whole: ok with -timeout 45m (847s). internal/gui whole: ok but TestMainWindowCorners/macOS_26_SDK (#3949, mote is on macOS 27). The first cli run was red on TestEveryRouteHasACommandOrIsNamed: the new route had no CLI entry. cliForRoute maps it to search count now. - Broken by hand: searchCount returning searchHits again makes TestSearchCountSetting fail ("set 11: a search naming no count asks for 6, want 11"). Restored, it passes. - GUI: node --test --test-concurrency=4 over routing-*, usage-*, ledger-*, panel-*, call-*, search-*, searcher-*, settings-*, i18n-* and gui-* (Chromium and WebKit; the files cover en, zh, zh-TW, ja and de), with #2398 and #5545 in one tree: 4961 of 5016 ok. The 55 reds are in 12 files. i18n-keys' "a plugin's words are its own" names other files' strings (#5327, #5426, #5482). The other 11 files were run alone at dc49feca9 (this tree without either change) and at this tree: 52 and 51 reds, the same tests. The one apart is usage-focus-steady, which flakes (#3501). Each is filed: #5392, #3414, #5048, #3799, #3415, #3954, #4627, #4628, #5223, #1529. routing-canceled-row.test.cjs: 10 of 10. With the .rt-req.gone rule taken out, its dot is the answered green rgb(24, 154, 88) and it fails. Restored, it passes. - Not tried: a real Claude Code WebSearch through this tree with a live search API key. The tests use fake search APIs. Co-authored-by: mdwsk88 <11055210+mdwsk88@users.noreply.github.com> Reported-by: mdwsk88 (dawei on Discord)
252 lines
7.8 KiB
Go
252 lines
7.8 KiB
Go
package gateway
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"sync"
|
||
"testing"
|
||
|
||
"github.com/yetone/magpie/internal/plugin"
|
||
"github.com/yetone/magpie/internal/provider"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
)
|
||
|
||
// web_search on the MCP server takes a count (treeman on Discord): how
|
||
// many results, 6 when not given, held to 1 through 20. The plugin's
|
||
// search API is asked for that many and the tool says that many, though
|
||
// the API found more.
|
||
func TestSearchMCPCount(t *testing.T) {
|
||
fresh(t)
|
||
const spec = "@acme/opencode-mojeek-search@1.0.0"
|
||
plugged(t, spec, false)
|
||
if err := provider.SetSearchAPI(provider.SearchAPI{Vendor: "mojeek-search:mojeek", Key: "good-key-0003"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
var mu sync.Mutex
|
||
var counts []int
|
||
old := runPluginSearch
|
||
t.Cleanup(func() { runPluginSearch = old })
|
||
runPluginSearch = func(_ context.Context, _, _, _ string, count int, _, _ string) ([]plugin.SearchPage, error) {
|
||
mu.Lock()
|
||
counts = append(counts, count)
|
||
mu.Unlock()
|
||
var pages []plugin.SearchPage
|
||
for i := range 30 { // more than any count, as an API may give
|
||
pages = append(pages, plugin.SearchPage{Title: fmt.Sprint("page ", i), URL: fmt.Sprintf("https://ex.test/%d", i), Text: "text"})
|
||
}
|
||
return pages, nil
|
||
}
|
||
h := lanGuard(New().Handler())
|
||
const here = "127.0.0.1:50000"
|
||
|
||
var list struct {
|
||
Result struct {
|
||
Tools []struct {
|
||
Name string
|
||
InputSchema struct {
|
||
Properties map[string]struct {
|
||
Type string
|
||
Minimum, Maximum int
|
||
Default int
|
||
}
|
||
Required []string
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if err := json.Unmarshal(mcpPost(h, here, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`).Body.Bytes(), &list); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
c := list.Result.Tools[0].InputSchema.Properties["count"]
|
||
if list.Result.Tools[0].Name != "web_search" || c.Type != "integer" || c.Minimum != 1 || c.Maximum != 20 || c.Default != 6 ||
|
||
len(list.Result.Tools[0].InputSchema.Required) != 1 {
|
||
t.Fatalf("web_search's schema: %+v", list.Result.Tools[0])
|
||
}
|
||
|
||
for _, tc := range []struct {
|
||
arg string // the count as the call writes it, "" for none
|
||
want int
|
||
}{
|
||
{"", 6},
|
||
{`12`, 12},
|
||
{`20`, 20},
|
||
{`50`, 20},
|
||
{`0`, 1},
|
||
{`-3`, 1},
|
||
{`"8"`, 8},
|
||
{`9.7`, 9},
|
||
{`"many"`, 6},
|
||
{`null`, 6},
|
||
} {
|
||
args := `{"query":"latest go"}`
|
||
if tc.arg != "" {
|
||
args = `{"query":"latest go","count":` + tc.arg + `}`
|
||
}
|
||
mu.Lock()
|
||
counts = nil
|
||
mu.Unlock()
|
||
m := readMCP(t, mcpPost(h, here, `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"web_search","arguments":`+args+`}}`))
|
||
if m.Error != nil || m.Result.IsError || len(m.Result.Content) != 1 {
|
||
t.Fatalf("count %s: %+v", tc.arg, m)
|
||
}
|
||
said := m.Result.Content[0].Text
|
||
if n := strings.Count(said, " — https://ex.test/"); n != tc.want {
|
||
t.Errorf("count %s: said %d results, want %d", tc.arg, n, tc.want)
|
||
}
|
||
mu.Lock()
|
||
if len(counts) != 1 || counts[0] != tc.want {
|
||
t.Errorf("count %s: the plugin was asked for %v, want %d", tc.arg, counts, tc.want)
|
||
}
|
||
mu.Unlock()
|
||
}
|
||
}
|
||
|
||
// Every built-in search API, and a Kimi Code plan's search service, is
|
||
// asked for the count the search names, in its own field.
|
||
func TestSearchCountReachesEveryAPI(t *testing.T) {
|
||
fresh(t)
|
||
var mu sync.Mutex
|
||
var last string
|
||
f := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
b, _ := io.ReadAll(r.Body)
|
||
mu.Lock()
|
||
last = r.URL.RawQuery + " " + string(b)
|
||
mu.Unlock()
|
||
io.WriteString(w, `{}`)
|
||
}))
|
||
t.Cleanup(f.Close)
|
||
s := New()
|
||
ctx := withSearchCount(context.Background(), 12)
|
||
for _, c := range []struct{ vendor, want string }{
|
||
{"tavily", `"max_results":12`},
|
||
{"brave", "count=12"},
|
||
{"exa", `"numResults":12`},
|
||
{"firecrawl", `"limit":12`},
|
||
{"deepseek", `"count":12`},
|
||
{"volcengine", `"Count":12`},
|
||
{"wsa", `"Cnt":20`},
|
||
{"you", `"count":12`},
|
||
} {
|
||
s.askSearchAPI(ctx, provider.SearchAPI{Vendor: c.vendor, Key: "k", URL: f.URL}, "latest go")
|
||
mu.Lock()
|
||
if !strings.Contains(last, c.want) {
|
||
t.Errorf("%s was asked %q, not %s", c.vendor, last, c.want)
|
||
}
|
||
mu.Unlock()
|
||
}
|
||
s.askSearchAPI(withSearchCount(context.Background(), 3), provider.SearchAPI{Vendor: "wsa", Key: "k", URL: f.URL}, "q")
|
||
mu.Lock()
|
||
if !strings.Contains(last, `"Cnt":10`) {
|
||
t.Errorf("Yuanbao Search for 3 was asked %q, not its fewest, 10", last)
|
||
}
|
||
mu.Unlock()
|
||
|
||
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"}}
|
||
for _, n := range []int{0, 12} {
|
||
ctx := context.Background()
|
||
want := "| 6,false,30"
|
||
if n > 0 {
|
||
ctx, want = withSearchCount(ctx, n), fmt.Sprint("| ", n, ",false,30")
|
||
}
|
||
s.kimiSearch(ctx, plan, "latest go")
|
||
kimi.mu.Lock()
|
||
got := kimi.searched[len(kimi.searched)-1]
|
||
kimi.mu.Unlock()
|
||
if !strings.HasSuffix(got, want) {
|
||
t.Errorf("Kimi Code's search asked %q, want limit %d", got, n)
|
||
}
|
||
}
|
||
}
|
||
|
||
// A search that names no count, as Claude Code's WebSearch and Codex's
|
||
// web_search never do, is answered with the count set in Settings › Web
|
||
// search (#5545, mdwsk88), and web_search's schema says it as its
|
||
// default; a count the call names still wins, and with none set it is 6.
|
||
func TestSearchCountSetting(t *testing.T) {
|
||
fresh(t)
|
||
const spec = "@acme/opencode-mojeek-search@1.0.0"
|
||
plugged(t, spec, false)
|
||
if err := provider.SetSearchAPI(provider.SearchAPI{Vendor: "mojeek-search:mojeek", Key: "good-key-0003"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
var mu sync.Mutex
|
||
var counts []int
|
||
old := runPluginSearch
|
||
t.Cleanup(func() { runPluginSearch = old })
|
||
runPluginSearch = func(_ context.Context, _, _, _ string, count int, _, _ string) ([]plugin.SearchPage, error) {
|
||
mu.Lock()
|
||
counts = append(counts, count)
|
||
mu.Unlock()
|
||
var pages []plugin.SearchPage
|
||
for i := range 30 {
|
||
pages = append(pages, plugin.SearchPage{Title: fmt.Sprint("page ", i), URL: fmt.Sprintf("https://ex.test/%d", i), Text: "text"})
|
||
}
|
||
return pages, nil
|
||
}
|
||
h := lanGuard(New().Handler())
|
||
const here = "127.0.0.1:50000"
|
||
set := func(n int) {
|
||
t.Helper()
|
||
s := settings.Load()
|
||
s.SearchCount = n
|
||
if err := settings.Save(s); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
schemaDefault := func() int {
|
||
t.Helper()
|
||
var list struct {
|
||
Result struct {
|
||
Tools []struct {
|
||
InputSchema struct {
|
||
Properties map[string]struct{ Default int }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if err := json.Unmarshal(mcpPost(h, here, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`).Body.Bytes(), &list); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
return list.Result.Tools[0].InputSchema.Properties["count"].Default
|
||
}
|
||
call := func(args string) (said, asked int) {
|
||
t.Helper()
|
||
mu.Lock()
|
||
counts = nil
|
||
mu.Unlock()
|
||
m := readMCP(t, mcpPost(h, here, `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"web_search","arguments":`+args+`}}`))
|
||
if m.Error != nil || m.Result.IsError || len(m.Result.Content) != 1 {
|
||
t.Fatalf("%s: %+v", args, m)
|
||
}
|
||
mu.Lock()
|
||
defer mu.Unlock()
|
||
if len(counts) != 1 {
|
||
t.Fatalf("%s: the plugin was asked %v", args, counts)
|
||
}
|
||
return strings.Count(m.Result.Content[0].Text, " — https://ex.test/"), counts[0]
|
||
}
|
||
|
||
for _, tc := range []struct{ set, want int }{{11, 11}, {20, 20}, {1, 1}, {0, 6}} {
|
||
set(tc.set)
|
||
if got := searchCount(context.Background()); got != tc.want {
|
||
t.Errorf("set %d: a search naming no count asks for %d, want %d", tc.set, got, tc.want)
|
||
}
|
||
if got := schemaDefault(); got != tc.want {
|
||
t.Errorf("set %d: web_search's schema says %d by default, want %d", tc.set, got, tc.want)
|
||
}
|
||
if said, asked := call(`{"query":"latest go"}`); said != tc.want || asked != tc.want {
|
||
t.Errorf("set %d: no count said %d results, asked %d, want %d", tc.set, said, asked, tc.want)
|
||
}
|
||
if said, asked := call(`{"query":"latest go","count":4}`); said != 4 || asked != 4 {
|
||
t.Errorf("set %d: count 4 said %d results, asked %d", tc.set, said, asked)
|
||
}
|
||
}
|
||
}
|