Files
magpie/internal/gateway/search_codex.go
T
yetone 73c98ebc20 gateway: a search API's or plan service's ask is routed and archived (axelburks)
axelburks on Discord: when magpie searches for a model through a search
API, the Routing page doesn't say which API answered, and Usage →
Requests lists the row but has no request archive, so the request and
response sent to the API can't be seen.

A model's search (searchWith) was already routed and archived. A search
API's ask (askSearchAPIKeys, a plugin's search hook, a signed-in search
API) and a plan's own search service (Kimi Code, GLM Coding Plan,
WorkBuddy, Codex's searchBy, and searchBy's Kimi branch, which wrote no
row at all before) only wrote a usage row.

Now every ask that writes a row goes through keepSearch
(search_archive.go): a done web_search route, agent magpie, For the
agent searched for, the API's name as model and its host as provider,
seated on the masked key or account; a Recent call with ring-cut
bodies; and with the request archive on, archiveOwn of the exchange as
sent. The row gets RouteID and Archive, which the Requests tab already
opens. Each ask has a searchWire in its context, filled where it is sent
(tapSearch tees the answer as read; the last exchange stands, so GLM's
MCP tools/call is the one kept). A plugin's search is noted as what
magpie handed the hook (key masked) and what came back (PLUGIN method).
The key is replaced by MaskSearchKey in path, query, headers and both
bodies before anything is kept; the archive's own scrub runs on top.
Bodies are kept up to RequestArchiveMaxMB (longer marked cut); archive
off keeps nothing on disk, the route and Recent call stay.

Run (mote's disk filled mid-work, see below; the rest on WSL
magpie-desktop via wsl-run):
- mote-run: go test -run 'Search|Archive' ./internal/gateway ok; GUI
  usage-ledger-search, routing-side-calls, request-archive-usage in
  chromium+webkit, every language and width: 38 pass.
- wsl-run: go test -run 'Search|Archive' ./internal/gateway ok; go vet
  ./internal/gateway ok; GOOS=windows go vet ./internal/gateway
  ./internal/gui ok; GOOS=darwin CGO_ENABLED=0 go vet ./internal/gateway
  ok; GUI usage-ledger-search, routing-side-calls, request-archive-usage,
  search-instead, search-mcp in both engines: 68 pass, 0 fail.
- gofmt -l clean.

Broken by hand: keepSearch's call commented out turns all five new
gateway tests red; taken()'s key masking turned off turns
TestSearchAPIAskIsRoutedAndArchived and TestSearchAPIKeyInQueryNotArchived
red. TestSearchRowKeepsRouteAndArchive (internal/gui) is a guard that the
row's RouteID and Archive reach /api/usage/requests; it passed on the
old code too.

Not tried: real Tavily/Brave/Kimi/GLM/WorkBuddy/Codex accounts (httptest
stubs only); Linux go vet of internal/gui (WSL lacks gtk4/webkitgtk);
web_fetch (search_fetch.go) and a vendor's own search (search_native.go),
which magpie doesn't send, are unchanged. Full suite runs after the push.
No GUI asset, API call or i18n string changed.
2026-11-05 12:40:35 +08:00

235 lines
7.9 KiB
Go

