@huaqiu/dsh-auth 0.6.22 → 0.6.23

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
@@ -631,6 +631,9 @@ window.__ModuleLoader__.load({
631
631
  "subscription.ai": "AI 用量",
632
632
  "subscription.aiUsagePercent": "{percent}%",
633
633
  "subscription.aiUsageUnknown": "暂无数据",
634
+ "turnError.aiQuotaCta": "升级获取更多 AI 额度",
635
+ "turnError.aiQuotaSignIn": "登录后升级",
636
+ "turnError.aiQuotaOpenFailed": "无法打开订阅页面,请稍后重试。",
634
637
  "card.title": "华秋 EDA AI(eda.cn)登录",
635
638
  "card.desc": "工具「{tool}」需要登录华秋 EDA AI 账号才能继续。请在下方的登录框完成登录(或点击左侧「华秋EDA AI登录」按钮);登录完成后,回复助手「已登录,请重试」,助手会自动重新调用该工具。",
636
639
  "card.loggedIn": "✓ 已登录{nickname} —— 现在可以回复助手「已登录,请重试」,助手会重新调用工具。",
@@ -669,6 +672,9 @@ window.__ModuleLoader__.load({
669
672
  "subscription.ai": "AI usage",
670
673
  "subscription.aiUsagePercent": "{percent}%",
671
674
  "subscription.aiUsageUnknown": "No data",
675
+ "turnError.aiQuotaCta": "Upgrade for more AI tokens",
676
+ "turnError.aiQuotaSignIn": "Sign in to upgrade",
677
+ "turnError.aiQuotaOpenFailed": "Could not open the subscription page. Please try again later.",
672
678
  "card.title": "Huaqiu EDA AI (eda.cn) login",
673
679
  "card.desc": "Tool \"{tool}\" needs a Huaqiu EDA AI account. Complete the login below (or use the Huaqiu EDA AI button in the sidebar), then reply \"I have logged in, please retry\" so the assistant can retry the tool.",
674
680
  "card.loggedIn": "✓ Logged in{nickname} — reply \"I have logged in, please retry\" and the assistant will retry the tool.",
@@ -1053,6 +1059,12 @@ window.__ModuleLoader__.load({
1053
1059
  let hostSession = null;
1054
1060
  let hostSessionLoaded = false;
1055
1061
  /**
1062
+ * Why the last host-session resolution failed, if it did. Transport errors
1063
+ * must NOT masquerade as a healthy `signed-out`: the dialog opener surfaces
1064
+ * them as a rejection so callers can show retry copy instead of "sign in".
1065
+ */
1066
+ let hostResolveError;
1067
+ /**
1056
1068
  * Subscription origin resolved by HQ Edge from `HQ_AI_ENV` and served on the
1057
1069
  * node `/config` route. Null in standalone → lib's PROD fallback.
1058
1070
  */
@@ -1092,6 +1104,7 @@ window.__ModuleLoader__.load({
1092
1104
  */
1093
1105
  const resolveHost = async () => {
1094
1106
  hostSessionLoaded = true;
1107
+ hostResolveError = void 0;
1095
1108
  if (!hostMode) {
1096
1109
  hostSession = null;
1097
1110
  return;
@@ -1099,8 +1112,9 @@ window.__ModuleLoader__.load({
1099
1112
  try {
1100
1113
  const session = await transport.fetchSession();
1101
1114
  hostSession = session.authenticated ? normalizeHostUserPayload(session.user) : null;
1102
- } catch {
1115
+ } catch (error) {
1103
1116
  hostSession = null;
1117
+ hostResolveError = error;
1104
1118
  }
1105
1119
  };
1106
1120
  const auth = {
@@ -1165,13 +1179,16 @@ window.__ModuleLoader__.load({
1165
1179
  openSubscriptionDialog: async () => {
1166
1180
  if (hostMode && !hostSessionLoaded) await resolveHost();
1167
1181
  const token = await auth.getAccessToken();
1168
- if (!token) return;
1182
+ if (!token) {
1183
+ if (hostMode && hostResolveError !== void 0) throw hostResolveError;
1184
+ return { status: "signed-out" };
1185
+ }
1169
1186
  const url = buildSubscriptionUrl({
1170
1187
  token,
1171
1188
  ...subscriptionOrigin ? { origin: subscriptionOrigin } : {}
1172
1189
  });
1173
1190
  if (hostMode) {
1174
- if (!deps.openHostDialog) return;
1191
+ if (!deps.openHostDialog) return { status: "host-unavailable" };
1175
1192
  await deps.openHostDialog(url, {
1176
1193
  title: "Huaqiu EDA subscription",
1177
1194
  size: {
@@ -1180,10 +1197,11 @@ window.__ModuleLoader__.load({
1180
1197
  },
1181
1198
  key: "hq-subscription-dialog"
1182
1199
  });
1183
- return;
1200
+ return { status: "opened" };
1184
1201
  }
1185
- if (isSubscriptionDialogOpen()) return;
1202
+ if (isSubscriptionDialogOpen()) return { status: "already-open" };
1186
1203
  openSubscriptionDialog(url, { size: SUBSCRIPTION_PREFERRED_SIZE });
1204
+ return { status: "opened" };
1187
1205
  },
1188
1206
  onAuthStateChanged: (listener) => {
1189
1207
  listeners.add(listener);
@@ -2343,6 +2361,152 @@ window.__ModuleLoader__.load({
2343
2361
  });
2344
2362
  });
2345
2363
  //#endregion
2364
+ //#region src/client/ui/turn-error-cta.tsx
2365
+ /**
2366
+ * `conversation.chat.turn-error.action` entry: a subscription button inside
2367
+ * the Chat terminal-failure card.
2368
+ *
2369
+ * The huaqiu gateway reports an exhausted token balance as a provider error
2370
+ * whose own payload code is `insufficient_user_quota`. The harness keeps that
2371
+ * code on the durable failure as `providerErrorCode` and attributes the
2372
+ * failure to the routed `provider`; this entry gates on those structured
2373
+ * fields only, so a wording change or an unrelated error simply renders no
2374
+ * button. Clicking it opens the subscription dialog owned by this package's
2375
+ * auth client (`openSubscriptionDialog`), which is the same path the sidebar
2376
+ * upgrade entry uses.
2377
+ *
2378
+ * Fallbacks stay inside the card: no host dialog service → the existing
2379
+ * "host provides no dialog service" copy; transport failure → retry copy; no
2380
+ * credential → the auth client's login surface. The entry never throws, so a
2381
+ * missing or older slot harness degrades to the shipped card.
2382
+ */
2383
+ /** Gateway provider whose balance signature this entry understands. */
2384
+ const QUOTA_PROVIDER = "huaqiu";
2385
+ /** The gateway's own code for a token balance that can be topped up. */
2386
+ const HUAQIU_QUOTA_SIGNATURE = "insufficient_user_quota";
2387
+ /**
2388
+ * Whether a durable failure is the huaqiu gateway's exhausted token balance.
2389
+ *
2390
+ * Two structured shapes qualify:
2391
+ * - the adapter extracted the gateway's own `insufficient_user_quota` code, or
2392
+ * - the routed provider is huaqiu and the harness classified the failure as
2393
+ * `QUOTA` (covers classifier fixes that stop carrying the payload code).
2394
+ *
2395
+ * Never gate on `message`: unrelated failures can quote provider text, and a
2396
+ * payload/wording change must degrade to "no button", not to a false positive.
2397
+ * @param node - durable turn-error node, possibly absent at runtime boundaries.
2398
+ * @returns true only for the huaqiu token-quota signature.
2399
+ */
2400
+ function isHuaqiuQuotaFailure(node) {
2401
+ if (node === null || node === void 0) return false;
2402
+ if (node.code !== "PI_AI_ERROR" && node.code !== "QUOTA") return false;
2403
+ if (node.providerErrorCode === HUAQIU_QUOTA_SIGNATURE) return true;
2404
+ return node.provider === QUOTA_PROVIDER && node.code === "QUOTA";
2405
+ }
2406
+ const ctaStyle = {
2407
+ display: "block",
2408
+ width: "fit-content",
2409
+ marginTop: 4,
2410
+ padding: "1px 10px",
2411
+ border: "1px solid var(--dsw-alias-brand-primary, #e62e2d)",
2412
+ borderRadius: 999,
2413
+ background: "transparent",
2414
+ color: "var(--dsw-alias-brand-primary, #e62e2d)",
2415
+ font: "inherit",
2416
+ fontSize: 13,
2417
+ lineHeight: "20px",
2418
+ cursor: "pointer"
2419
+ };
2420
+ const noticeStyle = {
2421
+ display: "block",
2422
+ marginTop: 2,
2423
+ color: "var(--dsw-alias-state-error-primary, #d4380d)",
2424
+ fontSize: 12,
2425
+ lineHeight: "18px"
2426
+ };
2427
+ const stopStyle = {
2428
+ borderRadius: 999,
2429
+ opacity: .6,
2430
+ cursor: "default"
2431
+ };
2432
+ /** Turn-error recovery affordance for the huaqiu token quota. */
2433
+ const HuaqiuTurnErrorSubscribeAction = (0, react.memo)(function HuaqiuTurnErrorSubscribeAction({ node }) {
2434
+ const auth = getAuth();
2435
+ const authState = (0, react.useSyncExternalStore)(subscribeAuth, getAuthState, getAuthState);
2436
+ const locale = useLocale();
2437
+ const dark = useIsDark();
2438
+ const t = useT();
2439
+ const [busy, setBusy] = (0, react.useState)(false);
2440
+ const [hovered, setHovered] = (0, react.useState)(false);
2441
+ const [notice, setNotice] = (0, react.useState)(null);
2442
+ const nodeKey = node.message;
2443
+ const visibleNotice = notice !== null && notice.key === nodeKey ? notice.text : null;
2444
+ const open = (0, react.useCallback)(() => {
2445
+ if (busy) return;
2446
+ const fail = () => {
2447
+ setNotice({
2448
+ key: nodeKey,
2449
+ text: t("turnError.aiQuotaOpenFailed")
2450
+ });
2451
+ };
2452
+ if (!auth) {
2453
+ fail();
2454
+ return;
2455
+ }
2456
+ const requestLogin = () => {
2457
+ try {
2458
+ auth.login({
2459
+ lang: locale,
2460
+ theme: dark ? "dark" : "light"
2461
+ }).catch(fail);
2462
+ } catch {
2463
+ fail();
2464
+ }
2465
+ };
2466
+ if (!authState.authenticated) {
2467
+ requestLogin();
2468
+ return;
2469
+ }
2470
+ setNotice(null);
2471
+ setBusy(true);
2472
+ auth.openSubscriptionDialog().then((outcome) => {
2473
+ if (outcome.status === "host-unavailable") setNotice({
2474
+ key: nodeKey,
2475
+ text: t("subscription.hostUnavailable")
2476
+ });
2477
+ else if (outcome.status === "signed-out") requestLogin();
2478
+ }).catch(fail).finally(() => setBusy(false));
2479
+ }, [
2480
+ auth,
2481
+ authState.authenticated,
2482
+ busy,
2483
+ dark,
2484
+ locale,
2485
+ nodeKey,
2486
+ t
2487
+ ]);
2488
+ if (!isHuaqiuQuotaFailure(node)) return null;
2489
+ const label = busy ? t("subscription.subscribing") : authState.authenticated ? t("turnError.aiQuotaCta") : t("turnError.aiQuotaSignIn");
2490
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
2491
+ type: "button",
2492
+ style: {
2493
+ ...ctaStyle,
2494
+ ...hovered && !busy ? { background: "var(--dsw-alias-interactive-bg-hover, rgba(230, 46, 45, 0.08))" } : {},
2495
+ ...busy ? stopStyle : {}
2496
+ },
2497
+ disabled: busy,
2498
+ "aria-busy": busy,
2499
+ onClick: open,
2500
+ onMouseEnter: () => setHovered(true),
2501
+ onMouseLeave: () => setHovered(false),
2502
+ children: label
2503
+ }), visibleNotice === null ? null : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
2504
+ style: noticeStyle,
2505
+ "aria-live": "polite",
2506
+ children: visibleNotice
2507
+ })] });
2508
+ });
2509
+ //#endregion
2346
2510
  //#region src/client/index.tsx
2347
2511
  /**
2348
2512
  * `@huaqiu/dsh-auth` — browser half (the Phase 0A POC).
@@ -2400,7 +2564,7 @@ window.__ModuleLoader__.load({
2400
2564
  documentLike: document,
2401
2565
  openHostDialog: async (url, options) => {
2402
2566
  const hqEdge = ctx.get?.("hqEdge");
2403
- if (!hqEdge || typeof hqEdge.openUrl !== "function") return;
2567
+ if (!hqEdge || typeof hqEdge.openUrl !== "function") throw new Error("hqEdge.openUrl is unavailable");
2404
2568
  await hqEdge.openUrl(url, {
2405
2569
  ...options.title ? { title: options.title } : {},
2406
2570
  ...options.size ? { size: options.size } : {},
@@ -2470,6 +2634,10 @@ window.__ModuleLoader__.load({
2470
2634
  name: "sidebar.footer.action",
2471
2635
  id: "huaqiu-auth"
2472
2636
  }, HuaqiuAuthSidebarAction)));
2637
+ disposers.push(slots.inject("conversation.chat.turn-error.action", () => slots.register({
2638
+ name: "conversation.chat.turn-error.action",
2639
+ id: "huaqiu-turn-error-cta"
2640
+ }, HuaqiuTurnErrorSubscribeAction)));
2473
2641
  }
2474
2642
  });
2475
2643
  return () => {