@huaqiu/dsh-auth 0.6.20 → 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 +199 -143
- package/lib/client.js.map +1 -1
- package/lib/index.d.mts +96 -1
- package/lib/index.mjs +295 -44
- package/package.json +2 -2
- package/src/apiKeySync.ts +157 -0
- package/src/client/client.ts +36 -10
- package/src/client/i18n.ts +8 -14
- package/src/client/lib.ts +37 -37
- package/src/client/transport.ts +73 -19
- package/src/client/ui/sidebar-action.tsx +148 -101
- package/src/host.ts +24 -0
- package/src/index.ts +53 -1
- package/src/routes.ts +89 -63
- package/src/service.ts +55 -0
- package/src/subscription-balances.ts +122 -0
|
@@ -17,16 +17,20 @@
|
|
|
17
17
|
* `UserMenu`, portalled to `document.body` with fixed positioning so the
|
|
18
18
|
* sidebar's `overflow: hidden` can never clip it.
|
|
19
19
|
*
|
|
20
|
-
* THEMING:
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
20
|
+
* THEMING: the LIGHT palette stays token-first (DSH `--dsw-alias-*` with an
|
|
21
|
+
* explicit fallback), so a custom host theme is honored. The DARK palette is
|
|
22
|
+
* deliberately EXPLICIT: it follows `docs/prototype/avatar_popup_redesign_prototype.html`,
|
|
23
|
+
* whose "before" is exactly what the host token produces — `--dsw-alias-bg-overlay`
|
|
24
|
+
* resolves to `rgb(97, 102, 107)` in dark mode, the muddy medium-gray surface
|
|
25
|
+
* the prototype calls out as the contrast defect. Keeping the token would
|
|
26
|
+
* silently re-introduce it, so the popup pins its own deep-slate color schema
|
|
27
|
+
* instead. `useIsDark()` still selects the pair, driven by the same
|
|
28
|
+
* `body[data-ds-dark-theme]` snapshot ui-layout's presenter writes.
|
|
25
29
|
*/
|
|
26
30
|
import { memo, useEffect, useLayoutEffect, useRef, useState, useSyncExternalStore, type CSSProperties } from 'react'
|
|
27
31
|
import { createPortal } from 'react-dom'
|
|
28
32
|
import { getAuth, getAuthState, getHqEdge, setProfile, subscribeAuth } from '../auth-state.js'
|
|
29
|
-
import { buildProfileUrl, ERC_PRODUCT_CODE, GEN_PRODUCT_CODE, type
|
|
33
|
+
import { buildProfileUrl, ERC_PRODUCT_CODE, GEN_PRODUCT_CODE, type AiUsage, type SubscriptionQuota } from '../lib.js'
|
|
30
34
|
import { useIsDark, useLocale } from '../ui-env.js'
|
|
31
35
|
import { useT, type Translate } from '../i18n.js'
|
|
32
36
|
import { HQ_ICON } from './hq-icon.jsx'
|
|
@@ -38,43 +42,68 @@ export interface SidebarFooterActionOwnerProps {
|
|
|
38
42
|
const AVATAR_SIZE = 26
|
|
39
43
|
const ICON_SIZE = 22
|
|
40
44
|
|
|
41
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* The popup's color schema. Every painted value comes from here, so the dark
|
|
47
|
+
* palette can be swapped as one coherent unit. The values are the concrete
|
|
48
|
+
* equivalents of the prototype's Tailwind classes (see `DARK_PALETTE`).
|
|
49
|
+
*/
|
|
42
50
|
interface Palette {
|
|
43
51
|
surface: string
|
|
52
|
+
/** Popup border + the 1px section dividers. */
|
|
44
53
|
border: string
|
|
45
54
|
text: string
|
|
55
|
+
/** Header name + metric headline (prototype `text-white` in dark). */
|
|
56
|
+
heading: string
|
|
46
57
|
muted: string
|
|
47
58
|
hover: string
|
|
48
59
|
danger: string
|
|
49
60
|
dangerHover: string
|
|
50
61
|
avatarBg: string
|
|
51
62
|
shadow: string
|
|
52
|
-
/** Profile-header card background
|
|
63
|
+
/** Profile-header card background + hairline. */
|
|
53
64
|
card: string
|
|
54
|
-
|
|
65
|
+
headerBorder: string
|
|
66
|
+
/** 1px hairline used on the quota metric card. */
|
|
55
67
|
cardBorder: string
|
|
56
|
-
/** Quota metric card background
|
|
68
|
+
/** Quota metric card background. */
|
|
57
69
|
metric: string
|
|
58
70
|
metricHover: string
|
|
59
|
-
/**
|
|
71
|
+
/** Plan badge: fill, label and hairline (prototype red-500/20 + red-400). */
|
|
72
|
+
badgeBg: string
|
|
73
|
+
badgeText: string
|
|
74
|
+
badgeBorder: string
|
|
75
|
+
/** "Online" pill: dot/text, translucent fill and hairline (emerald). */
|
|
76
|
+
online: string
|
|
77
|
+
onlineBg: string
|
|
78
|
+
onlineBorder: string
|
|
79
|
+
/** Huaqiu brand red — CTA gradient start + hover start. */
|
|
60
80
|
primary: string
|
|
61
81
|
primaryHover: string
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
-
|
|
82
|
+
/** CTA gradient end + hover end. */
|
|
83
|
+
ctaEnd: string
|
|
84
|
+
ctaHoverEnd: string
|
|
85
|
+
/** CTA glow. */
|
|
86
|
+
ctaShadow: string
|
|
87
|
+
/** Progress-bar track. */
|
|
65
88
|
track: string
|
|
66
|
-
/**
|
|
89
|
+
/** AI metric sparkles glyph. */
|
|
90
|
+
aiIcon: string
|
|
91
|
+
/** GEN / ERC / AI progress gradients. */
|
|
67
92
|
genGradient: string
|
|
68
93
|
ercGradient: string
|
|
69
94
|
aiGradient: string
|
|
70
|
-
/** "Online" dot green. */
|
|
71
|
-
online: string
|
|
72
95
|
}
|
|
73
96
|
|
|
74
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Light scheme keeps the DSH token-first convention (custom host themes win).
|
|
99
|
+
* The extra tokens introduced for the dark redesign are filled with the values
|
|
100
|
+
* that were previously hardcoded inline, so light rendering is unchanged.
|
|
101
|
+
*/
|
|
102
|
+
export const LIGHT_PALETTE: Palette = {
|
|
75
103
|
surface: 'var(--dsw-alias-bg-overlay, #ffffff)',
|
|
76
104
|
border: 'var(--dsw-alias-border-l1, #e4e7ec)',
|
|
77
105
|
text: 'var(--dsw-alias-label-primary, #3a4356)',
|
|
106
|
+
heading: 'var(--dsw-alias-label-primary, #3a4356)',
|
|
78
107
|
muted: 'var(--dsw-alias-label-secondary, #8a94a6)',
|
|
79
108
|
hover: 'var(--dsw-alias-interactive-bg-hover, #f5f7fa)',
|
|
80
109
|
danger: 'var(--dsw-alias-state-error-primary, #d4380d)',
|
|
@@ -82,41 +111,68 @@ const LIGHT_PALETTE: Palette = {
|
|
|
82
111
|
avatarBg: 'var(--dsw-alias-bg-layer-2, #eef2f7)',
|
|
83
112
|
shadow: '0 20px 40px -15px rgba(0, 0, 0, 0.12), 0 0 1px 1px rgba(0, 0, 0, 0.05)',
|
|
84
113
|
card: '#f7f8fa',
|
|
114
|
+
headerBorder: '#e8ebf0',
|
|
85
115
|
cardBorder: '#e8ebf0',
|
|
86
116
|
metric: '#f7f8fa',
|
|
87
117
|
metricHover: '#eef1f5',
|
|
118
|
+
badgeBg: 'rgba(230, 46, 45, 0.08)',
|
|
119
|
+
badgeText: '#e62e2d',
|
|
120
|
+
badgeBorder: 'rgba(230, 46, 45, 0.08)',
|
|
121
|
+
online: '#10b981',
|
|
122
|
+
onlineBg: 'rgba(16, 185, 129, 0.10)',
|
|
123
|
+
onlineBorder: 'rgba(16, 185, 129, 0.20)',
|
|
88
124
|
primary: '#e62e2d',
|
|
89
125
|
primaryHover: '#cc2423',
|
|
90
|
-
|
|
126
|
+
ctaEnd: '#e11d48',
|
|
127
|
+
ctaHoverEnd: '#be123c',
|
|
128
|
+
ctaShadow: '0 8px 20px rgba(230, 46, 45, 0.22)',
|
|
91
129
|
track: '#e4e7ec',
|
|
130
|
+
aiIcon: '#10b981',
|
|
92
131
|
genGradient: 'linear-gradient(90deg, #3b82f6, #6366f1)',
|
|
93
132
|
ercGradient: 'linear-gradient(90deg, #a855f7, #ec4899)',
|
|
94
133
|
aiGradient: 'linear-gradient(90deg, #10b981, #06b6d4)',
|
|
95
|
-
online: '#10b981',
|
|
96
134
|
}
|
|
97
135
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Dark color schema from `avatar_popup_redesign_prototype.html` (the "AFTER"
|
|
138
|
+
* card and the interactive popover's `dark:` classes). Explicit values, not
|
|
139
|
+
* `var(--dsw-alias-*, …)`: the host's dark `--dsw-alias-bg-overlay` is
|
|
140
|
+
* `rgb(97, 102, 107)`, the muddy surface the prototype replaces with a crisp
|
|
141
|
+
* deep slate. Alpha-tinted badges, the cyan→blue meter and the brighter brand
|
|
142
|
+
* red are the other three contrast fixes the prototype prescribes.
|
|
143
|
+
*/
|
|
144
|
+
export const DARK_PALETTE: Palette = {
|
|
145
|
+
surface: '#1C1E24', // dark:bg-[#1C1E24]
|
|
146
|
+
border: 'rgba(255, 255, 255, 0.10)', // dark:border-white/10, dividers
|
|
147
|
+
text: '#e2e8f0', // dark:text-slate-200
|
|
148
|
+
heading: '#ffffff', // dark:text-white
|
|
149
|
+
muted: '#94a3b8', // dark:text-slate-400
|
|
150
|
+
hover: 'rgba(255, 255, 255, 0.05)', // dark:hover:bg-white/5
|
|
151
|
+
danger: '#f87171', // dark:text-red-400
|
|
152
|
+
dangerHover: 'rgba(239, 68, 68, 0.10)', // dark:hover:bg-red-500/10
|
|
153
|
+
avatarBg: 'rgba(255, 255, 255, 0.10)',
|
|
154
|
+
shadow: '0 16px 36px -4px rgba(0, 0, 0, 0.6), 0 0 1px 1px rgba(255, 255, 255, 0.08)', // shadow-popover-dark
|
|
155
|
+
card: 'transparent', // flat header, no muddy card
|
|
156
|
+
headerBorder: 'transparent', // prototype has no header outline
|
|
157
|
+
cardBorder: 'rgba(255, 255, 255, 0.05)', // dark:border-white/5
|
|
158
|
+
metric: '#252831', // dark:bg-[#252831]
|
|
159
|
+
metricHover: '#2C303A', // subtle lift on hover
|
|
160
|
+
badgeBg: 'rgba(239, 68, 68, 0.20)', // dark:bg-red-500/20
|
|
161
|
+
badgeText: '#f87171', // dark:text-red-400
|
|
162
|
+
badgeBorder: 'rgba(239, 68, 68, 0.20)', // border-red-500/20
|
|
163
|
+
online: '#34d399', // emerald-400 dot + label
|
|
164
|
+
onlineBg: 'rgba(16, 185, 129, 0.10)', // bg-emerald-500/10
|
|
165
|
+
onlineBorder: 'rgba(16, 185, 129, 0.20)', // border-emerald-500/20
|
|
166
|
+
primary: '#FF4D4F', // eda.brand-dark
|
|
167
|
+
primaryHover: '#FF6B6D', // eda.brand-dark-hover
|
|
168
|
+
ctaEnd: '#ef4444', // dark:to-red-500
|
|
169
|
+
ctaHoverEnd: '#f87171', // brightness-110 equivalent
|
|
170
|
+
ctaShadow: '0 8px 20px rgba(255, 77, 79, 0.24)',
|
|
171
|
+
track: '#334155', // dark:bg-slate-700
|
|
172
|
+
aiIcon: '#22d3ee', // dark:text-cyan-400
|
|
116
173
|
genGradient: 'linear-gradient(90deg, #3b82f6, #6366f1)',
|
|
117
174
|
ercGradient: 'linear-gradient(90deg, #a855f7, #ec4899)',
|
|
118
|
-
aiGradient: 'linear-gradient(90deg, #
|
|
119
|
-
online: '#10b981',
|
|
175
|
+
aiGradient: 'linear-gradient(90deg, #06b6d4, #3b82f6)', // cyan-500 → blue-500
|
|
120
176
|
}
|
|
121
177
|
|
|
122
178
|
const TRIGGER_BASE: CSSProperties = {
|
|
@@ -208,7 +264,16 @@ function MenuItem({
|
|
|
208
264
|
onClick={onSelect}
|
|
209
265
|
>
|
|
210
266
|
<span style={{ display: 'flex', alignItems: 'center', gap: 10, minWidth: 0 }}>
|
|
211
|
-
|
|
267
|
+
<span
|
|
268
|
+
style={{
|
|
269
|
+
display: 'flex',
|
|
270
|
+
flex: '0 0 auto',
|
|
271
|
+
color: danger ? palette.danger : (hovered ? palette.text : palette.muted),
|
|
272
|
+
transition: 'color 0.15s ease',
|
|
273
|
+
}}
|
|
274
|
+
>
|
|
275
|
+
{icon}
|
|
276
|
+
</span>
|
|
212
277
|
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{label}</span>
|
|
213
278
|
</span>
|
|
214
279
|
{trailing ? (
|
|
@@ -353,7 +418,7 @@ function ChevronRightIcon(): React.JSX.Element {
|
|
|
353
418
|
}
|
|
354
419
|
|
|
355
420
|
/** AI usage metric glyph (lucide `sparkles`) — the token balance. */
|
|
356
|
-
function SparklesIcon(): React.JSX.Element {
|
|
421
|
+
function SparklesIcon({ color }: { color: string }): React.JSX.Element {
|
|
357
422
|
return (
|
|
358
423
|
<svg
|
|
359
424
|
xmlns="http://www.w3.org/2000/svg"
|
|
@@ -361,7 +426,7 @@ function SparklesIcon(): React.JSX.Element {
|
|
|
361
426
|
height={14}
|
|
362
427
|
viewBox="0 0 24 24"
|
|
363
428
|
fill="none"
|
|
364
|
-
stroke=
|
|
429
|
+
stroke={color}
|
|
365
430
|
strokeWidth={2}
|
|
366
431
|
strokeLinecap="round"
|
|
367
432
|
strokeLinejoin="round"
|
|
@@ -431,7 +496,7 @@ function QuotaMetric({
|
|
|
431
496
|
<span
|
|
432
497
|
style={{
|
|
433
498
|
fontWeight: 700,
|
|
434
|
-
color: reading?.ok ? palette.
|
|
499
|
+
color: reading?.ok ? palette.heading : palette.danger,
|
|
435
500
|
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
|
|
436
501
|
fontSize: 12,
|
|
437
502
|
whiteSpace: 'nowrap',
|
|
@@ -463,45 +528,37 @@ export function quotaReading(quota: SubscriptionQuota | null, t: Translate): Met
|
|
|
463
528
|
const percent = quota.hasSubscription && typeof total === 'number' && total > 0
|
|
464
529
|
? Math.max(0, Math.min(100, Math.round((used / total) * 100)))
|
|
465
530
|
: 0
|
|
531
|
+
const subscribed = quota.hasSubscription
|
|
466
532
|
return {
|
|
467
|
-
|
|
533
|
+
// Headline shows the usage percent (e.g. "65%"); the exact
|
|
534
|
+
// "{used} / {total}" count is preserved as the hover title.
|
|
535
|
+
value: subscribed
|
|
468
536
|
? (typeof total === 'number'
|
|
469
|
-
? t('subscription.
|
|
470
|
-
:
|
|
537
|
+
? t('subscription.quotaPercent', { percent })
|
|
538
|
+
: `${percent}%`)
|
|
471
539
|
: t('subscription.unsubscribed'),
|
|
540
|
+
title: subscribed && typeof total === 'number'
|
|
541
|
+
? t('subscription.quotaCount', { used, total })
|
|
542
|
+
: undefined,
|
|
472
543
|
percent,
|
|
473
|
-
ok:
|
|
544
|
+
ok: subscribed,
|
|
474
545
|
}
|
|
475
546
|
}
|
|
476
547
|
|
|
477
548
|
/**
|
|
478
|
-
* AI
|
|
549
|
+
* AI reading from the subscription-balances AI Points group.
|
|
479
550
|
*
|
|
480
|
-
* The
|
|
481
|
-
*
|
|
482
|
-
* `
|
|
483
|
-
*
|
|
484
|
-
* `usedPoints` here made the AI card the only surface in the product that
|
|
485
|
-
* reported consumption as the balance: an account that burned 77 of its 100
|
|
486
|
-
* points read "77 / 100 pts" where the user expects "23 / 100 pts".
|
|
487
|
-
*
|
|
488
|
-
* The balance is reported as `usedPoints` + `availablePoints`, so the total is
|
|
489
|
-
* their sum — there is no separate `totalPoints` field to read. Percent keeps
|
|
490
|
-
* one decimal: a fresh account has well over 99% of its balance left, and
|
|
491
|
-
* rounding 99.6% up to "100%" would read as an exhausted account.
|
|
551
|
+
* The percentage is the upstream `quotaRemainPercent` — the REMAINING share —
|
|
552
|
+
* passed through unchanged: no local ratio and no re-rounding, so `99.68`
|
|
553
|
+
* renders as `99.68%`. A missing / `null` / non-finite value is unavailable
|
|
554
|
+
* (the card shows「—」); the bar clamps to 0–100 while the label does not.
|
|
492
555
|
*/
|
|
493
|
-
export function aiUsageReading(usage:
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
const available = Math.max(0, usage.availablePoints)
|
|
497
|
-
const total = used + available
|
|
498
|
-
const percent = total > 0
|
|
499
|
-
? Math.min(100, Math.round((available / total) * 1000) / 10)
|
|
500
|
-
: 0
|
|
556
|
+
export function aiUsageReading(usage: AiUsage | null, t: Translate): MetricReading | null {
|
|
557
|
+
const percent = usage?.quotaRemainPercent
|
|
558
|
+
if (typeof percent !== 'number' || !Number.isFinite(percent)) return null
|
|
501
559
|
return {
|
|
502
|
-
value: t('subscription.
|
|
503
|
-
|
|
504
|
-
percent,
|
|
560
|
+
value: t('subscription.aiUsagePercent', { percent }),
|
|
561
|
+
percent: Math.max(0, Math.min(100, percent)),
|
|
505
562
|
ok: true,
|
|
506
563
|
}
|
|
507
564
|
}
|
|
@@ -530,7 +587,7 @@ function ProfileHeaderCard({
|
|
|
530
587
|
padding: 10,
|
|
531
588
|
borderRadius: 12,
|
|
532
589
|
background: palette.card,
|
|
533
|
-
border: `1px solid ${palette.
|
|
590
|
+
border: `1px solid ${palette.headerBorder}`,
|
|
534
591
|
display: 'flex',
|
|
535
592
|
alignItems: 'center',
|
|
536
593
|
justifyContent: 'space-between',
|
|
@@ -545,7 +602,7 @@ function ProfileHeaderCard({
|
|
|
545
602
|
style={{
|
|
546
603
|
fontSize: 14,
|
|
547
604
|
fontWeight: 700,
|
|
548
|
-
color: palette.
|
|
605
|
+
color: palette.heading,
|
|
549
606
|
overflow: 'hidden',
|
|
550
607
|
textOverflow: 'ellipsis',
|
|
551
608
|
whiteSpace: 'nowrap',
|
|
@@ -557,12 +614,12 @@ function ProfileHeaderCard({
|
|
|
557
614
|
<span
|
|
558
615
|
style={{
|
|
559
616
|
fontSize: 10,
|
|
560
|
-
background: palette.
|
|
561
|
-
color: palette.
|
|
617
|
+
background: palette.badgeBg,
|
|
618
|
+
color: palette.badgeText,
|
|
562
619
|
padding: '2px 6px',
|
|
563
620
|
borderRadius: 4,
|
|
564
621
|
fontWeight: 500,
|
|
565
|
-
border: `1px solid ${palette.
|
|
622
|
+
border: `1px solid ${palette.badgeBorder}`,
|
|
566
623
|
whiteSpace: 'nowrap',
|
|
567
624
|
flexShrink: 0,
|
|
568
625
|
}}
|
|
@@ -590,12 +647,12 @@ function ProfileHeaderCard({
|
|
|
590
647
|
<span
|
|
591
648
|
style={{
|
|
592
649
|
fontSize: 10,
|
|
593
|
-
background:
|
|
650
|
+
background: palette.onlineBg,
|
|
594
651
|
color: palette.online,
|
|
595
652
|
padding: '2px 8px',
|
|
596
653
|
borderRadius: 999,
|
|
597
654
|
fontWeight: 500,
|
|
598
|
-
border:
|
|
655
|
+
border: `1px solid ${palette.onlineBorder}`,
|
|
599
656
|
display: 'flex',
|
|
600
657
|
alignItems: 'center',
|
|
601
658
|
gap: 5,
|
|
@@ -630,11 +687,9 @@ function UpgradeButton({
|
|
|
630
687
|
padding: '9px 12px',
|
|
631
688
|
borderRadius: 12,
|
|
632
689
|
border: 'none',
|
|
633
|
-
background: subscribing
|
|
634
|
-
? `linear-gradient(90deg, ${palette.
|
|
635
|
-
:
|
|
636
|
-
? `linear-gradient(90deg, ${palette.primaryHover}, #be123c)`
|
|
637
|
-
: `linear-gradient(90deg, ${palette.primary}, #e11d48)`,
|
|
690
|
+
background: hovered && !subscribing
|
|
691
|
+
? `linear-gradient(90deg, ${palette.primaryHover}, ${palette.ctaHoverEnd})`
|
|
692
|
+
: `linear-gradient(90deg, ${palette.primary}, ${palette.ctaEnd})`,
|
|
638
693
|
color: '#ffffff',
|
|
639
694
|
fontSize: 12,
|
|
640
695
|
fontWeight: 600,
|
|
@@ -644,7 +699,7 @@ function UpgradeButton({
|
|
|
644
699
|
gap: 8,
|
|
645
700
|
cursor: subscribing ? 'default' : 'pointer',
|
|
646
701
|
opacity: subscribing ? 0.72 : 1,
|
|
647
|
-
boxShadow:
|
|
702
|
+
boxShadow: palette.ctaShadow,
|
|
648
703
|
transition: 'background 0.15s ease, opacity 0.15s ease',
|
|
649
704
|
}}
|
|
650
705
|
onMouseEnter={() => setHovered(true)}
|
|
@@ -667,7 +722,7 @@ export const HuaqiuAuthSidebarAction = memo(function HuaqiuAuthSidebarAction({ w
|
|
|
667
722
|
const [menuStyle, setMenuStyle] = useState<CSSProperties | null>(null)
|
|
668
723
|
const [avatarBroken, setAvatarBroken] = useState(false)
|
|
669
724
|
const [hovered, setHovered] = useState(false)
|
|
670
|
-
const [quota, setQuota] = useState<{ gen: SubscriptionQuota | null; erc: SubscriptionQuota | null; ai:
|
|
725
|
+
const [quota, setQuota] = useState<{ gen: SubscriptionQuota | null; erc: SubscriptionQuota | null; ai: AiUsage | null } | null>(null)
|
|
671
726
|
const [subscribing, setSubscribing] = useState(false)
|
|
672
727
|
const triggerRef = useRef<HTMLButtonElement | null>(null)
|
|
673
728
|
const menuRef = useRef<HTMLDivElement | null>(null)
|
|
@@ -724,10 +779,9 @@ export const HuaqiuAuthSidebarAction = memo(function HuaqiuAuthSidebarAction({ w
|
|
|
724
779
|
|
|
725
780
|
// Live quota: EVERY time the menu opens (and every time the auth state
|
|
726
781
|
// flips to authenticated) re-fetch the active subscription for GEN and ERC
|
|
727
|
-
//
|
|
728
|
-
//
|
|
729
|
-
//
|
|
730
|
-
// three staggered ones.
|
|
782
|
+
// from the environment's subscription API, and the AI percentage from the
|
|
783
|
+
// node-proxied subscription balances. All three share one loading window
|
|
784
|
+
// instead of three staggered ones.
|
|
731
785
|
useEffect(() => {
|
|
732
786
|
if (!menuOpen || !authenticated) {
|
|
733
787
|
if (!menuOpen) setQuota(null)
|
|
@@ -913,17 +967,10 @@ export const HuaqiuAuthSidebarAction = memo(function HuaqiuAuthSidebarAction({ w
|
|
|
913
967
|
</div>
|
|
914
968
|
<div role="presentation" style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
|
|
915
969
|
<QuotaMetric
|
|
916
|
-
label={t('subscription.
|
|
917
|
-
icon={<
|
|
918
|
-
reading={
|
|
919
|
-
gradient={palette.
|
|
920
|
-
palette={palette}
|
|
921
|
-
/>
|
|
922
|
-
<QuotaMetric
|
|
923
|
-
label={t('subscription.erc')}
|
|
924
|
-
icon={<WaveIcon />}
|
|
925
|
-
reading={quotaReading(quota?.erc ?? null, t)}
|
|
926
|
-
gradient={palette.ercGradient}
|
|
970
|
+
label={t('subscription.ai')}
|
|
971
|
+
icon={<SparklesIcon color={palette.aiIcon} />}
|
|
972
|
+
reading={aiUsageReading(quota?.ai ?? null, t)}
|
|
973
|
+
gradient={palette.aiGradient}
|
|
927
974
|
palette={palette}
|
|
928
975
|
/>
|
|
929
976
|
</div>
|
package/src/host.ts
CHANGED
|
@@ -45,6 +45,17 @@ export interface HuaqiuAuthConfig {
|
|
|
45
45
|
hostSessionTtlSeconds?: number
|
|
46
46
|
/** Seconds remote token-validation results are cached. Default 60. */
|
|
47
47
|
validationTtlSeconds?: number
|
|
48
|
+
/**
|
|
49
|
+
* Subscription balances endpoint resolved by HQ Edge from `HQ_AI_ENV`
|
|
50
|
+
* (`GET /sub-api/subscriptions/balances/current`). Absent → `/ai-usage`
|
|
51
|
+
* returns null instead of calling a stale/default endpoint.
|
|
52
|
+
*/
|
|
53
|
+
subscriptionBalancesUrl?: string
|
|
54
|
+
/**
|
|
55
|
+
* Subscription landing-page origin resolved by HQ Edge from the SAME
|
|
56
|
+
* `HQ_AI_ENV`. Absent → the browser half keeps its standalone PROD default.
|
|
57
|
+
*/
|
|
58
|
+
subscriptionOrigin?: string
|
|
48
59
|
}
|
|
49
60
|
|
|
50
61
|
export const DEFAULT_HOST_AUTH_PATH = '/api/v1/auth/token'
|
|
@@ -90,6 +101,17 @@ export function resolveHostConfig(
|
|
|
90
101
|
const parsed = Number.parseInt(vTtlRaw, 10)
|
|
91
102
|
if (Number.isFinite(parsed) && parsed > 0) validationTtlSeconds = parsed
|
|
92
103
|
}
|
|
104
|
+
// Resolved AI endpoints: overlay config (what HQ Edge injects) wins; explicit
|
|
105
|
+
// env URLs are the standalone escape hatch. HQ_AI_ENV itself is NOT read here
|
|
106
|
+
// — the prod/test mapping lives only in HQ Edge's central config.
|
|
107
|
+
const subscriptionBalancesUrl = (
|
|
108
|
+
config?.subscriptionBalancesUrl
|
|
109
|
+
?? env.HQ_AI_SUBSCRIPTION_BALANCES_URL
|
|
110
|
+
?? ''
|
|
111
|
+
).trim()
|
|
112
|
+
const subscriptionOrigin = (config?.subscriptionOrigin ?? env.HQ_AI_SUBSCRIPTION_ORIGIN ?? '')
|
|
113
|
+
.trim()
|
|
114
|
+
.replace(/\/+$/, '')
|
|
93
115
|
return {
|
|
94
116
|
hqEdgeBaseUrl: baseUrl,
|
|
95
117
|
hostAuthPath,
|
|
@@ -97,6 +119,8 @@ export function resolveHostConfig(
|
|
|
97
119
|
hostLogoutPath,
|
|
98
120
|
hostSessionTtlSeconds: ttl,
|
|
99
121
|
validationTtlSeconds,
|
|
122
|
+
subscriptionBalancesUrl,
|
|
123
|
+
subscriptionOrigin,
|
|
100
124
|
}
|
|
101
125
|
}
|
|
102
126
|
|
package/src/index.ts
CHANGED
|
@@ -5,9 +5,15 @@
|
|
|
5
5
|
* mounts the browser→node credential routes on `ctx.webServer`.
|
|
6
6
|
*/
|
|
7
7
|
import type { Context } from '@deepseek-ai/cordis'
|
|
8
|
+
import type { CredentialRef } from '@deepseek-ai/dsh-credentials'
|
|
8
9
|
import type {} from '@deepseek-ai/dsh-host-webserver'
|
|
9
10
|
import { getLogger } from '@huaqiu/dsh-plugin-log'
|
|
10
|
-
import
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_HOST_AUTH_PATH,
|
|
13
|
+
resolveHostConfig,
|
|
14
|
+
type HuaqiuAuthConfig,
|
|
15
|
+
} from './host.js'
|
|
16
|
+
import { HuaqiuApiKeySync, HUAQIU_API_KEY_REF } from './apiKeySync.js'
|
|
11
17
|
import { InMemoryHuaqiuAuthService, type HuaqiuAuthService } from './service.js'
|
|
12
18
|
import { AUTH_ROUTE_PREFIX, createAuthHandler } from './routes.js'
|
|
13
19
|
|
|
@@ -16,6 +22,12 @@ const log = getLogger('dsh-auth')
|
|
|
16
22
|
export type { HuaqiuAuthApi, HuaqiuAuthService, HuaqiuUserInfo } from './service.js'
|
|
17
23
|
export { InMemoryHuaqiuAuthService } from './service.js'
|
|
18
24
|
export { AUTH_ROUTE_PREFIX, createAuthHandler, type AuthHandler } from './routes.js'
|
|
25
|
+
export {
|
|
26
|
+
DEFAULT_HUAQIU_API_KEY_SYNC_MS,
|
|
27
|
+
HuaqiuApiKeySync,
|
|
28
|
+
HUAQIU_API_KEY_REF,
|
|
29
|
+
type CredentialStore,
|
|
30
|
+
} from './apiKeySync.js'
|
|
19
31
|
export type { HuaqiuAuthConfig } from './host.js'
|
|
20
32
|
|
|
21
33
|
export const name = '@huaqiu/dsh-auth'
|
|
@@ -55,5 +67,45 @@ export function apply(ctx: Context, config?: Partial<HuaqiuAuthConfig>): void {
|
|
|
55
67
|
handler: createAuthHandler(service),
|
|
56
68
|
}))
|
|
57
69
|
|
|
70
|
+
// Host mode: mirror the operator token into the DSH credential reference
|
|
71
|
+
// `HUAQIU_API_KEY` so the bundled `huaqiu` AI provider can resolve it per
|
|
72
|
+
// request (task huaqiu-ai-gateway §5). The sync re-fetches the loopback
|
|
73
|
+
// token endpoint on an immediate round plus a 5 s non-overlapping poll;
|
|
74
|
+
// it is host-scoped only — standalone DSH keeps its browser-login flow and
|
|
75
|
+
// the reference stays unset (missing credential → explicit
|
|
76
|
+
// MISSING_CREDENTIAL, never a fallback key).
|
|
77
|
+
if (service.hostMode) {
|
|
78
|
+
ctx.effect(() => {
|
|
79
|
+
const resolved = resolveHostConfig(config)
|
|
80
|
+
const sync = new HuaqiuApiKeySync(
|
|
81
|
+
resolved.hqEdgeBaseUrl ?? '',
|
|
82
|
+
resolved.hostAuthPath ?? DEFAULT_HOST_AUTH_PATH,
|
|
83
|
+
// Lazy per-round resolution: the `credentials` service may not be
|
|
84
|
+
// activated yet while this plugin boots (activation order). Direct
|
|
85
|
+
// `ctx.credentials` property access would throw "cannot get property
|
|
86
|
+
// ... without inject" in that window; `ctx.get('credentials')`
|
|
87
|
+
// returns undefined instead, so the round is skipped and the next
|
|
88
|
+
// poll retries once the service is live (self-healing, no restart).
|
|
89
|
+
() => {
|
|
90
|
+
const credentials = ctx.get('credentials') as
|
|
91
|
+
| { set(ref: string, value: string): Promise<void>; unset(ref: string): Promise<void> }
|
|
92
|
+
| undefined
|
|
93
|
+
if (credentials === undefined) return null
|
|
94
|
+
return {
|
|
95
|
+
set: (ref, value) => credentials.set(ref as CredentialRef, value),
|
|
96
|
+
unset: (ref) => credentials.unset(ref as CredentialRef),
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
debug: (msg, ctx2) => log.debug(msg, ctx2),
|
|
101
|
+
info: (msg, ctx2) => log.info(msg, ctx2),
|
|
102
|
+
warn: (msg, ctx2) => log.warn(msg, ctx2),
|
|
103
|
+
},
|
|
104
|
+
)
|
|
105
|
+
sync.start()
|
|
106
|
+
return () => sync.dispose()
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
58
110
|
log.info('dsh-auth node half ready', { routes: AUTH_ROUTE_PREFIX, hostMode: service.hostMode })
|
|
59
111
|
}
|