Files
magpie/internal/gateway/side_effort.go
T
yetone dc2f0da820 gateway, gui: Image recognition and Web search pick how hard their model thinks (MOMO on Discord)
MOMO asked on Discord, "外置模型能不能提供思考深度选项": the model that
describes images (Image recognition, analyze_image) and the searcher's
model (e.g. Antigravity's Gemini 3 Flash) had no reasoning level of
their own to pick.

Settings › Models now has a chip beside each of those two pickers. It
opens the app's menu (openProtoMenu, no native select) and offers
Default plus only the levels that model has (Efforts, weakest first;
none shown when the model's levels aren't known, or the row is Off).
The pick is saved as settings.visionEffort / searcherEffort, one of
catalog.EffortRank or empty, and is checked on save. prefsKeep carries
both, so saving another setting keeps them.

Default keeps what magpie did before. Image recognition is still asked
at the least level the model has (classifyEffort), and the searcher
still gets no level. A picked level is fitted to the model's levels at
call time (fitEffort), and only these two calls carry it
(side_effort.go):
- askVisionWith sends reasoning_effort. The image cache key includes
  the level, so changing the level reads the image again.
- searchBody puts thinking (disabled for none, else enabled with
  budgetOf's budget) and output_config.effort (except for pre-adaptive
  Claude) on the searcher's Anthropic body. The gateway's translation
  turns that into each vendor's own form: adaptive or between_tools
  for new Claude, reasoning.effort for Responses, and thinkingLevel
  for Gemini 3 on Code Assist / Antigravity.
- A picked level raises max_tokens by its budget (replyRoom), so the
  thinking doesn't eat the reply.
- ownSearch and Kimi's search service are sent no level. A fallback
  searcher is sent the level fitted to its own model.

docs/subsystems/gateway-routing.md: new "How hard a side model thinks".

Tests:
- TestVisionEffortPicked and TestVisionEffortOnAnthropic check the
  vision body.
- TestSearcherEffortPicked checks the searcher body on new Claude, old
  Claude and Responses.
- TestSearcherEffortOnGemini checks the searcher body through
  buildCodeAssist for antigravity.
- TestSideEffortChecked covers settings validation.
- side-effort.test.cjs covers the chips: levels shown, a pick saved,
  Default clears it, toggle and Escape, no scroll, fit in the row and
  the menu inside the window. It runs in Chromium and WebKit at 1100
  and 420, in en/zh/zh-TW/ja/de.

Run:
- go test -tags nogui ./internal/gateway/ (full, -timeout 30m) on
  origin/main 3a9d73c5c + this change: everything passes except
  TestRateLimitIsWaitedOut "an hour away" (1.9s against its 1s bound,
  load ~190). That test passes on freshly fetched origin/main 41972b25f
  alone (-count=10) and in 8 parallel -cpu 1 copies. Filed as #1971.
- go test -tags nogui ./internal/settings/ ./internal/gui/: ok.
- Broken by hand, each goes red:
  - searchBody dropping the effort fails TestSearcherEffortPicked and
    TestSearcherEffortOnGemini;
  - askVisionWith back to classifyEffort fails TestVisionEffortPicked
    and TestVisionEffortOnAnthropic;
  - the settings check removed fails TestSideEffortChecked;
  - prefsKeep without the two fields fails side-effort.test.cjs.
- GUI:
  - side-effort.test.cjs: 10/10.
  - searcher-pick, images-*, vision-mcp, settings-search/sections/
    general, routing-vision-why and i18n-keys: 123/123.
  - 67 more Settings files: 778 pass. text-size "zh, dark ... 150%"
    failed in WebKit under parallel load; it passes 11/11 alone and is
    #1866.
- gofmt -l is clean. go vet passes for darwin, plus GOOS=linux and
  GOOS=windows with -tags nogui, on gateway, settings and gui. go build
  passes.
- Not tried against a real Antigravity or other real account: the
  request shapes are checked against fakes and the gateway's own
  translators.
2026-10-13 16:40:24 +08:00

105 lines
3.1 KiB
Go

package gateway
import (
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// The reasoning level picked in Settings for a model magpie asks on its
// own, beside the model in use: Image recognition's and the searcher's
// (MOMO on Discord, "外置模型能不能提供思考深度选项"). Only those calls
// are asked so; every other request goes as it did.
// ModelEfforts are the reasoning levels model ("<provider>/<model>") has,
// weakest first, for Settings to offer beside it; none for one whose
// levels aren't known, or a routing group.
func ModelEfforts(model string) []string {
p, m, ok := provider.Resolve(model)
if !ok {
return nil
}
return ByStrength(p.Efforts(m))
}
// sideEffort is want fitted to model's own levels: "" when none is
// wanted, or the model's levels aren't known, so nothing is sent that it
// might refuse.
func sideEffort(model, want string) string {
levels := ModelEfforts(model)
if want == "" || len(levels) == 0 {
return ""
}
return fitEffort(want, levels)
}
// visionEffort is the level the Image recognition model is asked to read
// an image at: the one picked in Settings, else magpie's own, the least
// it has (classifyEffort), as a description is wanted quickly.
func visionEffort(model string) string {
if e := sideEffort(model, provider.HeldSettings().VisionEffort); e != "" {
return e
}
return classifyEffort(model)
}
// VisionEffortNow is the level the Image recognition model in use is
// asked at when one is picked in Settings ("" when none is): the picked
// one fitted to the model's levels, for the row to show.
func VisionEffortNow() string {
m, ok := seer()
if !ok {
return ""
}
return sideEffort(m, provider.HeldSettings().VisionEffort)
}
// VisionEfforts are the levels of the model that describes images now.
func VisionEfforts() []string {
if m, ok := seer(); ok {
return ModelEfforts(m)
}
return nil
}
// searchEffort is the level the searcher's model, provider p's model, is
// asked to search at: the one picked in Settings fitted to its levels,
// or "" to send none.
func searchEffort(p provider.Provider, model string) string {
if model == "" {
return ""
}
return sideEffort(p.ID+"/"+model, settings.Load().SearcherEffort)
}
// SearchEffortNow is the level the searcher in use is asked at, when one
// is picked in Settings.
func SearchEffortNow() string {
if p, m, ok := searcher(); ok {
return searchEffort(p, m)
}
return ""
}
// SearchEfforts are the levels of the model that searches now: none for
// a Kimi Code plan's search service, which has no model.
func SearchEfforts() []string {
if p, m, ok := searcher(); ok && m != "" {
return ModelEfforts(p.ID + "/" + m)
}
return nil
}
// replyRoom is the reply limit a side call asked to think at effort is
// sent with: its usual limit with room for the thinking above it, so a
// level picked higher doesn't spend the reply on thinking and leave no
// answer. The model's own limit still caps it (withMaxOutput).
func replyRoom(usual int, effort string) int {
switch effort {
case "", "none":
return usual
case "minimal":
return usual + 1024
}
return usual + budgetOf(effortOf(effort))
}