What was wrong: WorkBuddy's models have no search tool of their own.
When magpie had nothing else that searches (no Claude or Codex account,
no search API), canSearchFor was false. magpie then took the
web_search_20250305 server tool out of Claude Code's WebSearch request,
the model answered from nothing, and WebSearch showed no results.
WorkBuddy's own client searches through a service of its plan.
Its CLI's WebSearch tool (WorkBuddy.app's
app.asar.unpacked/cli/dist/codebuddy.js, WEB_SEARCH_API_PATH) sends
POST <site>/agenttool/v1/search with
{"query","type":"text2text","max_results":5}.
What changed:
- search_workbuddy.go: wbSearch asks that service. Provider.Sign
signs the request as the plan's chats are signed, and Provider.Do
sends it, so it works the same for the built-in and for a WorkBuddy
moved onto its plugin (the plugin's fetch signs it, as for
/v3/config and images). No plugin change is needed.
- searchesOwn includes a WorkBuddy plan, as it does a Kimi Code plan.
Its own models' searches go there first, before magpie's picked
searcher and the search APIs. Other providers' models never use it.
It is off when provider search is off in Settings.
- A {code, msg} refusal (15001 is its search quota) is reported as an
error, not as an empty result.
- docs/subsystems/gateway-routing.md: new section, tests added to
Verification.
What was run:
- The real service, read-only, with the owner's desktop sign-in (China
build) and the plugin's WorkBuddy AI sign-in. Both answered 200 with
results, including with only the built-in's smaller header set. The
test fixture is WorkBuddy AI's real reply bytes.
- End to end with the real China-build account: a throwaway program
(not committed) under a sandbox HOME holding a copy of the desktop
sign-in. It sent Claude Code's literal WebSearch request through
gateway.New().Handler() to workbuddy/deepseek-v4.1-flash and got 200
with one web_search_tool_result block holding real URLs.
- New tests: TestWorkBuddySearchesForItsModels (built-in, Claude Code
2.1.285's literal request) and TestWorkBuddyPluginSearches (plugin).
- Broken by hand (wbSearches returning false): both go red. The
built-in test's reply is the model's "I can't search." with no
web_search_tool_result, which is the reporter's case.
- go test ./internal/gateway -run 'Search|WorkBuddy|Kimi|Vision|Spent|PassesOver'
- gofmt -l, go vet ./... (darwin), GOOS=linux go vet -tags nogui ./...,
and GOOS=windows go vet ./... are all clean.
- Full go test -timeout 40m ./... in a sandbox HOME at load 66-100:
47 packages ok. internal/gateway and internal/agent hit the 40m
package timeout. Gateway also had failures, each filed:
- TestOTelWaterfall* (#1794, fixed on main since).
- TestCodexHeardWithinItsIdleTimeout (#1800).
- TestIntentClassifierGroup (3.17s against a 3s bound, filed as
#1813).
All of them pass alone, and go test ./internal/gateway on its own is
ok (982s).
Not verified: WorkBuddy AI end to end through the gateway (its desktop
sign-in in the sandbox wasn't picked up; the service itself answered).
Not tried through a running magpie app with the plugin installed.
130 lines
4.4 KiB
Go
130 lines
4.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"cmp"
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A WorkBuddy plan, the China build's or WorkBuddy AI's, searches the web
|
|
// with its own search service, as WorkBuddy's own client does (its CLI's
|
|
// WebSearch tool, app.asar.unpacked/cli/dist/codebuddy.js: WEB_SEARCH_API_PATH):
|
|
// a POST to <site>/agenttool/v1/search of
|
|
//
|
|
// {"query":"…","type":"text2text","max_results":5}
|
|
//
|
|
// signed as the plan's chats are, answered with
|
|
//
|
|
// {"query","type","provider","results":[{"title","url","snippet","site","favicon"}],"total_results","response_time_ms"}
|
|
//
|
|
// or a refusal as {"code","msg"} (15001 is its search quota spent). Its
|
|
// models have no search tool of their own, so before, a Claude Code turn
|
|
// on WorkBuddy with nothing else in magpie that searches had the server
|
|
// tool taken away and WebSearch came back empty (#1798). As with a Kimi
|
|
// Code plan, a WorkBuddy model's searches are done here first, and only
|
|
// for its own models. It is the same for the built-in and for a WorkBuddy
|
|
// moved onto its plugin: magpie shapes the request, Sign and Do send it
|
|
// (the plugin's fetch signs it there).
|
|
|
|
// wbSearchPath is where a WorkBuddy site's search service is, and
|
|
// wbSearchResults how many results its client asks for.
|
|
const (
|
|
wbSearchPath = "/agenttool/v1/search"
|
|
wbSearchResults = 5
|
|
)
|
|
|
|
// wbSearches is whether p is a WorkBuddy plan that searches for its models.
|
|
func wbSearches(p provider.Provider) bool { return drawsWorkBuddy(p) }
|
|
|
|
// wbSearch asks p's WorkBuddy site to search the web.
|
|
func (s *Server) wbSearch(ctx context.Context, p provider.Provider, query string) (string, []Hit, error) {
|
|
origin := p.Origin(ctx)
|
|
if origin == "" {
|
|
return "", nil, fmt.Errorf("%s: no site to search with", p.Name)
|
|
}
|
|
// its client gives the service 20 s
|
|
ctx, cancel := context.WithTimeout(s.metered(p.Via(ctx), p, ""), searchTimeout/2)
|
|
defer cancel()
|
|
body, _ := json.Marshal(map[string]any{"query": query, "type": "text2text", "max_results": wbSearchResults})
|
|
r, err := http.NewRequestWithContext(ctx, http.MethodPost, origin+wbSearchPath, bytes.NewReader(body))
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
if err := p.Sign(ctx, r, provider.Chat, body); err != nil {
|
|
return "", nil, fmt.Errorf("%s: %w", p.Name, err)
|
|
}
|
|
r.Header.Set("Content-Type", "application/json")
|
|
r.Header.Set("Accept", "application/json")
|
|
r.Header.Set("X-Requested-With", "XMLHttpRequest")
|
|
if r.Header.Get("X-Request-ID") == "" {
|
|
id := wbSearchID()
|
|
r.Header.Set("X-Request-ID", id)
|
|
r.Header.Set("X-Conversation-Message-ID", id)
|
|
}
|
|
res, err := p.Do(s.client, r) // a moved one's through its plugin
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return "", nil, fmt.Errorf("%s: no answer in time", p.Name)
|
|
}
|
|
return "", nil, fmt.Errorf("%s: %w", p.Name, err)
|
|
}
|
|
defer res.Body.Close()
|
|
b, _ := io.ReadAll(io.LimitReader(res.Body, 4<<20))
|
|
if res.StatusCode >= 300 {
|
|
return "", nil, errors.New(p.Name + "'s search: " + provider.APIError(b, res.Status))
|
|
}
|
|
var out struct {
|
|
Code json.RawMessage `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Results *[]struct {
|
|
Title string `json:"title"`
|
|
URL string `json:"url"`
|
|
Snippet string `json:"snippet"`
|
|
Site string `json:"site"`
|
|
} `json:"results"`
|
|
}
|
|
if err := json.Unmarshal(b, &out); err != nil {
|
|
return "", nil, fmt.Errorf("%s's search: unexpected reply: %s", p.Name, provider.APIError(b, res.Status))
|
|
}
|
|
if c := strings.Trim(string(out.Code), `"`); c != "" && c != "0" && c != "null" {
|
|
return "", nil, fmt.Errorf("%s's search: %s (%s)", p.Name, cmp.Or(out.Msg, "refused"), c)
|
|
}
|
|
if out.Results == nil {
|
|
return "", nil, fmt.Errorf("%s's search: unexpected reply: %s", p.Name, provider.APIError(b, res.Status))
|
|
}
|
|
var pages []foundPage
|
|
for _, x := range *out.Results {
|
|
if x.URL == "" {
|
|
continue
|
|
}
|
|
title := cmp.Or(searchPlain(x.Title), searchPlain(x.Site), x.URL)
|
|
pages = append(pages, foundPage{Title: title, URL: x.URL, Text: clipText(searchPlain(x.Snippet), pageText)})
|
|
if len(pages) == searchHits {
|
|
break
|
|
}
|
|
}
|
|
if len(pages) == 0 {
|
|
return "", nil, fmt.Errorf("%s found nothing", p.Name)
|
|
}
|
|
said, hits := pagesSaid(pages)
|
|
return said, hits, nil
|
|
}
|
|
|
|
// wbSearchID is a request id as WorkBuddy's client makes them: a UUID's
|
|
// hex without its dashes.
|
|
func wbSearchID() string {
|
|
var b [16]byte
|
|
rand.Read(b[:])
|
|
return hex.EncodeToString(b[:])
|
|
}
|