mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
feat: expose cost and reasoning levels in /v1/models (#65)
Discovery clients only saw reasoning: true, so harnesses such as OpenClaw offered just off and max. Reasoning models now list the levels the gateway forwards in reasoning_efforts and compat.supportedReasoningEfforts, capped at the probed ceiling for auto, and report a forced or probed level as default_reasoning_effort. Models with published pricing carry a cost object, including cache read and write prices now decoded from models.dev. Closes #65.
This commit is contained in:
1 parent
d04a9c750d
commit
d434794866
6 files changed
+67
No files matched your search
@@ -198,6 +198,8 @@ Allowed protocol values are `chat`, `responses`, and `anthropic`. Models using u
|
||||
|
||||
Cost, deprecation, and model limits come from the official [OpenCode metadata endpoint](https://models.opencode.ai/api.json), refreshed every 24 hours. OpenCode's capability catalog remains authoritative for context and token limits; metadata `limit.context`, `limit.input`, and `limit.output` fill fields missing from that catalog. These limits are exposed by `/v1/models` as `context_window`/`context_length`, `max_input`, and `max_output` for discovery clients such as CLIProxyAPI and Pi. Metadata requests use a 30-second timeout per HTTP client. Refresh failures retain existing data.
|
||||
|
||||
Each `/v1/models` entry also carries `cost` (USD per million tokens: `input`, `output`, `cache_read`, `cache_write`, plus OpenClaw's `cacheRead`/`cacheWrite`) when the metadata states a price. Reasoning models list their levels in `reasoning_efforts` and in OpenClaw's `compat.supportedReasoningEfforts`: `minimal` through `max`, capped at the probed ceiling when the model's effort is `auto`. A forced or probed level is reported as `default_reasoning_effort`. Cache prices appear after the next metadata refresh.
|
||||
|
||||
### Anonymous access and fallback
|
||||
|
||||
With `anonymous: true`, either condition makes a model eligible for the anonymous Zen lane:
|
||||
|
||||
@@ -198,6 +198,8 @@ System One 将自由文本状态与类型化问题配对,并返回类型化答
|
||||
|
||||
成本、弃用信息及模型限制来自官方 [OpenCode 元数据地址](https://models.opencode.ai/api.json),每 24 小时刷新一次。能力目录中的限制优先;元数据中的 `limit.context`、`limit.input` 和 `limit.output` 仅补充缺失字段,通过 `/v1/models` 的 `context_window` / `context_length`、`max_input`、`max_output` 暴露。每个 HTTP 客户端的拉取超时为 30 秒,刷新失败时保留已有数据。升级后继续兼容原有 `.models.dev.json` 缓存文件名,无需删除缓存。
|
||||
|
||||
元数据给出价格时,`/v1/models` 的每个条目还会带上 `cost`(每百万 token 的美元价格:`input`、`output`、`cache_read`、`cache_write`,以及 OpenClaw 使用的 `cacheRead` / `cacheWrite`)。推理模型会在 `reasoning_efforts` 和 OpenClaw 读取的 `compat.supportedReasoningEfforts` 中列出可用档位:`minimal` 到 `max`;思考强度为 `auto` 时截止到探测得到的上限。强制或探测得到的档位通过 `default_reasoning_effort` 给出。缓存价格字段会在下一次元数据刷新后出现。
|
||||
|
||||
### 匿名通道与回退
|
||||
|
||||
启用 `anonymous: true` 后,满足以下任一条件的模型可以进入 Zen 匿名通道:
|
||||
|
||||
@@ -2,6 +2,7 @@ package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"opencode2api/internal/buildinfo"
|
||||
@@ -118,6 +119,22 @@ func (g *Gateway) availableModels() ([]modelcatalog.Route, modelcatalog.CatalogS
|
||||
return g.catalog.AvailableModels(g.zenNodes.Len() > 0, g.goNodes.Len() > 0, g.cfg.Anonymous)
|
||||
}
|
||||
|
||||
// reasoningLevels are the effort values the gateway accepts and forwards, from
|
||||
// lowest to highest. "auto" and "none" are gateway controls, not levels.
|
||||
var reasoningLevels = []string{"minimal", "low", "medium", "high", "xhigh", "max"}
|
||||
|
||||
// reasoningEfforts lists the levels a reasoning model can be asked for. When an
|
||||
// "auto" probe has found the highest level the upstream accepts, the list stops
|
||||
// there so clients do not offer levels that would be rejected.
|
||||
func (g *Gateway) reasoningEfforts(model string, route modelcatalog.Route) []string {
|
||||
if g.cfg.ForcedEffort(model) == "auto" {
|
||||
if ceiling := slices.Index(reasoningLevels, g.resolvedEffort(model, route.Tier, route.Protocol)); ceiling >= 0 {
|
||||
return slices.Clone(reasoningLevels[:ceiling+1])
|
||||
}
|
||||
}
|
||||
return slices.Clone(reasoningLevels)
|
||||
}
|
||||
|
||||
func (g *Gateway) handleModels(w http.ResponseWriter, _ *http.Request) {
|
||||
now := time.Now().Unix()
|
||||
routes, _ := g.availableModels()
|
||||
@@ -152,6 +169,35 @@ func (g *Gateway) handleModels(w http.ResponseWriter, _ *http.Request) {
|
||||
if md.Reasoning {
|
||||
entry["reasoning"] = true
|
||||
entry["supports_reasoning"] = true
|
||||
efforts := g.reasoningEfforts(model, route)
|
||||
entry["reasoning_efforts"] = efforts
|
||||
// OpenClaw and similar harnesses read effort levels from compat
|
||||
// rather than inferring them, and otherwise offer only off/max.
|
||||
entry["compat"] = map[string]any{"supportsReasoningEffort": true, "supportedReasoningEfforts": efforts}
|
||||
if effort := g.resolvedEffort(model, route.Tier, route.Protocol); effort != "" && effort != "none" {
|
||||
entry["default_reasoning_effort"] = effort
|
||||
}
|
||||
}
|
||||
if md.Cost != nil {
|
||||
cost := map[string]any{}
|
||||
// Camel-case aliases match OpenClaw's model cost shape.
|
||||
for _, field := range []struct {
|
||||
names []string
|
||||
value *float64
|
||||
}{
|
||||
{[]string{"input"}, md.Cost.Input},
|
||||
{[]string{"output"}, md.Cost.Output},
|
||||
{[]string{"cache_read", "cacheRead"}, md.Cost.CacheRead},
|
||||
{[]string{"cache_write", "cacheWrite"}, md.Cost.CacheWrite},
|
||||
} {
|
||||
if field.value == nil {
|
||||
continue
|
||||
}
|
||||
for _, name := range field.names {
|
||||
cost[name] = *field.value
|
||||
}
|
||||
}
|
||||
entry["cost"] = cost
|
||||
}
|
||||
if md.ToolCall {
|
||||
entry["tool_call"] = true
|
||||
|
||||
@@ -515,6 +515,9 @@ func (c *Catalog) MetadataForTier(model string, tier config.Tier) Metadata {
|
||||
if md.MaxOutput == 0 {
|
||||
md.MaxOutput = price.MaxOutput
|
||||
}
|
||||
if price.Input != nil || price.Output != nil {
|
||||
md.Cost = &Cost{Input: price.Input, Output: price.Output, CacheRead: price.CacheRead, CacheWrite: price.CacheWrite}
|
||||
}
|
||||
return md
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,17 @@ type Metadata struct {
|
||||
StructuredOutput bool `json:"structured_output,omitempty"`
|
||||
InputModalities []string `json:"input_modalities,omitempty"`
|
||||
OutputModalities []string `json:"output_modalities,omitempty"`
|
||||
// Cost is filled from models.dev at read time, in USD per million tokens.
|
||||
Cost *Cost `json:"cost,omitempty"`
|
||||
}
|
||||
|
||||
// Cost is the published per-million-token price of a model. A nil field means
|
||||
// the metadata did not state it.
|
||||
type Cost struct {
|
||||
Input *float64 `json:"input,omitempty"`
|
||||
Output *float64 `json:"output,omitempty"`
|
||||
CacheRead *float64 `json:"cache_read,omitempty"`
|
||||
CacheWrite *float64 `json:"cache_write,omitempty"`
|
||||
}
|
||||
|
||||
func (m *capabilityModel) metadata() Metadata {
|
||||
|
||||
@@ -30,6 +30,8 @@ type Price struct {
|
||||
ID string `json:"id"`
|
||||
Input *float64 `json:"input_cost,omitempty"`
|
||||
Output *float64 `json:"output_cost,omitempty"`
|
||||
CacheRead *float64 `json:"cache_read_cost,omitempty"`
|
||||
CacheWrite *float64 `json:"cache_write_cost,omitempty"`
|
||||
ContextWindow int `json:"context_window,omitempty"`
|
||||
MaxInput int `json:"max_input,omitempty"`
|
||||
MaxOutput int `json:"max_output,omitempty"`
|
||||
@@ -326,6 +328,7 @@ func decodeModelsDev(data []byte) (map[string]Price, error) {
|
||||
limit := jsonutil.MapAt(model, "limit")
|
||||
result[modelID] = Price{
|
||||
ID: modelID, Input: numberPointer(cost, "input"), Output: numberPointer(cost, "output"),
|
||||
CacheRead: numberPointer(cost, "cache_read"), CacheWrite: numberPointer(cost, "cache_write"),
|
||||
ContextWindow: jsonutil.IntAt(limit, "context"), MaxInput: jsonutil.IntAt(limit, "input"), MaxOutput: jsonutil.IntAt(limit, "output"),
|
||||
Deprecated: metadataDeprecated(model),
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user