Skip to content

Commit 58f5667

Browse files
committed
feat(webui): theme toggle, paged ledger, working refresh status, and collapsed proxy chips
- Dark theme: an auto/light/dark cycle in the topbar, persisted per browser and applied before first paint by an inline head script; the dark palette moves behind data-theme so the runtime choice wins over the media query (#122). - Request routing and upstream attempts tables are paged (50/100/200/500 rows per page) instead of a hard last-100 cap, with translated pager controls (#122). - Free-model refresh status now calls POST /api/models/probe, which marks every automatic model due and wakes the probe loop, then reloads the snapshot; the button re-probes instead of only rereading the cache (#122). - The config page renders long key/proxy lists collapsed behind a 100-chip cap with an expand toggle, so a large proxy pool no longer freezes the page (#146). - New strings translated for en/vi/it; READMEs document the new admin endpoint.
1 parent 2bf547c commit 58f5667

16 files changed

Lines changed: 370 additions & 80 deletions

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ Manual control also pauses effort discovery for `reasoning.effort=auto`; learned
278278

279279
State survives restarts in `config.json.<upstream-fingerprint>.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.
280280

281-
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.
281+
Administration endpoints (login required; writes also require CSRF): `GET /api/models/availability`, `POST /api/models/probe` (marks every automatic free model due and starts a probe sweep right away, so the WebUI's refresh status button re-probes instead of only reloading the cached view), `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.
282282

283283
### Sessions and proxies
284284

‎README.zh-CN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ System One 使用原生 `noul` 类型询问“1+1 是否等于 2”,不注入
278278

279279
状态保存在 `config.json.<上游地址指纹>.availability.json`,重启后保留,按 Zen 上游 URL 隔离。后台每分钟检查到期项,跳过手动控制的模型,串行探测其余模型,每条通道最多等待 60 秒。
280280

281-
管理接口:`GET /api/models/availability` 查看状态;`POST /api/models/restore` 携带 `{"model":"MODEL_ID"}` 恢复并进入手动控制;`POST /api/models/manual` 携带 `{"model":"MODEL_ID","manual":true|false}` 在手动与自动之间切换。均需管理登录,写入接口还需 CSRF 校验。
281+
管理接口:`GET /api/models/availability` 查看状态;`POST /api/models/probe` 将所有自动探测的免费模型标记为到期并立即开始一轮探测,WebUI「刷新状态」按钮即调用它(真正的重新探测,而不是只重载缓存视图);`POST /api/models/restore` 携带 `{"model":"MODEL_ID"}` 恢复并进入手动控制;`POST /api/models/manual` 携带 `{"model":"MODEL_ID","manual":true|false}` 在手动与自动之间切换。均需管理登录,写入接口还需 CSRF 校验。
282282

283283
### 会话与代理
284284

‎internal/admin/availability.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ func (a *Server) handleAvailability(w http.ResponseWriter, r *http.Request) {
1212
httpx.WriteJSON(w, http.StatusOK, map[string]any{"models": a.manager.AvailabilitySnapshot()})
1313
}
1414

15+
// handleProbeModels marks every automatic free model due and wakes the probe
16+
// loop, so the availability page's refresh triggers a real re-probe instead
17+
// of only reloading the cached snapshot. It answers with the current
18+
// availability list; the fresh results land as the sweep records them.
19+
func (a *Server) handleProbeModels(w http.ResponseWriter, r *http.Request) {
20+
a.manager.RequestAvailabilitySweep()
21+
a.handleAvailability(w, r)
22+
}
23+
1524
// handleRestoreModel returns a disabled free model to manual control, which
1625
// re-enables it and keeps automatic probes from disabling it again. It answers
1726
// with the refreshed availability list.

‎internal/admin/server.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func (a *Server) Handler() http.Handler {
6767
mux.Handle("GET /api/models/availability", a.authenticate(http.HandlerFunc(a.handleAvailability)))
6868
mux.Handle("POST /api/models/restore", a.authenticate(a.csrf(http.HandlerFunc(a.handleRestoreModel))))
6969
mux.Handle("POST /api/models/manual", a.authenticate(a.csrf(http.HandlerFunc(a.handleManualModel))))
70+
mux.Handle("POST /api/models/probe", a.authenticate(a.csrf(http.HandlerFunc(a.handleProbeModels))))
7071
mux.Handle("GET /api/rotation", a.authenticate(http.HandlerFunc(a.handleRotation)))
7172
mux.Handle("PUT /api/rotation", a.authenticate(a.csrf(http.HandlerFunc(a.handleSaveRotation))))
7273
mux.Handle("POST /api/rotation/current", a.authenticate(a.csrf(http.HandlerFunc(a.handleSelectRotation))))

‎internal/gateway/availability.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func (m *RuntimeController) SetManualModel(model string, manual bool) error {
6868
func (m *RuntimeController) startAvailabilityChecks() {
6969
ctx, cancel := context.WithCancel(m.root)
7070
m.availabilityCancel = cancel
71+
m.availabilitySweep = make(chan struct{}, 1)
7172
go func() {
7273
ticker := time.NewTicker(time.Minute)
7374
defer ticker.Stop()
@@ -76,12 +77,32 @@ func (m *RuntimeController) startAvailabilityChecks() {
7677
select {
7778
case <-ctx.Done():
7879
return
80+
case <-m.availabilitySweep:
7981
case <-ticker.C:
8082
}
8183
}
8284
}()
8385
}
8486

87+
// RequestAvailabilitySweep marks every automatic free model due for a probe
88+
// and wakes the probe loop, so the availability page's refresh re-probes
89+
// instead of waiting out the minute ticker. A request while a sweep is
90+
// already queued is a no-op: the queued sweep reads the freshly marked
91+
// records as it iterates.
92+
func (m *RuntimeController) RequestAvailabilitySweep() {
93+
r := m.current.Load()
94+
if r == nil {
95+
return
96+
}
97+
if err := r.availability.ScheduleSweep(); err != nil {
98+
m.logger.Warn("model availability sweep could not be scheduled", "error", err)
99+
}
100+
select {
101+
case m.availabilitySweep <- struct{}{}:
102+
default:
103+
}
104+
}
105+
85106
// checkFreeModels probes, one at a time, every free model under automatic
86107
// control whose next check is due. A result is recorded only while the
87108
// runtime that ran the probe is still current.

‎internal/gateway/runtime.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type RuntimeController struct {
6464
effective EffectiveListeners
6565
metadata *modelcatalog.PricingRegistry
6666
availabilityCancel context.CancelFunc
67+
// availabilitySweep wakes the probe loop ahead of its minute tick; the
68+
// capacity-one buffer coalesces requests while a sweep is in flight.
69+
availabilitySweep chan struct{}
6770
// warpPool is the pool shared by successive runtimes when the WARP size
6871
// and stack are unchanged. warpKey is the identity used for that reuse.
6972
warpPool *warp.Pool

‎internal/models/availability.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ func (s *AvailabilityRegistry) Restore(model string, now time.Time) error {
150150
return s.SetManual(model, true, now)
151151
}
152152

153+
// ScheduleSweep marks every automatically controlled model due for a probe,
154+
// so the next check pass re-probes them instead of waiting out their
155+
// scheduled backoff. Manually controlled records are left untouched.
156+
func (s *AvailabilityRegistry) ScheduleSweep() error {
157+
s.mu.Lock()
158+
defer s.mu.Unlock()
159+
for model, item := range s.items {
160+
if item.Manual {
161+
continue
162+
}
163+
item.NextCheck = time.Time{}
164+
s.items[model] = item
165+
}
166+
return s.saveLocked()
167+
}
168+
153169
// Record stores the result of an automatic probe that started when the record
154170
// was at generation. The result is dropped when Generation has moved on since
155171
// then or the model is under manual control. Only the reason

‎webui/css/components.css‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,30 @@ textarea {
290290
color: var(--bad);
291291
}
292292

293+
.chip.chip-toggle {
294+
cursor: pointer;
295+
color: var(--text-2);
296+
border-style: dashed;
297+
background: transparent;
298+
}
299+
300+
.chip.chip-toggle:hover {
301+
color: var(--text-1);
302+
}
303+
304+
.pager {
305+
display: flex;
306+
align-items: center;
307+
gap: var(--sp-3);
308+
margin-top: var(--sp-3);
309+
}
310+
311+
.pager-info {
312+
color: var(--text-2);
313+
font-size: var(--fs-sm);
314+
white-space: nowrap;
315+
}
316+
293317
.code-card {
294318
margin: var(--sp-3) 0;
295319
border: 1px solid var(--border-1);

‎webui/css/tokens.css‎

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,29 @@
5656
--content-max: 1280px;
5757
}
5858

59-
@media (prefers-color-scheme: dark) {
60-
:root {
61-
color-scheme: dark;
59+
/* The theme module (webui/js/theme.js) resolves the stored choice and the
60+
system preference before first paint and sets data-theme accordingly. */
61+
:root[data-theme="dark"] {
62+
color-scheme: dark;
6263

63-
--bg: #0a0a0a;
64-
--bg-subtle: #171717;
65-
--surface: #121212;
66-
--surface-2: #0d0d0d;
67-
--text-1: #ededed;
68-
--text-2: #a0a0a0;
69-
--text-3: #787878;
70-
--border-1: #262626;
71-
--border-2: #3a3a3a;
72-
--inverse-bg: #ededed;
73-
--inverse-text: #0a0a0a;
74-
--inverse-hover: #cfcfcf;
75-
--focus-ring: rgba(237, 237, 237, 0.18);
76-
--overlay: rgba(0, 0, 0, 0.6);
64+
--bg: #0a0a0a;
65+
--bg-subtle: #171717;
66+
--surface: #121212;
67+
--surface-2: #0d0d0d;
68+
--text-1: #ededed;
69+
--text-2: #a0a0a0;
70+
--text-3: #787878;
71+
--border-1: #262626;
72+
--border-2: #3a3a3a;
73+
--inverse-bg: #ededed;
74+
--inverse-text: #0a0a0a;
75+
--inverse-hover: #cfcfcf;
76+
--focus-ring: rgba(237, 237, 237, 0.18);
77+
--overlay: rgba(0, 0, 0, 0.6);
7778

78-
--ok: #3fb950;
79-
--warn: #d29922;
80-
--warn-bg: rgba(210, 153, 34, 0.15);
81-
--bad: #f85149;
82-
--bad-bg: rgba(248, 81, 73, 0.15);
83-
}
79+
--ok: #3fb950;
80+
--warn: #d29922;
81+
--warn-bg: rgba(210, 153, 34, 0.15);
82+
--bad: #f85149;
83+
--bad-bg: rgba(248, 81, 73, 0.15);
8484
}

‎webui/index.html‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
<link rel="stylesheet" href="/css/base.css" />
1414
<link rel="stylesheet" href="/css/components.css" />
1515
<link rel="stylesheet" href="/css/pages.css" />
16+
<script>
17+
// Apply the stored theme before first paint; theme.js owns every change.
18+
try {
19+
var opencodeTheme = localStorage.getItem("opencode2api.ui.theme") || "auto";
20+
var opencodeDark =
21+
opencodeTheme === "dark" ||
22+
(opencodeTheme === "auto" && matchMedia("(prefers-color-scheme: dark)").matches);
23+
document.documentElement.setAttribute("data-theme", opencodeDark ? "dark" : "light");
24+
} catch (_) {}
25+
</script>
1626
</head>
1727
<body>
1828
<a class="skip-link" href="#main" data-i18n="a11y.skip">跳到主要内容</a>
@@ -81,6 +91,7 @@ <h1 id="page-title" class="page-title"></h1>
8191
<p id="page-sub" class="page-sub"></p>
8292
</div>
8393
<div class="topbar-actions">
94+
<button id="theme-toggle" class="btn btn-sm"></button>
8495
<select class="lang-switch" data-lang-switch aria-label="语言"></select>
8596
<button id="refresh-now" class="btn btn-sm" data-i18n="topbar.refresh">刷新</button>
8697
<button id="logout" class="btn btn-sm btn-ghost" data-i18n="topbar.logout">退出</button>
@@ -326,7 +337,7 @@ <h2 class="section-title" data-i18n="playground.result.title">诊断结果</h2>
326337
<div class="section-head">
327338
<div>
328339
<h2 class="section-title" data-i18n="diagnostics.requests.title">请求路由</h2>
329-
<p class="section-caption" data-i18n="diagnostics.requests.caption">最多展示最近 100 条。</p>
340+
<p class="section-caption" data-i18n="diagnostics.requests.caption">最多展示最近 500 条,分页显示。</p>
330341
</div>
331342
</div>
332343
<div class="table-wrap">
@@ -347,12 +358,28 @@ <h2 class="section-title" data-i18n="diagnostics.requests.title">请求路由</h
347358
<tbody id="upstream-requests"></tbody>
348359
</table>
349360
</div>
361+
<div class="pager">
362+
<button id="requests-pager-prev" class="btn btn-sm" data-i18n="diag.pager.prev">上一页</button>
363+
<span id="requests-pager-info" class="pager-info"></span>
364+
<button id="requests-pager-next" class="btn btn-sm" data-i18n="diag.pager.next">下一页</button>
365+
<select
366+
id="requests-pager-size"
367+
class="pager-size"
368+
aria-label="每页条数"
369+
data-i18n-attr="aria-label:diag.pager.size"
370+
>
371+
<option value="50">50</option>
372+
<option value="100" selected>100</option>
373+
<option value="200">200</option>
374+
<option value="500">500</option>
375+
</select>
376+
</div>
350377
</section>
351378
<section class="section">
352379
<div class="section-head">
353380
<div>
354381
<h2 class="section-title" data-i18n="diagnostics.attempts.title">上游尝试</h2>
355-
<p class="section-caption" data-i18n="diagnostics.attempts.caption">最多展示最近 100 条。</p>
382+
<p class="section-caption" data-i18n="diagnostics.attempts.caption">最多展示最近 500 条,分页显示。</p>
356383
</div>
357384
</div>
358385
<div class="table-wrap">
@@ -372,6 +399,22 @@ <h2 class="section-title" data-i18n="diagnostics.attempts.title">上游尝试</h
372399
<tbody id="upstream-attempts"></tbody>
373400
</table>
374401
</div>
402+
<div class="pager">
403+
<button id="attempts-pager-prev" class="btn btn-sm" data-i18n="diag.pager.prev">上一页</button>
404+
<span id="attempts-pager-info" class="pager-info"></span>
405+
<button id="attempts-pager-next" class="btn btn-sm" data-i18n="diag.pager.next">下一页</button>
406+
<select
407+
id="attempts-pager-size"
408+
class="pager-size"
409+
aria-label="每页条数"
410+
data-i18n-attr="aria-label:diag.pager.size"
411+
>
412+
<option value="50">50</option>
413+
<option value="100" selected>100</option>
414+
<option value="200">200</option>
415+
<option value="500">500</option>
416+
</select>
417+
</div>
375418
</section>
376419
</div>
377420
<div data-panel="logs" class="stack hidden" role="tabpanel">

0 commit comments

Comments
 (0)