gateway: a Gemini 3+ on an Anthropic or Chat relay is asked a thinking level, never a budget (Mamba_asr on X)
A relay serving Gemini under a host of its own (Mamba_asr's peta-gemini)
was sent Claude Code's thinking budget_tokens as it came on Anthropic's
Messages, the translation's budget from a Chat or Responses effort, or a
Chat client's extra_body.google.thinking_config thinking_budget. Such a
relay passes it on to Gemini as thinkingBudget, which Gemini 4 turns
away ("Thinking budget is not supported for this model. Use thinking
level instead.", #2083). AI Studio, Vertex AI and the translated Gemini
routes already sent thinkingLevel; the relay routes didn't.
geminiLevelBody (gemini_level_body.go), run in passthrough and
forwardTranslated after xaiBody, acts only for a Gemini 3 or later model
id on an Anthropic or Chat upstream:
- Anthropic: thinking enabled + budget_tokens goes as thinking adaptive
with output_config.effort, the budget bucketed by effortOfBudget
(<=4096 low, <=12000 medium, else high) unless the request names an
effort.
- Chat: thinking_config's budget goes as thinking_level (-1: none, 0:
low), and reasoning_effort is fitted.
Gemini's own OpenAI-compatible API (geminiCompat: AI Studio, or a
proxy on this computer or the LAN) is left alone: it takes a budget,
and the Chat path already fits its levels and asks again on a refused
one (TestGeminiThinkingLevelRefused and
TestPassthroughGeminiMinimalRefused went red until it was left out).
geminiLevelFor fits a level to the model: the catalog's levels where it
lists them, else nothing above high, and medium as high on a Gemini 3
Pro (which takes low and high only, per Google's model docs).
Run (mote, at this head rebased on origin/main):
- TestGeminiRelayAskedALevel: a fake relay that returns Gemini's 400
for any budget sent to a gemini-4 model. Its 7 Gemini cases are red
with gateway.go's old body (budget_tokens / thinking_budget / xhigh
sent as they came); the 3 other cases (claude-sonnet-4-5 budget_tokens,
gpt-5.5 reasoning_effort xhigh with a thinking_config budget, and
gemini-2.5-flash budget_tokens) pass on both, unchanged.
- TestGeminiLevelFor.
- gofmt -l clean; go vet ./internal/gateway on darwin, linux, windows.
- go test -count=1 -timeout 45m ./internal/gateway: ok.
That full run was at 862660144; rebased onto 049073c97 (the
Antigravity retirement touched internal/gateway), go vet and
go test -run 'Gemini|Thinking|Effort|Vertex|Anthropic|Relay'
./internal/gateway: ok at this head.
- docs/subsystems/gateway-routing.md names the rule.
Not tried: the reporter's real relay, or any real relay. A relay that
doesn't understand thinking "adaptive" will drop it and use the model's
default thinking, which beats a 400. Not covered: a Responses upstream,
plugin routes, and a Chat body's own "thinking" budget field.
This commit is contained in:
@@ -388,6 +388,7 @@ recorded as a prediction failure (`noPrediction`).
|
||||
- An agent whose config can't carry an effort has one picked in magpie (`provider.AgentEffort`, settings' `agentEfforts`; Cursor Private Inference's row, #1003, and Antigravity's, #4838). On an Antigravity account the level picks the family's variant, as a request's own effort does (`AntigravitySentID`, `TestAntigravityAgentEffort`). Antigravity's app lists a model with levels once a level, `<id>:<level>` (agent-wiring), so the level chosen in its own picker arrives as the suffix and outranks the row's pick (`TestAntigravityAgentEffort`). It is asked for on that agent's requests to a model that reasons (`Provider.Thinks`), in place of the level the request sent, fitted to the model's levels and bounded by the group's. Order: a member's fixed effort, then the effort the rule that put the member first sets (`Rule.UseEffort`), then a model's `:effort` suffix, then the turn's pick (a routing rule's), then the agent's pick, then the request's own. A model that doesn't reason is sent as asked; the default (none picked) leaves every request as it came. The pick beats the request's own because Cursor sends its own default as often as a real choice. Picking Default hands the choice back to Cursor.
|
||||
- In models.dev, entries from `deepseek`, `kimi-code-plan-cn` and `kimi-code-plan-global` gain `none` only when they have both a `toggle` and effort levels; the DeepSeek and Kimi APIs accepted that value in [#1098](https://github.com/yetone/magpie/issues/1098). Other providers keep their listed levels because their APIs may reject `none`; toggle-only entries remain level-less ([`mdModel.efforts`](../../internal/catalog/catalog.go)).
|
||||
- Gemini's OpenAI-compatible API (AI Studio's, or a proxy in front of Gemini on this computer or the LAN: `geminiCompat`) is asked for the model's thoughts in `extra_body.google.thinking_config`. A 400 or 422 whose error names `extra_body`, `include_thoughts` or thinking at all (`refusesThinkingConfig`) marks the provider unfit for the field (`thinking_config`), and it is asked again with `reasoning_effort` alone. Gemini 3 can't stop thinking, so reasoning turned off goes at `minimal`, its least, or at the model's lowest level where it has no `minimal` (`geminiLevels`: those of the model's levels it hasn't turned away, or of Gemini 3's where its own aren't known or none is left). Gemini 3 turns away a level the model hasn't with a 400 that names the level (`geminiRefusedLevel`): AI Studio's "Thinking level MINIMAL is not supported for this model. Please retry with other thinking level." for gemini-3.8-flash, Vertex AI's "Thinking level is unsupported: THINKING_LEVEL_MINIMAL". It names thinking too, but isn't the field turned away. The level is marked refused for that model (`levelRefused`), and the model is asked again at the nearest level it has left, still for its thoughts, and at that level from then on. A level turned away again, with none left, is the client's 400. Passed through, a Chat client's own `none` or `minimal` that Gemini turns away this way is asked again at the model's lowest level (`onEffort`), and that level goes at the lowest from then on (`turnedAway`); the other still goes as sent, as AI Studio takes `none` from gemini-3.8-flash. A refusal that names the levels the model takes (`effortLevelsNamed`, Command Code's for Qoder's `none`) marks reasoning off as a whole (`offRefused`): both go at the lowest from then on; one that lists the other among the levels taken (`otherOffNamed`, quoted, OpenAI's for gpt-5.1's `minimal`) marks only the level turned away. Both routes read and write the same marks. Vertex AI's OpenAI-compatible API marks each streamed chunk of thoughts in the delta's `extra_content.google.thought`, and such a chunk is read as thinking.
|
||||
- A Gemini 3 or later model (by its id, `geminiGeneration`) on an Anthropic or Chat upstream that isn't Gemini's own API (a relay serving Gemini under a host of its own) is asked a thinking level, never a budget, as Gemini 4 turns every `thinkingBudget` away (#2083) and such a relay makes one from what it is sent (`geminiLevelBody` in [`gemini_level_body.go`](../../internal/gateway/gemini_level_body.go), run in `passthrough` and `forwardTranslated` after `xaiBody`; Mamba_asr on X). Anthropic: thinking `enabled` with `budget_tokens` goes as thinking `adaptive` with `output_config.effort`, the budget bucketed by `effortOfBudget` unless the request names an effort. Chat: `extra_body.google.thinking_config`'s budget goes as its `thinking_level` (-1 as none, 0 as `low`). Either way the level is fitted to the model's (`geminiLevelFor`: the catalog's levels, else nothing above `high`, and `medium` as `high` on a Gemini 3 Pro). Another vendor's model and a Gemini 2.x keep the body as sent (`TestGeminiRelayAskedALevel`). A relay that doesn't know `adaptive` uses the model's default thinking.
|
||||
- A Claude that thinks only adaptively (`adaptiveOnly` in [`anthropic.go`](../../internal/gateway/anthropic.go): Opus, Sonnet and Haiku from 4.6 on, Fable and Mythos 5 and 5.1, read by `claudeVersion` however a relay spells them) is never sent a thinking budget, which they answer with 400. magpie's own request from a Chat or Responses client (`buildAnthropic`) goes as `thinking: {type: "adaptive", display: "summarized"}` with the effort in `output_config.effort`, so the client sees the reasoning Opus 4.7 and later otherwise leave out (display `omitted` by default), and a relay that refuses `display` is asked again without it (`refuseShapes`), as is one that refuses a turn's effort on a `{role: "system"}` message (`withoutTurnEffort`: a relay's "messages.1.output_config: Extra inputs are not permitted", or OpenRouter's "Mid-conversation reasoning effort (configuration_update) is not supported on <model>" on DeepSeek and GLM, #3696; `TestOpenRouterTurnEffortLeftOut`), as is one that refuses auto mode's `safeguards` (Copilot's Messages API: 400 "safeguards: Extra inputs are not permitted" on claude-opus-5.5, #1756; `shapeSafeguards`, `TestSafeguardsRefusedLeftOut`), and neither is sent it again while magpie runs; a client's `thinking: {type: "enabled", budget_tokens}` is turned into the same in `forwardOnce` (`adaptiveThinking`), the budget as its nearest effort, above `high` as `max`, and `display: "summarized"` unless the client named its own display, so the thinking that enabled shows stays shown (the refused shapes are taken out after it, and `refuseShapes` reads the display as it is sent); and Pi, dsh and omp are written to ask it adaptively (`AdaptiveThinking`: `forceAdaptiveThinking`, omp's `anthropic-adaptive`; #1470). A client's `xhigh` goes as `xhigh`, except to Opus and Sonnet 4.6, which have no `xhigh` and are asked `max` (`noXhigh`, #1471).
|
||||
- A vendor's model that takes its level in `output_config.effort` beside thinking on (`effortInOutputConfig` in [`anthropic.go`](../../internal/gateway/anthropic.go): Z.ai's GLM-5.2 and GLM-5.3, MiniMax's MiniMax-M3.1-Flash and its -Preview, and any `gpt-<digit>` id on a Messages endpoint, such as a relay's: it showed every request at medium on the relay's console, zxz8009195489 on Discord, `TestGPTMessagesEffortInOutputConfig`) is sent the effort there next to the budget: built from a Chat or Responses client's effort (`buildAnthropic`, when it sends a budget), or added to an Anthropic client's request from its budget's nearest level, fitted to the model's levels (`withOutputEffort`, in `passthrough`). A budget alone left GLM at its default, and under an 8192 cap gave MiniMax's medium to max one budget, 7168 (shin4 on plugins#316). A client that asks such a model adaptively, with its level in `output_config.effort`, has both kept (Z.ai takes adaptive with the level, tried live on GLM-5.3); dsh is written to ask them so (GLM-5.2 and GLM-5.3: `AdaptiveWithEffort`, `forceAdaptiveThinking` in `dshKindConfig`; MiniMax not yet, #3496), since pi-ai's budget path clamps Max to high's 16384 and its Max reached GLM-5.3 as high (01huadalang on Discord, 10-24). Every other model, Claude and MiniMax-M3 included, gets the budget alone. `TestGLMEffortInOutputConfig`, `TestMiniMaxPluginGetsTheNativeEffort`, `TestNativeEffortLeavesOthersAlone`, `TestAdaptiveOnOutputEffortModelAsksItsLevel`, `TestDshMaxReachesOutputEffortModels`.
|
||||
- A request to an Anthropic endpoint that turns thinking off is sent as the model takes it (`thinkingOffAsTaken`, in `forwardOnce`, so relayed requests, count_tokens, magpie's own thinking-off and retries all get it; the model is read as the vendor names it, from the body, or from the path for Bedrock and Vertex). Claude Sonnet 5.5 refuses `thinking: {type: "disabled"}` and is sent `{type: "between_tools"}` alone, at effort `high` at most. Opus 5.5, Fable 5 and Mythos 5 refuse it at every effort and are sent no thinking, at effort `low` unless one was asked, with `classifierRoom` added to a reply limit below it, as Claude Code does. Opus 5 and Haiku 5.5 take it only up to `high`, so `xhigh` and `max` are asked as `high`. `between_tools` sent to any other Claude goes as `disabled`, or as none where that is refused too. Other vendors' models, older Claudes and later lines than these go as they came (#1454). A Sonnet 5.5 by a name these rules can't read, answering 400 that asks for `between_tools` (`asksBetweenTools`), is asked again with it, once; a refusal of `disabled` at any effort (`thinkingOffRefused`) is asked again with thinking left to the model, as `alwaysThinks` is. So is a relay that reads `disabled` as reasoning effort `none` and turns that level away naming the ones it takes (Sub2API's gpt-6.1-sol, "does not support reasoning effort \"none\"; use low, medium, high, xhigh or max", #1465); `refusesThinkingOff` is the one list of these refusals for both routes. A relayed request refused so marks the model (`markUnfit(offRefused)`), and its later requests go with thinking left to the model at once until magpie restarts, as auto mode's classifier asks on every action.
|
||||
|
||||
@@ -6916,6 +6916,9 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider.
|
||||
// xAI's own API takes neither a sealed reasoning item's null content
|
||||
// nor a system turn in Messages (xai_body.go, #3392)
|
||||
body = xaiBody(p, proto, body)
|
||||
// a Gemini 3 or later on an Anthropic or Chat relay is asked a
|
||||
// thinking level, never a budget (gemini_level_body.go)
|
||||
body = geminiLevelBody(p, proto, model, body)
|
||||
// a Grok subscription is given Codex's namespaced functions flat
|
||||
// (grokBody), and Zed's plugin likewise (ZedBody); a call to one goes
|
||||
// back under its namespace (#404). Any vendor asked as Codex sends it
|
||||
@@ -8369,6 +8372,9 @@ func (s *Server) forwardTranslated(ctx context.Context, p provider.Provider, to
|
||||
// content nor a system turn in Messages (xai_body.go, #3392) —
|
||||
// the built body as the relayed one
|
||||
body = xaiBody(p, to, body)
|
||||
// a Gemini 3 or later on an Anthropic or Chat relay is asked a
|
||||
// thinking level, never a budget (gemini_level_body.go)
|
||||
body = geminiLevelBody(p, to, model, body)
|
||||
if to == provider.CodeAssist && p.Account != nil {
|
||||
// the envelope is named in there, where the id it carries is
|
||||
// known: on Antigravity that is the variant the effort picked,
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// geminiLevelBody asks a Gemini 3 or later model, on an Anthropic or Chat
|
||||
// upstream that isn't Gemini's own API (a relay serving Gemini under a
|
||||
// host of its own, Mamba_asr's peta-gemini), for a thinking level, never a
|
||||
// budget. Gemini 4 turns every thinkingBudget away ("Thinking budget is
|
||||
// not supported for this model. Use thinking level instead.", #2083), and
|
||||
// such a relay makes the thinkingBudget it sends Gemini from what it was
|
||||
// sent:
|
||||
// - Anthropic: thinking enabled with budget_tokens (Claude Code's, or the
|
||||
// translation's from a Chat or Responses effort) goes as thinking
|
||||
// adaptive with output_config's effort, the budget bucketed into a
|
||||
// level as effortOfBudget reads it; an effort the request names is the
|
||||
// one kept.
|
||||
// - Chat: extra_body's google thinking_config budget goes as its
|
||||
// thinking_level.
|
||||
//
|
||||
// Either way the level is fitted to the model's (geminiLevelFor), and a
|
||||
// reasoning_effort or output_config effort above what it has comes down
|
||||
// to its highest. Any other model's body is returned as it came, and so is
|
||||
// one for Gemini's own OpenAI-compatible API (geminiCompat), which takes
|
||||
// a budget and whose levels the Chat path fits, and asks again when one
|
||||
// is turned away (geminiRefusedLevel).
|
||||
func geminiLevelBody(p provider.Provider, proto provider.Protocol, model string, body []byte) []byte {
|
||||
if proto != provider.Anthropic && proto != provider.Chat {
|
||||
return body
|
||||
}
|
||||
if proto == provider.Chat && geminiCompat(p.VendorHost(), model) {
|
||||
return body
|
||||
}
|
||||
m := strings.ToLower(model)
|
||||
if geminiGeneration(m) < 3 {
|
||||
return body
|
||||
}
|
||||
if !bodyHas(body, `"thinking`) && !bodyHas(body, `"reasoning_effort"`) && !bodyHas(body, `"output_config"`) {
|
||||
return body
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewReader(body))
|
||||
dec.UseNumber()
|
||||
var top map[string]any
|
||||
if dec.Decode(&top) != nil {
|
||||
return body
|
||||
}
|
||||
fields := map[string]any{}
|
||||
switch proto {
|
||||
case provider.Anthropic:
|
||||
th, _ := top["thinking"].(map[string]any)
|
||||
oc, _ := top["output_config"].(map[string]any)
|
||||
said, _ := oc["effort"].(string)
|
||||
switch t, _ := th["type"].(string); t {
|
||||
case "enabled":
|
||||
level := effortOf(said)
|
||||
if level == "" {
|
||||
n, _ := th["budget_tokens"].(json.Number)
|
||||
b, _ := n.Int64()
|
||||
level = effortOfBudget(int(b))
|
||||
}
|
||||
adaptive := map[string]any{"type": "adaptive"}
|
||||
if d, ok := th["display"]; ok {
|
||||
adaptive["display"] = d
|
||||
}
|
||||
fields["thinking"] = adaptive
|
||||
if level != "" {
|
||||
if oc == nil {
|
||||
oc = map[string]any{}
|
||||
}
|
||||
oc["effort"] = geminiLevelFor(m, level)
|
||||
fields["output_config"] = oc
|
||||
}
|
||||
case "adaptive":
|
||||
if said != "" {
|
||||
if fit := geminiLevelFor(m, said); fit != said {
|
||||
oc["effort"] = fit
|
||||
fields["output_config"] = oc
|
||||
}
|
||||
}
|
||||
}
|
||||
case provider.Chat:
|
||||
if e, ok := top["reasoning_effort"].(string); ok && e != "" {
|
||||
if fit := geminiLevelFor(m, e); fit != e {
|
||||
fields["reasoning_effort"] = fit
|
||||
}
|
||||
}
|
||||
eb, _ := top["extra_body"].(map[string]any)
|
||||
google, _ := eb["google"].(map[string]any)
|
||||
key := "thinking_config"
|
||||
if _, ok := google[key]; !ok {
|
||||
key = "thinkingConfig"
|
||||
}
|
||||
tc, _ := google[key].(map[string]any)
|
||||
changed := false
|
||||
for _, bk := range []string{"thinking_budget", "thinkingBudget"} {
|
||||
n, ok := tc[bk].(json.Number)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
delete(tc, bk)
|
||||
changed = true
|
||||
if tc["thinking_level"] != nil || tc["thinkingLevel"] != nil {
|
||||
continue
|
||||
}
|
||||
switch b, _ := n.Int64(); {
|
||||
case b < 0:
|
||||
// -1: the model decides, which a level left out says
|
||||
case b == 0:
|
||||
// off: Gemini 3 can't stop thinking, and thinks least
|
||||
// at low, which every Gemini 3 text model takes
|
||||
tc["thinking_level"] = "low"
|
||||
default:
|
||||
tc["thinking_level"] = geminiLevelFor(m, effortOfBudget(int(b)))
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
google[key] = tc
|
||||
eb["google"] = google
|
||||
fields["extra_body"] = eb
|
||||
}
|
||||
}
|
||||
if len(fields) == 0 {
|
||||
return body
|
||||
}
|
||||
return withFields(body, fields)
|
||||
}
|
||||
|
||||
// geminiLevelFor is effort as a Gemini 3 or later model takes it: the
|
||||
// nearest of its levels where the catalog lists them, else low, medium or
|
||||
// high as Google's model table has them (medium to Flash and from Gemini
|
||||
// 4, high to a Gemini 3 Pro, which took low and high only), and nothing
|
||||
// above high. Minimal is kept for a Gemini that may have it; one that
|
||||
// hasn't turns it away by name and is asked again at its lowest
|
||||
// (geminiRefusedLevel).
|
||||
func geminiLevelFor(model, effort string) string {
|
||||
level := strings.ToLower(strings.TrimSpace(effort))
|
||||
if level == "none" {
|
||||
return "low"
|
||||
}
|
||||
if level != "minimal" {
|
||||
if l := effortOf(level); l != "" {
|
||||
level = l
|
||||
}
|
||||
}
|
||||
if ls := catalog.EffortsOf(model); len(ls) > 0 {
|
||||
ls = slices.DeleteFunc(ls, func(l string) bool { return l == "none" })
|
||||
if len(ls) > 0 {
|
||||
return catalog.NearestEffort(level, ls)
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case slices.Index(effortRank, level) > slices.Index(effortRank, "high"):
|
||||
return "high"
|
||||
case level == "medium" && geminiGeneration(model) == 3 && !strings.Contains(model, "flash"):
|
||||
return "high"
|
||||
}
|
||||
return level
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// levelRelay is a relay serving Gemini under a host of its own, on
|
||||
// Anthropic's Messages and on Chat completions (Mamba_asr's peta-gemini),
|
||||
// that turns away a Gemini 4 asked a budget as Gemini does (#2083) and
|
||||
// keeps every body it was sent.
|
||||
func levelRelay(t *testing.T) (*httptest.Server, *[]map[string]any) {
|
||||
var bodies []map[string]any
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
var m map[string]any
|
||||
json.Unmarshal(b, &m)
|
||||
bodies = append(bodies, m)
|
||||
model, _ := m["model"].(string)
|
||||
if strings.HasPrefix(model, "gemini-4") && (strings.Contains(string(b), `"budget_tokens"`) || budgetNamed(string(b))) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
io.WriteString(w, argonBudgetRefused)
|
||||
return
|
||||
}
|
||||
stream, _ := m["stream"].(bool)
|
||||
if strings.HasSuffix(r.URL.Path, "/messages") {
|
||||
if stream {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
for _, ev := range []string{
|
||||
`{"type":"message_start","message":{"id":"m1","type":"message","role":"assistant","model":"m","content":[],"usage":{"input_tokens":2,"output_tokens":0}}}`,
|
||||
`{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
|
||||
`{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ok"}}`,
|
||||
`{"type":"content_block_stop","index":0}`,
|
||||
`{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}`,
|
||||
`{"type":"message_stop"}`,
|
||||
} {
|
||||
io.WriteString(w, "data: "+ev+"\n\n")
|
||||
}
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
io.WriteString(w, `{"id":"msg_1","type":"message","role":"assistant","model":"m","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`)
|
||||
return
|
||||
}
|
||||
sigReply(w, stream, map[string]any{"role": "assistant", "content": "ok"}, "stop")
|
||||
}))
|
||||
t.Cleanup(up.Close)
|
||||
return up, &bodies
|
||||
}
|
||||
|
||||
func budgetNamed(b string) bool {
|
||||
return strings.Contains(b, `"thinking_budget"`) || strings.Contains(b, `"thinkingBudget"`)
|
||||
}
|
||||
|
||||
// Mamba_asr on X: a Gemini model that takes only a thinking level, on a
|
||||
// relay of its own host, was sent Claude Code's thinking budget as it
|
||||
// came (Anthropic upstream) or a Chat client's thinking_config budget,
|
||||
// which the relay passes on to Gemini as thinkingBudget, and Gemini 4
|
||||
// turns away. It is now asked a level: the budget bucketed into low,
|
||||
// medium or high, an effort above the model's highest at its highest.
|
||||
// Another vendor's model, and a Gemini 2.5, are sent what they were.
|
||||
func TestGeminiRelayAskedALevel(t *testing.T) {
|
||||
up, bodies := levelRelay(t)
|
||||
addr := strings.TrimPrefix(up.URL, "http://")
|
||||
type want map[string]any
|
||||
for _, c := range []struct {
|
||||
name, upstream, model, path, body string
|
||||
want want // fields of the body the relay was sent; nil: absent
|
||||
}{
|
||||
{"messages budget low to Anthropic", "anth", "gemini-4-argon", "/v1/messages",
|
||||
`{"max_tokens":40000,"thinking":{"type":"enabled","budget_tokens":2000}}`,
|
||||
want{"thinking": map[string]any{"type": "adaptive"}, "output_config": map[string]any{"effort": "low"}}},
|
||||
{"messages budget large to Anthropic", "anth", "gemini-4-argon", "/v1/messages",
|
||||
`{"max_tokens":40000,"thinking":{"type":"enabled","budget_tokens":31999}}`,
|
||||
want{"thinking": map[string]any{"type": "adaptive"}, "output_config": map[string]any{"effort": "high"}}},
|
||||
{"messages effort max to Anthropic", "anth", "gemini-4-argon", "/v1/messages",
|
||||
`{"max_tokens":40000,"thinking":{"type":"adaptive"},"output_config":{"effort":"max"}}`,
|
||||
want{"thinking": map[string]any{"type": "adaptive"}, "output_config": map[string]any{"effort": "high"}}},
|
||||
{"chat effort to Anthropic", "anth", "gemini-4-argon", "/v1/chat/completions",
|
||||
`{"reasoning_effort":"medium"}`,
|
||||
want{"thinking": map[string]any{"type": "adaptive"}, "output_config": map[string]any{"effort": "medium"}}},
|
||||
{"messages budget large to Chat", "chat", "gemini-4-argon", "/v1/messages",
|
||||
`{"max_tokens":40000,"thinking":{"type":"enabled","budget_tokens":31999}}`,
|
||||
want{"reasoning_effort": "high"}},
|
||||
{"chat thinking_config budget to Chat", "chat", "gemini-4-argon", "/v1/chat/completions",
|
||||
`{"extra_body":{"google":{"thinking_config":{"thinking_budget":2000,"include_thoughts":true}}}}`,
|
||||
want{"extra_body": map[string]any{"google": map[string]any{"thinking_config": map[string]any{"thinking_level": "low", "include_thoughts": true}}}}},
|
||||
{"chat reasoning_effort xhigh to Chat", "chat", "gemini-4-argon", "/v1/chat/completions",
|
||||
`{"reasoning_effort":"xhigh"}`,
|
||||
want{"reasoning_effort": "high"}},
|
||||
// another vendor's model, and a Gemini that takes a budget: as sent
|
||||
{"Claude budget to Anthropic", "anth", "claude-sonnet-4-5", "/v1/messages",
|
||||
`{"max_tokens":40000,"thinking":{"type":"enabled","budget_tokens":2000}}`,
|
||||
want{"thinking": map[string]any{"type": "enabled", "budget_tokens": float64(2000)}, "output_config": nil}},
|
||||
{"GPT thinking_config budget to Chat", "chat", "gpt-5.5", "/v1/chat/completions",
|
||||
`{"reasoning_effort":"xhigh","extra_body":{"google":{"thinking_config":{"thinking_budget":2000}}}}`,
|
||||
want{"reasoning_effort": "xhigh", "extra_body": map[string]any{"google": map[string]any{"thinking_config": map[string]any{"thinking_budget": float64(2000)}}}}},
|
||||
{"Gemini 2.5 budget to Anthropic", "anth", "gemini-2.5-flash", "/v1/messages",
|
||||
`{"max_tokens":40000,"thinking":{"type":"enabled","budget_tokens":2000}}`,
|
||||
want{"thinking": map[string]any{"type": "enabled", "budget_tokens": float64(2000)}}},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
fresh(t)
|
||||
p := provider.Provider{ID: "peta", Name: "peta", Key: "k", Models: []string{c.model}}
|
||||
if c.upstream == "anth" {
|
||||
p.Anthropic = "http://api.peta.example.com"
|
||||
} else {
|
||||
p.Chat = "http://api.peta.example.com/v1"
|
||||
}
|
||||
if err := provider.Save(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
s.client = &http.Client{Transport: &http.Transport{DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
|
||||
return (&net.Dialer{}).DialContext(ctx, network, addr)
|
||||
}}}
|
||||
body := `{"model":"peta/` + c.model + `","messages":[{"role":"user","content":"hi"}],` + strings.TrimPrefix(c.body, "{")
|
||||
*bodies = nil
|
||||
rec := httptest.NewRecorder()
|
||||
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", c.path, strings.NewReader(body)))
|
||||
if rec.Code != 200 || len(*bodies) == 0 {
|
||||
t.Fatalf("status %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
sent := (*bodies)[0]
|
||||
for k, v := range c.want {
|
||||
got, _ := json.Marshal(sent[k])
|
||||
exp, _ := json.Marshal(v)
|
||||
if string(got) != string(exp) {
|
||||
t.Errorf("%s: sent %s, want %s (body %v)", k, got, exp, sent)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// geminiLevelFor fits an effort to a Gemini's levels where the catalog
|
||||
// doesn't list them: nothing above high, medium as high on a Gemini 3 Pro.
|
||||
func TestGeminiLevelFor(t *testing.T) {
|
||||
fresh(t)
|
||||
for _, c := range []struct{ model, effort, want string }{
|
||||
{"gemini-4-argon", "xhigh", "high"},
|
||||
{"gemini-4-argon", "max", "high"},
|
||||
{"gemini-4-argon", "medium", "medium"},
|
||||
{"gemini-3.8-flash", "medium", "medium"},
|
||||
{"gemini-3-pro-preview", "medium", "high"},
|
||||
{"gemini-4-argon", "low", "low"},
|
||||
{"gemini-4-argon", "none", "low"},
|
||||
} {
|
||||
if got := geminiLevelFor(c.model, c.effort); got != c.want {
|
||||
t.Errorf("%s at %s: %s, want %s", c.model, c.effort, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user