diff --git a/README.md b/README.md index b1ef24a..a6dac84 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,8 @@ Requests are encoded for each tier's own protocol. With `performance.first_event When only anonymous access is configured, `/v1/models` exposes only models eligible for that lane. +Free models never need a Zen key: with `anonymous: true` they are served by the public lane, and a configured key is only their fallback. To keep keys but expose free models only, set `models.free_only: true`. Paid models then disappear from `/v1/models`, direct requests for them are rejected, and rotation aliases keep only free members; free models may still fall back to the configured keys. + ### Free model availability The gateway probes models whose IDs contain `free` (case-insensitive), or whose metadata reports zero input/output cost without deprecation, with a short request using its native protocol. It tries the enabled anonymous lane first and then one configured Zen Key. A valid completed response on either lane keeps the model enabled; the model is disabled in discovery and routing only when every attempted lane explicitly reports that the model does not exist or has been retired. Authentication errors, rate limits, timeouts, transport errors, 5xx responses, and malformed replies are inconclusive: they do not create a new disable, and are checked again in one hour. Probe traffic does not alter production key/proxy cooldowns or request statistics. It still reaches the upstream and may consume its free allowance. @@ -262,6 +264,7 @@ See [config.example.json](config.example.json) for a complete starting configura | `proxyfile` | Optional; relative paths resolve beside the configuration file. | | `models.refresh_seconds` | `300`; minimum 1. | | `models.protocols` | `{}`; per-model native protocol overrides. | +| `models.free_only` | `false`; expose and route only free models, even with Zen or Go keys. | | `reasoning.effort` | Empty (off); `minimal`, `low`, `medium`, `high`, `xhigh`, `max`, or `none`. | | `reasoning.effort_by_model` | `{}`; per-model overrides of `reasoning.effort`. | diff --git a/README.zh-CN.md b/README.zh-CN.md index 5a695a7..7d80622 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -219,6 +219,8 @@ System One 将自由文本状态与类型化问题配对,并返回类型化答 只有匿名通道可用时,`/v1/models` 仅展示符合匿名条件的模型。 +免费模型不需要 Zen Key:启用 `anonymous: true` 后由匿名通道提供,已配置的 Key 只作为回退。如果想保留 Key 但只暴露免费模型,设置 `models.free_only: true`:付费模型不再出现在 `/v1/models`,直接请求会被拒绝,轮转别名只保留免费成员;免费模型仍可回退到已配置的 Key。 + ### 免费模型可用性 只探测 ID 含 `free`(不区分大小写),或元数据输入、输出价格均为零且未弃用的模型,按模型原生协议发送简短探测请求。先尝试已启用的匿名通道,再尝试一个配置的 Zen Key;任一通道返回完整有效响应即视为可用,只有所有实际尝试的通道都明确返回模型不存在或已下架时才自动禁用,从模型列表与路由中移除。认证失败、429、超时、网络错误、5xx 和响应格式异常均视为结果不确定,不新增禁用,1 小时后复测。探测不修改生产 Key / 代理的冷却状态,也不计入请求统计,但仍会访问上游并可能消耗免费额度。 @@ -262,6 +264,7 @@ Key 初始化时均衡分配到代理。真实流量可以触发代理检查、K | `proxyfile` | 可选;相对路径基于配置文件所在目录解析。 | | `models.refresh_seconds` | `300`;最小为 1。 | | `models.protocols` | `{}`;按模型 ID 覆盖原生协议。 | +| `models.free_only` | `false`;即使配置了 Zen / Go Key,也只暴露和路由免费模型。 | | `reasoning.effort` | 空(关闭);可选 `auto`、`minimal`、`low`、`medium`、`high`、`xhigh`、`max`、`none`。 | | `reasoning.effort_by_model` | `{}`;按模型 ID 覆盖 `reasoning.effort`。 | diff --git a/config.example.json b/config.example.json index 083d85c..bcca4b8 100644 --- a/config.example.json +++ b/config.example.json @@ -17,7 +17,8 @@ }, "models": { "refresh_seconds": 300, - "protocols": {} + "protocols": {}, + "free_only": false }, "performance": { "max_idle_conns": 2048, diff --git a/internal/config/config.go b/internal/config/config.go index 2675fe8..4dc8b11 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -68,6 +68,9 @@ type RetryConfig struct { type ModelsConfig struct { RefreshSeconds int `json:"refresh_seconds"` Protocols map[string]string `json:"protocols"` + // FreeOnly limits discovery and routing to free models, even when Zen or + // Go keys could serve paid ones. Free models may still fall back to keys. + FreeOnly bool `json:"free_only"` } type LoggingConfig struct { diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index de8c6a0..5fe54b9 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -78,6 +78,7 @@ func New(cfg config.Config, logger *slog.Logger, monitor *telemetry.Monitor) (*G } catalog := models.NewCatalog(cfg.Prefer, cfg.Models.Protocols) catalog.SetRefreshInterval(time.Duration(cfg.Models.RefreshSeconds) * time.Second) + catalog.SetFreeOnly(cfg.Models.FreeOnly) return &Gateway{ cfg: cfg, rotation: rotationManager, diff --git a/internal/models/catalog.go b/internal/models/catalog.go index fd7d3cb..955b3ce 100644 --- a/internal/models/catalog.go +++ b/internal/models/catalog.go @@ -66,6 +66,7 @@ type Catalog struct { cacheSource string stale bool refreshAfter time.Duration + freeOnly bool } type CatalogSnapshot struct { @@ -127,6 +128,23 @@ func (c *Catalog) SetRefreshInterval(interval time.Duration) { c.mu.Unlock() } +// SetFreeOnly hides every model that is not free from discovery and routing. +func (c *Catalog) SetFreeOnly(enabled bool) { + c.mu.Lock() + c.freeOnly = enabled + c.mu.Unlock() +} + +// freeOnlyErrorLocked rejects paid models while models.free_only is set. Both +// routing entry points check it, so /v1/models, inference, rotation aliases and +// diagnostics agree on what is exposed. +func (c *Catalog) freeOnlyErrorLocked(model string) error { + if c.freeOnly && !c.anonymousDecision(model).Allowed { + return fmt.Errorf("model %q is not free and models.free_only is enabled", model) + } + return nil +} + func (c *Catalog) Replace(zen, goModels []string) { c.ReplaceWithCapabilities(zen, goModels, nil, nil, nil) } @@ -207,6 +225,9 @@ func (c *Catalog) routeLocked(model string, hasZenKeys, hasGoKeys, hasAnonymous if c.availability != nil && c.availability.Disabled(model) { return Route{}, fmt.Errorf("model %q is disabled after a failed availability check; restore it in the WebUI or wait for the next probe", model) } + if err := c.freeOnlyErrorLocked(model); err != nil { + return Route{}, err + } keyTiers := c.keyTierOrderLocked(model, hasZenKeys, hasGoKeys) // OpenCode's public credential is a Zen-only lane. Every free model starts // there, even if the current catalog only advertises it on Go: an upstream @@ -304,6 +325,9 @@ func (c *Catalog) RouteForTier(model string, tier config.Tier, hasZenKeys, hasGo if c.availability != nil && c.availability.Disabled(model) { return Route{}, fmt.Errorf("model %q is disabled after a failed availability check", model) } + if err := c.freeOnlyErrorLocked(model); err != nil { + return Route{}, err + } catalogPending := len(c.zen) == 0 && len(c.goModels) == 0 advertised := c.zen[model] if tier == config.TierGo { diff --git a/webui/app.js b/webui/app.js index 33f8419..ec41c23 100644 --- a/webui/app.js +++ b/webui/app.js @@ -149,6 +149,7 @@ function fillConfig(value) { $("#c-timeout").value = value.retry.timeout_seconds; $("#c-refresh").value = value.models.refresh_seconds; $("#c-protocols").value = JSON.stringify(value.models.protocols || {}, null, 2); + $("#c-free-only").checked = !!value.models.free_only; const reasoning = value.reasoning || {}; $("#c-reasoning-effort").value = reasoning.effort || ""; $("#c-reasoning-by-model").value = JSON.stringify(reasoning.effort_by_model || {}, null, 2); @@ -215,6 +216,7 @@ $("#config-form").addEventListener("submit", async (event) => { models: { refresh_seconds: number("#c-refresh"), protocols: JSON.parse($("#c-protocols").value || "{}"), + free_only: $("#c-free-only").checked, }, reasoning: { effort: $("#c-reasoning-effort").value, diff --git a/webui/index.html b/webui/index.html index 86bd542..66bd888 100644 --- a/webui/index.html +++ b/webui/index.html @@ -571,6 +571,11 @@