feat: add models.free_only to expose only free models (#66)

With Zen or Go keys configured, every paid model was exposed and routable.
models.free_only rejects models that are not free in both routing entry
points, so /v1/models, inference, rotation aliases and diagnostics agree.
Free models keep their key fallback. The WebUI gains a toggle so saving
the form no longer resets the field, and the docs note that free models
never require a Zen key.

Closes #66.
This commit is contained in:
Jason Xu committed 2026-10-15 08:08:01 +08:00
1 parent cf104d8f17
commit d04a9c750d
8 files changed
+43 -1

No files matched your search

+3
View File
@@ -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`. |
+3
View File
@@ -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`。 |
+2 -1
View File
@@ -17,7 +17,8 @@
},
"models": {
"refresh_seconds": 300,
"protocols": {}
"protocols": {},
"free_only": false
},
"performance": {
"max_idle_conns": 2048,
+3
View File
@@ -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 {
+1
View File
@@ -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,
+24
View File
@@ -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 {
+2
View File
@@ -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,
+5
View File
@@ -571,6 +571,11 @@
</div>
</div>
<div class="form-grid">
<div class="field full">
<label class="check-label"
><input id="c-free-only" type="checkbox" />仅暴露免费模型(即使配置了 Zen / Go Key)</label
>
</div>
<div class="field"><label>Zen URL</label><input id="c-up-zen" /></div>
<div class="field"><label>Go URL</label><input id="c-up-go" /></div>
<div class="field">