dsh-sub2api 0.1.7 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/client.js CHANGED
@@ -5,7 +5,7 @@ window.__ModuleLoader__.load({
5
5
  var module = { exports: {} };
6
6
  var exports = module.exports;
7
7
  const h = react.createElement;
8
- const { useCallback, useEffect, useRef, useState } = react;
8
+ const { Component, useCallback, useEffect, useMemo, useRef, useState } = react;
9
9
 
10
10
  // ---------------------------------------------------------------- api
11
11
 
@@ -69,21 +69,23 @@ window.__ModuleLoader__.load({
69
69
  marginBottom: 12,
70
70
  overflow: "hidden",
71
71
  },
72
- cardHead: {
72
+ head: {
73
73
  display: "flex",
74
74
  alignItems: "center",
75
75
  gap: 10,
76
- padding: "14px 18px 12px",
76
+ padding: "13px 18px 11px",
77
77
  borderBottom: "0.5px solid " + C.line,
78
78
  },
79
- cardTitle: { margin: 0, fontSize: 14, fontWeight: 600, lineHeight: "22px", color: C.text },
80
- cardBody: { padding: "16px 18px" },
79
+ title: { margin: 0, fontSize: 14, fontWeight: 600, lineHeight: "22px", color: C.text },
80
+ body: { padding: "16px 18px" },
81
81
  meta: { marginLeft: "auto", fontSize: 12, lineHeight: "18px", color: C.muted },
82
82
  label: { fontSize: 13, fontWeight: 500, lineHeight: "20px", color: C.second },
83
83
  hint: { fontSize: 12, lineHeight: "18px", color: C.muted },
84
- small: { fontSize: 12, lineHeight: "18px", color: C.second },
84
+ small: { fontSize: 12.5, lineHeight: "19px", color: C.second },
85
85
  row: { display: "flex", gap: 10, alignItems: "center", flexWrap: "wrap" },
86
86
  mono: { fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace", fontSize: 12.5 },
87
+ kv: { display: "flex", gap: 8, alignItems: "baseline", padding: "3px 0", fontSize: 12.5 },
88
+ kvKey: { width: 68, flex: "0 0 68px", color: C.muted },
87
89
  };
88
90
 
89
91
  function Button({ variant = "default", size = "md", style, children, ...rest }) {
@@ -108,23 +110,22 @@ window.__ModuleLoader__.load({
108
110
  const variants = {
109
111
  primary: { border: "1px solid transparent", background: C.accent, color: "#fff" },
110
112
  ghost: { border: "0.5px solid transparent", background: "transparent", color: C.second },
111
- danger: { border: "0.5px solid " + C.line, background: "transparent", color: C.bad },
112
113
  };
113
114
  return h("button", { style: { ...base, ...(variants[variant] || {}), ...style }, ...rest }, children);
114
115
  }
115
116
 
116
- function Card({ title, meta, children, bodyStyle, id }) {
117
+ function Card({ title, meta, children, id, aside }) {
117
118
  return h("section", { id, style: T.card },
118
119
  title
119
- ? h("div", { style: T.cardHead },
120
- h("h3", { style: T.cardTitle }, title),
121
- meta ? h("span", { style: T.meta }, meta) : null)
120
+ ? h("div", { style: T.head },
121
+ h("h3", { style: T.title }, title),
122
+ meta ? h("span", { style: T.meta }, meta) : null,
123
+ aside ?? null)
122
124
  : null,
123
- h("div", { style: { ...T.cardBody, ...bodyStyle } }, children),
125
+ h("div", { style: T.body }, children),
124
126
  );
125
127
  }
126
128
 
127
- /** A labelled input with an optional trailing control and a focus ring. */
128
129
  function TextField({ label, hint, trailing, style, ...rest }) {
129
130
  const [focused, setFocused] = useState(false);
130
131
  return h("label", { style: { display: "flex", flexDirection: "column", gap: 6, minWidth: 0, ...style } },
@@ -138,7 +139,7 @@ window.__ModuleLoader__.load({
138
139
  padding: "0 4px 0 12px",
139
140
  borderRadius: 10,
140
141
  border: "0.5px solid " + (focused ? C.accent : C.line),
141
- boxShadow: focused ? "0 0 0 3px " + "color-mix(in srgb, " + C.accent + " 14%, transparent)" : "none",
142
+ boxShadow: focused ? "0 0 0 3px color-mix(in srgb, " + C.accent + " 14%, transparent)" : "none",
142
143
  background: C.base,
143
144
  transition: "border-color 120ms ease, box-shadow 120ms ease",
144
145
  },
@@ -191,18 +192,21 @@ window.__ModuleLoader__.load({
191
192
  );
192
193
  }
193
194
 
194
- function money(value) {
195
- const n = Number(value);
196
- return Number.isFinite(n) ? "$" + n.toFixed(2) : "—";
197
- }
198
-
199
195
  function Divider() {
200
196
  return h("div", { style: { height: 1, background: C.line, margin: "14px 0" } });
201
197
  }
202
198
 
203
- // ---------------------------------------------------------- account
199
+ function Row({ label, children }) {
200
+ return h("div", { style: T.kv },
201
+ h("span", { style: T.kvKey }, label),
202
+ h("span", { style: { flex: 1, minWidth: 0, color: C.text } }, children));
203
+ }
204
+
205
+ function money(value) {
206
+ const n = Number(value);
207
+ return Number.isFinite(n) ? "$" + n.toFixed(2) : "—";
208
+ }
204
209
 
205
- /** Never hand React an object: it would render "[object Object]". */
206
210
  function asText(value) {
207
211
  if (typeof value === "string") return value;
208
212
  if (value === null || value === undefined) return "";
@@ -217,6 +221,49 @@ window.__ModuleLoader__.load({
217
221
  return String(value);
218
222
  }
219
223
 
224
+ /** Progress values arrive either as 0..1 or 0..100 depending on the endpoint. */
225
+ function pct(value) {
226
+ const n = Number(value);
227
+ if (!Number.isFinite(n)) return null;
228
+ return Math.max(0, Math.min(100, n <= 1 ? n * 100 : n));
229
+ }
230
+
231
+ function Bar({ label, value }) {
232
+ const p = pct(value);
233
+ if (p === null) return null;
234
+ const tone = p >= 90 ? C.bad : p >= 70 ? C.warn : C.accent;
235
+ return h("div", { style: { marginTop: 6 } },
236
+ h("div", { style: { display: "flex", justifyContent: "space-between", fontSize: 11.5, color: C.muted } },
237
+ h("span", null, label),
238
+ h("span", null, Math.round(p) + "%")),
239
+ h("div", { style: { height: 4, borderRadius: 999, background: C.sunken, marginTop: 3, overflow: "hidden" } },
240
+ h("div", { style: { width: p + "%", height: "100%", background: tone, borderRadius: 999 } })),
241
+ );
242
+ }
243
+
244
+ // ------------------------------------------------------------- error
245
+
246
+ /** A render failure must not take the whole settings page down. */
247
+ class Boundary extends Component {
248
+ constructor(props) {
249
+ super(props);
250
+ this.state = { error: null };
251
+ }
252
+ static getDerivedStateFromError(error) {
253
+ return { error };
254
+ }
255
+ render() {
256
+ if (this.state.error) {
257
+ return h(Alert, { tone: "error", title: "Sub2API 面板渲染失败" },
258
+ asText(this.state.error) || "未知错误",
259
+ h("div", { style: { ...T.hint, marginTop: 6 } }, "把这段信息发给我即可定位。"));
260
+ }
261
+ return this.props.children;
262
+ }
263
+ }
264
+
265
+ // ---------------------------------------------------------- account
266
+
220
267
  function ProvisionReport({ steps, ready }) {
221
268
  if (!Array.isArray(steps)) return null;
222
269
  const notable = steps.filter((s) => !s.ok && s.tone !== "warn");
@@ -269,26 +316,46 @@ window.__ModuleLoader__.load({
269
316
 
270
317
  if (state.loggedIn) {
271
318
  const user = state.user || {};
272
- const balance = state.profile ? state.profile.balance : undefined;
319
+ const profile = state.profile || {};
320
+ const usage = state.usage || {};
321
+ const subs = Array.isArray(state.subscriptions?.subscriptions) ? state.subscriptions.subscriptions : [];
322
+
323
+ const cost = (key) => {
324
+ const value = Number(usage[key]);
325
+ return Number.isFinite(value) ? money(value) : null;
326
+ };
327
+ const today = cost("today_cost") ?? cost("todayCost") ?? cost("cost_today");
328
+ const month = cost("total_cost") ?? cost("month_cost") ?? cost("cost_month");
329
+ const requests = usage.total_requests ?? usage.request_count;
330
+
273
331
  return h(Card, {
274
332
  title: "账户",
275
333
  meta: state.apiBase,
334
+ aside: h(Button, { onClick: onLogout, disabled: busy, size: "sm", style: { marginLeft: "auto" } }, "退出登录"),
276
335
  },
277
- h("div", { style: { display: "flex", alignItems: "center", gap: 12, flexWrap: "wrap" } },
278
- h("div", { style: { display: "flex", flexDirection: "column", gap: 2, minWidth: 0 } },
279
- h("span", { style: { fontSize: 14, fontWeight: 600, color: C.text } },
280
- state.email || user.email || "已登录"),
281
- h("span", { style: T.hint }, "余额 " + money(balance)),
282
- ),
283
- h("div", { style: { marginLeft: "auto" } },
284
- h(Button, { onClick: onLogout, disabled: busy }, "退出登录")),
285
- ),
286
- Array.isArray(state.subscriptions?.subscriptions) && state.subscriptions.subscriptions.length > 0
287
- ? h("div", { style: { marginTop: 12 } },
288
- h(Alert, { tone: "info" },
289
- state.subscriptions.subscriptions
290
- .map((s) => `${s.group_name || "订阅"} · 剩余 ${s.days_remaining ?? "?"} 天`)
291
- .join(" ")))
336
+ h(Row, { label: "账号" }, state.email || user.email || "已登录"),
337
+ h(Row, { label: "余额" },
338
+ h("span", { style: { fontSize: 15, fontWeight: 600, color: Number(profile.balance) > 0 ? C.ok : C.warn } },
339
+ money(profile.balance)),
340
+ Number(profile.concurrency) > 0
341
+ ? h("span", { style: { ...T.hint, marginLeft: 8 } }, `并发 ${profile.concurrency}`)
342
+ : null),
343
+ today || month || Number.isFinite(Number(requests))
344
+ ? h(Row, { label: "用量" },
345
+ [today ? `今日 ${today}` : null, month ? `累计 ${month}` : null,
346
+ Number.isFinite(Number(requests)) ? `${requests} 次请求` : null]
347
+ .filter(Boolean).join(" · "))
348
+ : null,
349
+ subs.length > 0
350
+ ? h("div", { style: { marginTop: 10 } },
351
+ subs.map((entry, index) =>
352
+ h("div", { key: index, style: { marginBottom: 8 } },
353
+ h("div", { style: { ...T.small, fontWeight: 500 } },
354
+ `${entry.group_name || "订阅"}${entry.days_remaining !== null && entry.days_remaining !== undefined ? ` · 剩余 ${entry.days_remaining} 天` : ""}`),
355
+ h(Bar, { label: "日", value: entry.daily_progress }),
356
+ h(Bar, { label: "周", value: entry.weekly_progress }),
357
+ h(Bar, { label: "月", value: entry.monthly_progress }),
358
+ )))
292
359
  : null,
293
360
  h(ProvisionReport, { steps, ready }),
294
361
  );
@@ -376,10 +443,8 @@ window.__ModuleLoader__.load({
376
443
  whiteSpace: "nowrap",
377
444
  },
378
445
  }, revealed || state.keyMasked || "尚未创建"),
379
- h(Button, { onClick: copy, disabled: !state.loggedIn || !state.hasKey },
380
- copied ? "已复制" : "复制"),
381
- h(Button, { onClick: onCreate, disabled: busy || !state.loggedIn },
382
- state.hasKey ? "重建密钥" : "一键创建"),
446
+ h(Button, { onClick: copy, disabled: !state.loggedIn || !state.hasKey }, copied ? "已复制" : "复制"),
447
+ h(Button, { onClick: onCreate, disabled: busy || !state.loggedIn }, state.hasKey ? "重建密钥" : "一键创建"),
383
448
  ),
384
449
  h("div", { style: { ...T.hint, marginTop: 10 } },
385
450
  "密钥固定命名为 ", h("code", { style: T.mono }, state.keyName || "dsh"),
@@ -391,12 +456,14 @@ window.__ModuleLoader__.load({
391
456
 
392
457
  const PRESETS = [10, 20, 50, 100];
393
458
 
394
- function RechargeCard({ state, busy, onRecharge, onPoll, onRefresh }) {
459
+ function RechargeCard({ state, busy, onRecharge, onPoll, onRefresh, onRedeem }) {
395
460
  const [amount, setAmount] = useState(20);
396
461
  const [custom, setCustom] = useState("");
397
462
  const [order, setOrder] = useState(null);
398
463
  const [status, setStatus] = useState(null);
399
464
  const [error, setError] = useState("");
465
+ const [code, setCode] = useState("");
466
+ const [redeemMsg, setRedeemMsg] = useState("");
400
467
  const timer = useRef(null);
401
468
 
402
469
  const value = custom !== "" ? Number(custom) : amount;
@@ -438,6 +505,18 @@ window.__ModuleLoader__.load({
438
505
  }
439
506
  };
440
507
 
508
+ const redeem = async () => {
509
+ setRedeemMsg("");
510
+ setError("");
511
+ try {
512
+ const result = await onRedeem(code.trim());
513
+ setCode("");
514
+ setRedeemMsg(result?.profile ? `已兑换,当前余额 ${money(result.profile.balance)}` : "兑换成功");
515
+ } catch (e) {
516
+ setError(e instanceof Error ? e.message : String(e));
517
+ }
518
+ };
519
+
441
520
  return h(Card, {
442
521
  id: "sub2api-recharge",
443
522
  title: "余额与充值",
@@ -493,22 +572,29 @@ window.__ModuleLoader__.load({
493
572
  }, "关闭"),
494
573
  }, status ? status.text : "已打开支付页,等待付款…"))
495
574
  : null,
575
+ h(Divider),
576
+ h("div", { style: T.row },
577
+ h(TextField, {
578
+ placeholder: "兑换码",
579
+ value: code,
580
+ onChange: (e) => setCode(e.target.value),
581
+ onKeyDown: (e) => { if (e.key === "Enter" && code.trim()) redeem(); },
582
+ style: { flex: "1 1 200px" },
583
+ }),
584
+ h(Button, { onClick: redeem, disabled: busy || !state.loggedIn || !code.trim() }, "兑换"),
585
+ ),
586
+ redeemMsg ? h("div", { style: { ...T.hint, color: C.ok, marginTop: 8 } }, redeemMsg) : null,
496
587
  error ? h("div", { style: { marginTop: 10 } }, h(Alert, { tone: "error" }, error)) : null,
497
588
  );
498
589
  }
499
590
 
500
591
  // ------------------------------------------------------------ models
501
592
 
502
- /**
503
- * Show the capacity the host will actually use, and say where it came from:
504
- * a defaulted figure is the difference between "chat works" and "the model
505
- * cannot be switched to".
506
- */
507
593
  function contextBadge(model) {
508
594
  if (!model || !model.contextWindow) return null;
509
595
  const k = Math.round(Number(model.contextWindow) / 1000);
510
596
  const source = model.contextSource;
511
- const label = source === "default" ? "默认" : source === "known" ? "实测" : null;
597
+ const label = source === "default" ? "默认" : source === "known" ? "实测" : source === "panel" ? "手填" : null;
512
598
  return h("span", {
513
599
  style: {
514
600
  fontSize: 11.5,
@@ -518,7 +604,66 @@ window.__ModuleLoader__.load({
518
604
  }, k + "k" + (label ? " · " + label : ""));
519
605
  }
520
606
 
521
- function ModelsCard({ state, busy, onRefreshModels, onReload, onGoRecharge }) {
607
+ function ModelRow({ model, shown, checked, onToggle, onSaveContext }) {
608
+ const [editing, setEditing] = useState(false);
609
+ const [draft, setDraft] = useState("");
610
+ const current = shown.find((m) => m.id === model.id) ?? model;
611
+ const effective = current.contextWindow ? Math.round(Number(current.contextWindow) / 1000) : null;
612
+
613
+ const submit = async () => {
614
+ const value = draft.trim() === "" ? null : Number(draft) * 1000;
615
+ await onSaveContext(model.id, value);
616
+ setEditing(false);
617
+ setDraft("");
618
+ };
619
+
620
+ return h("div", {
621
+ style: {
622
+ display: "flex",
623
+ alignItems: "center",
624
+ gap: 8,
625
+ padding: "7px 12px",
626
+ borderTop: "0.5px solid " + C.line,
627
+ fontSize: 13,
628
+ },
629
+ },
630
+ h("input", { type: "checkbox", checked: checked, onChange: () => onToggle(model.id) }),
631
+ h("code", {
632
+ style: { ...T.mono, flex: "1 1 auto", minWidth: 0, overflow: "hidden", textOverflow: "ellipsis" },
633
+ }, model.id),
634
+ editing
635
+ ? h("span", { style: { display: "flex", gap: 6, alignItems: "center" } },
636
+ h("input", {
637
+ type: "number",
638
+ min: 1,
639
+ step: 1,
640
+ placeholder: effective === null ? "" : String(Math.round(effective / 1000)),
641
+ value: draft,
642
+ onChange: (e) => setDraft(e.target.value),
643
+ onKeyDown: (e) => { if (e.key === "Enter") submit(); },
644
+ style: {
645
+ width: 64, height: 26, padding: "0 6px", borderRadius: 8,
646
+ border: "0.5px solid " + C.line, background: C.base, color: C.text, fontSize: 12,
647
+ },
648
+ }),
649
+ h("span", { style: T.hint }, "k"),
650
+ h(Button, { size: "sm", variant: "primary", onClick: submit, style: { height: 26 } }, "存"),
651
+ h(Button, { size: "sm", onClick: () => { setEditing(false); setDraft(""); }, style: { height: 26 } }, "取消"),
652
+ )
653
+ : h("button", {
654
+ type: "button",
655
+ onClick: () => { setEditing(true); setDraft(effective === null ? "" : String(Math.round(effective / 1000))); },
656
+ title: "点击修改上下文窗口",
657
+ style: {
658
+ border: "none", background: "transparent", padding: "2px 4px", cursor: "pointer",
659
+ color: current.contextSource === "default" ? C.warn : C.muted, fontSize: 11.5,
660
+ },
661
+ }, contextBadge(current) ?? "设置"),
662
+ shown.some((m) => m.id === model.id && m.vision) ? h("span", { style: T.hint }, "图片") : null,
663
+ );
664
+ }
665
+
666
+ function ModelsCard({ state, busy, onRefreshModels, onReload, onGoRecharge, onSaveVisible, onSaveContext }) {
522
667
  const models = state.models || { models: [], all: [], allowed: [], fetchedAt: 0, stale: false, error: null };
523
668
  const shown = models.models || [];
524
669
  const everything = models.all || [];
@@ -527,17 +672,22 @@ window.__ModuleLoader__.load({
527
672
 
528
673
  const [open, setOpen] = useState(false);
529
674
  const [error, setError] = useState("");
530
- const [draft, setDraft] = useState(null); // null = untouched
675
+ const [draft, setDraft] = useState(null);
531
676
  const [saving, setSaving] = useState(false);
532
677
  const [saved, setSaved] = useState(false);
678
+ const [query, setQuery] = useState("");
533
679
 
534
680
  const checked = draft ?? (showingAll ? everything.map((m) => m.id) : allowed);
535
681
  const isOn = (id) => checked.includes(id);
536
682
  const dirty = draft !== null;
537
683
 
684
+ const filtered = useMemo(() => {
685
+ const q = query.trim().toLowerCase();
686
+ return q === "" ? everything : everything.filter((m) => m.id.toLowerCase().includes(q));
687
+ }, [everything, query]);
688
+
538
689
  const toggle = (id) => {
539
- const next = isOn(id) ? checked.filter((x) => x !== id) : [...checked, id];
540
- setDraft(next);
690
+ setDraft(isOn(id) ? checked.filter((x) => x !== id) : [...checked, id]);
541
691
  setSaved(false);
542
692
  };
543
693
 
@@ -556,12 +706,8 @@ window.__ModuleLoader__.load({
556
706
  setError("");
557
707
  setSaved(false);
558
708
  try {
559
- // Every model selected is the same as no restriction.
560
709
  const ids = checked.length === everything.length ? [] : checked;
561
- await call("/models/visible", { method: "POST", body: { ids } });
562
- // Saving only changes the allowlist, so re-read the panel state
563
- // instead of asking the upstream for its catalog again.
564
- await onReload();
710
+ await onSaveVisible(ids);
565
711
  setDraft(null);
566
712
  setSaved(true);
567
713
  setTimeout(() => setSaved(false), 2000);
@@ -607,49 +753,42 @@ window.__ModuleLoader__.load({
607
753
 
608
754
  open && everything.length > 0
609
755
  ? h("div", null,
756
+ everything.length > 8
757
+ ? h("div", { style: { marginBottom: 8 } },
758
+ h(TextField, {
759
+ placeholder: "搜索模型",
760
+ value: query,
761
+ onChange: (e) => setQuery(e.target.value),
762
+ }))
763
+ : null,
610
764
  h("div", {
611
765
  style: {
612
- maxHeight: 260,
766
+ maxHeight: 300,
613
767
  overflow: "auto",
614
768
  border: "0.5px solid " + C.line,
615
769
  borderRadius: 12,
616
770
  },
617
771
  },
618
- everything.map((model) =>
619
- h("label", {
620
- key: model.id,
621
- style: {
622
- display: "flex",
623
- alignItems: "center",
624
- gap: 8,
625
- padding: "7px 12px",
626
- borderTop: "0.5px solid " + C.line,
627
- cursor: "pointer",
628
- fontSize: 13,
629
- },
630
- },
631
- h("input", {
632
- type: "checkbox",
633
- checked: isOn(model.id),
634
- onChange: () => toggle(model.id),
635
- }),
636
- h("code", { style: { ...T.mono, flex: "1 1 auto", minWidth: 0, overflow: "hidden", textOverflow: "ellipsis" } }, model.id),
637
- contextBadge(shown.find((m) => m.id === model.id) ?? model),
638
- shown.some((m) => m.id === model.id && m.vision) ? h("span", { style: T.hint }, "图片") : null,
639
- ),
640
- )),
772
+ filtered.length === 0
773
+ ? h("div", { style: { ...T.hint, padding: 12 } }, "没有匹配的模型")
774
+ : filtered.map((model) =>
775
+ h(ModelRow, {
776
+ key: model.id,
777
+ model,
778
+ shown,
779
+ checked: isOn(model.id),
780
+ onToggle: toggle,
781
+ onSaveContext,
782
+ })),
783
+ ),
641
784
  h("div", { style: { ...T.row, marginTop: 10 } },
642
785
  h(Button, { size: "sm", onClick: () => { setDraft(everything.map((m) => m.id)); setSaved(false); } }, "全选"),
643
786
  h(Button, { size: "sm", onClick: () => { setDraft([]); setSaved(false); } }, "全不选"),
644
787
  h(Button, { size: "sm", onClick: () => { setDraft(null); setSaved(false); } }, "还原"),
645
788
  h("div", { style: { marginLeft: "auto", display: "flex", gap: 8, alignItems: "center" } },
646
789
  saved ? h("span", { style: { ...T.hint, color: C.ok } }, "已保存") : null,
647
- h(Button, {
648
- variant: "primary",
649
- size: "sm",
650
- onClick: save,
651
- disabled: !dirty || saving,
652
- }, saving ? "保存中…" : "保存选择"),
790
+ h(Button, { variant: "primary", size: "sm", onClick: save, disabled: !dirty || saving },
791
+ saving ? "保存中…" : "保存选择"),
653
792
  ),
654
793
  ),
655
794
  )
@@ -658,18 +797,74 @@ window.__ModuleLoader__.load({
658
797
  h("div", { style: { ...T.hint, marginTop: 10 } },
659
798
  everything.length === 0
660
799
  ? "目录来自上游 /v1/models,登录后自动同步。"
661
- : "勾选的模型会出现在对话的模型选择器里;全部勾选等于不做限制。"),
800
+ : "勾选的模型会出现在对话的模型选择器里;点容量数字可以手动改这个模型的上下文窗口。"),
801
+ );
802
+ }
803
+
804
+ // ---------------------------------------------------------- diagnose
805
+
806
+ function DiagnoseCard({ state, onDiagnose }) {
807
+ const [result, setResult] = useState(null);
808
+ const [busy, setBusy] = useState(false);
809
+ const [error, setError] = useState("");
810
+
811
+ const run = async () => {
812
+ setBusy(true);
813
+ setError("");
814
+ try {
815
+ setResult(await onDiagnose());
816
+ } catch (e) {
817
+ setError(e instanceof Error ? e.message : String(e));
818
+ } finally {
819
+ setBusy(false);
820
+ }
821
+ };
822
+
823
+ return h(Card, {
824
+ title: "诊断",
825
+ aside: result
826
+ ? h("span", {
827
+ style: { ...T.meta, marginLeft: "auto", color: result.ok ? C.ok : C.warn },
828
+ }, result.summary)
829
+ : null,
830
+ },
831
+ h("div", { style: T.row },
832
+ h(Button, { onClick: run, disabled: busy || !state.loggedIn }, busy ? "检查中…" : "运行诊断"),
833
+ ),
834
+ error ? h("div", { style: { marginTop: 10 } }, h(Alert, { tone: "error" }, error)) : null,
835
+ result
836
+ ? h("div", { style: { marginTop: 12, border: "0.5px solid " + C.line, borderRadius: 12 } },
837
+ result.checks.map((check) =>
838
+ h("div", {
839
+ key: check.id,
840
+ style: {
841
+ display: "flex",
842
+ gap: 10,
843
+ alignItems: "baseline",
844
+ padding: "7px 12px",
845
+ borderTop: "0.5px solid " + C.line,
846
+ fontSize: 12.5,
847
+ },
848
+ },
849
+ h("span", { style: { width: 6, height: 6, borderRadius: 999, flex: "0 0 6px", background: check.ok ? C.ok : C.warn, alignSelf: "center" } }),
850
+ h("span", { style: { width: 74, flex: "0 0 74px", color: C.muted } }, check.label),
851
+ h("span", { style: { flex: 1, minWidth: 0, color: check.ok ? C.text : C.warn } }, check.detail ?? (check.ok ? "正常" : "失败")),
852
+ )))
853
+ : null,
854
+ h("div", { style: { ...T.hint, marginTop: 10 } },
855
+ "诊断会依次检查登录、令牌、密钥、余额、密钥分组和模型目录,把失败的那一项直接指出来。"),
662
856
  );
663
857
  }
664
858
 
665
859
  // ------------------------------------------------------------- root
666
860
 
667
- function Section() {
861
+ function SectionInner() {
668
862
  const [state, setState] = useState(null);
669
863
  const [busy, setBusy] = useState(false);
670
864
  const [error, setError] = useState("");
671
865
  const [steps, setSteps] = useState(null);
672
866
  const [ready, setReady] = useState(undefined);
867
+ const [announcements, setAnnouncements] = useState([]);
673
868
 
674
869
  const refresh = useCallback(async () => {
675
870
  try {
@@ -685,6 +880,14 @@ window.__ModuleLoader__.load({
685
880
 
686
881
  useEffect(() => { refresh(); }, [refresh]);
687
882
 
883
+ useEffect(() => {
884
+ let cancelled = false;
885
+ call("/announcements")
886
+ .then((data) => { if (!cancelled) setAnnouncements(Array.isArray(data.items) ? data.items : []); })
887
+ .catch(() => {});
888
+ return () => { cancelled = true; };
889
+ }, [state && state.loggedIn]);
890
+
688
891
  const wrap = useCallback(async (fn) => {
689
892
  setBusy(true);
690
893
  setError("");
@@ -724,6 +927,18 @@ window.__ModuleLoader__.load({
724
927
  return result;
725
928
  }), [wrap, refresh]);
726
929
 
930
+ const onSaveVisible = useCallback((ids) => wrap(async () => {
931
+ await call("/models/visible", { method: "POST", body: { ids } });
932
+ await refresh();
933
+ }), [wrap, refresh]);
934
+
935
+ const onSaveContext = useCallback(async (id, contextWindow) => {
936
+ await call("/models/context", { method: "POST", body: { id, contextWindow } });
937
+ await refresh();
938
+ }, [refresh]);
939
+
940
+ const onDiagnose = useCallback(() => call("/diagnose"), []);
941
+
727
942
  const onRecharge = useCallback(async (amountYuan) => {
728
943
  try {
729
944
  return await call("/recharge", { method: "POST", body: { amountYuan } });
@@ -734,6 +949,7 @@ window.__ModuleLoader__.load({
734
949
  }, []);
735
950
 
736
951
  const onPoll = useCallback((orderNo) => call("/recharge/status", { method: "POST", body: { orderNo } }), []);
952
+ const onRedeem = useCallback((code) => call("/redeem", { method: "POST", body: { code } }), []);
737
953
  const onRefresh = useCallback(() => refresh(), [refresh]);
738
954
 
739
955
  if (state === null) {
@@ -741,15 +957,24 @@ window.__ModuleLoader__.load({
741
957
  error ? h(Alert, { tone: "error" }, error) : "加载中…");
742
958
  }
743
959
 
960
+ const unread = announcements.filter((item) => !item.is_read);
961
+
744
962
  return h("div", { style: { padding: "4px 0 18px" } },
963
+ unread.length > 0
964
+ ? h("div", { style: { marginBottom: 12 } },
965
+ h(Alert, { tone: "info", title: unread[0].title ?? "站点公告" },
966
+ asText(unread[0].content ?? unread[0].body ?? "").slice(0, 200)))
967
+ : null,
745
968
  h(AccountCard, { state, busy, steps, ready, onAuth, onLogout }),
746
969
  h(KeyCard, { state, busy, onCreate: onCreateKey, onReveal }),
747
- h(RechargeCard, { state, busy, onRecharge, onPoll, onRefresh }),
970
+ h(RechargeCard, { state, busy, onRecharge, onPoll, onRefresh, onRedeem }),
748
971
  h(ModelsCard, {
749
972
  state,
750
973
  busy,
751
974
  onRefreshModels,
752
975
  onReload: refresh,
976
+ onSaveVisible,
977
+ onSaveContext,
753
978
  onGoRecharge: () => {
754
979
  const target = document.getElementById("sub2api-recharge");
755
980
  if (target && typeof target.scrollIntoView === "function") {
@@ -757,10 +982,15 @@ window.__ModuleLoader__.load({
757
982
  }
758
983
  },
759
984
  }),
985
+ h(DiagnoseCard, { state, onDiagnose }),
760
986
  error ? h(Alert, { tone: "error" }, error) : null,
761
987
  );
762
988
  }
763
989
 
990
+ function Section() {
991
+ return h(Boundary, null, h(SectionInner, null));
992
+ }
993
+
764
994
  // ---------------------------------------------------------- sidebar
765
995
 
766
996
  function SidebarCard() {