dsh-sub2api 0.1.7 → 0.2.1
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/CHANGELOG.md +22 -0
- package/README.md +143 -100
- package/lib/adapter.js +33 -1
- package/lib/client.js +324 -90
- package/lib/commands.js +185 -0
- package/lib/config.js +4 -1
- package/lib/constants.js +31 -0
- package/lib/index.js +20 -9
- package/lib/panel-store.js +151 -0
- package/lib/routes.js +94 -2
- package/lib/schema.js +2 -0
- package/lib/sub2api/catalog.js +63 -16
- package/package.json +5 -3
- package/lib/selection.js +0 -66
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
|
-
|
|
72
|
+
head: {
|
|
73
73
|
display: "flex",
|
|
74
74
|
alignItems: "center",
|
|
75
75
|
gap: 10,
|
|
76
|
-
padding: "
|
|
76
|
+
padding: "13px 18px 11px",
|
|
77
77
|
borderBottom: "0.5px solid " + C.line,
|
|
78
78
|
},
|
|
79
|
-
|
|
80
|
-
|
|
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: "
|
|
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,
|
|
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.
|
|
120
|
-
h("h3", { style: T.
|
|
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:
|
|
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
|
|
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
|
-
|
|
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
|
|
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(
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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
|
-
|
|
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,70 @@ window.__ModuleLoader__.load({
|
|
|
518
604
|
}, k + "k" + (label ? " · " + label : ""));
|
|
519
605
|
}
|
|
520
606
|
|
|
521
|
-
function
|
|
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
|
+
Array.isArray(current.reasoningEfforts)
|
|
663
|
+
? h("span", { style: { ...T.hint, whiteSpace: "nowrap" } },
|
|
664
|
+
"思考 " + current.reasoningEfforts.length + " 档")
|
|
665
|
+
: null,
|
|
666
|
+
shown.some((m) => m.id === model.id && m.vision) ? h("span", { style: T.hint }, "图片") : null,
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function ModelsCard({ state, busy, onRefreshModels, onReload, onGoRecharge, onSaveVisible, onSaveContext }) {
|
|
522
671
|
const models = state.models || { models: [], all: [], allowed: [], fetchedAt: 0, stale: false, error: null };
|
|
523
672
|
const shown = models.models || [];
|
|
524
673
|
const everything = models.all || [];
|
|
@@ -527,17 +676,22 @@ window.__ModuleLoader__.load({
|
|
|
527
676
|
|
|
528
677
|
const [open, setOpen] = useState(false);
|
|
529
678
|
const [error, setError] = useState("");
|
|
530
|
-
const [draft, setDraft] = useState(null);
|
|
679
|
+
const [draft, setDraft] = useState(null);
|
|
531
680
|
const [saving, setSaving] = useState(false);
|
|
532
681
|
const [saved, setSaved] = useState(false);
|
|
682
|
+
const [query, setQuery] = useState("");
|
|
533
683
|
|
|
534
684
|
const checked = draft ?? (showingAll ? everything.map((m) => m.id) : allowed);
|
|
535
685
|
const isOn = (id) => checked.includes(id);
|
|
536
686
|
const dirty = draft !== null;
|
|
537
687
|
|
|
688
|
+
const filtered = useMemo(() => {
|
|
689
|
+
const q = query.trim().toLowerCase();
|
|
690
|
+
return q === "" ? everything : everything.filter((m) => m.id.toLowerCase().includes(q));
|
|
691
|
+
}, [everything, query]);
|
|
692
|
+
|
|
538
693
|
const toggle = (id) => {
|
|
539
|
-
|
|
540
|
-
setDraft(next);
|
|
694
|
+
setDraft(isOn(id) ? checked.filter((x) => x !== id) : [...checked, id]);
|
|
541
695
|
setSaved(false);
|
|
542
696
|
};
|
|
543
697
|
|
|
@@ -556,12 +710,8 @@ window.__ModuleLoader__.load({
|
|
|
556
710
|
setError("");
|
|
557
711
|
setSaved(false);
|
|
558
712
|
try {
|
|
559
|
-
// Every model selected is the same as no restriction.
|
|
560
713
|
const ids = checked.length === everything.length ? [] : checked;
|
|
561
|
-
await
|
|
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();
|
|
714
|
+
await onSaveVisible(ids);
|
|
565
715
|
setDraft(null);
|
|
566
716
|
setSaved(true);
|
|
567
717
|
setTimeout(() => setSaved(false), 2000);
|
|
@@ -607,49 +757,42 @@ window.__ModuleLoader__.load({
|
|
|
607
757
|
|
|
608
758
|
open && everything.length > 0
|
|
609
759
|
? h("div", null,
|
|
760
|
+
everything.length > 8
|
|
761
|
+
? h("div", { style: { marginBottom: 8 } },
|
|
762
|
+
h(TextField, {
|
|
763
|
+
placeholder: "搜索模型",
|
|
764
|
+
value: query,
|
|
765
|
+
onChange: (e) => setQuery(e.target.value),
|
|
766
|
+
}))
|
|
767
|
+
: null,
|
|
610
768
|
h("div", {
|
|
611
769
|
style: {
|
|
612
|
-
maxHeight:
|
|
770
|
+
maxHeight: 300,
|
|
613
771
|
overflow: "auto",
|
|
614
772
|
border: "0.5px solid " + C.line,
|
|
615
773
|
borderRadius: 12,
|
|
616
774
|
},
|
|
617
775
|
},
|
|
618
|
-
|
|
619
|
-
h("
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
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
|
-
)),
|
|
776
|
+
filtered.length === 0
|
|
777
|
+
? h("div", { style: { ...T.hint, padding: 12 } }, "没有匹配的模型")
|
|
778
|
+
: filtered.map((model) =>
|
|
779
|
+
h(ModelRow, {
|
|
780
|
+
key: model.id,
|
|
781
|
+
model,
|
|
782
|
+
shown,
|
|
783
|
+
checked: isOn(model.id),
|
|
784
|
+
onToggle: toggle,
|
|
785
|
+
onSaveContext,
|
|
786
|
+
})),
|
|
787
|
+
),
|
|
641
788
|
h("div", { style: { ...T.row, marginTop: 10 } },
|
|
642
789
|
h(Button, { size: "sm", onClick: () => { setDraft(everything.map((m) => m.id)); setSaved(false); } }, "全选"),
|
|
643
790
|
h(Button, { size: "sm", onClick: () => { setDraft([]); setSaved(false); } }, "全不选"),
|
|
644
791
|
h(Button, { size: "sm", onClick: () => { setDraft(null); setSaved(false); } }, "还原"),
|
|
645
792
|
h("div", { style: { marginLeft: "auto", display: "flex", gap: 8, alignItems: "center" } },
|
|
646
793
|
saved ? h("span", { style: { ...T.hint, color: C.ok } }, "已保存") : null,
|
|
647
|
-
h(Button, {
|
|
648
|
-
|
|
649
|
-
size: "sm",
|
|
650
|
-
onClick: save,
|
|
651
|
-
disabled: !dirty || saving,
|
|
652
|
-
}, saving ? "保存中…" : "保存选择"),
|
|
794
|
+
h(Button, { variant: "primary", size: "sm", onClick: save, disabled: !dirty || saving },
|
|
795
|
+
saving ? "保存中…" : "保存选择"),
|
|
653
796
|
),
|
|
654
797
|
),
|
|
655
798
|
)
|
|
@@ -658,18 +801,74 @@ window.__ModuleLoader__.load({
|
|
|
658
801
|
h("div", { style: { ...T.hint, marginTop: 10 } },
|
|
659
802
|
everything.length === 0
|
|
660
803
|
? "目录来自上游 /v1/models,登录后自动同步。"
|
|
661
|
-
: "
|
|
804
|
+
: "勾选的模型会出现在对话的模型选择器里;点容量数字可以手动改这个模型的上下文窗口。"),
|
|
805
|
+
);
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// ---------------------------------------------------------- diagnose
|
|
809
|
+
|
|
810
|
+
function DiagnoseCard({ state, onDiagnose }) {
|
|
811
|
+
const [result, setResult] = useState(null);
|
|
812
|
+
const [busy, setBusy] = useState(false);
|
|
813
|
+
const [error, setError] = useState("");
|
|
814
|
+
|
|
815
|
+
const run = async () => {
|
|
816
|
+
setBusy(true);
|
|
817
|
+
setError("");
|
|
818
|
+
try {
|
|
819
|
+
setResult(await onDiagnose());
|
|
820
|
+
} catch (e) {
|
|
821
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
822
|
+
} finally {
|
|
823
|
+
setBusy(false);
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
return h(Card, {
|
|
828
|
+
title: "诊断",
|
|
829
|
+
aside: result
|
|
830
|
+
? h("span", {
|
|
831
|
+
style: { ...T.meta, marginLeft: "auto", color: result.ok ? C.ok : C.warn },
|
|
832
|
+
}, result.summary)
|
|
833
|
+
: null,
|
|
834
|
+
},
|
|
835
|
+
h("div", { style: T.row },
|
|
836
|
+
h(Button, { onClick: run, disabled: busy || !state.loggedIn }, busy ? "检查中…" : "运行诊断"),
|
|
837
|
+
),
|
|
838
|
+
error ? h("div", { style: { marginTop: 10 } }, h(Alert, { tone: "error" }, error)) : null,
|
|
839
|
+
result
|
|
840
|
+
? h("div", { style: { marginTop: 12, border: "0.5px solid " + C.line, borderRadius: 12 } },
|
|
841
|
+
result.checks.map((check) =>
|
|
842
|
+
h("div", {
|
|
843
|
+
key: check.id,
|
|
844
|
+
style: {
|
|
845
|
+
display: "flex",
|
|
846
|
+
gap: 10,
|
|
847
|
+
alignItems: "baseline",
|
|
848
|
+
padding: "7px 12px",
|
|
849
|
+
borderTop: "0.5px solid " + C.line,
|
|
850
|
+
fontSize: 12.5,
|
|
851
|
+
},
|
|
852
|
+
},
|
|
853
|
+
h("span", { style: { width: 6, height: 6, borderRadius: 999, flex: "0 0 6px", background: check.ok ? C.ok : C.warn, alignSelf: "center" } }),
|
|
854
|
+
h("span", { style: { width: 74, flex: "0 0 74px", color: C.muted } }, check.label),
|
|
855
|
+
h("span", { style: { flex: 1, minWidth: 0, color: check.ok ? C.text : C.warn } }, check.detail ?? (check.ok ? "正常" : "失败")),
|
|
856
|
+
)))
|
|
857
|
+
: null,
|
|
858
|
+
h("div", { style: { ...T.hint, marginTop: 10 } },
|
|
859
|
+
"诊断会依次检查登录、令牌、密钥、余额、密钥分组和模型目录,把失败的那一项直接指出来。"),
|
|
662
860
|
);
|
|
663
861
|
}
|
|
664
862
|
|
|
665
863
|
// ------------------------------------------------------------- root
|
|
666
864
|
|
|
667
|
-
function
|
|
865
|
+
function SectionInner() {
|
|
668
866
|
const [state, setState] = useState(null);
|
|
669
867
|
const [busy, setBusy] = useState(false);
|
|
670
868
|
const [error, setError] = useState("");
|
|
671
869
|
const [steps, setSteps] = useState(null);
|
|
672
870
|
const [ready, setReady] = useState(undefined);
|
|
871
|
+
const [announcements, setAnnouncements] = useState([]);
|
|
673
872
|
|
|
674
873
|
const refresh = useCallback(async () => {
|
|
675
874
|
try {
|
|
@@ -685,6 +884,14 @@ window.__ModuleLoader__.load({
|
|
|
685
884
|
|
|
686
885
|
useEffect(() => { refresh(); }, [refresh]);
|
|
687
886
|
|
|
887
|
+
useEffect(() => {
|
|
888
|
+
let cancelled = false;
|
|
889
|
+
call("/announcements")
|
|
890
|
+
.then((data) => { if (!cancelled) setAnnouncements(Array.isArray(data.items) ? data.items : []); })
|
|
891
|
+
.catch(() => {});
|
|
892
|
+
return () => { cancelled = true; };
|
|
893
|
+
}, [state && state.loggedIn]);
|
|
894
|
+
|
|
688
895
|
const wrap = useCallback(async (fn) => {
|
|
689
896
|
setBusy(true);
|
|
690
897
|
setError("");
|
|
@@ -724,6 +931,18 @@ window.__ModuleLoader__.load({
|
|
|
724
931
|
return result;
|
|
725
932
|
}), [wrap, refresh]);
|
|
726
933
|
|
|
934
|
+
const onSaveVisible = useCallback((ids) => wrap(async () => {
|
|
935
|
+
await call("/models/visible", { method: "POST", body: { ids } });
|
|
936
|
+
await refresh();
|
|
937
|
+
}), [wrap, refresh]);
|
|
938
|
+
|
|
939
|
+
const onSaveContext = useCallback(async (id, contextWindow) => {
|
|
940
|
+
await call("/models/context", { method: "POST", body: { id, contextWindow } });
|
|
941
|
+
await refresh();
|
|
942
|
+
}, [refresh]);
|
|
943
|
+
|
|
944
|
+
const onDiagnose = useCallback(() => call("/diagnose"), []);
|
|
945
|
+
|
|
727
946
|
const onRecharge = useCallback(async (amountYuan) => {
|
|
728
947
|
try {
|
|
729
948
|
return await call("/recharge", { method: "POST", body: { amountYuan } });
|
|
@@ -734,6 +953,7 @@ window.__ModuleLoader__.load({
|
|
|
734
953
|
}, []);
|
|
735
954
|
|
|
736
955
|
const onPoll = useCallback((orderNo) => call("/recharge/status", { method: "POST", body: { orderNo } }), []);
|
|
956
|
+
const onRedeem = useCallback((code) => call("/redeem", { method: "POST", body: { code } }), []);
|
|
737
957
|
const onRefresh = useCallback(() => refresh(), [refresh]);
|
|
738
958
|
|
|
739
959
|
if (state === null) {
|
|
@@ -741,15 +961,24 @@ window.__ModuleLoader__.load({
|
|
|
741
961
|
error ? h(Alert, { tone: "error" }, error) : "加载中…");
|
|
742
962
|
}
|
|
743
963
|
|
|
964
|
+
const unread = announcements.filter((item) => !item.is_read);
|
|
965
|
+
|
|
744
966
|
return h("div", { style: { padding: "4px 0 18px" } },
|
|
967
|
+
unread.length > 0
|
|
968
|
+
? h("div", { style: { marginBottom: 12 } },
|
|
969
|
+
h(Alert, { tone: "info", title: unread[0].title ?? "站点公告" },
|
|
970
|
+
asText(unread[0].content ?? unread[0].body ?? "").slice(0, 200)))
|
|
971
|
+
: null,
|
|
745
972
|
h(AccountCard, { state, busy, steps, ready, onAuth, onLogout }),
|
|
746
973
|
h(KeyCard, { state, busy, onCreate: onCreateKey, onReveal }),
|
|
747
|
-
h(RechargeCard, { state, busy, onRecharge, onPoll, onRefresh }),
|
|
974
|
+
h(RechargeCard, { state, busy, onRecharge, onPoll, onRefresh, onRedeem }),
|
|
748
975
|
h(ModelsCard, {
|
|
749
976
|
state,
|
|
750
977
|
busy,
|
|
751
978
|
onRefreshModels,
|
|
752
979
|
onReload: refresh,
|
|
980
|
+
onSaveVisible,
|
|
981
|
+
onSaveContext,
|
|
753
982
|
onGoRecharge: () => {
|
|
754
983
|
const target = document.getElementById("sub2api-recharge");
|
|
755
984
|
if (target && typeof target.scrollIntoView === "function") {
|
|
@@ -757,10 +986,15 @@ window.__ModuleLoader__.load({
|
|
|
757
986
|
}
|
|
758
987
|
},
|
|
759
988
|
}),
|
|
989
|
+
h(DiagnoseCard, { state, onDiagnose }),
|
|
760
990
|
error ? h(Alert, { tone: "error" }, error) : null,
|
|
761
991
|
);
|
|
762
992
|
}
|
|
763
993
|
|
|
994
|
+
function Section() {
|
|
995
|
+
return h(Boundary, null, h(SectionInner, null));
|
|
996
|
+
}
|
|
997
|
+
|
|
764
998
|
// ---------------------------------------------------------- sidebar
|
|
765
999
|
|
|
766
1000
|
function SidebarCard() {
|