diff --git a/README.md b/README.md index 9778e3f..e8a7458 100644 --- a/README.md +++ b/README.md @@ -225,11 +225,13 @@ The gateway probes models whose IDs contain `free` (case-insensitive), or whose System One models use a native `noul` question asking whether 1 + 1 equals 2; they are never sent chat/tool payloads. Model discovery includes `native_protocol` and `supported_endpoints`; rotation aliases are listed only when Go keys and eligible models exist. Legacy v1.3.6 disables are cleared on load and scheduled for immediate recheck because those probes used invalid session IDs. -The intervals are fixed: enabled models are checked every **1 hour**, models confirmed unavailable every **24 hours**. A successful recheck restores the model automatically. The WebUI **Free model availability** page can restore it immediately, with its next check one hour later. An in-flight probe cannot undo a manual restore. Models without a usable Zen/anonymous lane, or without a supported inference protocol, are skipped. +The intervals are fixed: enabled models are checked every **1 hour**, models confirmed unavailable every **24 hours**. An inconclusive due recheck of a disabled model preserves its original disable reason and schedules another retry in 24 hours. A successful recheck restores the model automatically. The WebUI **Free model availability** page can restore it immediately, which puts the model under manual control: it stays enabled and is excluded from automatic probing, so later probe results cannot overwrite it. Use the per-model **manual/automatic toggle** on the same page to return it to automatic probing; the next scheduler pass then schedules a probe. Records left as `manually_enabled` by older versions migrate to manual control on load. An in-flight probe cannot undo a manual restore. Models without a usable Zen/anonymous lane, or without a supported inference protocol, are skipped. -State survives restarts in `config.json..availability.json`, separated by the Zen upstream URL. The background scheduler checks for due models once per minute and probes sequentially, with a 60-second timeout per lane. No channels available means no automatic disable. +Manual control also pauses effort discovery for `reasoning.effort=auto`; learned levels stop being forced after two hours without verification. Returning to automatic control allows effort discovery to resume. -Administration endpoints (login required; restore also requires CSRF): `GET /api/models/availability` and `POST /api/models/restore` with `{"model":"MODEL_ID"}`. +State survives restarts in `config.json..availability.json`, separated by the Zen upstream URL. The background scheduler checks for due models once per minute, skips manually controlled models, and probes the rest sequentially, with a 60-second timeout per lane. No channels available means no automatic disable. + +Administration endpoints (login required; writes also require CSRF): `GET /api/models/availability`, `POST /api/models/restore` with `{"model":"MODEL_ID"}` (restores and takes manual control), and `POST /api/models/manual` with `{"model":"MODEL_ID","manual":true|false}` to switch between manual and automatic control. ### Sessions and proxies diff --git a/README.zh-CN.md b/README.zh-CN.md index fbcfb65..344b8ea 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -225,11 +225,13 @@ System One 将自由文本状态与类型化问题配对,并返回类型化答 System One 使用原生 `noul` 类型询问“1+1 是否等于 2”,不注入聊天或工具字段。模型列表标明 `native_protocol` 和 `supported_endpoints`;仅在配置 Go Key 且有可用模型时展示轮转别名。升级时解除 v1.3.6 旧探测生成的禁用并立即安排复测,避免非法会话 ID 造成的误判继续保留。 -周期硬编码:正常模型每 **1 小时**探测,明确失效模型每 **24 小时**复测。复测成功自动恢复;也可在 WebUI「免费模型可用性」页立即恢复启用,下一次探测在 1 小时后。手动恢复不会被已经在途的旧探测结果覆盖。没有可用 Zen / 匿名通道,或推理协议不支持探测时跳过,不自动禁用。 +周期硬编码:正常模型每 **1 小时**探测,明确失效模型每 **24 小时**复测。已禁用模型到期复测遇到临时错误时,保留原禁用原因并在 24 小时后重试;复测成功自动恢复。也可在 WebUI「免费模型可用性」页立即恢复启用,进入手动控制:保持启用且不再自动探测,也不会被在途或后续探测结果覆盖;如需回到自动探测,在同一页面按「切回自动」,下一轮调度即安排探测。旧版本留下的 `manually_enabled` 记录在启动时自动迁移为手动控制。没有可用 Zen / 匿名通道,或推理协议不支持探测时跳过,不自动禁用。 -状态保存在 `config.json.<上游地址指纹>.availability.json`,重启后保留,按 Zen 上游 URL 隔离。后台每分钟检查到期项,串行探测,每条通道最多等待 60 秒。 +手动控制也会暂停 `reasoning.effort=auto` 的档位探测;已验证档位超过 2 小时后不再强制下发。切回自动后可重新探测档位。 -管理接口:`GET /api/models/availability` 查看状态;`POST /api/models/restore` 携带 `{"model":"MODEL_ID"}` 恢复。两者均需管理登录,恢复接口还需 CSRF 校验。 +状态保存在 `config.json.<上游地址指纹>.availability.json`,重启后保留,按 Zen 上游 URL 隔离。后台每分钟检查到期项,跳过手动控制的模型,串行探测其余模型,每条通道最多等待 60 秒。 + +管理接口:`GET /api/models/availability` 查看状态;`POST /api/models/restore` 携带 `{"model":"MODEL_ID"}` 恢复并进入手动控制;`POST /api/models/manual` 携带 `{"model":"MODEL_ID","manual":true|false}` 在手动与自动之间切换。均需管理登录,写入接口还需 CSRF 校验。 ### 会话与代理 diff --git a/internal/admin/availability.go b/internal/admin/availability.go index 7e0c619..6b9a3fd 100644 --- a/internal/admin/availability.go +++ b/internal/admin/availability.go @@ -24,3 +24,23 @@ func (a *Server) handleRestoreModel(w http.ResponseWriter, r *http.Request) { } a.handleAvailability(w, r) } + +func (a *Server) handleManualModel(w http.ResponseWriter, r *http.Request) { + var input struct { + Model string `json:"model"` + Manual *bool `json:"manual"` + } + if err := decodeAdminJSON(w, r, &input); err != nil { + writeAdminError(w, 400, "invalid_request", err.Error()) + return + } + if input.Manual == nil { + writeAdminError(w, 400, "invalid_request", "manual is required") + return + } + if err := a.manager.SetManualModel(input.Model, *input.Manual); err != nil { + writeAdminError(w, 400, "manual_failed", a.manager.Redact(err.Error())) + return + } + a.handleAvailability(w, r) +} diff --git a/internal/admin/server.go b/internal/admin/server.go index 3e6453a..88bcb3e 100644 --- a/internal/admin/server.go +++ b/internal/admin/server.go @@ -57,6 +57,7 @@ func (a *Server) Handler() http.Handler { mux.Handle("GET /api/debug/models", a.authenticate(http.HandlerFunc(a.handleDebugModels))) mux.Handle("GET /api/models/availability", a.authenticate(http.HandlerFunc(a.handleAvailability))) mux.Handle("POST /api/models/restore", a.authenticate(a.csrf(http.HandlerFunc(a.handleRestoreModel)))) + mux.Handle("POST /api/models/manual", a.authenticate(a.csrf(http.HandlerFunc(a.handleManualModel)))) mux.Handle("GET /api/rotation", a.authenticate(http.HandlerFunc(a.handleRotation))) mux.Handle("PUT /api/rotation", a.authenticate(a.csrf(http.HandlerFunc(a.handleSaveRotation)))) mux.Handle("POST /api/rotation/current", a.authenticate(a.csrf(http.HandlerFunc(a.handleSelectRotation)))) diff --git a/internal/gateway/availability.go b/internal/gateway/availability.go index 956d216..bcb2d46 100644 --- a/internal/gateway/availability.go +++ b/internal/gateway/availability.go @@ -27,9 +27,30 @@ func (m *RuntimeManager) AvailabilitySnapshot() []models.Availability { func (m *RuntimeManager) RestoreModel(model string) error { m.updateMu.Lock() defer m.updateMu.Unlock() + r := m.current.Load() + if r == nil { + return errors.New("gateway runtime is unavailable") + } for _, item := range m.AvailabilitySnapshot() { if item.Model == model { - return m.current.Load().availability.Restore(model, time.Now().UTC()) + return r.availability.Restore(model, time.Now().UTC()) + } + } + return errors.New("unknown free model") +} + +// SetManualModel switches a free model between manual and automatic control. +// Manual models stay enabled and are excluded from automatic probing. +func (m *RuntimeManager) SetManualModel(model string, manual bool) error { + m.updateMu.Lock() + defer m.updateMu.Unlock() + r := m.current.Load() + if r == nil { + return errors.New("gateway runtime is unavailable") + } + for _, item := range m.AvailabilitySnapshot() { + if item.Model == model { + return r.availability.SetManual(model, manual, time.Now().UTC()) } } return errors.New("unknown free model") @@ -62,6 +83,9 @@ func (m *RuntimeManager) checkFreeModels(ctx context.Context) { return } item = r.availability.Get(item.Model) + if item.Manual { + continue + } if time.Now().Before(item.NextCheck) { continue } diff --git a/internal/models/availability.go b/internal/models/availability.go index 847ae59..001835e 100644 --- a/internal/models/availability.go +++ b/internal/models/availability.go @@ -24,6 +24,7 @@ type Availability struct { NextCheck time.Time `json:"next_check"` Reason string `json:"reason,omitempty"` Channel string `json:"channel,omitempty"` + Manual bool `json:"manual,omitempty"` Generation uint64 `json:"-"` ProbeVersion int `json:"probe_version,omitempty"` AutoEffort EffortProbe `json:"auto_effort,omitempty"` @@ -53,6 +54,8 @@ func NewAvailabilityStore(path string) (*AvailabilityStore, error) { } // Legacy probes used invalid session IDs and treated temporary errors as // permanent failures. These records cannot establish model unavailability. + // The migration stays in memory and is re-derived on every load; the next + // explicit write persists it. for model, item := range s.items { if item.ProbeVersion == 0 && item.Disabled { item.Disabled = false @@ -60,6 +63,13 @@ func NewAvailabilityStore(path string) (*AvailabilityStore, error) { item.Reason = "legacy_probe_recheck" s.items[model] = item } + // Manual restores predate the manual lock flag. Preserve the + // operator's intent: a model left as manually_enabled stays under + // manual control instead of falling back under automatic probing. + if !item.Disabled && !item.Manual && item.Reason == "manually_enabled" { + item.Manual = true + s.items[model] = item + } } return s, nil } @@ -74,16 +84,30 @@ func (s *AvailabilityStore) Get(model string) Availability { func (s *AvailabilityStore) Disabled(model string) bool { return s.Get(model).Disabled } -// A manual restore invalidates results from probes that were already running. -func (s *AvailabilityStore) Restore(model string, now time.Time) error { +// SetManual switches a model between manual and automatic control. Manual +// mode keeps the model enabled and excludes it from automatic probing; +// switching back to automatic schedules a probe on the next check. +func (s *AvailabilityStore) SetManual(model string, manual bool, now time.Time) error { s.mu.Lock() defer s.mu.Unlock() - item := s.items[model] - previous, existed := s.items[model] - item.Model, item.Disabled, item.Reason = model, false, "manually_enabled" - item.NextCheck = now.Add(FreeModelCheckInterval) + item, existed := s.items[model] + // Retried requests must not erase disable evidence or reset the schedule. + if item.Manual == manual { + return nil + } + previous := item + item.Model = model + item.Manual = manual item.Generation++ item.ProbeVersion = 1 + if manual { + item.Disabled = false + item.Reason = "manually_enabled" + item.NextCheck = now.Add(FreeModelCheckInterval) + } else { + item.Reason = "auto_probe" + item.NextCheck = time.Time{} + } s.items[model] = item if err := s.saveLocked(); err != nil { if existed { @@ -96,6 +120,12 @@ func (s *AvailabilityStore) Restore(model string, now time.Time) error { return nil } +// A manual restore takes the model under manual control and invalidates +// results from probes that were already running. +func (s *AvailabilityStore) Restore(model string, now time.Time) error { + return s.SetManual(model, true, now) +} + func (s *AvailabilityStore) Record(model string, generation uint64, success bool, reason, channel string, now time.Time, effort EffortProbe) error { s.mu.Lock() defer s.mu.Unlock() @@ -103,11 +133,29 @@ func (s *AvailabilityStore) Record(model string, generation uint64, success bool if item.Generation != generation { return nil } + // Manual control freezes the record: automatic probes must never + // overwrite a manually_enabled state (neither Disabled nor Reason). + if item.Manual { + return nil + } item.Model = model if success { item.Disabled = false } else if reason == "model_unavailable" { item.Disabled = true + } else if item.Disabled { + // An inconclusive recheck of a still-disabled model carries no new + // evidence. Keep the previous disable reason and backoff instead of + // resetting to an hourly retry with the transient error. + item.CheckedAt = now + item.ProbeVersion = 1 + // A scheduled recheck has already reached NextCheck. Advance it to + // avoid probing on every scheduler tick after an inconclusive result. + if !item.NextCheck.After(now) { + item.NextCheck = now.Add(FailedModelCheckInterval) + } + s.items[model] = item + return s.saveLocked() } item.CheckedAt, item.Reason, item.Channel = now, reason, channel item.ProbeVersion = 1 diff --git a/webui/app.js b/webui/app.js index 56f8169..33f8419 100644 --- a/webui/app.js +++ b/webui/app.js @@ -12,7 +12,7 @@ let csrf = "", logRenderPending = false, toastTimer = null; const pages = { - availability: ["13", "免费模型可用性", "每小时探测,明确失效后禁用并每 24 小时复测。"], + availability: ["13", "免费模型可用性", "每小时探测,手动控制的模型不受影响。"], rotation: ["12", "模型轮转", "共享当前模型、故障切换与尝试记录。"], overview: ["01", "运行桌面", "最近一小时、进程累计与当前资源状态。"], guide: ["02", "首次运行", "用六个检查点完成从配置到首个请求。"], @@ -1401,11 +1401,12 @@ async function loadAvailability() { item.model, item.disabled ? "已禁用" : "已启用", [item.channel, item.reason].filter(Boolean).join(" / ") || "待探测", + item.manual ? "手动" : "自动", item.auto_effort?.effort ? `${item.auto_effort.effort}(${date(item.effort_checked_at)})` : "未验证", date(item.checked_at), - date(item.next_check), + item.manual ? "手动控制,不自动探测" : date(item.next_check), ]) { const cell = document.createElement("td"); cell.textContent = value; @@ -1417,6 +1418,7 @@ async function loadAvailability() { button.disabled = !item.disabled; button.onclick = async () => { button.disabled = true; + toggle.disabled = true; try { await api("/api/models/restore", { method: "POST", @@ -1426,10 +1428,31 @@ async function loadAvailability() { await loadDebugModels(); } catch (error) { $("#availability-error").textContent = error.message; - button.disabled = false; + button.disabled = !item.disabled; + toggle.disabled = false; } }; cell.append(button); + const toggle = document.createElement("button"); + toggle.className = "ghost"; + toggle.textContent = item.manual ? "切回自动" : "切为手动"; + toggle.onclick = async () => { + button.disabled = true; + toggle.disabled = true; + try { + await api("/api/models/manual", { + method: "POST", + body: JSON.stringify({ model: item.model, manual: !item.manual }), + }); + await loadAvailability(); + await loadDebugModels(); + } catch (error) { + $("#availability-error").textContent = error.message; + button.disabled = !item.disabled; + toggle.disabled = false; + } + }; + cell.append(toggle); row.append(cell); $("#availability-models").append(row); } diff --git a/webui/index.html b/webui/index.html index 485a070..86bd542 100644 --- a/webui/index.html +++ b/webui/index.html @@ -96,9 +96,10 @@

检查名称含 free 或元数据零价格且未弃用的模型。优先匿名通道,再尝试 Zen - Key;明确模型失效才禁用并在 24 小时后复测。限流、超时、网络和服务端错误不新增禁用,1 - 小时后重试。 System One 使用专用的 1+1 决策问题。设置 auto - 强度后,显示探测验证过的最高档位。 + Key;明确模型失效才禁用并在 24 小时后复测。临时错误不新增禁用:已启用模型 1 + 小时后重试,已禁用模型保留原原因并在 24 + 小时后重试。恢复启用或切为手动后,该模型保持启用且不再自动探测;切回自动后,下一轮调度即安排探测。 + System One 使用专用的 1+1 决策问题。设置 auto 强度后,显示探测验证过的最高档位。

@@ -108,6 +109,7 @@ 模型 状态 通道 / 结果 + 控制 已验证思考档位 上次探测 下次探测