dsh-sub2api 0.2.2 → 0.2.4
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 +18 -0
- package/README.md +4 -2
- package/lib/client.js +12 -3
- package/lib/constants.js +2 -2
- package/lib/index.js +17 -0
- package/lib/routes.js +49 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.4
|
|
4
|
+
|
|
5
|
+
- Sync the catalog by itself once the account is funded. A top-up normally
|
|
6
|
+
happens on the payment site, so the balance is the only signal available:
|
|
7
|
+
`/status` now re-syncs when it sees a positive balance with an empty catalog,
|
|
8
|
+
or a stale snapshot, and announces the change to the picker. The panel polls
|
|
9
|
+
status every 45 seconds while visible, so models appear on their own.
|
|
10
|
+
- Failures back off for two minutes, so a group that legitimately has no models
|
|
11
|
+
is not retried on every poll.
|
|
12
|
+
|
|
13
|
+
## 0.2.3
|
|
14
|
+
|
|
15
|
+
- Show the running build's version in the panel's 诊断 card. A fix that shipped
|
|
16
|
+
minutes ago and an install that never updated look identical in a screenshot;
|
|
17
|
+
now the version says which one it is.
|
|
18
|
+
- The 诊断 card and the diagnostic detail quote balances in ¥ like the rest of
|
|
19
|
+
the panel.
|
|
20
|
+
|
|
3
21
|
## 0.2.2
|
|
4
22
|
|
|
5
23
|
- Quote quota in yuan, which is what the site actually sells. The panel labelled
|
package/README.md
CHANGED
|
@@ -174,7 +174,7 @@ Agent 工具(只读):`sub2api_status`、`sub2api_models`。模型可以回
|
|
|
174
174
|
4. 多个组时优先 `platform` 为 `openai` 的组(中间层走 `chat/completions`,别的平台服务不了)
|
|
175
175
|
5. 否则取第一个非独占组
|
|
176
176
|
|
|
177
|
-
### 2. 余额为 0
|
|
177
|
+
### 2. 余额为 0 拿不到模型,到账后自动同步
|
|
178
178
|
|
|
179
179
|
Sub2API 的网关对 `/v1/models` 有余额门槛,零余额直接 `403 INSUFFICIENT_BALANCE`:
|
|
180
180
|
|
|
@@ -185,7 +185,9 @@ Sub2API 的网关对 `/v1/models` 有余额门槛,零余额直接 `403 INSUFFI
|
|
|
185
185
|
[404] GET /api/v1/models → 控制面没有模型接口
|
|
186
186
|
```
|
|
187
187
|
|
|
188
|
-
|
|
188
|
+
插件把它当作**待充值状态**而不是错误:面板显示「余额不足,暂时拿不到模型列表」+「去充值」。
|
|
189
|
+
|
|
190
|
+
**充值到账后不需要手动刷新**:面板每 45 秒静默读一次状态,宿主机发现余额从 0 变成正数且目录仍为空时,会自己重新拉一次模型并通知选择器。失败后有两分钟退避,避免空分组被反复重试。
|
|
189
191
|
|
|
190
192
|
> 运营建议:给新用户送一点初始余额,否则注册完第一屏必然是空的。
|
|
191
193
|
|
package/lib/client.js
CHANGED
|
@@ -828,10 +828,9 @@ window.__ModuleLoader__.load({
|
|
|
828
828
|
|
|
829
829
|
return h(Card, {
|
|
830
830
|
title: "诊断",
|
|
831
|
+
meta: state.version ? "v" + state.version : null,
|
|
831
832
|
aside: result
|
|
832
|
-
? h("span", {
|
|
833
|
-
style: { ...T.meta, marginLeft: "auto", color: result.ok ? C.ok : C.warn },
|
|
834
|
-
}, result.summary)
|
|
833
|
+
? h("span", { style: { ...T.meta, color: result.ok ? C.ok : C.warn } }, result.summary)
|
|
835
834
|
: null,
|
|
836
835
|
},
|
|
837
836
|
h("div", { style: T.row },
|
|
@@ -886,6 +885,16 @@ window.__ModuleLoader__.load({
|
|
|
886
885
|
|
|
887
886
|
useEffect(() => { refresh(); }, [refresh]);
|
|
888
887
|
|
|
888
|
+
// A top-up usually happens on the payment site, so the panel has to
|
|
889
|
+
// notice the new balance by itself: the host turns that into a model
|
|
890
|
+
// sync, and this poll is what makes it appear without a manual refresh.
|
|
891
|
+
useEffect(() => {
|
|
892
|
+
const timer = setInterval(() => {
|
|
893
|
+
if (document.visibilityState !== "hidden") refresh();
|
|
894
|
+
}, 45000);
|
|
895
|
+
return () => clearInterval(timer);
|
|
896
|
+
}, [refresh]);
|
|
897
|
+
|
|
889
898
|
useEffect(() => {
|
|
890
899
|
let cancelled = false;
|
|
891
900
|
call("/announcements")
|
package/lib/constants.js
CHANGED
|
@@ -48,8 +48,8 @@ export const KEY_PAGE_SIZE = 100;
|
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
50
|
* Currency of every amount this plugin shows. Sub2API quotes quota in yuan and
|
|
51
|
-
* treats one quota unit as one yuan ("1 额度 = 1 元"), so
|
|
52
|
-
*
|
|
51
|
+
* treats one quota unit as one yuan ("1 额度 = 1 元"), so the panel never uses a
|
|
52
|
+
* foreign-currency figure.
|
|
53
53
|
*/
|
|
54
54
|
export const CURRENCY_SYMBOL = "¥";
|
|
55
55
|
|
package/lib/index.js
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import { PROVIDER, PLUGIN_NAME, SETTINGS_NS } from "./constants.js";
|
|
13
|
+
import { readFileSync } from "node:fs";
|
|
13
14
|
import { resolveConfig } from "./config.js";
|
|
14
15
|
import { Sub2ApiAdapter } from "./adapter.js";
|
|
15
16
|
import { makeRoutes } from "./routes.js";
|
|
@@ -27,6 +28,21 @@ export const name = PLUGIN_NAME;
|
|
|
27
28
|
/** Services required before anything can mount. */
|
|
28
29
|
export const inject = ["llm"];
|
|
29
30
|
|
|
31
|
+
/**
|
|
32
|
+
* The running build's version, read from the package next to the module.
|
|
33
|
+
*
|
|
34
|
+
* The panel shows it, because "which version am I looking at" is otherwise
|
|
35
|
+
* unanswerable from a screenshot — and a fix that shipped ten minutes ago is
|
|
36
|
+
* indistinguishable from one that never arrived.
|
|
37
|
+
*/
|
|
38
|
+
export const VERSION = (() => {
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
41
|
+
} catch {
|
|
42
|
+
return "unknown";
|
|
43
|
+
}
|
|
44
|
+
})();
|
|
45
|
+
|
|
30
46
|
/** Order of the announcement section within the tool-guidance band. */
|
|
31
47
|
const SECTION_ORDER = 172;
|
|
32
48
|
|
|
@@ -159,6 +175,7 @@ export function apply(ctx, rawConfig = {}) {
|
|
|
159
175
|
...deps,
|
|
160
176
|
cfg,
|
|
161
177
|
panel,
|
|
178
|
+
version: VERSION,
|
|
162
179
|
announceCatalog,
|
|
163
180
|
hostname: () => "dsh",
|
|
164
181
|
refreshCatalog: async (key) => {
|
package/lib/routes.js
CHANGED
|
@@ -37,17 +37,61 @@ import {
|
|
|
37
37
|
* @returns {Array<{kind: "exact", path: string, handler: Function}>}
|
|
38
38
|
*/
|
|
39
39
|
export function makeRoutes(deps) {
|
|
40
|
-
const { api, session, catalog, cfg, logger, panel, announceCatalog } = deps;
|
|
40
|
+
const { api, session, catalog, cfg, logger, panel, announceCatalog, version } = deps;
|
|
41
41
|
|
|
42
42
|
/** Orders created by this process, keyed by the pay centre's order number. */
|
|
43
43
|
const orders = new Map();
|
|
44
44
|
|
|
45
|
+
/** When the last automatic catalog sync ran, so a poll cannot storm the gateway. */
|
|
46
|
+
let autoRefreshAt = 0;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Re-sync the catalog when the panel has a reason to believe it changed.
|
|
50
|
+
*
|
|
51
|
+
* A top-up is the usual trigger and it usually happens on another site, so
|
|
52
|
+
* nothing here can observe it directly: the balance is the only signal. Once
|
|
53
|
+
* it is positive and the catalog is still empty, the gate `/v1/models`
|
|
54
|
+
* enforces is open, and the picker should fill without the user pressing
|
|
55
|
+
* anything. A stale snapshot is refreshed on the same path.
|
|
56
|
+
* @param {number} balance reported account balance, NaN when unknown
|
|
57
|
+
*/
|
|
58
|
+
async function refreshCatalogIfNeeded(balance) {
|
|
59
|
+
const now = Date.now();
|
|
60
|
+
if (now - autoRefreshAt < 15000) return;
|
|
61
|
+
if (catalog.inflight !== undefined) return;
|
|
62
|
+
|
|
63
|
+
// Back off after a failure: a group with no models would otherwise be
|
|
64
|
+
// retried on every poll, and the answer will not change within seconds.
|
|
65
|
+
const failedAt = catalog.lastError?.at;
|
|
66
|
+
if (typeof failedAt === "number" && now - failedAt < 120000) return;
|
|
67
|
+
|
|
68
|
+
const empty = catalog.visible().length === 0;
|
|
69
|
+
const funded = Number.isFinite(balance) && balance > 0;
|
|
70
|
+
if (empty ? !funded : !catalog.stale) return;
|
|
71
|
+
|
|
72
|
+
autoRefreshAt = now;
|
|
73
|
+
try {
|
|
74
|
+
await catalog.refresh();
|
|
75
|
+
announceCatalog?.();
|
|
76
|
+
} catch {
|
|
77
|
+
// The failure is recorded on the catalog and shown by the panel.
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
45
81
|
/** Assemble the panel's view of the world. */
|
|
46
82
|
async function status() {
|
|
47
83
|
const stored = await session.read();
|
|
48
84
|
const apiKey = await session.apiKey();
|
|
49
85
|
await catalog.load();
|
|
50
86
|
|
|
87
|
+
// Balance first: it decides whether a model sync can succeed at all.
|
|
88
|
+
let profile;
|
|
89
|
+
if (stored !== undefined) {
|
|
90
|
+
const token = await session.accessToken().catch(() => undefined);
|
|
91
|
+
if (token !== undefined) profile = await safe(() => api.profile(token));
|
|
92
|
+
}
|
|
93
|
+
await refreshCatalogIfNeeded(Number(profile?.balance));
|
|
94
|
+
|
|
51
95
|
/** @type {Record<string, unknown>} */
|
|
52
96
|
const view = {
|
|
53
97
|
apiBase: cfg().apiBase,
|
|
@@ -62,21 +106,21 @@ export function makeRoutes(deps) {
|
|
|
62
106
|
panel: panel.snapshot(),
|
|
63
107
|
rechargeMultiplier: cfg().rechargeMultiplier,
|
|
64
108
|
currency: "CNY",
|
|
109
|
+
version: version ?? "unknown",
|
|
65
110
|
showSidebarQuota: cfg().showSidebarQuota,
|
|
66
111
|
};
|
|
67
112
|
|
|
68
113
|
if (stored !== undefined) {
|
|
69
114
|
const token = await session.accessToken().catch(() => undefined);
|
|
70
115
|
if (token !== undefined) {
|
|
71
|
-
const [
|
|
72
|
-
safe(() => api.profile(token)),
|
|
116
|
+
const [subscriptions, usage] = await Promise.all([
|
|
73
117
|
safe(() => api.subscriptionSummary(token)),
|
|
74
118
|
safe(() => api.usageDashboardStats(token)),
|
|
75
119
|
]);
|
|
76
|
-
view.profile = profile ?? null;
|
|
77
120
|
view.subscriptions = subscriptions ?? null;
|
|
78
121
|
view.usage = usage ?? null;
|
|
79
122
|
}
|
|
123
|
+
view.profile = profile ?? null;
|
|
80
124
|
}
|
|
81
125
|
return view;
|
|
82
126
|
}
|
|
@@ -289,7 +333,7 @@ export function makeRoutes(deps) {
|
|
|
289
333
|
"profile",
|
|
290
334
|
"账号资料",
|
|
291
335
|
profile !== undefined,
|
|
292
|
-
profile === undefined ? "读取失败" : `余额
|
|
336
|
+
profile === undefined ? "读取失败" : `余额 ¥${Number.isFinite(balance) ? balance.toFixed(2) : "?"}`,
|
|
293
337
|
);
|
|
294
338
|
add(
|
|
295
339
|
"balance",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-sub2api",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Sub2API provider for DeepSeek Harness: zero-friction signup, auto-provisioned API key, upstream model sync, balance and recharge",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "GPL-3.0-only",
|