Files
magpie/internal/gateway/codex_search.go
T
yetone 3c9d79d282 gateway: a key can set how the routing groups it asks for route (justforlxz on X)
justforlxz asked on X for a routing strategy among a gateway key's
options. A key now has Routing: unset follows each group's own (the
default), "smart" is the default Quota first, or any built-in routing
(order, drain, rotate, usage, pace, weight, free, fast, cheap, expiring,
fit) or a plugin's <plugin>/<name>. Manual is refused, and a Manual group
still goes to its pick. A provider or nested group with its own routing
keeps it (#576), as under the group's own.

- access: Key.Routing, Identity.Routing, the routing-key action
  (CleanRouting). An issued key that sets none follows its issuer's.
- provider: GroupRouting is the list of routings a group takes. SaveGroup
  and CleanRouting both use it.
- gateway: keyRouted is applied wherever a request's group is resolved:
  handle (Chat, Messages, Responses, and the masked-route move),
  embeddings/rerank, draw, video, a System One decision group and relayed
  search. count_tokens plans no group. planned carries the routing the plan
  was made by, so leastHeld weighs members the same way and the trace's
  group shows it.
- CLI: magpie gateway-key routing <id> [follow|smart|...].
- GUI: a "Group routing" badge on the key row, hidden until hover like
  the tier and masking badges, opens the app's menu (openProtoMenu). The
  routings the rotation plugin serves are left out when it is off. zh,
  zh-TW, ja and de strings are included.
- docs/subsystems/gateway-routing.md: the paragraph after ServiceTier.

Run (at cf5840a37 rebased on eb1da1572):
- gofmt -l: clean. On mote: go vet ./..., GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./..., and go build for darwin, linux
  (nogui) and windows: all ok.
- On mote, go test -count=1 -timeout 45m for internal/access, cli,
  provider, gui and gateway: access, cli and provider ok. internal/gui
  red only on TestBuiltinsTranslated (#7257) and
  TestSettingsSaveKeepsWhatItDoesNotSend (#7240), both red on main.
  internal/gateway red only on TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes
  (#7117, red on main). A doneHold "close of closed channel" panic was
  logged by the HTTP server (#6690).
- On mica, the GUI files gateway-key-*, gateway-keys-*,
  gateway-caller-keys, i18n-keys, i18n-en-no-cjk and key-row-chips-fit in
  Chromium and WebKit, 5 languages, 1100/420 widths: 549 of 550 pass. The
  one red is gateway-key-account-groups (rename typed as "DayDaytime"). It
  is red alone 2 of 3 on origin/main eb1da1572 too, filed as #7316.
- Red on the old code, broken by hand: TestKeyGroupRouting fails on Chat,
  Messages and Responses with keyRouted made a no-op (the Free first key
  goes to the paid member first). TestKeyRouting,
  TestGatewayKeyRoutingCLI and gateway-key-routing.test.cjs don't build or
  find their field, action or badge on the old code.
- TestKeyGroupRouting's Manual case stays green with keyRouted's Manual
  guard removed, because a Manual group is sent to its pick before
  routing. The guard is held by TestKeyRoutedCases.

- Rebased on 6635dec7c (it changes i18n.js and the core budget). At this
  head on mote: internal/gui -run Budget|I18n|Strings|Keys ok, the
  access/cli Routing tests ok, and gateway-key-routing, gateway-key-tier,
  i18n-keys and i18n-en-no-cjk in both engines: 48 of 48 pass.

Not run or tried: the full go test ./... and the whole GUI suite (both
mote and mica were running other sessions' GUI suites, at load 27/18 and
70/12). Windows and Linux test runs. A real gateway with a real key and
real accounts.
2026-11-20 16:01:53 +08:00

342 lines
12 KiB
Go

package gateway
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"log"
"net/http"
"slices"
"strings"
"time"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
)
// AlphaSearchPath is where Codex's own web search, its web.run tool, goes:
// "alpha/search" under the model provider's base URL, so /v1/alpha/search
// on magpie's provider and CodexPath+"/alpha/search" when Codex is signed in
// to ChatGPT. Codex offers web.run since 0.143 for a provider that sends
// x-openai-actor-authorization, as magpie's does, on a model whose catalog
// entry says use_responses_lite, as a ChatGPT account's do in magpie
// (codexcat copies Codex's own entry). Unanswered, the 404 ended the turn:
// Codex takes any failure of web.run as fatal (anton on Discord).
const AlphaSearchPath = "/alpha/search"
// alphaSearchRequest is the part of Codex's SearchRequest magpie reads; the
// rest goes on to the ChatGPT backend as it came.
type alphaSearchRequest struct {
Model string `json:"model"`
Commands struct {
SearchQuery []alphaQuery `json:"search_query"`
ImageQuery []alphaQuery `json:"image_query"`
} `json:"commands"`
Settings struct {
// Codex's web_search mode: false "cached", true "live"
ExternalWebAccess json.RawMessage `json:"external_web_access"`
} `json:"settings"`
}
type alphaQuery struct {
Q string `json:"q"`
Domains []string `json:"domains"`
}
// alphaSearchReply is Codex's SearchResponse: output is what the model is
// shown as the tool's result.
type alphaSearchReply struct {
EncryptedOutput *string `json:"encrypted_output"`
Output string `json:"output"`
}
// alphaSearch answers POST /v1/alpha/search. A ChatGPT account's model
// goes to the ChatGPT backend's search, as Codex sends it there itself,
// on the account's sign-in. Any other model's search_query is searched as
// magpie searches for a model that can't (webSearch). What magpie can't
// do is said in the tool's output with a 200, which the model reads; a
// failure status would end the turn.
func (s *Server) alphaSearch(w http.ResponseWriter, r *http.Request) {
body, ok := s.readRequestBody(w, r, provider.Responses, 0)
if !ok {
return
}
s.alphaSearchBody(w, r, body)
}
func (s *Server) alphaSearchBody(w http.ResponseWriter, r *http.Request, body []byte) {
var q alphaSearchRequest
if err := json.Unmarshal(body, &q); err != nil {
writeError(w, provider.Responses, http.StatusBadRequest, "magpie couldn't read the web search request: "+err.Error())
return
}
if q.Model == "" {
writeError(w, provider.Responses, http.StatusBadRequest, "the web search request names no model")
return
}
note := ""
if ons, model := chatGPTSearchers(r, q.Model); len(ons) > 0 {
ts := make([]searchTarget, len(ons))
for i, on := range ons {
ts[i] = searchTarget{p: on, model: model, url: provider.CodexBase + AlphaSearchPath}
}
answered, why := s.alphaSearchOn(w, r, body, ts)
if answered {
return
}
// a failure would end the turn: magpie searches in its place
log.Printf("web search on ChatGPT for %s: %s", q.Model, why)
note = "ChatGPT's web search failed (" + why + "); magpie searched in its place.\n\n"
} else if ts := s.relaySearchers(r, q.Model); len(ts) > 0 {
answered, why := s.alphaSearchOn(w, r, body, ts)
if answered {
return
}
log.Printf("web search on %s for %s: %s", ts[0].p.Name, q.Model, why)
note = ts[0].p.Name + "'s web search failed (" + why + "); magpie searched in its place.\n\n"
}
s.alphaSearchHere(w, r, q, note)
}
// searchTarget is where a web.run search goes: the provider, its model
// as it knows it, and the address its search is at.
type searchTarget struct {
p provider.Provider
model string
url string
}
// upstreamSearches is whether p answers Codex's web.run itself, at
// alpha/search under its Responses address: a key+URL provider set to
// search the web by itself (Searches, #359), as a relay of OpenAI's API
// or of the ChatGPT backend does. Such a relay's web.run was searched by
// magpie, and the relay's own search never used (YIXIE233 on X).
func upstreamSearches(p provider.Provider) bool {
return p.Account == nil && !p.IsPlugin() && p.Searches && p.Base(provider.Responses) != ""
}
// relaySearchers is where a web.run search for model goes when a provider
// set to search by itself serves it (upstreamSearches): the model's
// provider, or the members of a routing group, in the order a turn of
// the model is routed, those that search by themselves alone. None when
// the one a turn goes to first can't, or Settings has magpie search for
// every model (magpieSearches): magpie searches then, as before.
func (s *Server) relaySearchers(r *http.Request, model string) []searchTarget {
who, keyHeld := keyHolds(r)
ref := model
if !provider.IsGroupRef(ref) {
if gid, ok := keyGroupFor(r, ref); ok {
ref = gid
}
}
var cs []candidate
if provider.IsGroupRef(ref) {
g, ms, ok := provider.FindGroup(ref)
if !ok {
return nil
}
if keyHeld {
keep := groupKeeps(who, g, ms)
ms = slices.DeleteFunc(slices.Clone(ms), func(m provider.Member) bool { return !keep[m.Provider.ID+"/"+m.Model] })
}
cs, _ = s.planGroup(keyRouted(access.Caller(r.Context()), g.Live()), ms, provider.Responses)
} else {
p, bare, ok := provider.Resolve(model)
if !ok || keyHeld && !modelAllowed(who, p, bare) {
return nil
}
cs = s.candidates(p, bare, provider.Responses)
}
var out []searchTarget
for _, c := range cs {
if !accountAllowed(who, c) {
continue
}
if !upstreamSearches(c.p) || magpieSearches(r.Context(), c.p) {
if len(out) == 0 {
return nil // the turn goes to one that can't: magpie's
}
continue
}
out = append(out, searchTarget{p: c.p, model: c.model, url: c.p.Base(provider.Responses) + AlphaSearchPath})
}
return out
}
// chatGPTSearchers is the ChatGPT accounts a web search for model may go
// to, the one it is served on first and those resting or with a revoked
// sign-in last, with the model as the backend knows it: none when model
// isn't a ChatGPT account's, or the calling key may use none of them.
func chatGPTSearchers(r *http.Request, model string) ([]provider.Provider, string) {
p, bare, ok := provider.Resolve(model)
if !ok || p.Account == nil || p.Account.Agent != "codex" {
return nil, ""
}
who, keyHeld := keyHolds(r)
if keyHeld && !modelAllowed(who, p, bare) {
return nil, ""
}
var all []provider.Provider
if !p.OwnPaused() {
all = append(all, p)
}
all = append(all, p.AlsoOn()...)
var ready, later []provider.Provider
for _, o := range all {
c := candidate{p: o, model: bare, rest: o.ID}
if !accountAllowed(who, c) {
continue
}
if resting(c) || provider.SignInMarked(o.Account) {
later = append(later, o)
continue
}
ready = append(ready, o)
}
return append(ready, later...), bare
}
// alphaSearchOn sends the search to each target in turn — the ChatGPT
// backend on each account, or a provider that searches by itself — the
// next asked when one is refused, limited or fails, and relays the
// answer as it is. It says whether one answered: when none did, nothing
// is written, and the search is magpie's (alphaSearchHere), with why
// returned.
func (s *Server) alphaSearchOn(w http.ResponseWriter, r *http.Request, body []byte, ts []searchTarget) (bool, string) {
start := time.Now()
// as written only when every account it may be asked on is set to go
// unmasked (provider.SkipsRedaction, isee1_1 on Discord); magpie's own
// search in its place masks its query itself (maskedQuery)
if len(ts) > 0 && !slices.ContainsFunc(ts, func(t searchTarget) bool { return !t.p.SkipsRedaction() }) {
return s.alphaSearchAsked(w, r, body, ts, start)
}
w, body, unmask, hits, err := redactedHits(r.Context(), w, body)
defer unmask()
if err != nil {
return false, err.Error()
}
noteMasked(r, "search", hits, sentOf(hits, body), 0, "openai", ts[0].model)
return s.alphaSearchAsked(w, r, body, ts, start)
}
// alphaSearchAsked is alphaSearchOn once body is as it goes.
func (s *Server) alphaSearchAsked(w http.ResponseWriter, r *http.Request, body []byte, ts []searchTarget, start time.Time) (bool, string) {
var errs []string
for _, t := range ts {
on, model := t.p, t.model
who := on.Name + ": "
if on.Account != nil {
who = "OpenAI (" + on.Account.User + "): "
}
ctx := on.Via(r.Context())
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.url, bytes.NewReader(withModel(body, model)))
if err != nil {
errs = append(errs, who+err.Error())
continue
}
for k, vs := range r.Header {
if codexHeader(k) {
req.Header[k] = vs
}
}
if on.Account == nil {
// a relay is asked as Codex asks it: its originator, and its
// User-Agent where the provider passes it on
if o := r.Header.Get("originator"); o != "" {
req.Header.Set("originator", o)
}
req.Header.Set("User-Agent", userAgentFor(on, r.Header.Get("User-Agent")))
}
req.Header.Set("Content-Type", "application/json")
// a nil body: the search is answered in JSON, not streamed
if err := on.Sign(ctx, req, provider.Responses, nil); err != nil {
errs = append(errs, who+err.Error())
continue
}
// a sign-in its vendor revoked isn't sent again (#1793)
if why, revoked := provider.RevokedSignIn(on.Account, req); revoked {
errs = append(errs, who+why)
continue
}
res, err := s.client.Do(req)
if err != nil {
if r.Context().Err() != nil {
return true, ""
}
errs = append(errs, who+err.Error())
continue
}
noteSignIn(ctx, on.Account, req, res)
c := Call{Time: start, From: provider.Responses, To: provider.Responses, Model: model, Provider: on.ID,
Agent: callerOf(r).agent, Via: callerOf(r).via, Kind: "web_search", Status: res.StatusCode}
if res.StatusCode < 200 || res.StatusCode > 299 {
msg, _ := io.ReadAll(io.LimitReader(res.Body, 4<<10))
res.Body.Close()
errs = append(errs, who+res.Status+": "+strings.TrimSpace(string(msg)))
c.Millis, c.Error = time.Since(start).Milliseconds(), res.Status
s.record(c)
continue
}
for k, vs := range res.Header {
if !hopHeader(k) {
w.Header()[k] = vs
}
}
w.WriteHeader(res.StatusCode)
io.Copy(w, res.Body)
res.Body.Close()
c.Millis = time.Since(start).Milliseconds()
s.record(c)
return true, ""
}
return false, strings.Join(errs, "; ")
}
// alphaSearchHere answers a search for a model no ChatGPT account serves:
// its search_query and image_query searched as magpie searches for a
// model that can't, the findings the tool's output. Opening, clicking or
// finding in a page is the ChatGPT backend's alone, and said so.
func (s *Server) alphaSearchHere(w http.ResponseWriter, r *http.Request, q alphaSearchRequest, note string) {
var queries []string
for _, x := range append(q.Commands.SearchQuery, q.Commands.ImageQuery...) {
text := strings.TrimSpace(x.Q)
if text == "" {
continue
}
for _, d := range x.Domains {
if d = strings.TrimSpace(d); d != "" {
text += " site:" + d
}
}
queries = append(queries, text)
}
if len(queries) == 0 {
writeJSON(w, http.StatusOK, alphaSearchReply{Output: note + "magpie can only search the web for " + q.Model + ": give web.run a search_query. Opening, clicking or finding in a page goes to the ChatGPT backend, for a ChatGPT account's model in magpie."})
return
}
ctx := withWebAccess(context.WithValue(r.Context(), searchForKey{}, &CallFor{Agent: callerOf(r).agent, Model: q.Model}), q.Settings.ExternalWebAccess)
var said []string
for _, query := range queries {
found, _, err := s.webSearch(ctx, query)
if err != nil {
if r.Context().Err() != nil {
return
}
log.Printf("web search for %s: %v", q.Model, err)
msg := "magpie couldn't search the web for " + query + ": " + err.Error()
if errors.Is(err, errNoSearcher) {
msg += ": add a search API in magpie's Settings → Models → Web search (or magpie search add <api> <key>), or a provider that can search"
}
said = append(said, msg)
continue
}
if len(queries) > 1 {
found = "Search: " + query + "\n" + found
}
said = append(said, found)
}
writeJSON(w, http.StatusOK, alphaSearchReply{Output: note + strings.Join(said, "\n\n")})
}