Files
magpie/internal/gateway/wire_effort.go
T
yetone 2926b7beff usage, routing: the model id a request went out under is kept and shown
daoche_wuzui on X asked how magpie serves Antigravity and whether a
request goes out under a suffixed model: nothing showed the id actually
sent. Antigravity's plugin turns gemini-3.7-flash at high effort into
gemini-3.7-flash-high, and a relay's upstream name (ModelWires) renames
a model too; the ledger and Routing showed only the routed model.

- A plugin's reply may carry X-Magpie-Sent-Model
  (provider.SentModelHeader). forward() reads it from plugin providers
  only (a non-plugin's header is ignored) and deletes it, so it never
  reaches the agent. Without it, provider.SentNameOnIn (the upstream
  name, or Antigravity's variant) is used.
- wireName keeps the id only when it differs from the model. It goes in
  usage.Record.Wire (`wire`, omitempty), the trace's Try.Wire, the
  ledger cache and a last CSV column, sent_model (docs/reference.md).
- Usage › Requests: the Sent cell and the detail show the sent id when
  there is one, with "model → sent id" on hover. Routing: a try sent
  under another id gets one aside line, "Sent to the vendor as {model}"
  (zh, zh-TW, ja, de).
- Antigravity's mover min is 0.2.4: plugins 67fb1258 sets the header on
  every reply (stream, whole and errors); its tgz is on the plugins
  repo's dist branch.

Run at this head (ef997d186 + this), on mote:
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok.
- go test -tags nogui -count=1 -timeout 45m ./...: every package ok but
  internal/gateway, red only in TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes,
  red 5 of 5 alone at origin/main ef997d186 too (#7117, taken).
- GUI: the 46 files that reach the Usage ledger, Routing's tries and
  i18n (i18n-*, ledger-*, usage-ledger-*, routing-try-fixed, sent-model
  and the files that read the Sent cell or an aside): 230 ok; red only
  aside-native (#6212) and usage-ledger-chart's remembered metric
  (#6790), both red the same way at origin/main ef997d186. The whole
  GUI suite (1609 files) was still running at push; its reds so far are
  in account-arrange and the Codex lone-capped-account files.
- sent-model.test.cjs: 10/10, Chromium and WebKit, en zh zh-TW ja de.
- Plugin: bun test packages/antigravity 136 pass, check.mjs ok.

Rebased onto 593ea437d (three gateway/routing commits) before the push,
then again: gofmt; vet of gateway, usage and provider for darwin, linux
and windows; the new tests with the gateway's Wire, Ultrafast and Prefix
tests; sent-model and i18n-keys (14 pass): ok.

Broken by hand, each red:
- no noteWireModel call: TestPluginSentModelIsKept;
- the IsPlugin check removed: the spoofed-header case;
- no SentNameOnIn fallback: TestUpstreamNameSentIsKept;
- an id equal to the model kept: both tests;
- the old GUI assets: every sent-model case; the old usage page.js
  alone: the Sent cell assertion;
- the plugin's old index.mjs: its new test (31 pass, 1 fail).

Not tried: a real Antigravity account (the plugin's header was tested
with its fake vendor only).
2026-11-19 14:45:14 +08:00

104 lines
3.2 KiB
Go

package gateway
import (
"cmp"
"context"
"net/http"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// thinkingBackOn is req with the thinking off it asked for taken back, for
// a model that refused it: at the least the model has, as a request asking
// none was sent before thinking could be turned off (#1636).
func thinkingBackOn(p provider.Provider, model string, req *Request) *Request {
r := *req
r.ThinkOff = false
if r.Effort == "" || offEffort(r.Effort) {
r.Effort = onEffort(p, model)
}
return &r
}
// wireAt is where a translated request leaves the reasoning it was built
// at, for the log (wireEffortOf); set says it was.
type wireAt struct {
effort string
set bool
// tier: the service tier the try's request went out at, whichever
// path sent it (noteWireTier)
tier string
// model: the model id the plugin the try went to said it sent its
// vendor (noteWireModel)
model string
}
type wireAtKey struct{}
// withWireAt is ctx carrying w, which forwardTranslated fills.
func withWireAt(ctx context.Context, w *wireAt) context.Context {
return context.WithValue(ctx, wireAtKey{}, w)
}
// noteWireEffort leaves, where the request's context has a place for it,
// the reasoning req goes to to at.
func noteWireEffort(ctx context.Context, to provider.Protocol, req *Request) {
w, _ := ctx.Value(wireAtKey{}).(*wireAt)
if w == nil {
return
}
if e, ok := wireEffort(to, req); ok {
w.effort, w.set = e, true
}
}
// wireEffort is the reasoning a request built from req asks the model for,
// in the words the log uses: what the client asked can go as another
// level once translated — effort none and minimal read as low where the
// model's levels don't say it can stop (effortOf), thinking turned off
// goes as off to Anthropic's API — and the log said the one asked while
// another was sent (#1636). false for an API whose builder says it its own
// way (Gemini's levels and budgets), which the log leaves as asked.
func wireEffort(to provider.Protocol, req *Request) (string, bool) {
switch {
case req.GeminiCompat:
return "", false
case to == provider.Anthropic:
if req.ThinkOff {
return "none", true
}
return req.Effort, true
case to == provider.Chat || to == provider.Responses:
return req.Effort, true
}
return "", false
}
// noteWireModel leaves, where the request's context has a place for it, the
// model id a plugin's answer res says it sent its vendor
// (provider.SentModelHeader). Only a plugin is taken at its word: a vendor
// or a relay naming one is not what magpie sent.
func noteWireModel(ctx context.Context, p provider.Provider, res *http.Response) {
if res == nil || !p.IsPlugin() {
return
}
m := strings.TrimSpace(res.Header.Get(provider.SentModelHeader))
res.Header.Del(provider.SentModelHeader)
w, _ := ctx.Value(wireAtKey{}).(*wireAt)
if w == nil || m == "" || len(m) > 200 {
return
}
w.model = m
}
// wireName is the model id a try went out under, when it is another than
// model, the id magpie asked it for: what the plugin said it sent, else the
// name the user gave the model upstream (sentAs, provider.SentNameOnIn).
func wireName(model, plugin, sentAs string) string {
if w := cmp.Or(plugin, sentAs); w != model {
return w
}
return ""
}