diff --git a/internal/gateway/effort_test.go b/internal/gateway/effort_test.go index bf65d26a3..952a2e611 100644 --- a/internal/gateway/effort_test.go +++ b/internal/gateway/effort_test.go @@ -227,6 +227,37 @@ func TestTraceRecordsTheEffortSent(t *testing.T) { } } +// A Gemini request's thinkingConfig is the reasoning it asks for, read +// as parseGemini reads it (#4529): its level, or its budget as the level +// it is nearest, by either spelling of the fields' names. +func TestGeminiRequestEffort(t *testing.T) { + for _, c := range []struct { + body string + want string + }{ + {`{"generationConfig":{"thinkingConfig":{"thinkingLevel":"high"}}}`, "high"}, + {`{"generationConfig":{"thinkingConfig":{"thinking_level":"LOW"}}}`, "low"}, + {`{"generationConfig":{"thinkingConfig":{"thinkingBudget":512}}}`, "low"}, + {`{"generationConfig":{"thinkingConfig":{"thinking_budget":32768}}}`, "xhigh"}, + {`{"generationConfig":{"thinkingConfig":{"thinkingBudget":-1}}}`, "medium"}, + {`{"generationConfig":{"thinkingConfig":{"thinkingBudget":0}}}`, ""}, + {`{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}`, ""}, + {`{"generationConfig":{}}`, ""}, + {`{"contents":[{"parts":[{"text":"hi"}]}]}`, ""}, + } { + if got := requestEffort(provider.Gemini, []byte(c.body)); got != c.want { + t.Errorf("%s: requestEffort %q, want %q", c.body, got, c.want) + } + r, err := parseGemini([]byte(c.body)) + if err != nil { + t.Fatalf("%s: %v", c.body, err) + } + if r.Effort != c.want { + t.Errorf("%s: parseGemini effort %q, want %q", c.body, r.Effort, c.want) + } + } +} + // requestEffort reads the reasoning a request asks for in each API's words. func TestRequestEffort(t *testing.T) { for _, c := range []struct { diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 7aebd9061..3f7321784 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -7960,7 +7960,8 @@ var otherOffNamed = map[string]*regexp.Regexp{ var noneQuoted = regexp.MustCompile(`(?i)['"]none\\?['"]`) // bodyEffort is the reasoning effort a Chat or Responses request asks for, -// or an Anthropic one in its output_config. A Responses request that +// or an Anthropic one in its output_config, or a Gemini one in its +// generationConfig's thinkingConfig. A Responses request that // changed it mid-thread asks for its last configuration_update's // (effortUpdated). func bodyEffort(proto provider.Protocol, body []byte) string { @@ -7986,6 +7987,8 @@ func bodyEffort(proto provider.Protocol, body []byte) string { return v.Reasoning.Effort case proto == provider.Anthropic && v.OutputConfig != nil: return v.OutputConfig.Effort + case proto == provider.Gemini: + return geminiBodyEffort(body) } return "" } diff --git a/internal/gateway/gemini.go b/internal/gateway/gemini.go index 2d6b2b40a..46b3402c0 100644 --- a/internal/gateway/gemini.go +++ b/internal/gateway/gemini.go @@ -106,6 +106,22 @@ func (tc *gThinking) effort() string { return "" } +// geminiBodyEffort is the reasoning a Gemini request's generationConfig +// asks for through its thinkingConfig, as parseGemini reads it (#4529): +// the level it names, else its budget as the level it is nearest. "" +// when it asks for none, or there is no thinkingConfig. +func geminiBodyEffort(body []byte) string { + var g struct { + GenerationConfig *struct { + ThinkingConfig *gThinking `json:"thinkingConfig"` + } `json:"generationConfig"` + } + if decodeJSON(geminiCamel(body), &g) != nil || g.GenerationConfig == nil || g.GenerationConfig.ThinkingConfig == nil { + return "" + } + return g.GenerationConfig.ThinkingConfig.effort() +} + // buildGemini is a generateContent body for an upstream that speaks Gemini // itself (Factory's /api/llm/g). The contents are the ones Code Assist // sends inside its envelope. systemInstruction has no role: droid sends