@huaqiu/dsh-auth 0.6.21 → 0.6.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/client.js CHANGED
@@ -49,6 +49,12 @@ window.__ModuleLoader__.load({
49
49
  return null;
50
50
  }
51
51
  }
52
+ /** Accept only a finite `quotaRemainPercent`; anything else → unavailable. */
53
+ function normalizeUsage(value) {
54
+ if (value === null || typeof value !== "object") return null;
55
+ const percent = value.quotaRemainPercent;
56
+ return typeof percent === "number" && Number.isFinite(percent) ? { quotaRemainPercent: percent } : null;
57
+ }
52
58
  function createWebServerAuthTransport(base = "/api/v1/huaqiu/auth", doFetch = globalThis.fetch.bind(globalThis)) {
53
59
  return {
54
60
  async pushSession(info) {
@@ -76,17 +82,20 @@ window.__ModuleLoader__.load({
76
82
  });
77
83
  if (!res.ok) return {
78
84
  hostMode: false,
79
- subscriptionOrigin: null
85
+ subscriptionOrigin: null,
86
+ subscriptionApiBaseUrl: null
80
87
  };
81
88
  const body = await res.json();
82
89
  return {
83
90
  hostMode: body.hostMode === true,
84
- subscriptionOrigin: normalizeOrigin(body.subscriptionOrigin)
91
+ subscriptionOrigin: normalizeOrigin(body.subscriptionOrigin),
92
+ subscriptionApiBaseUrl: normalizeOrigin(body.subscriptionApiBaseUrl)
85
93
  };
86
94
  } catch {
87
95
  return {
88
96
  hostMode: false,
89
- subscriptionOrigin: null
97
+ subscriptionOrigin: null,
98
+ subscriptionApiBaseUrl: null
90
99
  };
91
100
  }
92
101
  },
@@ -132,7 +141,7 @@ window.__ModuleLoader__.load({
132
141
  headers: { accept: "application/json" }
133
142
  });
134
143
  if (!res.ok) return null;
135
- return (await res.json()).usage ?? null;
144
+ return normalizeUsage((await res.json()).usage);
136
145
  } catch {
137
146
  return null;
138
147
  }
@@ -186,18 +195,23 @@ window.__ModuleLoader__.load({
186
195
  };
187
196
  }
188
197
  /**
189
- * Fetch the active subscription quota DIRECTLY from eda.cn
190
- * (`GET {baseUrl}/sub-api/subscriptions/active?userId&productCode` with
191
- * `x-token` + `X-User-Id` headers) — the same API `hq-eda-ai`'s
192
- * `getActiveSubscription` and `hq-edge`'s ERC quota proxy call, so displaying
193
- * the available quota never depends on an hq-edge hop.
198
+ * Fetch the active subscription quota (`GET {baseUrl}/sub-api/subscriptions/
199
+ * active?userId&productCode` with `x-token` + `X-User-Id` headers) — the same
200
+ * API `hq-eda-ai`'s `getActiveSubscription` and `hq-edge`'s ERC quota proxy
201
+ * call.
202
+ *
203
+ * `baseUrl` is the origin the node half derives from the resolved
204
+ * subscription-balances endpoint (`HQ_AI_ENV`), so this call follows the
205
+ * selected environment and DSH Auth never hardcodes a production host. No
206
+ * configured origin → null (the UI shows「—」).
194
207
  *
195
208
  * Returns null when the fetch/unwrap fails (callers decide fail-open vs
196
209
  * fail-closed); a well-formed response always yields a `SubscriptionQuota`
197
210
  * (possibly with `hasSubscription: false`).
198
211
  */
