dsh-sub2api 0.3.0 → 0.4.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 +23 -0
- package/README.md +17 -5
- package/lib/adapter.js +4 -4
- package/lib/client.js +545 -348
- package/lib/commands.js +6 -6
- package/lib/constants.js +1 -1
- package/lib/index.js +2 -2
- package/lib/pay.js +5 -1
- package/lib/sub2api/auth.js +1 -1
- package/lib/sub2api/session.js +1 -1
- package/lib/tools.js +4 -4
- package/package.json +3 -3
package/lib/client.js
CHANGED
|
@@ -357,7 +357,7 @@ window.__ModuleLoader__.load({
|
|
|
357
357
|
}
|
|
358
358
|
render() {
|
|
359
359
|
if (this.state.error) {
|
|
360
|
-
return h(Alert, { tone: "error", title: "
|
|
360
|
+
return h(Alert, { tone: "error", title: "Psapi 面板渲染失败" },
|
|
361
361
|
asText(this.state.error) || "未知错误",
|
|
362
362
|
h("div", { style: { ...T.hint, marginTop: 6 } }, "把这段信息发给我即可定位。"));
|
|
363
363
|
}
|
|
@@ -399,7 +399,189 @@ window.__ModuleLoader__.load({
|
|
|
399
399
|
});
|
|
400
400
|
}
|
|
401
401
|
|
|
402
|
-
|
|
402
|
+
// -------------------------------------------------------------- toast
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Short confirmations.
|
|
406
|
+
*
|
|
407
|
+
* Actions used to change a number somewhere on the page with no
|
|
408
|
+
* acknowledgement, which is how a working plugin still feels unresponsive.
|
|
409
|
+
*/
|
|
410
|
+
const toastStore = {
|
|
411
|
+
items: [],
|
|
412
|
+
nextId: 1,
|
|
413
|
+
listeners: new Set(),
|
|
414
|
+
push(text, tone = "ok") {
|
|
415
|
+
const item = { id: this.nextId++, text, tone };
|
|
416
|
+
this.items = [...this.items, item];
|
|
417
|
+
this.emit();
|
|
418
|
+
setTimeout(() => this.dismiss(item.id), 2600);
|
|
419
|
+
},
|
|
420
|
+
dismiss(id) {
|
|
421
|
+
this.items = this.items.filter((item) => item.id !== id);
|
|
422
|
+
this.emit();
|
|
423
|
+
},
|
|
424
|
+
subscribe(fn) {
|
|
425
|
+
this.listeners.add(fn);
|
|
426
|
+
return () => { this.listeners.delete(fn); };
|
|
427
|
+
},
|
|
428
|
+
emit() {
|
|
429
|
+
for (const fn of [...this.listeners]) {
|
|
430
|
+
try { fn(); } catch { /* ignore */ }
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
function useToasts() {
|
|
436
|
+
const [items, setItems] = useState(toastStore.items);
|
|
437
|
+
useEffect(() => toastStore.subscribe(() => setItems(toastStore.items)), []);
|
|
438
|
+
return items;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function Toasts({ items }) {
|
|
442
|
+
if (items.length === 0) return null;
|
|
443
|
+
return h("div", {
|
|
444
|
+
style: {
|
|
445
|
+
position: "sticky",
|
|
446
|
+
top: 0,
|
|
447
|
+
zIndex: 5,
|
|
448
|
+
display: "flex",
|
|
449
|
+
flexDirection: "column",
|
|
450
|
+
gap: 6,
|
|
451
|
+
alignItems: "flex-end",
|
|
452
|
+
marginBottom: 8,
|
|
453
|
+
pointerEvents: "none",
|
|
454
|
+
},
|
|
455
|
+
},
|
|
456
|
+
items.map((item) =>
|
|
457
|
+
h("div", {
|
|
458
|
+
key: item.id,
|
|
459
|
+
style: {
|
|
460
|
+
pointerEvents: "auto",
|
|
461
|
+
padding: "7px 12px",
|
|
462
|
+
borderRadius: 999,
|
|
463
|
+
fontSize: 12.5,
|
|
464
|
+
background: item.tone === "error" ? "color-mix(in srgb, " + C.bad + " 12%, " + C.surface + ")" : "color-mix(in srgb, " + C.ok + " 12%, " + C.surface + ")",
|
|
465
|
+
color: item.tone === "error" ? C.bad : C.ok,
|
|
466
|
+
border: "0.5px solid " + C.line,
|
|
467
|
+
boxShadow: "0 6px 20px rgba(0,0,0,0.08)",
|
|
468
|
+
},
|
|
469
|
+
}, item.text)),
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// ----------------------------------------------------------- section
|
|
474
|
+
|
|
475
|
+
/** A titled block that can be folded away, so the page stays scannable. */
|
|
476
|
+
function Collapsible({ title, meta, open, onToggle, children, id }) {
|
|
477
|
+
return h("section", { id, style: T.card },
|
|
478
|
+
h("button", {
|
|
479
|
+
type: "button",
|
|
480
|
+
onClick: onToggle,
|
|
481
|
+
"aria-expanded": open,
|
|
482
|
+
style: {
|
|
483
|
+
display: "flex",
|
|
484
|
+
alignItems: "center",
|
|
485
|
+
gap: 10,
|
|
486
|
+
width: "100%",
|
|
487
|
+
padding: "13px 18px",
|
|
488
|
+
border: "none",
|
|
489
|
+
borderBottom: open ? "0.5px solid " + C.line : "none",
|
|
490
|
+
background: "transparent",
|
|
491
|
+
cursor: "pointer",
|
|
492
|
+
textAlign: "left",
|
|
493
|
+
color: "inherit",
|
|
494
|
+
},
|
|
495
|
+
},
|
|
496
|
+
h("span", {
|
|
497
|
+
style: {
|
|
498
|
+
display: "inline-block",
|
|
499
|
+
width: 0,
|
|
500
|
+
height: 0,
|
|
501
|
+
borderLeft: "5px solid " + C.muted,
|
|
502
|
+
borderTop: "4px solid transparent",
|
|
503
|
+
borderBottom: "4px solid transparent",
|
|
504
|
+
transform: open ? "rotate(90deg)" : "none",
|
|
505
|
+
transition: "transform 120ms ease",
|
|
506
|
+
flex: "0 0 auto",
|
|
507
|
+
},
|
|
508
|
+
}),
|
|
509
|
+
h("h3", { style: T.title }, title),
|
|
510
|
+
meta ? h("span", { style: T.meta }, meta) : null,
|
|
511
|
+
),
|
|
512
|
+
open ? h("div", { style: T.body }, children) : null,
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// ------------------------------------------------------------ header
|
|
517
|
+
|
|
518
|
+
/** The one line that answers "is this thing connected and funded". */
|
|
519
|
+
function StatusHeader({ state, onOpenRecharge, onRefresh, refreshing }) {
|
|
520
|
+
const profile = state.profile || {};
|
|
521
|
+
const balance = Number(profile.balance);
|
|
522
|
+
const known = Number.isFinite(balance);
|
|
523
|
+
const low = known && balance <= 0;
|
|
524
|
+
const models = state.models?.models?.length ?? 0;
|
|
525
|
+
const pending = state.models?.needsRecharge === true;
|
|
526
|
+
const dot = !state.loggedIn ? C.muted : low ? C.bad : known && balance < 5 ? C.warn : C.ok;
|
|
527
|
+
|
|
528
|
+
return h("div", {
|
|
529
|
+
style: {
|
|
530
|
+
...T.card,
|
|
531
|
+
padding: "16px 18px",
|
|
532
|
+
display: "flex",
|
|
533
|
+
alignItems: "center",
|
|
534
|
+
gap: 14,
|
|
535
|
+
flexWrap: "wrap",
|
|
536
|
+
},
|
|
537
|
+
},
|
|
538
|
+
h("span", {
|
|
539
|
+
style: { width: 8, height: 8, borderRadius: 999, background: dot, flex: "0 0 8px" },
|
|
540
|
+
}),
|
|
541
|
+
h("div", { style: { display: "flex", flexDirection: "column", gap: 1, minWidth: 0 } },
|
|
542
|
+
h("span", { style: { fontSize: 13.5, fontWeight: 600, color: C.text, overflow: "hidden", textOverflow: "ellipsis" } },
|
|
543
|
+
state.loggedIn ? (state.email || "已连接") : "未连接 Psapi"),
|
|
544
|
+
h("span", { style: T.hint },
|
|
545
|
+
state.loggedIn
|
|
546
|
+
? models > 0
|
|
547
|
+
? `${models} 个模型可用`
|
|
548
|
+
: pending
|
|
549
|
+
? "余额为 0,充值后才能同步模型"
|
|
550
|
+
: "尚未同步模型"
|
|
551
|
+
: "在下方填写邮箱和密码"),
|
|
552
|
+
),
|
|
553
|
+
h("div", {
|
|
554
|
+
style: { marginLeft: "auto", display: "flex", alignItems: "center", gap: 10, flexWrap: "wrap" },
|
|
555
|
+
},
|
|
556
|
+
state.loggedIn
|
|
557
|
+
? h("span", {
|
|
558
|
+
style: {
|
|
559
|
+
fontSize: 19,
|
|
560
|
+
fontWeight: 700,
|
|
561
|
+
fontVariantNumeric: "tabular-nums",
|
|
562
|
+
color: low ? C.bad : C.text,
|
|
563
|
+
},
|
|
564
|
+
}, known ? money(balance) : "—")
|
|
565
|
+
: null,
|
|
566
|
+
state.loggedIn
|
|
567
|
+
? h(Button, {
|
|
568
|
+
size: "sm",
|
|
569
|
+
onClick: onRefresh,
|
|
570
|
+
disabled: refreshing,
|
|
571
|
+
title: "重新读取余额与模型",
|
|
572
|
+
}, refreshing ? "刷新中…" : "刷新")
|
|
573
|
+
: null,
|
|
574
|
+
state.loggedIn
|
|
575
|
+
? h(Button, { size: "sm", variant: low ? "primary" : "default", onClick: onOpenRecharge },
|
|
576
|
+
low ? "去充值" : "充值")
|
|
577
|
+
: null,
|
|
578
|
+
),
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// ----------------------------------------------------------- account
|
|
583
|
+
|
|
584
|
+
function LoginCard({ busy, onAuth }) {
|
|
403
585
|
const [email, setEmail] = useState("");
|
|
404
586
|
const [password, setPassword] = useState("");
|
|
405
587
|
const [promo, setPromo] = useState("");
|
|
@@ -417,54 +599,7 @@ window.__ModuleLoader__.load({
|
|
|
417
599
|
}
|
|
418
600
|
}, [email, password, promo, onAuth]);
|
|
419
601
|
|
|
420
|
-
|
|
421
|
-
const user = state.user || {};
|
|
422
|
-
const profile = state.profile || {};
|
|
423
|
-
const usage = state.usage || {};
|
|
424
|
-
const subs = Array.isArray(state.subscriptions?.subscriptions) ? state.subscriptions.subscriptions : [];
|
|
425
|
-
|
|
426
|
-
const cost = (key) => {
|
|
427
|
-
const value = Number(usage[key]);
|
|
428
|
-
return Number.isFinite(value) ? money(value) : null;
|
|
429
|
-
};
|
|
430
|
-
const today = cost("today_cost") ?? cost("todayCost") ?? cost("cost_today");
|
|
431
|
-
const month = cost("total_cost") ?? cost("month_cost") ?? cost("cost_month");
|
|
432
|
-
const requests = usage.total_requests ?? usage.request_count;
|
|
433
|
-
|
|
434
|
-
return h(Card, {
|
|
435
|
-
title: "账户",
|
|
436
|
-
meta: state.apiBase,
|
|
437
|
-
aside: h(Button, { onClick: onLogout, disabled: busy, size: "sm", style: { marginLeft: "auto" } }, "退出登录"),
|
|
438
|
-
},
|
|
439
|
-
h(Row, { label: "账号" }, state.email || user.email || "已登录"),
|
|
440
|
-
h(Row, { label: "余额" },
|
|
441
|
-
h("span", { style: { fontSize: 15, fontWeight: 600, color: Number(profile.balance) > 0 ? C.ok : C.warn } },
|
|
442
|
-
money(profile.balance)),
|
|
443
|
-
Number(profile.concurrency) > 0
|
|
444
|
-
? h("span", { style: { ...T.hint, marginLeft: 8 } }, `并发 ${profile.concurrency}`)
|
|
445
|
-
: null),
|
|
446
|
-
today || month || Number.isFinite(Number(requests))
|
|
447
|
-
? h(Row, { label: "用量" },
|
|
448
|
-
[today ? `今日 ${today}` : null, month ? `累计 ${month}` : null,
|
|
449
|
-
Number.isFinite(Number(requests)) ? `${requests} 次请求` : null]
|
|
450
|
-
.filter(Boolean).join(" · "))
|
|
451
|
-
: null,
|
|
452
|
-
subs.length > 0
|
|
453
|
-
? h("div", { style: { marginTop: 10 } },
|
|
454
|
-
subs.map((entry, index) =>
|
|
455
|
-
h("div", { key: index, style: { marginBottom: 8 } },
|
|
456
|
-
h("div", { style: { ...T.small, fontWeight: 500 } },
|
|
457
|
-
`${entry.group_name || "订阅"}${entry.days_remaining !== null && entry.days_remaining !== undefined ? ` · 剩余 ${entry.days_remaining} 天` : ""}`),
|
|
458
|
-
h(Bar, { label: "日", value: entry.daily_progress }),
|
|
459
|
-
h(Bar, { label: "周", value: entry.weekly_progress }),
|
|
460
|
-
h(Bar, { label: "月", value: entry.monthly_progress }),
|
|
461
|
-
)))
|
|
462
|
-
: null,
|
|
463
|
-
h(ProvisionReport, { steps, ready }),
|
|
464
|
-
);
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
return h(Card, { title: "登录或注册", meta: state.apiBase },
|
|
602
|
+
return h(Card, { title: "登录或注册" },
|
|
468
603
|
h("div", { style: { display: "flex", flexDirection: "column", gap: 12 } },
|
|
469
604
|
h(TextField, {
|
|
470
605
|
label: "邮箱",
|
|
@@ -499,38 +634,74 @@ window.__ModuleLoader__.load({
|
|
|
499
634
|
error ? h(Alert, { tone: "error" }, error) : null,
|
|
500
635
|
h(Divider),
|
|
501
636
|
h("div", { style: { display: "flex", alignItems: "center", gap: 8, flexWrap: "wrap" } },
|
|
502
|
-
h("span", { style: T.hint },
|
|
503
|
-
"没有账号会自动注册,注册后立即创建密钥 ", h("code", { style: T.mono }, state.keyName || "dsh"), " 并同步模型。"),
|
|
637
|
+
h("span", { style: T.hint }, "没有账号会自动注册,并创建密钥 dsh。"),
|
|
504
638
|
h(Button, {
|
|
505
639
|
variant: "ghost",
|
|
506
640
|
size: "sm",
|
|
507
641
|
onClick: () => setShowPromo((v) => !v),
|
|
508
642
|
style: { marginLeft: "auto" },
|
|
509
|
-
}, showPromo ? "取消优惠码" : "
|
|
643
|
+
}, showPromo ? "取消优惠码" : "有优惠码"),
|
|
510
644
|
),
|
|
511
645
|
),
|
|
512
646
|
);
|
|
513
647
|
}
|
|
514
648
|
|
|
515
|
-
|
|
649
|
+
function AccountDetail({ state, steps, ready, pending, onLogout }) {
|
|
650
|
+
const profile = state.profile || {};
|
|
651
|
+
const usage = state.usage || {};
|
|
652
|
+
const subs = Array.isArray(state.subscriptions?.subscriptions) ? state.subscriptions.subscriptions : [];
|
|
653
|
+
const cost = (key) => {
|
|
654
|
+
const value = Number(usage[key]);
|
|
655
|
+
return Number.isFinite(value) ? money(value) : null;
|
|
656
|
+
};
|
|
657
|
+
const today = cost("today_cost") ?? cost("todayCost") ?? cost("cost_today");
|
|
658
|
+
const total = cost("total_cost") ?? cost("month_cost") ?? cost("cost_month");
|
|
659
|
+
const requests = usage.total_requests ?? usage.request_count;
|
|
660
|
+
|
|
661
|
+
return h("div", null,
|
|
662
|
+
h(Row, { label: "账号" }, state.email || "已登录"),
|
|
663
|
+
h(Row, { label: "余额" }, money(profile.balance)),
|
|
664
|
+
|
|
665
|
+
today || total || Number.isFinite(Number(requests))
|
|
666
|
+
? h(Row, { label: "用量" },
|
|
667
|
+
[today ? `今日 ${today}` : null, total ? `累计 ${total}` : null,
|
|
668
|
+
Number.isFinite(Number(requests)) ? `${requests} 次` : null]
|
|
669
|
+
.filter(Boolean).join(" · "))
|
|
670
|
+
: null,
|
|
671
|
+
subs.length > 0
|
|
672
|
+
? h("div", { style: { marginTop: 10 } },
|
|
673
|
+
subs.map((entry, index) =>
|
|
674
|
+
h("div", { key: index, style: { marginBottom: 8 } },
|
|
675
|
+
h("div", { style: { ...T.small, fontWeight: 500 } },
|
|
676
|
+
`${entry.group_name || "订阅"}${entry.days_remaining !== null && entry.days_remaining !== undefined ? ` · 剩余 ${entry.days_remaining} 天` : ""}`),
|
|
677
|
+
h(Bar, { label: "日", value: entry.daily_progress }),
|
|
678
|
+
h(Bar, { label: "周", value: entry.weekly_progress }),
|
|
679
|
+
h(Bar, { label: "月", value: entry.monthly_progress }),
|
|
680
|
+
)))
|
|
681
|
+
: null,
|
|
682
|
+
h(ProvisionReport, { steps, ready }),
|
|
683
|
+
h("div", { style: { marginTop: 12 } },
|
|
684
|
+
h(Button, { onClick: onLogout, disabled: pending.logout }, pending.logout ? "退出中…" : "退出登录")),
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// --------------------------------------------------------------- key
|
|
516
689
|
|
|
517
|
-
function
|
|
690
|
+
function KeyDetail({ state, pending, onCreate, onReveal }) {
|
|
518
691
|
const [revealed, setRevealed] = useState("");
|
|
519
|
-
const [copied, setCopied] = useState(false);
|
|
520
692
|
|
|
521
693
|
const copy = async () => {
|
|
522
694
|
try {
|
|
523
695
|
const text = revealed || (await onReveal()).key;
|
|
524
696
|
setRevealed(text);
|
|
525
697
|
if (text) await navigator.clipboard.writeText(text);
|
|
526
|
-
|
|
527
|
-
setTimeout(() => setCopied(false), 1500);
|
|
698
|
+
toastStore.push("密钥已复制");
|
|
528
699
|
} catch {
|
|
529
|
-
|
|
700
|
+
toastStore.push("复制失败,请手动选择", "error");
|
|
530
701
|
}
|
|
531
702
|
};
|
|
532
703
|
|
|
533
|
-
return h(
|
|
704
|
+
return h("div", null,
|
|
534
705
|
h("div", { style: T.row },
|
|
535
706
|
h("code", {
|
|
536
707
|
style: {
|
|
@@ -546,33 +717,32 @@ window.__ModuleLoader__.load({
|
|
|
546
717
|
whiteSpace: "nowrap",
|
|
547
718
|
},
|
|
548
719
|
}, revealed || state.keyMasked || "尚未创建"),
|
|
549
|
-
h(Button, { onClick: copy, disabled: !state.
|
|
550
|
-
h(Button, { onClick: onCreate, disabled:
|
|
720
|
+
h(Button, { onClick: copy, disabled: !state.hasKey }, "复制"),
|
|
721
|
+
h(Button, { onClick: onCreate, disabled: pending.key },
|
|
722
|
+
pending.key ? "处理中…" : state.hasKey ? "重建" : "创建"),
|
|
551
723
|
),
|
|
552
724
|
h("div", { style: { ...T.hint, marginTop: 10 } },
|
|
553
725
|
"密钥固定命名为 ", h("code", { style: T.mono }, state.keyName || "dsh"),
|
|
554
|
-
"
|
|
726
|
+
",存在本机凭据库;页面只显示掩码。"),
|
|
555
727
|
);
|
|
556
728
|
}
|
|
557
729
|
|
|
558
|
-
//
|
|
730
|
+
// ----------------------------------------------------------- recharge
|
|
559
731
|
|
|
560
732
|
const PRESETS = [10, 20, 50, 100];
|
|
561
733
|
|
|
562
|
-
function
|
|
734
|
+
function RechargeDetail({ state, pending, onRecharge, onPoll, onRefresh, onRedeem }) {
|
|
563
735
|
const [amount, setAmount] = useState(20);
|
|
564
736
|
const [custom, setCustom] = useState("");
|
|
565
737
|
const [order, setOrder] = useState(null);
|
|
566
738
|
const [status, setStatus] = useState(null);
|
|
567
|
-
const [error, setError] = useState("");
|
|
568
739
|
const [code, setCode] = useState("");
|
|
569
|
-
const [
|
|
740
|
+
const [showRedeem, setShowRedeem] = useState(false);
|
|
570
741
|
const timer = useRef(null);
|
|
571
742
|
|
|
572
743
|
const multiplier = Number(state.rechargeMultiplier) > 0 ? Number(state.rechargeMultiplier) : 2;
|
|
573
744
|
const value = custom !== "" ? Number(custom) : amount;
|
|
574
745
|
const credit = Number.isFinite(value) ? value * multiplier : 0;
|
|
575
|
-
const balance = state.profile ? Number(state.profile.balance) : NaN;
|
|
576
746
|
|
|
577
747
|
useEffect(() => {
|
|
578
748
|
if (!order) return undefined;
|
|
@@ -583,10 +753,12 @@ window.__ModuleLoader__.load({
|
|
|
583
753
|
if (result.tone === "done" || result.tone === "error") {
|
|
584
754
|
clearInterval(timer.current);
|
|
585
755
|
timer.current = null;
|
|
586
|
-
if (result.tone === "done")
|
|
756
|
+
if (result.tone === "done") {
|
|
757
|
+
toastStore.push(result.text || "已到账");
|
|
758
|
+
await onRefresh();
|
|
759
|
+
}
|
|
587
760
|
}
|
|
588
|
-
} catch
|
|
589
|
-
setError(e instanceof Error ? e.message : String(e));
|
|
761
|
+
} catch {
|
|
590
762
|
clearInterval(timer.current);
|
|
591
763
|
timer.current = null;
|
|
592
764
|
}
|
|
@@ -595,7 +767,6 @@ window.__ModuleLoader__.load({
|
|
|
595
767
|
}, [order, onPoll, onRefresh]);
|
|
596
768
|
|
|
597
769
|
const start = async () => {
|
|
598
|
-
setError("");
|
|
599
770
|
const tab = window.open("", "_blank");
|
|
600
771
|
try {
|
|
601
772
|
const data = await onRecharge(value);
|
|
@@ -605,33 +776,28 @@ window.__ModuleLoader__.load({
|
|
|
605
776
|
if (data.orderNo) setOrder(data);
|
|
606
777
|
} catch (e) {
|
|
607
778
|
if (tab) tab.close();
|
|
608
|
-
|
|
779
|
+
toastStore.push(e instanceof Error ? e.message : String(e), "error");
|
|
609
780
|
}
|
|
610
781
|
};
|
|
611
782
|
|
|
612
783
|
const redeem = async () => {
|
|
613
|
-
setRedeemMsg("");
|
|
614
|
-
setError("");
|
|
615
784
|
try {
|
|
616
785
|
const result = await onRedeem(code.trim());
|
|
617
786
|
setCode("");
|
|
618
|
-
|
|
787
|
+
setShowRedeem(false);
|
|
788
|
+
toastStore.push(result?.profile ? `已兑换,余额 ${money(result.profile.balance)}` : "兑换成功");
|
|
789
|
+
await onRefresh();
|
|
619
790
|
} catch (e) {
|
|
620
|
-
|
|
791
|
+
toastStore.push(e instanceof Error ? e.message : String(e), "error");
|
|
621
792
|
}
|
|
622
793
|
};
|
|
623
794
|
|
|
624
|
-
return h(
|
|
625
|
-
|
|
626
|
-
title: "余额与充值",
|
|
627
|
-
meta: Number.isFinite(balance) ? "当前 " + money(balance) : null,
|
|
628
|
-
},
|
|
629
|
-
h("div", { style: { ...T.row, marginBottom: 12 } },
|
|
795
|
+
return h("div", null,
|
|
796
|
+
h("div", { style: { ...T.row, marginBottom: 10 } },
|
|
630
797
|
h("span", { style: T.small }, `1 元 = ${multiplier.toFixed(2)} 元额度`),
|
|
631
|
-
h("span", { style: { ...T.hint, marginLeft: "auto" } },
|
|
632
|
-
Number.isFinite(balance) && balance <= 0 ? "余额为 0 时无法拉取模型" : ""),
|
|
798
|
+
h("span", { style: { ...T.hint, marginLeft: "auto" } }, "到账后余额自动更新"),
|
|
633
799
|
),
|
|
634
|
-
h("div", { style: { ...T.row, marginBottom:
|
|
800
|
+
h("div", { style: { ...T.row, marginBottom: 10 } },
|
|
635
801
|
PRESETS.map((preset) =>
|
|
636
802
|
h(Button, {
|
|
637
803
|
key: preset,
|
|
@@ -646,49 +812,45 @@ window.__ModuleLoader__.load({
|
|
|
646
812
|
inputMode: "decimal",
|
|
647
813
|
value: custom,
|
|
648
814
|
onChange: (e) => setCustom(e.target.value.replace(/[^0-9.]/g, "")),
|
|
649
|
-
style: { flex: "0 1
|
|
815
|
+
style: { flex: "0 1 110px", minWidth: 92 },
|
|
650
816
|
}),
|
|
651
817
|
),
|
|
652
|
-
h("div", { style: { ...T.row
|
|
818
|
+
h("div", { style: { ...T.row } },
|
|
653
819
|
h("span", { style: T.small }, "预计到账"),
|
|
654
820
|
h("span", { style: { fontSize: 15, fontWeight: 600, color: C.text } }, money(credit)),
|
|
655
821
|
h("div", { style: { marginLeft: "auto", display: "flex", gap: 8 } },
|
|
656
|
-
h(Button, { onClick: onRefresh, disabled: !state.loggedIn }, "刷新余额"),
|
|
657
822
|
h(Button, {
|
|
658
823
|
variant: "primary",
|
|
659
824
|
onClick: start,
|
|
660
|
-
disabled:
|
|
825
|
+
disabled: !state.loggedIn || !Number.isFinite(value) || value <= 0,
|
|
661
826
|
}, "去支付"),
|
|
662
827
|
),
|
|
663
828
|
),
|
|
664
|
-
h("div", { style: T.hint },
|
|
665
|
-
"「去支付」会在新标签页打开支付页,到账后余额自动更新;订单 30 分钟内有效。"),
|
|
666
829
|
order
|
|
667
830
|
? h("div", { style: { marginTop: 12 } },
|
|
668
831
|
h(Alert, {
|
|
669
832
|
tone: status ? (status.tone === "done" ? "ok" : status.tone === "error" ? "error" : "info") : "info",
|
|
670
833
|
title: "订单 " + order.orderNo,
|
|
671
|
-
action: status && status.tone === "done" ? null : h(Button, {
|
|
672
|
-
size: "sm",
|
|
673
|
-
variant: "ghost",
|
|
674
|
-
onClick: () => setOrder(null),
|
|
675
|
-
style: { color: "inherit" },
|
|
676
|
-
}, "关闭"),
|
|
677
834
|
}, status ? status.text : "已打开支付页,等待付款…"))
|
|
678
835
|
: null,
|
|
679
|
-
h(
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
style: { flex:
|
|
687
|
-
|
|
688
|
-
|
|
836
|
+
h("div", { style: { marginTop: 12, display: "flex", gap: 8, alignItems: "center" } },
|
|
837
|
+
h(Button, {
|
|
838
|
+
variant: "ghost",
|
|
839
|
+
size: "sm",
|
|
840
|
+
onClick: () => setShowRedeem((v) => !v),
|
|
841
|
+
}, showRedeem ? "收起兑换码" : "有兑换码?"),
|
|
842
|
+
showRedeem
|
|
843
|
+
? h("div", { style: { flex: 1, display: "flex", gap: 8 } },
|
|
844
|
+
h(TextField, {
|
|
845
|
+
placeholder: "兑换码",
|
|
846
|
+
value: code,
|
|
847
|
+
onChange: (e) => setCode(e.target.value),
|
|
848
|
+
onKeyDown: (e) => { if (e.key === "Enter" && code.trim()) redeem(); },
|
|
849
|
+
}),
|
|
850
|
+
h(Button, { onClick: redeem, disabled: !code.trim() }, "兑换"),
|
|
851
|
+
)
|
|
852
|
+
: null,
|
|
689
853
|
),
|
|
690
|
-
redeemMsg ? h("div", { style: { ...T.hint, color: C.ok, marginTop: 8 } }, redeemMsg) : null,
|
|
691
|
-
error ? h("div", { style: { marginTop: 10 } }, h(Alert, { tone: "error" }, error)) : null,
|
|
692
854
|
);
|
|
693
855
|
}
|
|
694
856
|
|
|
@@ -716,7 +878,12 @@ window.__ModuleLoader__.load({
|
|
|
716
878
|
|
|
717
879
|
const submit = async () => {
|
|
718
880
|
const value = draft.trim() === "" ? null : Number(draft) * 1000;
|
|
719
|
-
|
|
881
|
+
try {
|
|
882
|
+
await onSaveContext(model.id, value);
|
|
883
|
+
toastStore.push(value === null ? "已恢复上游容量" : "容量已保存");
|
|
884
|
+
} catch (e) {
|
|
885
|
+
toastStore.push(e instanceof Error ? e.message : String(e), "error");
|
|
886
|
+
}
|
|
720
887
|
setEditing(false);
|
|
721
888
|
setDraft("");
|
|
722
889
|
};
|
|
@@ -731,7 +898,12 @@ window.__ModuleLoader__.load({
|
|
|
731
898
|
fontSize: 13,
|
|
732
899
|
},
|
|
733
900
|
},
|
|
734
|
-
h("input", {
|
|
901
|
+
h("input", {
|
|
902
|
+
type: "checkbox",
|
|
903
|
+
checked,
|
|
904
|
+
onChange: () => onToggle(model.id),
|
|
905
|
+
"aria-label": `${model.id} 是否显示在对话中`,
|
|
906
|
+
}),
|
|
735
907
|
h("code", {
|
|
736
908
|
style: { ...T.mono, flex: "1 1 auto", minWidth: 0, overflow: "hidden", textOverflow: "ellipsis" },
|
|
737
909
|
}, model.id),
|
|
@@ -741,7 +913,7 @@ window.__ModuleLoader__.load({
|
|
|
741
913
|
type: "number",
|
|
742
914
|
min: 1,
|
|
743
915
|
step: 1,
|
|
744
|
-
placeholder: effective === null ? "" : String(
|
|
916
|
+
placeholder: effective === null ? "" : String(effective),
|
|
745
917
|
value: draft,
|
|
746
918
|
onChange: (e) => setDraft(e.target.value),
|
|
747
919
|
onKeyDown: (e) => { if (e.key === "Enter") submit(); },
|
|
@@ -756,34 +928,31 @@ window.__ModuleLoader__.load({
|
|
|
756
928
|
)
|
|
757
929
|
: h("button", {
|
|
758
930
|
type: "button",
|
|
759
|
-
onClick: () => { setEditing(true); setDraft(effective === null ? "" : String(
|
|
760
|
-
title: "
|
|
931
|
+
onClick: () => { setEditing(true); setDraft(effective === null ? "" : String(effective)); },
|
|
932
|
+
title: "点击修改这个模型的上下文窗口",
|
|
761
933
|
style: {
|
|
762
934
|
border: "none", background: "transparent", padding: "2px 4px", cursor: "pointer",
|
|
763
935
|
color: current.contextSource === "default" ? C.warn : C.muted, fontSize: 11.5,
|
|
764
936
|
},
|
|
765
937
|
}, contextBadge(current) ?? "设置"),
|
|
766
938
|
Array.isArray(current.reasoningEfforts)
|
|
767
|
-
? h("span", { style: { ...T.hint, whiteSpace: "nowrap" } },
|
|
768
|
-
"思考 " + current.reasoningEfforts.length + " 档")
|
|
939
|
+
? h("span", { style: { ...T.hint, whiteSpace: "nowrap" } }, "思考 " + current.reasoningEfforts.length + " 档")
|
|
769
940
|
: null,
|
|
770
941
|
shown.some((m) => m.id === model.id && m.vision) ? h("span", { style: T.hint }, "图片") : null,
|
|
771
942
|
);
|
|
772
943
|
}
|
|
773
944
|
|
|
774
|
-
function
|
|
945
|
+
function ModelsDetail({ state, pending, onRefreshModels, onSaveVisible, onSaveContext, onGoRecharge }) {
|
|
775
946
|
const models = state.models || { models: [], all: [], allowed: [], fetchedAt: 0, stale: false, error: null };
|
|
776
947
|
const shown = models.models || [];
|
|
777
948
|
const everything = models.all || [];
|
|
778
949
|
const allowed = models.allowed || [];
|
|
779
950
|
const showingAll = allowed.length === 0;
|
|
780
951
|
|
|
781
|
-
const [open, setOpen] = useState(false);
|
|
782
|
-
const [error, setError] = useState("");
|
|
783
952
|
const [draft, setDraft] = useState(null);
|
|
784
953
|
const [saving, setSaving] = useState(false);
|
|
785
|
-
const [saved, setSaved] = useState(false);
|
|
786
954
|
const [query, setQuery] = useState("");
|
|
955
|
+
const [expanded, setExpanded] = useState(false);
|
|
787
956
|
|
|
788
957
|
const checked = draft ?? (showingAll ? everything.map((m) => m.id) : allowed);
|
|
789
958
|
const isOn = (id) => checked.includes(id);
|
|
@@ -796,70 +965,61 @@ window.__ModuleLoader__.load({
|
|
|
796
965
|
|
|
797
966
|
const toggle = (id) => {
|
|
798
967
|
setDraft(isOn(id) ? checked.filter((x) => x !== id) : [...checked, id]);
|
|
799
|
-
setSaved(false);
|
|
800
|
-
};
|
|
801
|
-
|
|
802
|
-
const refresh = async () => {
|
|
803
|
-
setError("");
|
|
804
|
-
try {
|
|
805
|
-
const result = await onRefreshModels();
|
|
806
|
-
if (result && result.error) setError(result.error.message || String(result.error));
|
|
807
|
-
} catch (e) {
|
|
808
|
-
setError(e instanceof Error ? e.message : String(e));
|
|
809
|
-
}
|
|
810
968
|
};
|
|
811
969
|
|
|
812
970
|
const save = async () => {
|
|
813
971
|
setSaving(true);
|
|
814
|
-
setError("");
|
|
815
|
-
setSaved(false);
|
|
816
972
|
try {
|
|
817
973
|
const ids = checked.length === everything.length ? [] : checked;
|
|
818
974
|
await onSaveVisible(ids);
|
|
819
975
|
setDraft(null);
|
|
820
|
-
|
|
821
|
-
setTimeout(() => setSaved(false), 2000);
|
|
976
|
+
toastStore.push(ids.length === 0 ? "已显示全部模型" : `对话里将显示 ${ids.length} 个模型`);
|
|
822
977
|
} catch (e) {
|
|
823
|
-
|
|
978
|
+
toastStore.push(e instanceof Error ? e.message : String(e), "error");
|
|
824
979
|
} finally {
|
|
825
980
|
setSaving(false);
|
|
826
981
|
}
|
|
827
982
|
};
|
|
828
983
|
|
|
829
|
-
const reason =
|
|
984
|
+
const reason = models.error?.message ?? "";
|
|
830
985
|
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
onClick: onGoRecharge,
|
|
845
|
-
style: { color: "inherit", borderColor: "currentColor", background: "transparent" },
|
|
846
|
-
}, "去充值"),
|
|
847
|
-
},
|
|
848
|
-
"Sub2API 在余额为 0 时会拒绝 /v1/models。充值到账后这里会自动同步,无需重新登录。"))
|
|
849
|
-
: null,
|
|
986
|
+
if (models.needsRecharge) {
|
|
987
|
+
return h("div", null,
|
|
988
|
+
h(Alert, {
|
|
989
|
+
tone: "warn",
|
|
990
|
+
title: "余额不足,暂时拿不到模型列表",
|
|
991
|
+
action: h(Button, {
|
|
992
|
+
size: "sm",
|
|
993
|
+
onClick: onGoRecharge,
|
|
994
|
+
style: { color: "inherit", borderColor: "currentColor", background: "transparent" },
|
|
995
|
+
}, "去充值"),
|
|
996
|
+
},
|
|
997
|
+
"Psapi 在余额为 0 时会拒绝 /v1/models;充值到账后自动同步。"));
|
|
998
|
+
}
|
|
850
999
|
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
1000
|
+
if (everything.length === 0) {
|
|
1001
|
+
return h("div", null,
|
|
1002
|
+
reason
|
|
1003
|
+
? h(Alert, { tone: "error", title: "同步失败" }, reason)
|
|
1004
|
+
: h("div", { style: T.hint }, "还没有模型。点「刷新模型」从上游同步。"),
|
|
1005
|
+
h("div", { style: { marginTop: 10 } },
|
|
1006
|
+
h(Button, { onClick: onRefreshModels, disabled: pending.models },
|
|
1007
|
+
pending.models ? "同步中…" : "刷新模型")),
|
|
1008
|
+
);
|
|
1009
|
+
}
|
|
854
1010
|
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
h(
|
|
858
|
-
|
|
859
|
-
|
|
1011
|
+
return h("div", null,
|
|
1012
|
+
h("div", { style: { ...T.row, marginBottom: 10 } },
|
|
1013
|
+
h("span", { style: { ...T.small, fontWeight: 500 } },
|
|
1014
|
+
showingAll && !dirty ? `全部 ${everything.length} 个` : `已选 ${checked.length} / ${everything.length}`),
|
|
1015
|
+
h("div", { style: { marginLeft: "auto", display: "flex", gap: 8 } },
|
|
1016
|
+
h(Button, { size: "sm", onClick: onRefreshModels, disabled: pending.models },
|
|
1017
|
+
pending.models ? "同步中…" : "刷新"),
|
|
1018
|
+
h(Button, { size: "sm", onClick: () => setExpanded((v) => !v) },
|
|
1019
|
+
expanded ? "收起" : "管理"),
|
|
1020
|
+
),
|
|
860
1021
|
),
|
|
861
|
-
|
|
862
|
-
open && everything.length > 0
|
|
1022
|
+
expanded
|
|
863
1023
|
? h("div", null,
|
|
864
1024
|
everything.length > 8
|
|
865
1025
|
? h("div", { style: { marginBottom: 8 } },
|
|
@@ -870,12 +1030,7 @@ window.__ModuleLoader__.load({
|
|
|
870
1030
|
}))
|
|
871
1031
|
: null,
|
|
872
1032
|
h("div", {
|
|
873
|
-
style: {
|
|
874
|
-
maxHeight: 300,
|
|
875
|
-
overflow: "auto",
|
|
876
|
-
border: "0.5px solid " + C.line,
|
|
877
|
-
borderRadius: 12,
|
|
878
|
-
},
|
|
1033
|
+
style: { maxHeight: 300, overflow: "auto", border: "0.5px solid " + C.line, borderRadius: 12 },
|
|
879
1034
|
},
|
|
880
1035
|
filtered.length === 0
|
|
881
1036
|
? h("div", { style: { ...T.hint, padding: 12 } }, "没有匹配的模型")
|
|
@@ -890,55 +1045,43 @@ window.__ModuleLoader__.load({
|
|
|
890
1045
|
})),
|
|
891
1046
|
),
|
|
892
1047
|
h("div", { style: { ...T.row, marginTop: 10 } },
|
|
893
|
-
h(Button, { size: "sm", onClick: () =>
|
|
894
|
-
h(Button, { size: "sm", onClick: () =>
|
|
895
|
-
h(Button, { size: "sm", onClick: () =>
|
|
896
|
-
h("div", { style: { marginLeft: "auto"
|
|
897
|
-
saved ? h("span", { style: { ...T.hint, color: C.ok } }, "已保存") : null,
|
|
1048
|
+
h(Button, { size: "sm", onClick: () => setDraft(everything.map((m) => m.id)) }, "全选"),
|
|
1049
|
+
h(Button, { size: "sm", onClick: () => setDraft([]) }, "全不选"),
|
|
1050
|
+
dirty ? h(Button, { size: "sm", onClick: () => setDraft(null) }, "还原") : null,
|
|
1051
|
+
h("div", { style: { marginLeft: "auto" } },
|
|
898
1052
|
h(Button, { variant: "primary", size: "sm", onClick: save, disabled: !dirty || saving },
|
|
899
|
-
saving ? "保存中…" : "保存选择"),
|
|
900
|
-
),
|
|
1053
|
+
saving ? "保存中…" : "保存选择")),
|
|
901
1054
|
),
|
|
902
1055
|
)
|
|
903
|
-
:
|
|
904
|
-
|
|
905
|
-
h("div", { style: { ...T.hint, marginTop: 10 } },
|
|
906
|
-
everything.length === 0
|
|
907
|
-
? "目录来自上游 /v1/models,登录后自动同步。"
|
|
908
|
-
: "勾选的模型会出现在对话的模型选择器里;点容量数字可以手动改这个模型的上下文窗口。"),
|
|
1056
|
+
: h("div", { style: T.hint },
|
|
1057
|
+
`${shown.length} 个模型会出现在对话的选择器里;点「管理」可以增减或改上下文容量。`),
|
|
909
1058
|
);
|
|
910
1059
|
}
|
|
911
1060
|
|
|
912
1061
|
// ---------------------------------------------------------- diagnose
|
|
913
1062
|
|
|
914
|
-
function
|
|
1063
|
+
function DiagnoseDetail({ state, onDiagnose }) {
|
|
915
1064
|
const [result, setResult] = useState(null);
|
|
916
1065
|
const [busy, setBusy] = useState(false);
|
|
917
|
-
const [error, setError] = useState("");
|
|
918
1066
|
|
|
919
1067
|
const run = async () => {
|
|
920
1068
|
setBusy(true);
|
|
921
|
-
setError("");
|
|
922
1069
|
try {
|
|
923
1070
|
setResult(await onDiagnose());
|
|
924
1071
|
} catch (e) {
|
|
925
|
-
|
|
1072
|
+
toastStore.push(e instanceof Error ? e.message : String(e), "error");
|
|
926
1073
|
} finally {
|
|
927
1074
|
setBusy(false);
|
|
928
1075
|
}
|
|
929
1076
|
};
|
|
930
1077
|
|
|
931
|
-
return h(
|
|
932
|
-
title: "诊断",
|
|
933
|
-
meta: state.version ? "v" + state.version : null,
|
|
934
|
-
aside: result
|
|
935
|
-
? h("span", { style: { ...T.meta, color: result.ok ? C.ok : C.warn } }, result.summary)
|
|
936
|
-
: null,
|
|
937
|
-
},
|
|
1078
|
+
return h("div", null,
|
|
938
1079
|
h("div", { style: T.row },
|
|
939
1080
|
h(Button, { onClick: run, disabled: busy || !state.loggedIn }, busy ? "检查中…" : "运行诊断"),
|
|
1081
|
+
result
|
|
1082
|
+
? h("span", { style: { ...T.hint, color: result.ok ? C.ok : C.warn } }, result.summary)
|
|
1083
|
+
: null,
|
|
940
1084
|
),
|
|
941
|
-
error ? h("div", { style: { marginTop: 10 } }, h(Alert, { tone: "error" }, error)) : null,
|
|
942
1085
|
result
|
|
943
1086
|
? h("div", { style: { marginTop: 12, border: "0.5px solid " + C.line, borderRadius: 12 } },
|
|
944
1087
|
result.checks.map((check) =>
|
|
@@ -955,105 +1098,11 @@ window.__ModuleLoader__.load({
|
|
|
955
1098
|
},
|
|
956
1099
|
h("span", { style: { width: 6, height: 6, borderRadius: 999, flex: "0 0 6px", background: check.ok ? C.ok : C.warn, alignSelf: "center" } }),
|
|
957
1100
|
h("span", { style: { width: 74, flex: "0 0 74px", color: C.muted } }, check.label),
|
|
958
|
-
h("span", { style: { flex: 1, minWidth: 0, color: check.ok ? C.text : C.warn } },
|
|
1101
|
+
h("span", { style: { flex: 1, minWidth: 0, color: check.ok ? C.text : C.warn } },
|
|
1102
|
+
check.detail ?? (check.ok ? "正常" : "失败")),
|
|
959
1103
|
)))
|
|
960
|
-
:
|
|
961
|
-
|
|
962
|
-
"诊断会依次检查登录、令牌、密钥、余额、密钥分组和模型目录,把失败的那一项直接指出来。"),
|
|
963
|
-
);
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
// ------------------------------------------------------------ badge
|
|
967
|
-
|
|
968
|
-
/**
|
|
969
|
-
* The always-visible balance pill in the chat input's trailing controls.
|
|
970
|
-
*
|
|
971
|
-
* It is the one number a user checks before sending a long request, so it
|
|
972
|
-
* lives where the send button does rather than behind settings.
|
|
973
|
-
*/
|
|
974
|
-
function BalanceBadge() {
|
|
975
|
-
const { state } = useStatus();
|
|
976
|
-
if (!state || !state.loggedIn) return null;
|
|
977
|
-
|
|
978
|
-
const balance = state.profile ? Number(state.profile.balance) : NaN;
|
|
979
|
-
const known = Number.isFinite(balance);
|
|
980
|
-
const low = known && balance <= 0;
|
|
981
|
-
const thin = known && balance > 0 && balance < 5;
|
|
982
|
-
const dot = low ? C.bad : thin ? C.warn : C.ok;
|
|
983
|
-
|
|
984
|
-
const models = state.models?.models?.length ?? 0;
|
|
985
|
-
const pending = state.models?.needsRecharge === true;
|
|
986
|
-
const detail = [
|
|
987
|
-
known ? `余额 ${money(balance)}` : "余额未知",
|
|
988
|
-
state.email ? `账号 ${state.email}` : null,
|
|
989
|
-
models > 0 ? `可用模型 ${models} 个` : pending ? "充值后可同步模型" : "尚未同步模型",
|
|
990
|
-
low ? "点击去充值" : "点击刷新",
|
|
991
|
-
].filter(Boolean).join("\n");
|
|
992
|
-
|
|
993
|
-
// Out of balance is the one state worth an action, so the pill becomes a
|
|
994
|
-
// shortcut to the payment page instead of a label the user has to decode.
|
|
995
|
-
const onClick = async () => {
|
|
996
|
-
if (low) {
|
|
997
|
-
try {
|
|
998
|
-
const link = await call("/recharge/link", { method: "POST", body: {} });
|
|
999
|
-
window.open(link.url, "_blank", "noopener");
|
|
1000
|
-
return;
|
|
1001
|
-
} catch {
|
|
1002
|
-
// Fall through to a refresh so the failure is not silent forever.
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
statusStore.refresh();
|
|
1006
|
-
};
|
|
1007
|
-
|
|
1008
|
-
return h("button", {
|
|
1009
|
-
type: "button",
|
|
1010
|
-
title: detail,
|
|
1011
|
-
"aria-label": `Sub2API ${known ? "余额 " + money(balance) : "余额未知"},点击刷新`,
|
|
1012
|
-
onClick,
|
|
1013
|
-
style: {
|
|
1014
|
-
display: "inline-flex",
|
|
1015
|
-
alignItems: "center",
|
|
1016
|
-
gap: 6,
|
|
1017
|
-
height: 28,
|
|
1018
|
-
padding: "0 10px",
|
|
1019
|
-
borderRadius: 999,
|
|
1020
|
-
border: "0.5px solid " + C.line,
|
|
1021
|
-
background: "transparent",
|
|
1022
|
-
color: low ? C.bad : C.second,
|
|
1023
|
-
fontSize: 12,
|
|
1024
|
-
fontVariantNumeric: "tabular-nums",
|
|
1025
|
-
cursor: "pointer",
|
|
1026
|
-
whiteSpace: "nowrap",
|
|
1027
|
-
},
|
|
1028
|
-
},
|
|
1029
|
-
h("span", {
|
|
1030
|
-
style: { width: 6, height: 6, borderRadius: 999, background: dot, flex: "0 0 6px" },
|
|
1031
|
-
}),
|
|
1032
|
-
h("span", null, known ? money(balance) : "余额"),
|
|
1033
|
-
low ? h("span", { style: { color: C.bad } }, "去充值") : null,
|
|
1034
|
-
);
|
|
1035
|
-
}
|
|
1036
|
-
|
|
1037
|
-
// --------------------------------------------------------- skeleton
|
|
1038
|
-
|
|
1039
|
-
/** Placeholder blocks while the first status read is in flight. */
|
|
1040
|
-
function Skeleton() {
|
|
1041
|
-
const bar = (width, height) =>
|
|
1042
|
-
h("div", {
|
|
1043
|
-
style: {
|
|
1044
|
-
width,
|
|
1045
|
-
height,
|
|
1046
|
-
borderRadius: 8,
|
|
1047
|
-
background: "color-mix(in srgb, " + C.muted + " 16%, transparent)",
|
|
1048
|
-
},
|
|
1049
|
-
});
|
|
1050
|
-
return h("div", { style: { padding: "4px 0 18px" } },
|
|
1051
|
-
h(Card, { title: "Sub2API" },
|
|
1052
|
-
bar("40%", 14),
|
|
1053
|
-
h("div", { style: { height: 10 } }),
|
|
1054
|
-
bar("70%", 12),
|
|
1055
|
-
h("div", { style: { height: 10 } }),
|
|
1056
|
-
bar("55%", 12)),
|
|
1104
|
+
: h("div", { style: { ...T.hint, marginTop: 10 } },
|
|
1105
|
+
"依次检查登录、令牌、密钥、余额、密钥分组和模型目录,指出失败的那一项。"),
|
|
1057
1106
|
);
|
|
1058
1107
|
}
|
|
1059
1108
|
|
|
@@ -1061,13 +1110,22 @@ window.__ModuleLoader__.load({
|
|
|
1061
1110
|
|
|
1062
1111
|
function SectionInner() {
|
|
1063
1112
|
const { state, error } = useStatus();
|
|
1064
|
-
const
|
|
1065
|
-
const [
|
|
1113
|
+
const toasts = useToasts();
|
|
1114
|
+
const [pending, setPending] = useState({});
|
|
1066
1115
|
const [steps, setSteps] = useState(null);
|
|
1067
1116
|
const [ready, setReady] = useState(undefined);
|
|
1068
1117
|
const [announcements, setAnnouncements] = useState([]);
|
|
1118
|
+
const [open, setOpen] = useState({ models: true, recharge: false, account: false, key: false, diagnose: false });
|
|
1069
1119
|
|
|
1070
|
-
const
|
|
1120
|
+
const setBusy = (key, value) => setPending((prev) => ({ ...prev, [key]: value }));
|
|
1121
|
+
const run = useCallback(async (key, fn) => {
|
|
1122
|
+
setPending((prev) => ({ ...prev, [key]: true }));
|
|
1123
|
+
try {
|
|
1124
|
+
return await fn();
|
|
1125
|
+
} finally {
|
|
1126
|
+
setPending((prev) => ({ ...prev, [key]: false }));
|
|
1127
|
+
}
|
|
1128
|
+
}, []);
|
|
1071
1129
|
|
|
1072
1130
|
useEffect(() => {
|
|
1073
1131
|
let cancelled = false;
|
|
@@ -1077,59 +1135,52 @@ window.__ModuleLoader__.load({
|
|
|
1077
1135
|
return () => { cancelled = true; };
|
|
1078
1136
|
}, [state && state.loggedIn]);
|
|
1079
1137
|
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
* bottom of the page reads like two different problems.
|
|
1086
|
-
*/
|
|
1087
|
-
const wrap = useCallback(async (fn, report = true) => {
|
|
1088
|
-
setBusy(true);
|
|
1089
|
-
if (report) setActionError("");
|
|
1090
|
-
try {
|
|
1091
|
-
return await fn();
|
|
1092
|
-
} catch (e) {
|
|
1093
|
-
if (report) setActionError(e instanceof Error ? e.message : String(e));
|
|
1094
|
-
throw e;
|
|
1095
|
-
} finally {
|
|
1096
|
-
setBusy(false);
|
|
1138
|
+
// A recharge is the one thing worth unfolding automatically.
|
|
1139
|
+
const balance = state?.profile ? Number(state.profile.balance) : NaN;
|
|
1140
|
+
useEffect(() => {
|
|
1141
|
+
if (Number.isFinite(balance) && balance <= 0) {
|
|
1142
|
+
setOpen((prev) => (prev.models ? { ...prev, recharge: true } : prev));
|
|
1097
1143
|
}
|
|
1098
|
-
}, []);
|
|
1144
|
+
}, [balance]);
|
|
1099
1145
|
|
|
1100
|
-
const onAuth = useCallback((input) =>
|
|
1146
|
+
const onAuth = useCallback((input) => run("auth", async () => {
|
|
1101
1147
|
const data = await call("/auth", { method: "POST", body: input });
|
|
1102
1148
|
statusStore.accept(data.state);
|
|
1103
1149
|
setSteps(data.steps ?? null);
|
|
1104
1150
|
setReady(data.ready);
|
|
1151
|
+
toastStore.push(data.mode === "register" ? "注册成功,密钥已就绪" : "已登录");
|
|
1105
1152
|
return data;
|
|
1106
|
-
}
|
|
1153
|
+
}), [run]);
|
|
1107
1154
|
|
|
1108
|
-
const onLogout = useCallback(() =>
|
|
1155
|
+
const onLogout = useCallback(() => run("logout", async () => {
|
|
1109
1156
|
const data = await call("/logout", { method: "POST", body: {} });
|
|
1110
1157
|
statusStore.accept(data.state);
|
|
1111
1158
|
setSteps(null);
|
|
1112
1159
|
setReady(undefined);
|
|
1113
|
-
|
|
1160
|
+
toastStore.push("已退出登录");
|
|
1161
|
+
}), [run]);
|
|
1114
1162
|
|
|
1115
|
-
const onCreateKey = useCallback(() =>
|
|
1116
|
-
const force = !!
|
|
1163
|
+
const onCreateKey = useCallback(() => run("key", async () => {
|
|
1164
|
+
const force = !!statusStore.state?.hasKey;
|
|
1117
1165
|
const data = await call("/key", { method: "POST", body: force ? { force: true } : {} });
|
|
1118
1166
|
statusStore.accept(data.state);
|
|
1119
|
-
|
|
1167
|
+
toastStore.push(force ? "密钥已重建" : "密钥已创建");
|
|
1168
|
+
}), [run]);
|
|
1120
1169
|
|
|
1121
1170
|
const onReveal = useCallback(() => call("/key/reveal", { method: "POST", body: {} }), []);
|
|
1122
1171
|
|
|
1123
|
-
const onRefreshModels = useCallback(() =>
|
|
1172
|
+
const onRefreshModels = useCallback(() => run("models", async () => {
|
|
1124
1173
|
const result = await call("/models/refresh", { method: "POST", body: {} });
|
|
1125
1174
|
await statusStore.refresh();
|
|
1175
|
+
if (result.error) toastStore.push(result.error.message || "同步失败", "error");
|
|
1176
|
+
else toastStore.push(`已同步 ${result.count} 个模型`);
|
|
1126
1177
|
return result;
|
|
1127
|
-
}), [
|
|
1178
|
+
}), [run]);
|
|
1128
1179
|
|
|
1129
|
-
const onSaveVisible = useCallback(
|
|
1180
|
+
const onSaveVisible = useCallback(async (ids) => {
|
|
1130
1181
|
await call("/models/visible", { method: "POST", body: { ids } });
|
|
1131
1182
|
await statusStore.refresh();
|
|
1132
|
-
}
|
|
1183
|
+
}, []);
|
|
1133
1184
|
|
|
1134
1185
|
const onSaveContext = useCallback(async (id, contextWindow) => {
|
|
1135
1186
|
await call("/models/context", { method: "POST", body: { id, contextWindow } });
|
|
@@ -1151,11 +1202,13 @@ window.__ModuleLoader__.load({
|
|
|
1151
1202
|
const onRedeem = useCallback((code) => call("/redeem", { method: "POST", body: { code } }), []);
|
|
1152
1203
|
const onRefresh = useCallback(() => statusStore.refresh(), []);
|
|
1153
1204
|
|
|
1205
|
+
const toggle = (key) => setOpen((prev) => ({ ...prev, [key]: !prev[key] }));
|
|
1206
|
+
|
|
1154
1207
|
if (state === null) {
|
|
1155
|
-
const failure = error ||
|
|
1208
|
+
const failure = error || "";
|
|
1156
1209
|
if (failure) {
|
|
1157
1210
|
return h("div", { style: { padding: "4px 0 18px" } },
|
|
1158
|
-
h(Card, { title: "
|
|
1211
|
+
h(Card, { title: "Psapi" },
|
|
1159
1212
|
h(Alert, { tone: "error", title: "面板无法连接插件后端" }, failure),
|
|
1160
1213
|
h("div", { style: { ...T.hint, marginTop: 12 } },
|
|
1161
1214
|
"常见原因:插件装上了但宿主那一半没有加载(版本不匹配、依赖缺失、或安装后没有重启)。"),
|
|
@@ -1172,31 +1225,87 @@ window.__ModuleLoader__.load({
|
|
|
1172
1225
|
|
|
1173
1226
|
const unread = announcements.filter((item) => !item.is_read);
|
|
1174
1227
|
|
|
1175
|
-
|
|
1228
|
+
if (!state.loggedIn) {
|
|
1229
|
+
return h("div", { style: { padding: "4px 0 18px" } },
|
|
1230
|
+
h(Toasts, { items: toasts }),
|
|
1231
|
+
h(LoginCard, { busy: pending.auth === true, onAuth }),
|
|
1232
|
+
);
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
const models = state.models || {};
|
|
1236
|
+
const shownCount = models.models?.length ?? 0;
|
|
1237
|
+
|
|
1238
|
+
return h("div", { style: { padding: "4px 0 18px", position: "relative" } },
|
|
1239
|
+
h(Toasts, { items: toasts }),
|
|
1176
1240
|
unread.length > 0
|
|
1177
1241
|
? h("div", { style: { marginBottom: 12 } },
|
|
1178
1242
|
h(Alert, { tone: "info", title: unread[0].title ?? "站点公告" },
|
|
1179
1243
|
asText(unread[0].content ?? unread[0].body ?? "").slice(0, 200)))
|
|
1180
1244
|
: null,
|
|
1181
|
-
|
|
1182
|
-
h(
|
|
1183
|
-
h(RechargeCard, { state, busy, onRecharge, onPoll, onRefresh, onRedeem }),
|
|
1184
|
-
h(ModelsCard, {
|
|
1245
|
+
|
|
1246
|
+
h(StatusHeader, {
|
|
1185
1247
|
state,
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
onSaveContext,
|
|
1191
|
-
onGoRecharge: () => {
|
|
1248
|
+
refreshing: pending.manualRefresh === true,
|
|
1249
|
+
onRefresh: () => run("manualRefresh", () => statusStore.refresh()),
|
|
1250
|
+
onOpenRecharge: () => {
|
|
1251
|
+
setOpen((prev) => ({ ...prev, recharge: true }));
|
|
1192
1252
|
const target = document.getElementById("sub2api-recharge");
|
|
1193
1253
|
if (target && typeof target.scrollIntoView === "function") {
|
|
1194
|
-
target.scrollIntoView({ behavior: "smooth", block: "center" });
|
|
1254
|
+
setTimeout(() => target.scrollIntoView({ behavior: "smooth", block: "center" }), 60);
|
|
1195
1255
|
}
|
|
1196
1256
|
},
|
|
1197
1257
|
}),
|
|
1198
|
-
|
|
1199
|
-
|
|
1258
|
+
|
|
1259
|
+
h(Collapsible, {
|
|
1260
|
+
id: "sub2api-models",
|
|
1261
|
+
title: "模型",
|
|
1262
|
+
meta: shownCount > 0 ? `${shownCount} 个在对话中可选` : "未同步",
|
|
1263
|
+
open: open.models,
|
|
1264
|
+
onToggle: () => toggle("models"),
|
|
1265
|
+
},
|
|
1266
|
+
h(ModelsDetail, {
|
|
1267
|
+
state,
|
|
1268
|
+
pending: { models: pending.models === true },
|
|
1269
|
+
onRefreshModels,
|
|
1270
|
+
onSaveVisible,
|
|
1271
|
+
onSaveContext,
|
|
1272
|
+
onGoRecharge: () => { setOpen((prev) => ({ ...prev, recharge: true })); },
|
|
1273
|
+
})),
|
|
1274
|
+
|
|
1275
|
+
h(Collapsible, {
|
|
1276
|
+
id: "sub2api-recharge",
|
|
1277
|
+
title: "充值",
|
|
1278
|
+
meta: Number.isFinite(balance) ? `余额 ${money(balance)}` : null,
|
|
1279
|
+
open: open.recharge,
|
|
1280
|
+
onToggle: () => toggle("recharge"),
|
|
1281
|
+
},
|
|
1282
|
+
h(RechargeDetail, { state, pending, onRecharge, onPoll, onRefresh, onRedeem })),
|
|
1283
|
+
|
|
1284
|
+
h(Collapsible, {
|
|
1285
|
+
title: "API 密钥",
|
|
1286
|
+
meta: state.hasKey ? state.keyMasked : "未创建",
|
|
1287
|
+
open: open.key,
|
|
1288
|
+
onToggle: () => toggle("key"),
|
|
1289
|
+
},
|
|
1290
|
+
h(KeyDetail, { state, pending, onCreate: onCreateKey, onReveal })),
|
|
1291
|
+
|
|
1292
|
+
h(Collapsible, {
|
|
1293
|
+
title: "账户与用量",
|
|
1294
|
+
meta: state.email || null,
|
|
1295
|
+
open: open.account,
|
|
1296
|
+
onToggle: () => toggle("account"),
|
|
1297
|
+
},
|
|
1298
|
+
h(AccountDetail, { state, steps, ready, pending, onLogout })),
|
|
1299
|
+
|
|
1300
|
+
h(Collapsible, {
|
|
1301
|
+
title: "诊断",
|
|
1302
|
+
meta: state.version ? "v" + state.version : null,
|
|
1303
|
+
open: open.diagnose,
|
|
1304
|
+
onToggle: () => toggle("diagnose"),
|
|
1305
|
+
},
|
|
1306
|
+
h(DiagnoseDetail, { state, onDiagnose })),
|
|
1307
|
+
|
|
1308
|
+
error ? h(Alert, { tone: "error" }, error) : null,
|
|
1200
1309
|
);
|
|
1201
1310
|
}
|
|
1202
1311
|
|
|
@@ -1204,6 +1313,96 @@ window.__ModuleLoader__.load({
|
|
|
1204
1313
|
return h(Boundary, null, h(SectionInner, null));
|
|
1205
1314
|
}
|
|
1206
1315
|
|
|
1316
|
+
// ------------------------------------------------------------ badge
|
|
1317
|
+
|
|
1318
|
+
/**
|
|
1319
|
+
* The always-visible balance pill in the chat input's trailing controls.
|
|
1320
|
+
*
|
|
1321
|
+
* It is the one number a user checks before sending a long request, so it
|
|
1322
|
+
* lives where the send button does rather than behind settings.
|
|
1323
|
+
*/
|
|
1324
|
+
function BalanceBadge() {
|
|
1325
|
+
const { state } = useStatus();
|
|
1326
|
+
if (!state || !state.loggedIn) return null;
|
|
1327
|
+
|
|
1328
|
+
const balance = state.profile ? Number(state.profile.balance) : NaN;
|
|
1329
|
+
const known = Number.isFinite(balance);
|
|
1330
|
+
const low = known && balance <= 0;
|
|
1331
|
+
const thin = known && balance > 0 && balance < 5;
|
|
1332
|
+
const dot = low ? C.bad : thin ? C.warn : C.ok;
|
|
1333
|
+
|
|
1334
|
+
const models = state.models?.models?.length ?? 0;
|
|
1335
|
+
const pendingRecharge = state.models?.needsRecharge === true;
|
|
1336
|
+
const detail = [
|
|
1337
|
+
known ? `余额 ${money(balance)}` : "余额未知",
|
|
1338
|
+
state.email ? `账号 ${state.email}` : null,
|
|
1339
|
+
models > 0 ? `可用模型 ${models} 个` : pendingRecharge ? "充值后可同步模型" : "尚未同步模型",
|
|
1340
|
+
low ? "点击去充值" : "点击刷新",
|
|
1341
|
+
].filter(Boolean).join("\n");
|
|
1342
|
+
|
|
1343
|
+
const onClick = async () => {
|
|
1344
|
+
if (low) {
|
|
1345
|
+
try {
|
|
1346
|
+
const link = await call("/recharge/link", { method: "POST", body: {} });
|
|
1347
|
+
window.open(link.url, "_blank", "noopener");
|
|
1348
|
+
return;
|
|
1349
|
+
} catch {
|
|
1350
|
+
// Fall through to a refresh so a failure is not silent forever.
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
statusStore.refresh();
|
|
1354
|
+
};
|
|
1355
|
+
|
|
1356
|
+
return h("button", {
|
|
1357
|
+
type: "button",
|
|
1358
|
+
title: detail,
|
|
1359
|
+
"aria-label": `Psapi ${known ? "余额 " + money(balance) : "余额未知"},点击${low ? "去充值" : "刷新"}`,
|
|
1360
|
+
onClick,
|
|
1361
|
+
style: {
|
|
1362
|
+
display: "inline-flex",
|
|
1363
|
+
alignItems: "center",
|
|
1364
|
+
gap: 6,
|
|
1365
|
+
height: 28,
|
|
1366
|
+
padding: "0 10px",
|
|
1367
|
+
borderRadius: 999,
|
|
1368
|
+
border: "0.5px solid " + C.line,
|
|
1369
|
+
background: "transparent",
|
|
1370
|
+
color: low ? C.bad : C.second,
|
|
1371
|
+
fontSize: 12,
|
|
1372
|
+
fontVariantNumeric: "tabular-nums",
|
|
1373
|
+
cursor: "pointer",
|
|
1374
|
+
whiteSpace: "nowrap",
|
|
1375
|
+
},
|
|
1376
|
+
},
|
|
1377
|
+
h("span", { style: { width: 6, height: 6, borderRadius: 999, background: dot, flex: "0 0 6px" } }),
|
|
1378
|
+
h("span", null, known ? money(balance) : "余额"),
|
|
1379
|
+
low ? h("span", { style: { color: C.bad } }, "去充值") : null,
|
|
1380
|
+
);
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
// --------------------------------------------------------- skeleton
|
|
1384
|
+
|
|
1385
|
+
/** Placeholder blocks while the first status read is in flight. */
|
|
1386
|
+
function Skeleton() {
|
|
1387
|
+
const bar = (width, height) =>
|
|
1388
|
+
h("div", {
|
|
1389
|
+
style: {
|
|
1390
|
+
width,
|
|
1391
|
+
height,
|
|
1392
|
+
borderRadius: 8,
|
|
1393
|
+
background: "color-mix(in srgb, " + C.muted + " 16%, transparent)",
|
|
1394
|
+
},
|
|
1395
|
+
});
|
|
1396
|
+
return h("div", { style: { padding: "4px 0 18px" } },
|
|
1397
|
+
h(Card, { title: "Psapi" },
|
|
1398
|
+
bar("40%", 14),
|
|
1399
|
+
h("div", { style: { height: 10 } }),
|
|
1400
|
+
bar("70%", 12),
|
|
1401
|
+
h("div", { style: { height: 10 } }),
|
|
1402
|
+
bar("55%", 12)),
|
|
1403
|
+
);
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1207
1406
|
// ---------------------------------------------------------- sidebar
|
|
1208
1407
|
|
|
1209
1408
|
function SidebarCard() {
|
|
@@ -1224,7 +1423,7 @@ window.__ModuleLoader__.load({
|
|
|
1224
1423
|
lineHeight: "18px",
|
|
1225
1424
|
},
|
|
1226
1425
|
},
|
|
1227
|
-
h("span", { style: { fontWeight: 600, color: C.text } }, "
|
|
1426
|
+
h("span", { style: { fontWeight: 600, color: C.text } }, "Psapi"),
|
|
1228
1427
|
h("span", { style: { marginLeft: "auto", color: low ? C.bad : C.muted } },
|
|
1229
1428
|
Number.isFinite(balance) ? (low ? "余额不足" : money(balance)) : "已登录"),
|
|
1230
1429
|
);
|
|
@@ -1238,7 +1437,7 @@ window.__ModuleLoader__.load({
|
|
|
1238
1437
|
try {
|
|
1239
1438
|
ctx.slots.inject("settings.section", () =>
|
|
1240
1439
|
ctx.slots.register(
|
|
1241
|
-
{ name: "settings.section", id: "sub2api", order: 300, label: () => "
|
|
1440
|
+
{ name: "settings.section", id: "sub2api", order: 300, label: () => "Psapi" },
|
|
1242
1441
|
Section,
|
|
1243
1442
|
),
|
|
1244
1443
|
);
|
|
@@ -1253,8 +1452,6 @@ window.__ModuleLoader__.load({
|
|
|
1253
1452
|
// The sidebar slot is optional.
|
|
1254
1453
|
}
|
|
1255
1454
|
try {
|
|
1256
|
-
// Bottom-right of the chat input: the balance a user checks before a
|
|
1257
|
-
// long request, always in view without opening settings.
|
|
1258
1455
|
ctx.slots.inject("conversation.input.right", () =>
|
|
1259
1456
|
ctx.slots.register(
|
|
1260
1457
|
{ name: "conversation.input.right", id: "sub2api-balance" },
|