Files
magpie/internal/gateway/decide.go
T
Ns2Kracy bb1aa3acbd feat(gateway): named gateway keys with per-client usage (#344)
* feat(gui): show and filter usage for each API key

* feat(gateway): issue per-user caller keys and attribute usage

* feat(gui): manage gateway users and filter caller-key usage

* test: verify caller-key failover and document user management

* refactor(gateway): use named caller keys without user accounts

* feat(gui): manage named caller keys with existing account styles

* fix: remove user breakdown from CLI and leftover gateway styling

* fix(usage): drop stale user field from request response

* feat(gateway): authenticate LAN calls with named caller keys

* fix(gateway): preserve LAN key rotation in the named caller store

* fix(gui): retain native LAN key copy and rotation controls

* fix(gui): inherit the global theme for API key inputs

* fix(usage): show client API keys using the existing terminology

* fix(gateway): rotate named keys without changing usage identity

* fix(gui): unify gateway keys and selectable connection examples

Keep sharing controls in Settings and manage credentials only in Gateway. Refresh the named-key list on navigation, ignore stale reads after key actions, and generate all API examples from the selected address and enabled key.

* fix(gui): keep caller key names literal in connection menus

* fix(access): use Magpie as default key and drop user migration

* refactor(gui): name caller key source and tests consistently

* refactor(usage): split provider-key accounting out of caller-key PR

* fix(gateway): preserve LAN compatibility and add headless key management

* fix(gui): clarify gateway keys and confirm destructive actions

* test(gateway): guard caller attribution under 120 concurrent requests

* fix(gateway): keep open access, tolerate migration errors and revoke legacy keys

* fix(backup): preserve named gateway keys and honor no-keys restores

* fix(gui): keep local API-key semantics and disambiguate shared credentials

* docs(gateway): keep review scope focused and append browser test documentation

* fix(backup): retain existing default keys when old backups lack their store

* fix(gateway): attribute video creation usage to the caller key

* fix(access): explain default key revocation blocked by read-only settings

* test(usage): preserve provider and caller identities after rebase

* test(gui): reconcile caller key fixtures with merged main

* docs(usage): clarify caller coverage and independent provider accounting

* fix(gui): initialize fast members for model-shortcut groups

Latest main introduced fast-member arrays but omitted the model shortcut's draft. The existing unlisted-models browser regression fails unchanged on main. Initialize fast to an empty list and assert a standard-speed group is saved, without relaxing existing assertions.

* fix(access): retain legacy LAN access when the key store cannot be created

Allow only the configured legacy credential before either migration marker exists. Refuse revoked mirrors, disabled matches and unreadable stores; keep the fallback's caller identity stable when migration later succeeds. Cover read-only configuration directories, remote credential transports and revocation.

* fix(backup): save settings before replacing gateway credentials

Leave the key store untouched when settings validation or writes fail. Restore the previous settings association when the key store refuses its write, and report a rollback failure separately. Cover invalid settings and read-only settings/store failures.

* fix(gui): reset missing caller filters and focus key controls without scrolling

* test(provider): synchronize Zhipu quota fixture observations
2026-10-01 21:28:54 +08:00

759 lines
26 KiB
Go

package gateway
// A group whose classifier is a decision provider's model (Jev, from
// TypeSafe) asks it in one call, as a user's turn begins, what the
// classifier model would be asked — which of the rules' intents the
// message is — and, for a group with Effort "auto", how hard the turn is
// to think about. Jev answers each with how likely every option was
// rather than with words: an intent it isn't sure of counts as none, and
// the effort is the level the likelihoods weigh out to. The turn's
// requests then ask their model for that effort, where the agent asked
// for reasoning at all.
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"slices"
"sort"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// jevSure is how confident Jev must be of an intent for it to count.
// Confidence runs from 0, every option as likely, to 1, one certain.
const jevSure = 0.4
// noIntent is the option Jev picks for a message that is none of the
// intents.
const noIntent = "none of these"
// jevLevels are the efforts Jev chooses among, with what each is for,
// lowest first.
var jevLevels = []struct{ effort, what string }{
{"low", "Little to think about: a greeting, a question answered from what is already known, a mechanical or one-line change, running a command"},
{"medium", "Some thought: an ordinary bug fix or a small feature in code already understood"},
{"high", "Careful thought: a change across several files, a bug whose cause is not known yet, a design choice with trade-offs"},
{"xhigh", "Deep thought: a subtle bug (concurrency, performance, security), an architecture or algorithm to design, a long multi-step plan"},
}
// levelWork is how much work a request is, least first: what a level of
// intents is told to be for (levelsOf), as Jev rated each level.
var levelWork = []string{
"little work: a question, an explanation, a one-line or mechanical change, a command to run",
"some work: an ordinary bug fix or a small feature in code already understood",
"much work: a change across several files, a bug whose cause is not known yet, a design choice",
"the most work: a new program, app or game, an architecture, a long multi-step plan",
}
// levelsOf is what each of intents, levels of one scale, is for: each of
// levelWork goes to the level Jev rated nearest it (scores, 0 to 3), so
// that two levels share them all. Told only their names, Jev was torn
// over short messages: 写个纸牌游戏 came out 复杂任务 at 0.62, too unsure
// to hold, and after a 简单任务 turn 简单任务 at 0.57, so it stayed there;
// told this, 复杂任务 at 0.99 either way.
func levelsOf(intents []string, scores []float64) map[string]string {
if len(intents) < 2 || len(scores) != len(intents) {
return nil
}
order := make([]int, len(intents))
for i := range order {
order[i] = i
}
sort.SliceStable(order, func(a, b int) bool { return scores[order[a]] < scores[order[b]] })
owns := make([][]string, len(intents))
for w, what := range levelWork {
near := 0
for i := range intents {
if math.Abs(scores[i]-float64(w)) < math.Abs(scores[near]-float64(w)) {
near = i
}
}
owns[near] = append(owns[near], what)
}
out := map[string]string{}
for pos, i := range order {
at := fmt.Sprintf("Level %d of %d, lowest first", pos+1, len(intents))
switch pos {
case 0:
at = "The lowest level"
case len(intents) - 1:
at = "The highest level"
}
if len(owns[i]) > 0 {
at += ": " + strings.Join(owns[i], "; ")
}
out[intents[i]] = at
}
return out
}
// jevBody is the System One request asking which of intents text is
// (when there are any) and, when effort, how hard it is. What was said of
// the turn before (prev) is in the state: a message that only carries on
// from it is of its kind and wants its reasoning. work is what each
// intent is for when they are levels (levelsOf), nil when not known.
func jevBody(model string, intents []string, work map[string]string, prev before, effort bool, text string) []byte {
qs := map[string]any{}
if len(intents) > 0 {
// Kinds may be topics, which a message can be none of, or levels
// (how hard or big a request is), which every message has one
// of. Jev takes "none of these" over any level for a greeting,
// and without it puts a poem in some topic, sure of it; so it is
// asked both ways, and whether the kinds are levels (jevLevelled)
// says which answer counts (readJev).
criteria := map[string]any{noIntent: "The message is none of the other kinds"}
levels := map[string]any{}
for _, in := range intents {
criteria[in], levels[in] = nil, nil
if w, ok := work[in]; ok {
levels[in] = w
}
}
qs["intent"] = map[string]any{"type": "choice", "instructions": intentAsk(prev), "criteria": criteria}
qs["level"] = map[string]any{"type": "choice", "instructions": levelAsk(prev, work != nil), "criteria": levels}
}
if effort {
levels := make([]string, len(jevLevels))
for i, l := range jevLevels {
levels[i] = l.what
}
qs["effort"] = map[string]any{
"type": "score",
"instructions": effortAsk(prev),
"criteria": levels,
}
}
b, _ := json.Marshal(map[string]any{
"model": model,
"state": jevState(prev, effort, text),
"questions": qs,
})
return b
}
func jevState(prev before, effort bool, text string) map[string]string {
st := map[string]string{"message": text}
if prev.Intent != "" {
st["previous_message_kind"] = prev.Intent
}
if effort && prev.Effort != "" {
st["previous_message_reasoning"] = prev.Effort
}
return st
}
func intentAsk(prev before) string {
q := "The `message` is what a user asked a coding assistant. Which kind of request fits it best?"
if prev.Intent != "" {
q += " A message that only carries on from the user's message before it (go on, yes, do it, fix that) is of `previous_message_kind`; one that asks for something of its own is of the kind that fits it."
}
return q
}
// levelAsk is intentAsk for levels, which, told what each is for, are
// judged by the work asked for.
func levelAsk(prev before, work bool) string {
q := intentAsk(prev)
if work {
q += " The kinds are levels of how much work a request is: judge the work the message asks for, not how short it is — a few words can ask for a whole program."
}
return q
}
func effortAsk(prev before) string {
q := "How much reasoning does a coding assistant need to handle the `message` well?"
if prev.Effort != "" {
q += " A message that only carries on from the user's message before it (go on, yes, do it) needs what that one did, `previous_message_reasoning` (low, medium, high or xhigh)."
}
return q
}
// readJev is the verdict in a System One reply to jevBody, the answer
// without "none of these" counting when the intents are levels.
func readJev(b []byte, intents []string, levels bool, prev before) (verdict, error) {
var out struct {
Model string `json:"model"`
Answers map[string]struct {
Choice string `json:"choice"`
Score float64 `json:"score"`
Confidence float64 `json:"confidence"`
} `json:"answers"`
Usage struct {
Input int `json:"input_tokens"`
Output int `json:"output_tokens"`
} `json:"usage"`
}
if err := json.Unmarshal(b, &out); err != nil {
return verdict{}, answerError{fmt.Errorf("not a System One answer")}
}
var v verdict
if a, ok := out.Answers["intent"]; ok && len(intents) > 0 && !levels {
v.Sure = a.Confidence
if a.Choice != noIntent && a.Confidence >= jevSure {
v.Intent = intentNamed(intents, a.Choice)
}
}
if a, ok := out.Answers["level"]; ok && len(intents) > 0 && levels {
// every message is at one of them, however unsure Jev is: when it
// is torn, the conversation stays at the level of the turn before
// (继续,再加一个基准测试 after a 复杂任务 came out 0.54 to 0.46),
// and a turn with none before takes the likelier
v.Sure = a.Confidence
v.Intent = intentNamed(intents, a.Choice)
if was := intentNamed(intents, prev.Intent); a.Confidence < jevSure && was != "" {
v.Intent = was
}
}
if a, ok := out.Answers["effort"]; ok {
i := int(math.Round(a.Score))
i = max(0, min(i, len(jevLevels)-1))
v.Effort, v.Score = jevLevels[i].effort, a.Score
}
return v, nil
}
// intentNamed is the one of intents named, or "".
func intentNamed(intents []string, name string) string {
for _, in := range intents {
if in == name {
return in
}
}
return ""
}
// levelled is what Jev said of each set of intents: whether they are
// levels every message is at, and what each is for (jevLevelled). It
// holds as long as the intents do.
var levelled = struct {
sync.Mutex
m map[string]levelling
}{m: map[string]levelling{}}
type levelling struct {
levels bool
work map[string]string // levelsOf; nil when Jev didn't rate them
}
// jevLevelled asks Jev whether intents are levels of one scale — how hard
// or big a request is — rather than topics a message may be none of, and
// in the same call how much work a request at each is, once for each set
// of them.
func (s *Server) jevLevelled(ctx context.Context, p provider.Provider, model string, intents []string) (levelling, error) {
key := model + "\x00" + strings.ToLower(strings.Join(intents, "\x00"))
levelled.Lock()
l, ok := levelled.m[key]
levelled.Unlock()
if ok {
return l, nil
}
qs := map[string]any{"levels": map[string]any{"type": "noul",
"instructions": "Are these `kinds` levels of one scale that every request to a coding assistant is at, such as how hard, how big or how urgent it is — rather than topics or tasks that a request may be none of?"}}
for i := range intents {
qs[fmt.Sprintf("work%d", i)] = map[string]any{"type": "score",
"instructions": fmt.Sprintf("Taking `kinds` as levels of one scale for requests to a coding assistant, how much work is a request at the level `kinds[%d]`?", i),
"criteria": levelWork}
}
body, _ := json.Marshal(map[string]any{"model": model, "state": map[string]any{"kinds": intents}, "questions": qs})
b, err := s.systemOne(ctx, p, model, body)
if err != nil {
return levelling{}, err
}
var out struct {
Answers map[string]struct {
Noul *float64 `json:"noul"`
Score *float64 `json:"score"`
} `json:"answers"`
}
if json.Unmarshal(b, &out) != nil || out.Answers["levels"].Noul == nil {
return levelling{}, answerError{fmt.Errorf("not a System One answer")}
}
l.levels = *out.Answers["levels"].Noul >= 0.5
if l.levels {
scores := make([]float64, 0, len(intents))
for i := range intents {
if a := out.Answers[fmt.Sprintf("work%d", i)]; a.Score != nil {
scores = append(scores, *a.Score)
}
}
l.work = levelsOf(intents, scores)
}
levelled.Lock()
levelled.m[key] = l
levelled.Unlock()
return l, nil
}
// askJev asks a decision provider's model through its System One API.
func (s *Server) askJev(p provider.Provider, model string, intents []string, prev before, effort bool, text string) (verdict, error) {
ctx, cancel := context.WithTimeout(context.Background(), classifyTimeout)
defer cancel()
if model == "" {
model = p.Jev()
}
var l levelling
if len(intents) > 0 {
var err error
if l, err = s.jevLevelled(ctx, p, model, intents); err != nil {
return verdict{}, err
}
}
b, err := s.systemOne(ctx, p, model, jevBody(model, intents, l.work, prev, effort, text))
if err != nil {
return verdict{}, err
}
return readJev(b, intents, l.levels, prev)
}
// systemOne posts body to p's System One API and returns the answer,
// counting it in the usage as magpie's own. A gateway serving Jev is asked
// and answers in its own way (decideAsk, decideAnswer).
func (s *Server) systemOne(ctx context.Context, p provider.Provider, model string, body []byte) ([]byte, error) {
start := time.Now()
status, b, _, err := s.postDecide(ctx, p, model, body)
if err != nil {
if ctx.Err() != nil {
return nil, fmt.Errorf("%s gave no answer in %s", p.Name, classifyTimeout)
}
return nil, err
}
if status >= 300 {
return nil, fmt.Errorf("%s: %s", p.Name, provider.APIError(b, fmt.Sprintf("%d %s", status, http.StatusText(status))))
}
var use struct {
Model string `json:"model"` // the one that answered, when the reply says
Usage struct {
Input int `json:"input_tokens"`
Output int `json:"output_tokens"`
} `json:"usage"`
}
_ = json.Unmarshal(b, &use)
var keyID, keyName string
if p.Account == nil && p.Key != "" {
keyID, keyName = provider.KeyID(p.Key), p.KeyName
}
usage.Append(usage.Record{Time: start, Agent: usage.AgentOf(RouterAgent), Provider: p.ID, Host: p.Where(), Model: model, Requested: model, Served: use.Model,
ProviderKeyID: keyID, ProviderKeyName: keyName,
Input: use.Usage.Input, Output: use.Usage.Output, Millis: time.Since(start).Milliseconds(), Status: status})
return b, nil
}
// postDecide posts body to p's decision API and returns the status, body
// and Content-Type. A success is rewritten into System One's shape when
// the provider answers in another one. DecideURL and Sign errors are as
// they are; a transport error is named with p.
func (s *Server) postDecide(ctx context.Context, p provider.Provider, model string, body []byte) (int, []byte, string, error) {
ctx = p.Via(ctx)
u, err := p.DecideURL(ctx)
if err != nil {
return 0, nil, "", err
}
via := p.DecideVia()
body = decideAsk(via, model, body)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, bytes.NewReader(body))
if err != nil {
return 0, nil, "", err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", RouterAgent)
if via == provider.ViaVercelEval {
// what the AI SDK's gateway provider sends with an evaluation
req.Header.Set("ai-gateway-protocol-version", "0.0.1")
req.Header.Set("ai-gateway-auth-method", "api-key")
req.Header.Set("ai-model-id", model)
req.Header.Set("ai-evaluation-model-specification-version", "4")
}
if err := p.Sign(ctx, req, provider.Chat, body); err != nil {
return 0, nil, "", err
}
res, err := s.client.Do(req)
if err != nil {
return 0, nil, "", fmt.Errorf("%s: %v", p.Name, err)
}
defer res.Body.Close()
b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
if res.StatusCode < 300 {
b = decideAnswer(via, b)
}
return res.StatusCode, b, res.Header.Get("Content-Type"), nil
}
// maxSystemOneBody is the most a System One request may be; larger is 413
// rather than a truncated body parsed as not a System One request.
const maxSystemOneBody = 1 << 20
// serveSystemOne is magpie's own System One API. The model's prefix names
// the Jev provider (gptload-jev/jev-latest); the rest is what that provider
// is asked, at its /v1/systemone. The call is a route of its own, as a
// conversation is: the Routing view and the day's jsonl would otherwise
// never see Jev, which answers no /v1/chat/completions.
func (s *Server) serveSystemOne(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(io.LimitReader(r.Body, maxSystemOneBody+1))
if err != nil {
writeError(w, provider.Chat, http.StatusBadRequest, err.Error())
return
}
if len(body) > maxSystemOneBody {
writeError(w, provider.Chat, http.StatusRequestEntityTooLarge, "request body too large")
return
}
var q struct {
Model string `json:"model"`
}
if json.Unmarshal(body, &q) != nil {
writeError(w, provider.Chat, http.StatusBadRequest, "not a System One request")
return
}
p, model, err := provider.RouteDecider(q.Model)
if err != nil {
writeError(w, provider.Chat, provider.DecideRouteStatus(err), err.Error())
return
}
body = withModel(body, model)
start := time.Now()
asked := q.Model
if asked == "" {
asked = p.ID + "/" + model
}
seat := decideSeat(p, model)
var used Usage
tr := s.trace.begin(Route{Time: start, Agent: agentOf(r), Session: sessionOf(r.Header), Model: asked, Provider: p.ID,
Order: []Weighed{seat}, Tries: []Try{{ID: seat.ID, Model: model, Start: start}}})
end := func(status int, msg string, tokens int) {
ms := time.Since(start).Milliseconds()
s.trace.update(tr, func(t *Route) {
try := &t.Tries[0]
try.Done, try.Status, try.Millis, try.Error = true, status, ms, msg
if status >= 400 {
try.Fail = failure(status, []byte(msg))
}
t.Done, t.Status, t.Error, t.Millis, t.Tokens = true, status, msg, ms, tokens
t.Usage = routeUsage(p.ID, model, used)
})
}
status, b, ctype, err := s.postDecide(r.Context(), p, model, body)
if err != nil {
writeError(w, provider.Chat, http.StatusBadGateway, err.Error())
end(http.StatusBadGateway, err.Error(), 0)
return
}
var use struct {
Model string `json:"model"` // the one that answered, when the reply says
Usage struct {
Input int `json:"input_tokens"`
Output int `json:"output_tokens"`
} `json:"usage"`
}
_ = json.Unmarshal(b, &use)
errMsg := ""
if status >= 300 {
errMsg = provider.APIError(b, fmt.Sprintf("%d %s", status, http.StatusText(status)))
}
tokens := use.Usage.Input + use.Usage.Output
used = Usage{Input: use.Usage.Input, Output: use.Usage.Output}
var keyID, keyName string
if p.Account == nil && p.Key != "" {
keyID, keyName = provider.KeyID(p.Key), p.KeyName
}
appendUsage(r, usage.Record{RouteID: tr.ID, Time: start, Agent: agentOf(r), Provider: p.ID, Host: p.Where(), Model: model, Requested: asked, Served: use.Model,
ProviderKeyID: keyID, ProviderKeyName: keyName,
Input: use.Usage.Input, Output: use.Usage.Output, Millis: time.Since(start).Milliseconds(), Status: status})
end(status, errMsg, tokens)
if ctype == "" || status < 300 {
ctype = "application/json"
}
w.Header().Set("Content-Type", ctype)
w.WriteHeader(status)
w.Write(b)
}
// decideSeat is the one account a System One request is sent to: a decision
// provider has no pool of conversation keys, only the key it is asked with.
func decideSeat(p provider.Provider, model string) Weighed {
w := Weighed{ID: p.ID, Provider: p.ID, Name: p.Name, Icon: p.Icon, Preset: p.Preset, Model: model, Kind: "provider"}
if p.Key != "" {
w.Kind, w.Who = "key", p.KeyName
if w.Who == "" {
w.Who = provider.Mask(p.Key)
}
}
return w
}
// decideAsk is a System One request (body) as via takes it: Vercel's
// TypeSafe API takes it as it is; its evaluation models name the model in
// a header and ask a noul as a boolean; Workers AI takes the state and
// questions as the input of a run of the model.
func decideAsk(via, model string, body []byte) []byte {
var q map[string]any
if via == provider.ViaSystemOne || via == provider.ViaVercel || json.Unmarshal(body, &q) != nil {
return body
}
delete(q, "model")
switch via {
case provider.ViaVercelEval:
qs, _ := q["questions"].(map[string]any)
for _, v := range qs {
if x, ok := v.(map[string]any); ok && x["type"] == "noul" {
x["type"] = "boolean"
}
}
b, _ := json.Marshal(q)
return b
case provider.ViaCloudflare:
b, _ := json.Marshal(map[string]any{"model": model, "input": q})
return b
}
return body
}
// decideAnswer is a gateway's answer as System One gives it: out of
// Cloudflare's envelope, or, from Vercel's evaluation models (its TypeSafe
// API answers as System One does), a boolean's probability as a
// noul, a confidence from the probabilities where there is none, and its
// usage named as System One names it.
func decideAnswer(via string, b []byte) []byte {
switch via {
case provider.ViaCloudflare:
var env struct {
Result json.RawMessage `json:"result"`
}
if json.Unmarshal(b, &env) == nil && len(env.Result) > 0 && env.Result[0] == '{' {
return env.Result
}
case provider.ViaVercelEval:
var v struct {
Model string `json:"model"`
Answers map[string]map[string]any `json:"answers"`
Usage struct {
Input int `json:"inputTokens"`
Output int `json:"outputTokens"`
} `json:"usage"`
}
if json.Unmarshal(b, &v) != nil || v.Answers == nil {
return b
}
for _, a := range v.Answers {
if a["type"] == "boolean" {
a["type"], a["noul"] = "noul", a["probability"]
continue
}
if _, ok := a["confidence"]; !ok {
if ps, ok := a["probabilities"].(map[string]any); ok {
a["confidence"] = confidence(ps)
}
}
}
out, _ := json.Marshal(map[string]any{"model": v.Model, "answers": v.Answers,
"usage": map[string]int{"input_tokens": v.Usage.Input, "output_tokens": v.Usage.Output}})
return out
}
return b
}
// confidence is how sure a set of probabilities is of its likeliest, as
// System One gives it: 0 with every option as likely, 1 with one certain
// (its 0.87 of three options is 0.8).
func confidence(ps map[string]any) float64 {
top, n := 0.0, 0
for _, v := range ps {
if f, ok := v.(float64); ok {
top, n = math.Max(top, f), n+1
}
}
if n < 2 {
return 1
}
return max(0, (top-1/float64(n))/(1-1/float64(n)))
}
// withEffort asks a request, in the client's own API, for reasoning at
// effort, in place of what the agent asked. Only a request that asked for
// reasoning is changed: one that didn't (a session title) stays without.
func withEffort(proto provider.Protocol, body []byte, effort string) []byte {
var v struct {
ReasoningEffort string `json:"reasoning_effort"`
Reasoning *struct {
Effort string `json:"effort"`
Summary string `json:"summary"`
} `json:"reasoning"`
Thinking *struct {
Type string `json:"type"`
} `json:"thinking"`
OutputConfig map[string]any `json:"output_config"`
MaxTokens int `json:"max_tokens"`
}
if effort == "" || json.Unmarshal(body, &v) != nil {
return body
}
switch proto {
case provider.Chat:
if v.ReasoningEffort == "" || v.ReasoningEffort == "none" {
return body
}
return withFields(body, map[string]any{"reasoning_effort": effort})
case provider.Responses:
if v.Reasoning == nil || v.Reasoning.Effort == "" || v.Reasoning.Effort == "none" {
return body
}
r := map[string]any{"effort": effort}
if v.Reasoning.Summary != "" {
r["summary"] = v.Reasoning.Summary
}
return withFields(body, map[string]any{"reasoning": r})
case provider.Anthropic:
if v.Thinking == nil {
return body
}
switch v.Thinking.Type {
case "adaptive":
oc := v.OutputConfig
if oc == nil {
oc = map[string]any{}
}
oc["effort"] = effort
return withFields(body, map[string]any{"output_config": oc})
case "enabled":
// the budget must leave room for the answer
budget := budgetOf(effort)
if v.MaxTokens > 0 {
budget = min(budget, v.MaxTokens-1)
}
if budget < 1024 {
return body
}
fields := map[string]any{"thinking": map[string]any{"type": "enabled", "budget_tokens": budget}}
if oc := v.OutputConfig; oc != nil && oc["effort"] != nil {
oc["effort"] = effort
fields["output_config"] = oc
}
return withFields(body, fields)
}
}
return body
}
// requestEffort is the reasoning a request asks its model for, in its own
// API's words: reasoning_effort, reasoning.effort, or — for Anthropic, when
// it asks to think — output_config's effort or thinking's budget as the
// level it is nearest. "" when it asks for none.
func requestEffort(proto provider.Protocol, body []byte) string {
if proto != provider.Anthropic {
return bodyEffort(proto, body)
}
var v struct {
Thinking *struct {
Type string `json:"type"`
Budget int `json:"budget_tokens"`
} `json:"thinking"`
OutputConfig struct {
Effort string `json:"effort"`
} `json:"output_config"`
}
if json.Unmarshal(body, &v) != nil || v.Thinking == nil || v.Thinking.Type != "enabled" && v.Thinking.Type != "adaptive" {
return ""
}
if e := effortOf(v.OutputConfig.Effort); e != "" {
return e
}
return effortOfBudget(v.Thinking.Budget)
}
// sentEffort is the reasoning a request goes to model at: what it asks
// for, fitted to the model's levels as it is fitted on the way out.
func sentEffort(proto provider.Protocol, body []byte, p provider.Provider, model string) string {
e := requestEffort(proto, body)
if e == "" {
return ""
}
return fitFor(p, model, e)
}
// fitFor is the effort a request asking for want goes to model at, at p:
// the level of its own nearest (fitEffort), and for a model known to have
// none to pick from — a thinking switch alone, as Xiaomi's mimo-v2.6-flash
// has — no more than high, which it takes where it turns max away (#214).
// A model whose levels just aren't known is asked as the agent asked.
func fitFor(p provider.Provider, model, want string) string {
levels := p.Efforts(model)
e := fitEffort(want, levels)
if len(levels) == 0 && slices.Index(effortRank, e) > slices.Index(effortRank, "high") && p.Levelless(model) {
return "high"
}
return e
}
// fitLevel is the level of the model's own nearest the one picked for it
// (fitEffort); one whose levels aren't known isn't asked for more than
// high, which every vendor with levels takes.
func fitLevel(want string, levels []string) string {
level := fitEffort(want, levels)
if len(levels) == 0 && level == "xhigh" {
level = "high"
}
return level
}
// withFixedEffort asks a request, in the client's own API, for reasoning
// at effort — that of a group's member fixed at it (#189) — whatever the
// agent asked: unlike withEffort, a request that asked for none is asked
// for it too, and "none" turns reasoning off.
func withFixedEffort(proto provider.Protocol, body []byte, effort string) []byte {
if effort == "" {
return body
}
switch proto {
case provider.Chat:
return withFields(body, map[string]any{"reasoning_effort": effort})
case provider.Responses:
var v struct {
Reasoning map[string]any `json:"reasoning"`
}
if json.Unmarshal(body, &v) != nil {
return body
}
if v.Reasoning == nil {
v.Reasoning = map[string]any{}
}
v.Reasoning["effort"] = effort
return withFields(body, map[string]any{"reasoning": v.Reasoning})
case provider.Anthropic:
var v struct {
Thinking *struct {
Type string `json:"type"`
} `json:"thinking"`
MaxTokens int `json:"max_tokens"`
}
if json.Unmarshal(body, &v) != nil {
return body
}
if effort == "none" {
return withFields(body, map[string]any{"thinking": map[string]any{"type": "disabled"}})
}
level := effortOf(effort) // in Anthropic's words: minimal is low
if v.Thinking != nil && (v.Thinking.Type == "enabled" || v.Thinking.Type == "adaptive") {
return withEffort(proto, body, level)
}
// asked to think, with room left for the answer
budget := budgetOf(level)
if v.MaxTokens > 0 {
budget = min(budget, v.MaxTokens-1)
}
if budget < 1024 {
return body
}
return withFields(body, map[string]any{"thinking": map[string]any{"type": "enabled", "budget_tokens": budget}})
}
return body
}