* usage: Requests lists the agents' own session-file calls, put to the vendor and account their files prove, charts them by provider, agent or model, shows what was said, and the tray panel gets a Usage tab The Usage page's Requests tab now covers requests an agent sends with its own sign-in (Claude Desktop's Code tab and Cowork, Claude Code, Codex), which never pass the gateway, and shows more of every request: why it failed, what was said in it, who it was of. Requests tab: a strip of four totals (tokens, requests, cost, cache hit rate); the trend of one metric — tokens, cost or requests — as columns by the hour, day or week, told apart by provider, agent or model (both remembered), beside a ranking of the same that is its legend and a way in (a click lists only that provider's or agent's requests; a provider picker sits beside the agent's). The ranking is as tall as the chart and scrolls in itself, with no scrollbar: a slim rail in the card's margin shows how much there is and where it is (its thumb dragged, the rail clicked), and the edge with more beyond it fades; under the chart (860px and narrower) it shows whole. A row opens to its details: the vendor's error type and words, request id, endpoint, and what was said in it (input and output, read from the agent's session file when opened, the call that ended nearest the request — nothing stored). A refresh period (off / 5 s / 10 s / 30 s / 1 min) and refresh-now. The Overview says it counts gateway calls only; Requests and the panel say they count both. The tray panel gets a Usage tab (today / 7 / 30 days, provider picker, four totals, a small chart, top five, "Open Usage"), and is as tall as its tallest tab (#124). Provider and account of a session-file call: never magpie's "claude" or "codex" subscription. Anthropic's when Claude Desktop's session metadata (not Claude-3p) names the account and organization, with that account's email; OpenAI's when Codex's rollout names a provider that is OpenAI's in config.toml — the built-in "openai", or a named one at OpenAI's address or signed in with the ChatGPT account and no base_url (requires_openai_auth) — with the creator account's email when the file records it. Anything else is "Local session", its recorded provider id and session account kept (CSV: session_provider, session_account). The config read is today's: a file names the provider id only. Gateway: a failed call keeps the vendor's error text and type, request id and endpoint; a Claude subscription's failure uses the status Claude Code reports (api_error_status). A request turned away before any vendor was asked is logged, marked rejected, listed in Requests and left out of every total. The client's own session header is kept as native_session beside X-Magpie-Session, and a session-file call is matched to a gateway record by request id, then by that session, equal tokens and an end within 2 s, only when unique both ways. Reading: session-file calls live in the session index main keeps on disk, parsed incrementally and outside its lock (Claude Code, Cowork in both Claude folders, Codex); the usage log is read on from where it was; a page builds the ledger once. Days are counted on calendar dates (DST). CSV gains request_id, endpoint, error_message, error_type, source, session_provider, session_account. i18n: one "Endpoint" (终结点), which a second key had turned for Azure's field. Tested: go vet ./..., go build (also GOOS=linux/windows, -tags nogui) and go test -tags nogui ./... pass. The whole GUI suite in Chromium and WebKit, en and zh (Playwright 1.62.1): all 393 tests pass. On this computer's files (7 days): 3,330 Claude Desktop calls resolve to Anthropic with the account, 1,991 Codex calls on a named ChatGPT-signed-in provider to OpenAI (620 with the account; older rollouts don't record it), 9 Claude Code CLI calls stay Local session. Co-authored-by: yetone <yetoneful@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * usage: a session-file call of one of magpie's own Codex or Claude accounts is told as that provider, beside the gateway's calls on it — "OpenAI" and "Codex" (or "Anthropic" and "Claude Code") read as two vendors when they were one subscription Codex: an official route (above) whose creator account and user are one of magpie's Codex sign-ins (the CLI's own or a saved one) is magpie's "codex", with that account's email as the gateway's rows have it. Claude Desktop (not Claude-3p): an account and organization that are one of magpie's Claude sign-ins is magpie's "claude". Only while magpie has that provider signed in; another account, a rollout with no account recorded, or an ambiguous workspace stays OpenAI or Anthropic, and a relay stays Local session. Tested: TestSessionCallsOfMagpiesAccountsAreItsProviders (the account matched, another account, none recorded, magpie's account through a relay, Desktop in magpie's and another organization, Claude-3p, magpie signed out) and TestLedgerTellsCodexSessionCallsAsMagpiesCodex (a gateway Codex call and a session call of its account one provider of two calls, another account's OpenAI). On this computer (7 days): 3,439 Claude Desktop calls are magpie's Claude, 626 Codex calls its Codex, 1,322 older Codex rollouts with no account stay OpenAI. go test -tags nogui ./... and the usage GUI tests pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: 修复请求统计缓存膨胀与会话账号误归属 #239 调用明细改为按会话文件分片保存并限制内存缓存,汇总索引仅保留统计数据;网关日志按需读取。 严格匹配订阅账号与用户 ID,排除自定义渠道、非官方端点覆盖、外部 CODEX_HOME 及关闭的供应商。补齐零 token 失败去重、Codex 原始会话 ID、拒绝标记导出和面板显示修复。 通过全量 Go 测试、静态检查、受影响模块的并发检查、52 项浏览器测试及 macOS/Linux/Windows 构建;使用 35 万条合成调用验证索引体积与内存改善。 * fix: 降低请求统计刷新与写入开销并明确本地账号展示 #239 请求分片改为追加帧和字符串去重,分页复用未变化的紧凑快照,采用 LRU 并移出全局锁内的磁盘读取。缓存 Cowork 目录,补齐同前缀改写检测及崩溃临时文件清理。 本地记录仅显示匹配账号或本地会话,明确官方登录标记与请求路由的区别。默认今日与模型维度,Auto Review 复用共享目录定价并单独标注估价参考。 完整 Go 测试、vet、相关 race 和 52 项浏览器回归通过;提供 2000 文件及四个大会话连续追加的可复现性能对照。 * fix: 优化用量徽标与耗时分级并保护分页边界 #239 官方登录、API、订阅和本地会话采用独立颜色与柔和底色,兼容明暗主题。耗时按 10 秒及 30 秒阈值着色,保留估算标识与原始毫秒提示;无有效耗时显示短横线。 限制分页选取范围,避免超大偏移整数溢出及越界空页的无效堆构造。补充阈值、徽标对比度和极端分页参数回归。 * fix: 将账号与来源徽标分为两行展示 #239 * fix: 提高用量表格模型文字对比度 #239 发送模型和已知实际模型使用主题正文次级色,表头与时间同步加深。保留缺失值弱化、转发提示和本地日志来源说明。Chromium 与 WebKit 的中英文、浅深色主题 10 项界面检查通过。 --------- Co-authored-by: yetone <yetoneful@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
533 lines
15 KiB
Go
533 lines
15 KiB
Go
// Package gateway is the local LLM endpoint agents talk to. It serves the
|
|
// wire APIs coding agents speak — OpenAI Chat Completions, OpenAI
|
|
// Responses, Anthropic Messages and Google Gemini — and forwards each call to whichever
|
|
// provider serves the requested model, translating between the APIs when
|
|
// the provider does not speak the one the agent used.
|
|
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// The APIs are close cousins; everything below is the shape they have
|
|
// in common. A request is parsed into it, and a reply is produced from it.
|
|
|
|
// Kind is what a part of a message holds.
|
|
type Kind string
|
|
|
|
const (
|
|
Text Kind = "text"
|
|
Image Kind = "image"
|
|
File Kind = "file"
|
|
ToolCall Kind = "tool_call"
|
|
ToolResult Kind = "tool_result"
|
|
Thinking Kind = "thinking"
|
|
Search Kind = "web_search" // a web search run for the model: its query and hits
|
|
)
|
|
|
|
// Part is one block of a message.
|
|
type Part struct {
|
|
Kind Kind
|
|
Text string // text, thinking, or a tool result's output
|
|
|
|
// image or file
|
|
MediaType string
|
|
Data string // base64
|
|
URL string
|
|
|
|
// tool_call
|
|
ID string
|
|
Name string
|
|
Args json.RawMessage // a JSON object
|
|
|
|
// tool_result
|
|
CallID string
|
|
IsError bool
|
|
Images []Part // the images the tool returned beside its text
|
|
|
|
// thinking
|
|
Signature string
|
|
|
|
// web_search: Text is the query
|
|
Hits []Hit
|
|
}
|
|
|
|
// Hit is a page a web search found.
|
|
type Hit struct {
|
|
Title string `json:"title"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
// attachmentText is the fallback when a protocol cannot carry a Gemini file.
|
|
// Inline data has no URL to show and must not be relabeled as an image.
|
|
func attachmentText(p Part) string {
|
|
if p.URL != "" {
|
|
return "[attachment " + p.MediaType + ": " + p.URL + "]"
|
|
}
|
|
return "[attachment " + p.MediaType + "]"
|
|
}
|
|
|
|
// Message is one turn.
|
|
type Message struct {
|
|
Role string // user | assistant
|
|
Parts []Part
|
|
}
|
|
|
|
// Tool is a function the model may call.
|
|
type Tool struct {
|
|
Name string
|
|
Description string
|
|
Schema json.RawMessage // JSON schema of the arguments
|
|
Strict bool // the client asked for its arguments held to the schema
|
|
}
|
|
|
|
// Request is a call to a model, whichever API it arrived in.
|
|
type Request struct {
|
|
Model string
|
|
System string
|
|
Messages []Message
|
|
Tools []Tool
|
|
ToolChoice string // "" | auto | none | required | name:<tool>
|
|
MaxTokens int
|
|
Temp *float64
|
|
TopP *float64
|
|
Stop []string
|
|
Stream bool
|
|
Effort string // low | medium | high | xhigh | max, when the client asked
|
|
Thinking bool // the client asked for visible reasoning
|
|
// ThinkOff is the client turning reasoning off: effort "none" (which
|
|
// Effort reads as low, for vendors with no way to turn it off) or an
|
|
// Anthropic request with thinking disabled.
|
|
ThinkOff bool
|
|
Parallel *bool // parallel tool calls allowed
|
|
WebSearch bool // the client offered its provider's own web search
|
|
Fast bool // the client asked for priority processing: service_tier priority (Codex's Fast mode)
|
|
// CacheKey is the client's prompt_cache_key (Codex sends its thread's
|
|
// id), which OpenAI, and relays in front of it, route a conversation by
|
|
// to where its prompt is cached.
|
|
CacheKey string
|
|
// Include is a Responses client's include, the extra output it asked
|
|
// for (Codex's reasoning.encrypted_content), which a Responses upstream
|
|
// is asked for too: a relay may refuse a request without it (#315).
|
|
Include []string
|
|
// ClientMetadata and Text are a Responses client's client_metadata
|
|
// (Codex's installation and session ids, which a relay may check, #374)
|
|
// and text (its verbosity, and the schema an answer must fit), which go
|
|
// on as they were sent when the request is built again for a Responses
|
|
// upstream; no other API takes them.
|
|
ClientMetadata json.RawMessage
|
|
Text json.RawMessage
|
|
// Metadata is an Anthropic client's metadata (Claude Code's user_id),
|
|
// which goes on as it was sent when the request is built again for an
|
|
// Anthropic upstream: a relay that serves only Claude Code turns a
|
|
// request without it away (#359).
|
|
Metadata json.RawMessage
|
|
// GeminiCompat is the upstream being Gemini's OpenAI-compatible API
|
|
// (AI Studio's, or a proxy in front of it on this machine or the LAN),
|
|
// which gives the model's thoughts only when asked in thinking_config.
|
|
GeminiCompat bool
|
|
// Namespaced are the tools a Responses client offered inside a
|
|
// namespace, by the flat name the model is offered them under.
|
|
Namespaced map[string]nsTool
|
|
}
|
|
|
|
// nsTool is a tool as a Responses client knows it: by its namespace and its
|
|
// name in it (Codex's collaboration.spawn_agent). Search is Codex's own
|
|
// tool search, offered to the model as a function and handed back as the
|
|
// tool_search_call Codex runs.
|
|
type nsTool struct {
|
|
Namespace, Name string
|
|
Search bool
|
|
}
|
|
|
|
// EventKind is what a streamed event carries.
|
|
type EventKind int
|
|
|
|
const (
|
|
KStart EventKind = iota // MsgID, Model, Usage (input side)
|
|
KText // Text
|
|
KThink // Text (reasoning)
|
|
KSig // Text (thinking signature)
|
|
KToolStart // ID, Name
|
|
KToolArgs // Text (partial JSON of the arguments)
|
|
KStop // Stop
|
|
KUsage // Usage
|
|
KError // Text
|
|
KSearch // Text (the query), Hits: a web search run for the model
|
|
)
|
|
|
|
// Event is one thing a streaming reply said.
|
|
type Event struct {
|
|
Kind EventKind
|
|
Status int // upstream HTTP status for KError, when known
|
|
Text string
|
|
ID string
|
|
Name string
|
|
MsgID string
|
|
Model string
|
|
Stop string // stop | length | tool | filter
|
|
// Code: for KError, the source error or safety-filter code (rate_limit,
|
|
// server_error, bio_policy, content_filter…); RequestID: the vendor's id for the
|
|
// request the event is of, when it is known by then
|
|
Code string
|
|
RequestID string
|
|
Usage Usage
|
|
Hits []Hit
|
|
}
|
|
|
|
// Usage counts tokens.
|
|
type Usage struct {
|
|
Input int `json:"input"`
|
|
Output int `json:"output"`
|
|
CacheRead int `json:"cache_read"`
|
|
CacheWrite int `json:"cache_write"`
|
|
Reasoning int `json:"reasoning"`
|
|
// Served: the model the vendor's reply says answered, when it named
|
|
// one — which may not be the one it was asked for
|
|
Served string `json:"served,omitempty"`
|
|
// RequestID: the id the vendor gave the request, from its reply's
|
|
// headers (Claude Code's own for a subscription); ErrType: what a
|
|
// failed request's error body called the error
|
|
RequestID string `json:"request_id,omitempty"`
|
|
ErrType string `json:"err_type,omitempty"`
|
|
}
|
|
|
|
// prompt is every token the prompt came to, as OpenAI's and Gemini's
|
|
// counts have it: Anthropic's leaves out what was read from its cache and
|
|
// what was written to it.
|
|
func (u Usage) prompt() int {
|
|
return u.Input + u.CacheRead + u.CacheWrite
|
|
}
|
|
|
|
func (u *Usage) add(v Usage) {
|
|
if v.Input > 0 {
|
|
u.Input = v.Input
|
|
}
|
|
if v.Output > 0 {
|
|
u.Output = v.Output
|
|
}
|
|
if v.CacheRead > 0 {
|
|
u.CacheRead = v.CacheRead
|
|
}
|
|
if v.CacheWrite > 0 {
|
|
u.CacheWrite = v.CacheWrite
|
|
}
|
|
if v.Reasoning > 0 {
|
|
u.Reasoning = v.Reasoning
|
|
}
|
|
if v.Served != "" {
|
|
u.Served = v.Served
|
|
}
|
|
if v.RequestID != "" {
|
|
u.RequestID = v.RequestID
|
|
}
|
|
if v.ErrType != "" {
|
|
u.ErrType = v.ErrType
|
|
}
|
|
}
|
|
|
|
// Result is a whole reply, for non-streaming clients.
|
|
type Result struct {
|
|
ID string
|
|
Model string
|
|
Parts []Part
|
|
Stop string
|
|
Usage Usage
|
|
}
|
|
|
|
// collector assembles a Result from events. Encoders use the same logic to
|
|
// know what the reply contained so far.
|
|
type collector struct {
|
|
res Result
|
|
args strings.Builder // arguments of the open tool call
|
|
err string
|
|
// the error's status and kind, as its event gave them
|
|
errStatus int
|
|
errCode string
|
|
}
|
|
|
|
func (c *collector) last(k Kind) *Part {
|
|
if n := len(c.res.Parts); n > 0 && c.res.Parts[n-1].Kind == k {
|
|
return &c.res.Parts[n-1]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *collector) closeTool() {
|
|
if p := c.last(ToolCall); p != nil && p.Args == nil {
|
|
s := strings.TrimSpace(c.args.String())
|
|
if s == "" {
|
|
s = "{}"
|
|
}
|
|
p.Args = json.RawMessage(s)
|
|
c.args.Reset()
|
|
}
|
|
}
|
|
|
|
func (c *collector) add(ev Event) {
|
|
switch ev.Kind {
|
|
case KStart:
|
|
c.res.ID, c.res.Model = ev.MsgID, ev.Model
|
|
c.res.Usage.add(ev.Usage)
|
|
case KText:
|
|
if p := c.last(Text); p != nil {
|
|
p.Text += ev.Text
|
|
} else {
|
|
c.closeTool()
|
|
c.res.Parts = append(c.res.Parts, Part{Kind: Text, Text: ev.Text})
|
|
}
|
|
case KThink:
|
|
if p := c.last(Thinking); p != nil {
|
|
p.Text += ev.Text
|
|
} else {
|
|
c.closeTool()
|
|
c.res.Parts = append(c.res.Parts, Part{Kind: Thinking, Text: ev.Text})
|
|
}
|
|
case KSig:
|
|
if p := c.last(Thinking); p != nil {
|
|
p.Signature += ev.Text
|
|
}
|
|
case KToolStart:
|
|
c.closeTool()
|
|
c.res.Parts = append(c.res.Parts, Part{Kind: ToolCall, ID: ev.ID, Name: ev.Name})
|
|
case KToolArgs:
|
|
c.args.WriteString(ev.Text)
|
|
case KStop:
|
|
c.closeTool()
|
|
c.res.Stop = ev.Stop
|
|
case KUsage:
|
|
c.res.Usage.add(ev.Usage)
|
|
c.res.Usage.add(Usage{RequestID: ev.RequestID})
|
|
case KError:
|
|
c.err, c.errStatus, c.errCode = ev.Text, ev.Status, ev.Code
|
|
case KSearch:
|
|
c.closeTool()
|
|
c.res.Parts = append(c.res.Parts, Part{Kind: Search, Text: ev.Text, Hits: ev.Hits})
|
|
}
|
|
}
|
|
|
|
func (c *collector) finish() Result {
|
|
c.closeTool()
|
|
if c.res.Stop == "" {
|
|
c.res.Stop = "stop"
|
|
for _, p := range c.res.Parts {
|
|
if p.Kind == ToolCall {
|
|
c.res.Stop = "tool"
|
|
}
|
|
}
|
|
}
|
|
return c.res
|
|
}
|
|
|
|
// hasTool reports whether a result calls any tool.
|
|
func hasTool(parts []Part) bool {
|
|
for _, p := range parts {
|
|
if p.Kind == ToolCall {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// argsOf is a tool call's arguments as a JSON object, never empty.
|
|
func argsOf(p Part) json.RawMessage {
|
|
if len(p.Args) == 0 || strings.TrimSpace(string(p.Args)) == "" {
|
|
return json.RawMessage("{}")
|
|
}
|
|
return p.Args
|
|
}
|
|
|
|
// argsString is the same as a string, for the APIs that want one.
|
|
func argsString(p Part) string { return string(argsOf(p)) }
|
|
|
|
// parseArgs turns the string form of arguments into an object; a string
|
|
// that is not JSON is wrapped so nothing is lost.
|
|
func parseArgs(s string) json.RawMessage {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
return json.RawMessage("{}")
|
|
}
|
|
if json.Valid([]byte(s)) && strings.HasPrefix(s, "{") {
|
|
return json.RawMessage(s)
|
|
}
|
|
b, _ := json.Marshal(map[string]string{"input": s})
|
|
return b
|
|
}
|
|
|
|
// text joins the text parts of a message.
|
|
func text(parts []Part) string {
|
|
var b strings.Builder
|
|
for _, p := range parts {
|
|
if p.Kind == Text {
|
|
b.WriteString(p.Text)
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// stringOrText reads a JSON value that is either a string or an array of
|
|
// {type:"text", text} blocks (Anthropic's system, tool results…).
|
|
func stringOrText(raw json.RawMessage) string {
|
|
if len(raw) == 0 {
|
|
return ""
|
|
}
|
|
var s string
|
|
if json.Unmarshal(raw, &s) == nil {
|
|
return s
|
|
}
|
|
var blocks []struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
ToolName string `json:"tool_name"`
|
|
}
|
|
if json.Unmarshal(raw, &blocks) == nil {
|
|
var b strings.Builder
|
|
for _, x := range blocks {
|
|
switch x.Type {
|
|
case "text", "input_text", "output_text":
|
|
b.WriteString(x.Text)
|
|
case "tool_reference":
|
|
// Claude Code's ToolSearch loads a deferred tool by naming
|
|
// it; every tool is already offered to a model elsewhere, so
|
|
// it is told the tool is there, not handed an empty result
|
|
if b.Len() > 0 {
|
|
b.WriteString("\n")
|
|
}
|
|
b.WriteString("Tool " + x.ToolName + " is loaded and can be called now.")
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// toolOutput reads a tool's result: its text, as stringOrText reads it,
|
|
// and the images in it. Anthropic's tool_result holds image blocks, a
|
|
// Responses function_call_output input_image parts and a Chat tool message,
|
|
// from clients that send them, image_url parts. An image named only by a
|
|
// vendor's file id has nothing to carry and is left out.
|
|
func toolOutput(raw json.RawMessage) (string, []Part) {
|
|
text := stringOrText(raw)
|
|
var blocks []struct {
|
|
Type string `json:"type"`
|
|
Source *struct {
|
|
MediaType string `json:"media_type"`
|
|
Data string `json:"data"`
|
|
URL string `json:"url"`
|
|
} `json:"source"`
|
|
ImageURL json.RawMessage `json:"image_url"`
|
|
}
|
|
if len(raw) == 0 || raw[0] != '[' || json.Unmarshal(raw, &blocks) != nil {
|
|
return text, nil
|
|
}
|
|
var images []Part
|
|
for _, b := range blocks {
|
|
switch b.Type {
|
|
case "image":
|
|
if s := b.Source; s != nil && s.Data != "" {
|
|
images = append(images, Part{Kind: Image, MediaType: s.MediaType, Data: s.Data})
|
|
} else if s != nil && s.URL != "" {
|
|
images = append(images, Part{Kind: Image, URL: s.URL})
|
|
}
|
|
case "input_image", "image_url":
|
|
var u string
|
|
if json.Unmarshal(b.ImageURL, &u) != nil {
|
|
var o struct {
|
|
URL string `json:"url"`
|
|
}
|
|
json.Unmarshal(b.ImageURL, &o)
|
|
u = o.URL
|
|
}
|
|
if u != "" {
|
|
images = append(images, imagePart(u))
|
|
}
|
|
}
|
|
}
|
|
return text, images
|
|
}
|
|
|
|
// effortOf normalises the reasoning effort names the APIs use.
|
|
func effortOf(s string) string {
|
|
switch strings.ToLower(strings.TrimSpace(s)) {
|
|
case "minimal", "none":
|
|
return "low"
|
|
case "low", "medium", "high", "xhigh", "max":
|
|
return strings.ToLower(s)
|
|
case "ultra": // Codex's max, with its own agents to hand work to
|
|
return "max"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// effortRank orders the reasoning levels agents and vendors name.
|
|
var effortRank = []string{"none", "minimal", "low", "medium", "high", "xhigh", "max", "ultra"}
|
|
|
|
// fitEffort is the level of the model's own nearest the one asked for — a
|
|
// tie goes up — or the one asked for when the model's aren't known. Codex
|
|
// asks "medium" of a model it was given no levels for, and an agent's
|
|
// setting can outlive the model it was picked for; GLM-5.3 takes low, high
|
|
// and max only.
|
|
//
|
|
// Codex's ultra is max to a model without an ultra of its own (a routing
|
|
// group offers it when a ChatGPT model in it does): sent as max, or the
|
|
// nearest the model has below it.
|
|
func fitEffort(want string, levels []string) string {
|
|
if want == "ultra" && !slices.Contains(levels, want) {
|
|
want = "max"
|
|
}
|
|
if len(levels) == 0 || slices.Contains(levels, want) {
|
|
return want
|
|
}
|
|
at := slices.Index(effortRank, want)
|
|
if at < 0 {
|
|
return want
|
|
}
|
|
best, dist := want, len(effortRank)
|
|
for _, l := range levels {
|
|
i := slices.Index(effortRank, l)
|
|
if i < 0 || l == "none" {
|
|
continue
|
|
}
|
|
d := i - at
|
|
if d < 0 {
|
|
d = -d
|
|
}
|
|
if d < dist || d == dist && i > at {
|
|
best, dist = l, d
|
|
}
|
|
}
|
|
return best
|
|
}
|
|
|
|
// budgetOf is the Anthropic thinking budget for an effort level.
|
|
func budgetOf(effort string) int {
|
|
switch effort {
|
|
case "low":
|
|
return 4096
|
|
case "medium":
|
|
return 10000
|
|
case "high":
|
|
return 24000
|
|
case "xhigh", "max":
|
|
return 32000
|
|
}
|
|
return 10000
|
|
}
|
|
|
|
// effortOfBudget goes the other way, for Anthropic clients asking others.
|
|
func effortOfBudget(n int) string {
|
|
switch {
|
|
case n <= 0:
|
|
return ""
|
|
case n <= 4096:
|
|
return "low"
|
|
case n <= 12000:
|
|
return "medium"
|
|
case n <= 24000:
|
|
return "high"
|
|
}
|
|
return "xhigh"
|
|
}
|