package gateway
import (
"bytes"
"cmp"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// A ChatGPT account searches the web for a model that can't by the ChatGPT
// backend's own search, the one Codex's web.run tool runs (AlphaSearchPath),
// not by a model of the account asked to search: the search is answered by
// itself, with no model turn, as Codex asks it (codex-rs ext/web-search,
// tool.rs: a SearchRequest, the response's output given to the model as the
// tool's result). On a real Plus account (2026-11-03) 151 such searches
// left the 5-hour window where it was, each answered in about 2s, while 51
// searches by gpt-6-luna through /responses (about 9k tokens in each) took
// 1% of it, each in 7-15s (lucy_85908 on Discord). The account's model
// searches only when the backend's search fails, or when Settings names a
// model of it.
// codexSearchModel is the "model" a ChatGPT account searches with when it
// searches by the backend's search: "<provider>/@search" in Settings'
// Searcher. No model id starts with "@".
const codexSearchModel = "@search"
// codexSearchBudget is the max_output_tokens Codex sends with a search: the
// truncation budget of its models' tool output (10,000 tokens). The backend
// answered the same whatever it was (1,000 to 10,000 tried).
const codexSearchBudget = 10000
// codexSearches is whether p searches by the ChatGPT backend's search: a
// ChatGPT account (Codex's sign-in).
func codexSearches(p provider.Provider) bool {
return p.Account != nil && p.Account.Agent == "codex"
}
// webAccessKey holds the client's web search mode, as Codex sends it in
// external_web_access: false for "cached", true for "live", an object for
// "indexed".
type webAccessKey struct{}
// withWebAccess is ctx whose searches are made in the mode v says, as the
// client sent it; none sent leaves ctx as it is.
func withWebAccess(ctx context.Context, v json.RawMessage) context.Context {
if len(bytes.TrimSpace(v)) == 0 || string(bytes.TrimSpace(v)) == "null" {
return ctx
}
return context.WithValue(ctx, webAccessKey{}, v)
}
// webAccess is the external_web_access a search with ctx is sent with: the
// client's (#5018: Codex's web_search = "cached" was dropped, and every
// search went live), else true, a live search, as Codex's "live" sends.
func webAccess(ctx context.Context) json.RawMessage {
if v, ok := ctx.Value(webAccessKey{}).(json.RawMessage); ok {
return v
}
return json.RawMessage("true")
}
// codexSearchReply is the ChatGPT backend's SearchResponse: output is what
// Codex shows the model, results the pages it names.
type codexSearchReply struct {
Output *string `json:"output"`
Results []struct {
RefID string `json:"ref_id"`
Title string `json:"title"`
URL string `json:"url"`
Snippet string `json:"snippet"`
} `json:"results"`
}
// codexSearchRule is the line between the pages of a search's output.
var codexSearchRule = "\n" + strings.Repeat("-", 80) + "\n"
// codexSearchBy searches with a ChatGPT account: by the backend's search,
// then, when it fails, by the account's small model.
func (s *Server) codexSearchBy(ctx context.Context, p provider.Provider, query string) (string, []Hit, error) {
said, hits, err := s.askOwnSearch(ctx, p, provider.CodexBase, query, s.codexSearch)
if err == nil || ctx.Err() != nil {
return said, hits, err
}
m := searcherModel(p)
if m == "" {
return "", nil, err
}
log.Printf("web search on ChatGPT (%s) failed, searching with %s: %v", p.Name, m, err)
said, hits, next := s.searchWith(ctx, p, m, query, searchEffort(p, m))
if next != nil {
return "", nil, errors.Join(err, next)
}
return said, hits, nil
}
// codexSearch asks the ChatGPT backend's search, as Codex's web.run does.
func (s *Server) codexSearch(ctx context.Context, p provider.Provider, query string) (string, []Hit, error) {
ctx, cancel := context.WithTimeout(s.metered(p.Via(ctx), p, ""), searchTimeout/2)
defer cancel()
q, err := maskedQuery(ctx, query, &p)
if err != nil {
return "", nil, err
}
// the model a search names is the turn's in Codex; the backend answers
// the same whichever it is, so the account's own small model
model := searcherModel(p)
if model == "" {
model = "gpt-5"
}
body, _ := json.Marshal(map[string]any{
"id": newUUID(),
"model": model,
"commands": map[string]any{"search_query": []map[string]any{{"q": q}}},
"settings": map[string]any{
"allowed_callers": []string{"direct"},
"external_web_access": webAccess(ctx),
},
"max_output_tokens": codexSearchBudget,
})
who := p.Name + ": "
r, err := http.NewRequestWithContext(ctx, http.MethodPost, provider.CodexBase+AlphaSearchPath, bytes.NewReader(body))
if err != nil {
return "", nil, err
}
r.Header.Set("Content-Type", "application/json")
// a nil body: the search is answered in JSON, not streamed
if err := p.Sign(ctx, r, provider.Responses, nil); err != nil {
return "", nil, errors.New(who + err.Error())
}
if why, revoked := provider.RevokedSignIn(p.Account, r); revoked {
return "", nil, errors.New(who + why)
}
res, err := s.client.Do(r)
res = tapSearch(ctx, r, body, res)
if err != nil {
if ctx.Err() != nil {
return "", nil, fmt.Errorf("%sno answer in time", who)
}
return "", nil, fmt.Errorf("%s%w", who, err)
}
defer res.Body.Close()
noteSignIn(ctx, p.Account, r, res)
b, _ := io.ReadAll(io.LimitReader(res.Body, 4<<20))
if res.StatusCode >= 300 {
err := errors.New(who + provider.APIError(b, res.Status))
if res.StatusCode == http.StatusTooManyRequests {
err = cantServe{err}
}
return "", nil, err
}
var out codexSearchReply
if err := json.Unmarshal(b, &out); err != nil || out.Output == nil {
return "", nil, fmt.Errorf("%sunexpected reply: %s", who, provider.APIError(b, res.Status))
}
pages := codexPages(*out.Output, out)
if len(pages) == 0 {
return "", nil, fmt.Errorf("%s%w", who, errFoundNothing)
}
said, hits := pagesSaid(pages)
return said, hits, nil
}
// codexPages are the pages a search's output names, in its order: each
// is "Title (URL)", then a line opening with its citation mark (the
// private-use "\ue200cite\ue202turn0search0\ue201") and "[wordlim: N]",
// then what the page says, and a rule of 80 dashes before the next. The
// results name each page's title and address by its ref_id, which the
// mark carries.
func codexPages(output string, out codexSearchReply) []foundPage {
type result struct{ title, url, snippet string }
byRef := map[string]result{}
for _, x := range out.Results {
if x.RefID != "" && x.URL != "" {
byRef[x.RefID] = result{x.Title, x.URL, x.Snippet}
}
}
var pages []foundPage
for _, block := range strings.Split(output, codexSearchRule) {
head, rest, _ := strings.Cut(strings.TrimSpace(block), "\n")
title, link := head, ""
if i := strings.LastIndex(head, " ("); i >= 0 && strings.HasSuffix(head, ")") {
title, link = head[:i], head[i+2:len(head)-1]
}
ref := ""
if i := strings.Index(rest, "\ue202"); i >= 0 {
if j := strings.Index(rest[i:], "\ue201"); j >= 0 {
ref = rest[i+len("\ue202") : i+j]
rest = rest[i+j+len("\ue201"):]
}
}
rest = strings.TrimSpace(rest)
if strings.HasPrefix(rest, "[wordlim:") {
if _, after, ok := strings.Cut(rest, "]"); ok {
rest = after
}
}
text := rest
if r, ok := byRef[ref]; ok {
title, link = cmp.Or(strings.TrimSpace(r.title), title), r.url
if strings.TrimSpace(text) == "" {
text = r.snippet
}
}
if !strings.HasPrefix(link, "http://") && !strings.HasPrefix(link, "https://") {
continue
}
title = searchPlain(title)
if title == "" {
title = link
}
pages = append(pages, foundPage{Title: title, URL: link, Text: clipText(searchPlain(strings.Map(dropPrivateUse, text)), pageText)})
if len(pages) == searchHits {
break
}
}
return pages
}
// dropPrivateUse leaves out the private-use characters OpenAI marks
// citations with.
func dropPrivateUse(r rune) rune {
if r >= 0xe000 && r <= 0xf8ff {
return -1
}
return r
}