dsh-sub2api 0.2.3 → 0.2.5

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 CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.5
4
+
5
+ - 0.2.4's failure back-off also swallowed the retry a top-up was supposed to
6
+ trigger: the balance rejection recorded seconds earlier counted as "recently
7
+ failed", so the catalog stayed empty until the two minutes elapsed. The
8
+ back-off now excludes `INSUFFICIENT_BALANCE`, because that answer changes the
9
+ moment money lands.
10
+
11
+ ## 0.2.4
12
+
13
+ - Sync the catalog by itself once the account is funded. A top-up normally
14
+ happens on the payment site, so the balance is the only signal available:
15
+ `/status` now re-syncs when it sees a positive balance with an empty catalog,
16
+ or a stale snapshot, and announces the change to the picker. The panel polls
17
+ status every 45 seconds while visible, so models appear on their own.
18
+ - Failures back off for two minutes, so a group that legitimately has no models
19
+ is not retried on every poll.
20
+
3
21
  ## 0.2.3
4
22
 
5
23
  - Show the running build's version in the panel's 诊断 card. A fix that shipped
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
@@ -885,6 +885,16 @@ window.__ModuleLoader__.load({
885
885
 
886
886
  useEffect(() => { refresh(); }, [refresh]);
887
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
+
888
898
  useEffect(() => {
889
899
  let cancelled = false;
890
900
  call("/announcements")
package/lib/routes.js CHANGED
@@ -42,12 +42,59 @@ export function makeRoutes(deps) {
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
+ // A balance rejection is excluded — funding the account changes the answer
66
+ // immediately, and that is exactly the case this path exists for.
67
+ const blockedByBalance = catalog.lastError?.code === "INSUFFICIENT_BALANCE";
68
+ const failedAt = catalog.lastError?.at;
69
+ if (!blockedByBalance && typeof failedAt === "number" && now - failedAt < 120000) return;
70
+
71
+ const empty = catalog.visible().length === 0;
72
+ const funded = Number.isFinite(balance) && balance > 0;
73
+ if (empty ? !funded : !catalog.stale) return;
74
+
75
+ autoRefreshAt = now;
76
+ try {
77
+ await catalog.refresh();
78
+ announceCatalog?.();
79
+ } catch {
80
+ // The failure is recorded on the catalog and shown by the panel.
81
+ }
82
+ }
83
+
45
84
  /** Assemble the panel's view of the world. */
46
85
  async function status() {
47
86
  const stored = await session.read();
48
87
  const apiKey = await session.apiKey();
49
88
  await catalog.load();
50
89
 
90
+ // Balance first: it decides whether a model sync can succeed at all.
91
+ let profile;
92
+ if (stored !== undefined) {
93
+ const token = await session.accessToken().catch(() => undefined);
94
+ if (token !== undefined) profile = await safe(() => api.profile(token));
95
+ }
96
+ await refreshCatalogIfNeeded(Number(profile?.balance));
97
+
51
98
  /** @type {Record<string, unknown>} */
52
99
  const view = {
53
100
  apiBase: cfg().apiBase,
@@ -69,15 +116,14 @@ export function makeRoutes(deps) {
69
116
  if (stored !== undefined) {
70
117
  const token = await session.accessToken().catch(() => undefined);
71
118
  if (token !== undefined) {
72
- const [profile, subscriptions, usage] = await Promise.all([
73
- safe(() => api.profile(token)),
119
+ const [subscriptions, usage] = await Promise.all([
74
120
  safe(() => api.subscriptionSummary(token)),
75
121
  safe(() => api.usageDashboardStats(token)),
76
122
  ]);
77
- view.profile = profile ?? null;
78
123
  view.subscriptions = subscriptions ?? null;
79
124
  view.usage = usage ?? null;
80
125
  }
126
+ view.profile = profile ?? null;
81
127
  }
82
128
  return view;
83
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-sub2api",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
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",