199
212
  async function fetchSubscriptionQuota(options) {
200
- const { userId, token, productCode, baseUrl = "https://www.eda.cn" } = options;
213
+ const { userId, token, productCode, baseUrl } = options;
214
+ if (!baseUrl) return null;
201
215
  const fetchImpl = options.fetchImpl ?? fetch;
202
216
  const url = `${baseUrl}${SUB_API_PREFIX}/subscriptions/active?${new URLSearchParams({
203
217
  userId: String(userId),
@@ -615,7 +629,6 @@ window.__ModuleLoader__.load({
615
629
  "subscription.upgrade": "订阅 / 升级套餐",
616
630
  "subscription.online": "在线",
617
631
  "subscription.ai": "AI 用量",
618
- "subscription.aiUsageCount": "{available} / {total} 点",
619
632
  "subscription.aiUsagePercent": "{percent}%",
620
633
  "subscription.aiUsageUnknown": "暂无数据",
621
634
  "card.title": "华秋 EDA AI(eda.cn)登录",
@@ -654,7 +667,6 @@ window.__ModuleLoader__.load({
654
667
  "subscription.upgrade": "Subscribe / Upgrade plan",
655
668
  "subscription.online": "Online",
656
669
  "subscription.ai": "AI usage",
657
- "subscription.aiUsageCount": "{available} / {total} pts",
658
670
  "subscription.aiUsagePercent": "{percent}%",
659
671
  "subscription.aiUsageUnknown": "No data",
660
672
  "card.title": "Huaqiu EDA AI (eda.cn) login",
@@ -1045,6 +1057,11 @@ window.__ModuleLoader__.load({
1045
1057
  * node `/config` route. Null in standalone → lib's PROD fallback.
1046
1058
  */
1047
1059
  let subscriptionOrigin = null;
1060
+ /**
1061
+ * Origin of the resolved subscription-balances API (node `/config`). Null
1062
+ * when no environment is configured → GEN/ERC quota calls are skipped.
1063
+ */
1064
+ let subscriptionApiBaseUrl = null;
1048
1065
  const emit = (info) => {
1049
1066
  for (const listener of listeners) listener(info);
1050
1067
  };
@@ -1136,7 +1153,8 @@ window.__ModuleLoader__.load({
1136
1153
  return fetchSubscriptionQuota({
1137
1154
  userId: id,
1138
1155
  token,
1139
- productCode
1156
+ productCode,
1157
+ ...subscriptionApiBaseUrl ? { baseUrl: subscriptionApiBaseUrl } : {}
1140
1158
  });
1141
1159
  },
1142
1160
  getAiTokenUsage: async () => {
@@ -1218,9 +1236,11 @@ window.__ModuleLoader__.load({
1218
1236
  const runtime = await transport.fetchRuntimeConfig();
1219
1237
  mode = runtime.hostMode;
1220
1238
  subscriptionOrigin = runtime.subscriptionOrigin;
1239
+ subscriptionApiBaseUrl = runtime.subscriptionApiBaseUrl;
1221
1240
  } catch {
1222
1241
  mode = false;
1223
1242
  subscriptionOrigin = null;
1243
+ subscriptionApiBaseUrl = null;
1224
1244
  }
1225
1245
  hostMode = mode;
1226
1246
  hostSessionLoaded = false;
@@ -1257,6 +1277,7 @@ window.__ModuleLoader__.load({
1257
1277
  hostSession = null;
1258
1278
  hostSessionLoaded = false;
1259
1279
  subscriptionOrigin = null;
1280
+ subscriptionApiBaseUrl = null;
1260
1281
  }
1261
1282
  };
1262
1283
  }
@@ -1886,34 +1907,19 @@ window.__ModuleLoader__.load({
1886
1907
  });
1887
1908
  }
1888
1909
  /**
1889
- * AI token reading from `/user-rest/api/ai/usage`.
1890
- *
1891
- * The headline number is the AVAILABLE (remaining) balance, not the consumed
1892
- * one — the same convention as the GEN/ERC cards beside it, which display
1893
- * `currentQuota` (the REMAINING quota: hq-edge exposes it as
1894
- * `availableQuota`/"Available Quota", hq-eda-ai as `remainingQuota`). Showing
1895
- * `usedPoints` here made the AI card the only surface in the product that
1896
- * reported consumption as the balance: an account that burned 77 of its 100
1897
- * points read "77 / 100 pts" where the user expects "23 / 100 pts".
1910
+ * AI reading from the subscription-balances AI Points group.
1898
1911
  *
1899
- * The balance is reported as `usedPoints` + `availablePoints`, so the total is
1900
- * their sum — there is no separate `totalPoints` field to read. Percent keeps
1901
- * one decimal: a fresh account has well over 99% of its balance left, and
1902
- * rounding 99.6% up to "100%" would read as an exhausted account.
1912
+ * The percentage is the upstream `quotaRemainPercent` — the REMAINING share —
1913
+ * passed through unchanged: no local ratio and no re-rounding, so `99.68`
1914
+ * renders as `99.68%`. A missing / `null` / non-finite value is unavailable
1915
+ * (the card shows「—」); the bar clamps to 0–100 while the label does not.
1903
1916
  */
1904
1917
  function aiUsageReading(usage, t) {
1905
- if (!usage) return null;
1906
- const used = Math.max(0, usage.usedPoints);
1907
- const available = Math.max(0, usage.availablePoints);
1908
- const total = used + available;
1909
- const percent = total > 0 ? Math.min(100, Math.round(available / total * 1e3) / 10) : 0;
1918
+ const percent = usage?.quotaRemainPercent;
1919
+ if (typeof percent !== "number" || !Number.isFinite(percent)) return null;
1910
1920
  return {
1911
- value: t("subscription.aiUsagePercent", { percent: Math.round(percent) }),
1912
- title: t("subscription.aiUsageCount", {
1913
- available,
1914
- total
1915
- }),
1916
- percent,
1921
+ value: t("subscription.aiUsagePercent", { percent }),
1922
+ percent: Math.max(0, Math.min(100, percent)),
1917
1923
  ok: true
1918
1924
  };
1919
1925
  }