@huaqiu/dsh-auth 0.6.22 → 0.6.24
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 +209 -20
- package/lib/client.js.map +1 -1
- package/lib/index.d.mts +12 -0
- package/lib/index.mjs +8 -2
- package/package.json +2 -2
- package/src/client/client.ts +56 -14
- package/src/client/i18n.ts +10 -0
- package/src/client/index.tsx +15 -3
- package/src/client/lib.ts +18 -8
- package/src/client/transport.ts +13 -5
- package/src/client/ui/sidebar-action.tsx +8 -1
- package/src/client/ui/turn-error-cta.tsx +165 -0
- package/src/host.ts +7 -0
- package/src/routes.ts +3 -0
- package/src/service.ts +8 -0
package/lib/index.d.mts
CHANGED
|
@@ -46,6 +46,11 @@ interface HuaqiuAuthConfig {
|
|
|
46
46
|
* `HQ_AI_ENV`. Absent → the browser half keeps its standalone PROD default.
|
|
47
47
|
*/
|
|
48
48
|
subscriptionOrigin?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Full「Go to profile」URL resolved by HQ Edge from the SAME `HQ_AI_ENV`.
|
|
51
|
+
* Absent → the browser half keeps its standalone PROD default.
|
|
52
|
+
*/
|
|
53
|
+
profileUrl?: string;
|
|
49
54
|
}
|
|
50
55
|
//#endregion
|
|
51
56
|
//#region src/validation.d.ts
|
|
@@ -167,6 +172,11 @@ interface HuaqiuAuthService {
|
|
|
167
172
|
* or null when none was configured (standalone keeps its PROD default).
|
|
168
173
|
*/
|
|
169
174
|
readonly subscriptionOrigin: string | null;
|
|
175
|
+
/**
|
|
176
|
+
* Full「Go to profile」URL injected by HQ Edge (`config.profileUrl`), or null
|
|
177
|
+
* when none was configured — the browser half then keeps its PROD default.
|
|
178
|
+
*/
|
|
179
|
+
readonly profileUrl: string | null;
|
|
170
180
|
}
|
|
171
181
|
declare class InMemoryHuaqiuAuthService implements HuaqiuAuthService {
|
|
172
182
|
private current;
|
|
@@ -199,6 +209,8 @@ declare class InMemoryHuaqiuAuthService implements HuaqiuAuthService {
|
|
|
199
209
|
readonly subscriptionApiBaseUrl: string | null;
|
|
200
210
|
/** Subscription origin resolved by HQ Edge (`HQ_AI_ENV`); null → standalone. */
|
|
201
211
|
readonly subscriptionOrigin: string | null;
|
|
212
|
+
/**「Go to profile」URL resolved by HQ Edge (`HQ_AI_ENV`); null → standalone. */
|
|
213
|
+
readonly profileUrl: string | null;
|
|
202
214
|
readonly auth: HuaqiuAuthApi;
|
|
203
215
|
/**
|
|
204
216
|
* Spec §6.2 resolution order: host → pushed → persisted → null.
|
package/lib/index.mjs
CHANGED
|
@@ -55,6 +55,7 @@ function resolveHostConfig(config, env = process.env) {
|
|
|
55
55
|
}
|
|
56
56
|
const subscriptionBalancesUrl = (config?.subscriptionBalancesUrl ?? env.HQ_AI_SUBSCRIPTION_BALANCES_URL ?? "").trim();
|
|
57
57
|
const subscriptionOrigin = (config?.subscriptionOrigin ?? env.HQ_AI_SUBSCRIPTION_ORIGIN ?? "").trim().replace(/\/+$/, "");
|
|
58
|
+
const profileUrl = (config?.profileUrl ?? env.HQ_AI_PROFILE_URL ?? "").trim();
|
|
58
59
|
return {
|
|
59
60
|
hqEdgeBaseUrl: baseUrl,
|
|
60
61
|
hostAuthPath,
|
|
@@ -63,7 +64,8 @@ function resolveHostConfig(config, env = process.env) {
|
|
|
63
64
|
hostSessionTtlSeconds: ttl,
|
|
64
65
|
validationTtlSeconds,
|
|
65
66
|
subscriptionBalancesUrl,
|
|
66
|
-
subscriptionOrigin
|
|
67
|
+
subscriptionOrigin,
|
|
68
|
+
profileUrl
|
|
67
69
|
};
|
|
68
70
|
}
|
|
69
71
|
/**
|
|
@@ -551,6 +553,7 @@ var InMemoryHuaqiuAuthService = class {
|
|
|
551
553
|
this.subscriptionBalancesUrl = resolved.subscriptionBalancesUrl?.length ? resolved.subscriptionBalancesUrl : null;
|
|
552
554
|
this.subscriptionApiBaseUrl = this.subscriptionBalancesUrl ? originOf(this.subscriptionBalancesUrl) : null;
|
|
553
555
|
this.subscriptionOrigin = resolved.subscriptionOrigin?.length ? resolved.subscriptionOrigin : null;
|
|
556
|
+
this.profileUrl = resolved.profileUrl?.length ? resolved.profileUrl : null;
|
|
554
557
|
this.log.info("auth service started", {
|
|
555
558
|
hostMode: this.hostMode,
|
|
556
559
|
hostAuthPath: resolved.hostAuthPath,
|
|
@@ -567,6 +570,8 @@ var InMemoryHuaqiuAuthService = class {
|
|
|
567
570
|
subscriptionApiBaseUrl;
|
|
568
571
|
/** Subscription origin resolved by HQ Edge (`HQ_AI_ENV`); null → standalone. */
|
|
569
572
|
subscriptionOrigin;
|
|
573
|
+
/**「Go to profile」URL resolved by HQ Edge (`HQ_AI_ENV`); null → standalone. */
|
|
574
|
+
profileUrl;
|
|
570
575
|
auth = {
|
|
571
576
|
isAuthenticated: async () => {
|
|
572
577
|
if (await this.resolve() === null) return false;
|
|
@@ -973,7 +978,8 @@ function createAuthHandler(service) {
|
|
|
973
978
|
sendJson(res, 200, {
|
|
974
979
|
hostMode: service.hostMode,
|
|
975
980
|
subscriptionOrigin: service.subscriptionOrigin,
|
|
976
|
-
subscriptionApiBaseUrl: service.subscriptionApiBaseUrl
|
|
981
|
+
subscriptionApiBaseUrl: service.subscriptionApiBaseUrl,
|
|
982
|
+
profileUrl: service.profileUrl
|
|
977
983
|
});
|
|
978
984
|
return;
|
|
979
985
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huaqiu/dsh-auth",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.24",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./lib/index.mjs",
|
|
6
6
|
"types": "./lib/index.d.mts",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@deepseek-ai/dsh-home-paths": "^0.1.0-rc.0",
|
|
36
|
-
"@huaqiu/dsh-plugin-log": "0.6.
|
|
36
|
+
"@huaqiu/dsh-plugin-log": "0.6.24"
|
|
37
37
|
},
|
|
38
38
|
"files": [
|
|
39
39
|
"lib",
|
package/src/client/client.ts
CHANGED
|
@@ -33,8 +33,8 @@ export interface AuthClientDeps {
|
|
|
33
33
|
* `hqEdge` service). In host mode the subscription dialog must be opened by
|
|
34
34
|
* the EDA host through DialogService.OpenUrl (hqEdge.openUrl) — a browser
|
|
35
35
|
* iframe cannot be shown over the host UI. Absent/undefined = the host has
|
|
36
|
-
* no dialog service; `openSubscriptionDialog()` then
|
|
37
|
-
* degrades gracefully.
|
|
36
|
+
* no dialog service; `openSubscriptionDialog()` then resolves
|
|
37
|
+
* `host-unavailable` and the caller degrades gracefully.
|
|
38
38
|
*/
|
|
39
39
|
openHostDialog?: (url: string, options: {
|
|
40
40
|
title?: string
|
|
@@ -43,6 +43,15 @@ export interface AuthClientDeps {
|
|
|
43
43
|
}) => Promise<void>
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
/** Explicit result of {@link AuthClient['auth']['openSubscriptionDialog']}. */
|
|
47
|
+
export type SubscriptionDialogOutcome =
|
|
48
|
+
| { readonly status: 'opened' }
|
|
49
|
+
| { readonly status: 'already-open' }
|
|
50
|
+
/** No usable credential; the caller should offer login. */
|
|
51
|
+
| { readonly status: 'signed-out' }
|
|
52
|
+
/** Host mode with no dialog service wired (or nothing to open). */
|
|
53
|
+
| { readonly status: 'host-unavailable' }
|
|
54
|
+
|
|
46
55
|
export interface AuthClient {
|
|
47
56
|
auth: {
|
|
48
57
|
isAuthenticated(): boolean
|
|
@@ -73,6 +82,12 @@ export interface AuthClient {
|
|
|
73
82
|
* no credential / no configured origin.
|
|
74
83
|
*/
|
|
75
84
|
getSubscriptionQuota(productCode: string): Promise<SubscriptionQuota | null>
|
|
85
|
+
/**
|
|
86
|
+
*「Go to profile」URL resolved by HQ Edge from the same `HQ_AI_ENV` (node
|
|
87
|
+
* `/config`). Null in standalone → callers fall back to lib.ts's PROD
|
|
88
|
+
* default via {@link buildProfileUrl}.
|
|
89
|
+
*/
|
|
90
|
+
getProfileUrl(): string | null
|
|
76
91
|
/**
|
|
77
92
|
* AI token usage for the signed-in account. Resolved by the NODE half
|
|
78
93
|
* (`GET /api/v1/huaqiu/auth/ai-usage`) — the endpoint is served over plain
|
|
@@ -85,11 +100,14 @@ export interface AuthClient {
|
|
|
85
100
|
* Open the subscription landing page (`gen.eda.cn/subscription` with the
|
|
86
101
|
* token). Host mode: ask the EDA host to open its NATIVE dialog through
|
|
87
102
|
* DialogService.OpenUrl (preferred size 1400×740). Standalone DSH: open
|
|
88
|
-
* the in-app iframe dialog at the same preferred size.
|
|
89
|
-
*
|
|
90
|
-
*
|
|
103
|
+
* the in-app iframe dialog at the same preferred size.
|
|
104
|
+
*
|
|
105
|
+
* Returns an explicit outcome instead of silently no-op'ing: without a
|
|
106
|
+
* credential the caller may open login; without a host dialog service it
|
|
107
|
+
* shows its own fallback copy; when a standalone dialog is already open
|
|
108
|
+
* it is left untouched. Transport/dialog failures reject.
|
|
91
109
|
*/
|
|
92
|
-
openSubscriptionDialog(): Promise<
|
|
110
|
+
openSubscriptionDialog(): Promise<SubscriptionDialogOutcome>
|
|
93
111
|
onAuthStateChanged(listener: (info: AuthTokenPayload | null) => void): () => void
|
|
94
112
|
}
|
|
95
113
|
/** Route window 'message' events here. Exposed for direct testing. */
|
|
@@ -123,6 +141,12 @@ export function createAuthClient(deps: AuthClientDeps): AuthClient {
|
|
|
123
141
|
let hostMode = false
|
|
124
142
|
let hostSession: AuthTokenPayload | null = null
|
|
125
143
|
let hostSessionLoaded = false
|
|
144
|
+
/**
|
|
145
|
+
* Why the last host-session resolution failed, if it did. Transport errors
|
|
146
|
+
* must NOT masquerade as a healthy `signed-out`: the dialog opener surfaces
|
|
147
|
+
* them as a rejection so callers can show retry copy instead of "sign in".
|
|
148
|
+
*/
|
|
149
|
+
let hostResolveError: unknown
|
|
126
150
|
/**
|
|
127
151
|
* Subscription origin resolved by HQ Edge from `HQ_AI_ENV` and served on the
|
|
128
152
|
* node `/config` route. Null in standalone → lib's PROD fallback.
|
|
@@ -133,6 +157,11 @@ export function createAuthClient(deps: AuthClientDeps): AuthClient {
|
|
|
133
157
|
* when no environment is configured → GEN/ERC quota calls are skipped.
|
|
134
158
|
*/
|
|
135
159
|
let subscriptionApiBaseUrl: string | null = null
|
|
160
|
+
/**
|
|
161
|
+
*「Go to profile」URL resolved by HQ Edge from `HQ_AI_ENV`. Null in
|
|
162
|
+
* standalone → lib.ts keeps its PROD fallback.
|
|
163
|
+
*/
|
|
164
|
+
let profileUrl: string | null = null
|
|
136
165
|
|
|
137
166
|
const emit = (info: AuthTokenPayload | null): void => {
|
|
138
167
|
for (const listener of listeners) listener(info)
|
|
@@ -176,6 +205,7 @@ export function createAuthClient(deps: AuthClientDeps): AuthClient {
|
|
|
176
205
|
*/
|
|
177
206
|
const resolveHost = async (): Promise<void> => {
|
|
178
207
|
hostSessionLoaded = true
|
|
208
|
+
hostResolveError = undefined
|
|
179
209
|
if (!hostMode) {
|
|
180
210
|
hostSession = null
|
|
181
211
|
return
|
|
@@ -183,9 +213,12 @@ export function createAuthClient(deps: AuthClientDeps): AuthClient {
|
|
|
183
213
|
try {
|
|
184
214
|
const session = await transport.fetchSession()
|
|
185
215
|
hostSession = session.authenticated ? normalizeHostUserPayload(session.user) : null
|
|
186
|
-
} catch {
|
|
216
|
+
} catch (error) {
|
|
187
217
|
// Host unreachable: stay logged out rather than inventing a session.
|
|
218
|
+
// The error is retained for the dialog opener, which must not report it
|
|
219
|
+
// as a genuine signed-out state.
|
|
188
220
|
hostSession = null
|
|
221
|
+
hostResolveError = error
|
|
189
222
|
}
|
|
190
223
|
}
|
|
191
224
|
|
|
@@ -263,6 +296,7 @@ export function createAuthClient(deps: AuthClientDeps): AuthClient {
|
|
|
263
296
|
...(subscriptionApiBaseUrl ? { baseUrl: subscriptionApiBaseUrl } : {}),
|
|
264
297
|
})
|
|
265
298
|
},
|
|
299
|
+
getProfileUrl: (): string | null => profileUrl,
|
|
266
300
|
getAiTokenUsage: async (): Promise<AiUsage | null> => {
|
|
267
301
|
if (hostMode && !hostSessionLoaded) await resolveHost()
|
|
268
302
|
// Gate on a credential first: the node route resolves the token itself,
|
|
@@ -270,27 +304,32 @@ export function createAuthClient(deps: AuthClientDeps): AuthClient {
|
|
|
270
304
|
if (!(await auth.getAccessToken())) return null
|
|
271
305
|
return transport.fetchAiUsage()
|
|
272
306
|
},
|
|
273
|
-
openSubscriptionDialog: async (): Promise<
|
|
307
|
+
openSubscriptionDialog: async (): Promise<SubscriptionDialogOutcome> => {
|
|
274
308
|
if (hostMode && !hostSessionLoaded) await resolveHost()
|
|
275
309
|
const token = await auth.getAccessToken()
|
|
276
|
-
if (!token)
|
|
310
|
+
if (!token) {
|
|
311
|
+
// A failed host resolution is a transport failure, not "signed out".
|
|
312
|
+
if (hostMode && hostResolveError !== undefined) throw hostResolveError
|
|
313
|
+
return { status: 'signed-out' }
|
|
314
|
+
}
|
|
277
315
|
const url = buildSubscriptionUrl({
|
|
278
316
|
token,
|
|
279
317
|
...(subscriptionOrigin ? { origin: subscriptionOrigin } : {}),
|
|
280
318
|
})
|
|
281
319
|
if (hostMode) {
|
|
282
|
-
// The EDA host owns the UI — open its native dialog
|
|
283
|
-
//
|
|
284
|
-
if (!deps.openHostDialog) return
|
|
320
|
+
// The EDA host owns the UI — open its native dialog. No dialog service
|
|
321
|
+
// means the caller keeps its own affordance and shows fallback copy.
|
|
322
|
+
if (!deps.openHostDialog) return { status: 'host-unavailable' }
|
|
285
323
|
await deps.openHostDialog(url, {
|
|
286
324
|
title: 'Huaqiu EDA subscription',
|
|
287
325
|
size: { width: SUBSCRIPTION_PREFERRED_SIZE.width, height: SUBSCRIPTION_PREFERRED_SIZE.height },
|
|
288
326
|
key: 'hq-subscription-dialog',
|
|
289
327
|
})
|
|
290
|
-
return
|
|
328
|
+
return { status: 'opened' }
|
|
291
329
|
}
|
|
292
|
-
if (isSubscriptionDialogOpen()) return
|
|
330
|
+
if (isSubscriptionDialogOpen()) return { status: 'already-open' }
|
|
293
331
|
openSubscriptionDialog(url, { size: SUBSCRIPTION_PREFERRED_SIZE })
|
|
332
|
+
return { status: 'opened' }
|
|
294
333
|
},
|
|
295
334
|
onAuthStateChanged: (listener: (info: AuthTokenPayload | null) => void): (() => void) => {
|
|
296
335
|
listeners.add(listener)
|
|
@@ -358,10 +397,12 @@ export function createAuthClient(deps: AuthClientDeps): AuthClient {
|
|
|
358
397
|
mode = runtime.hostMode
|
|
359
398
|
subscriptionOrigin = runtime.subscriptionOrigin
|
|
360
399
|
subscriptionApiBaseUrl = runtime.subscriptionApiBaseUrl
|
|
400
|
+
profileUrl = runtime.profileUrl
|
|
361
401
|
} catch {
|
|
362
402
|
mode = false
|
|
363
403
|
subscriptionOrigin = null
|
|
364
404
|
subscriptionApiBaseUrl = null
|
|
405
|
+
profileUrl = null
|
|
365
406
|
}
|
|
366
407
|
hostMode = mode
|
|
367
408
|
hostSessionLoaded = false
|
|
@@ -401,6 +442,7 @@ export function createAuthClient(deps: AuthClientDeps): AuthClient {
|
|
|
401
442
|
hostSessionLoaded = false
|
|
402
443
|
subscriptionOrigin = null
|
|
403
444
|
subscriptionApiBaseUrl = null
|
|
445
|
+
profileUrl = null
|
|
404
446
|
void trustedOrigin // referenced for clarity: the auth iframe URL comes from it
|
|
405
447
|
},
|
|
406
448
|
}
|
package/src/client/i18n.ts
CHANGED
|
@@ -50,6 +50,11 @@ const zh = {
|
|
|
50
50
|
'subscription.ai': 'AI 用量',
|
|
51
51
|
'subscription.aiUsagePercent': '{percent}%',
|
|
52
52
|
'subscription.aiUsageUnknown': '暂无数据',
|
|
53
|
+
// Turn-error quota CTA (rendered inside the Chat turn-error card when the
|
|
54
|
+
// huaqiu gateway reports `insufficient_user_quota`).
|
|
55
|
+
'turnError.aiQuotaCta': '升级获取更多 AI 额度',
|
|
56
|
+
'turnError.aiQuotaSignIn': '登录后升级',
|
|
57
|
+
'turnError.aiQuotaOpenFailed': '无法打开订阅页面,请稍后重试。',
|
|
53
58
|
|
|
54
59
|
'card.title': '华秋 EDA AI(eda.cn)登录',
|
|
55
60
|
'card.desc': '工具「{tool}」需要登录华秋 EDA AI 账号才能继续。请在下方的登录框完成登录(或点击左侧「华秋EDA AI登录」按钮);登录完成后,回复助手「已登录,请重试」,助手会自动重新调用该工具。',
|
|
@@ -100,6 +105,11 @@ const en: Record<AuthCopyKey, string> = {
|
|
|
100
105
|
'subscription.ai': 'AI usage',
|
|
101
106
|
'subscription.aiUsagePercent': '{percent}%',
|
|
102
107
|
'subscription.aiUsageUnknown': 'No data',
|
|
108
|
+
// Turn-error quota CTA (rendered inside the Chat turn-error card when the
|
|
109
|
+
// huaqiu gateway reports `insufficient_user_quota`).
|
|
110
|
+
'turnError.aiQuotaCta': 'Upgrade for more AI tokens',
|
|
111
|
+
'turnError.aiQuotaSignIn': 'Sign in to upgrade',
|
|
112
|
+
'turnError.aiQuotaOpenFailed': 'Could not open the subscription page. Please try again later.',
|
|
103
113
|
|
|
104
114
|
'card.title': 'Huaqiu EDA AI (eda.cn) login',
|
|
105
115
|
// The reply phrase used to be hardcoded to the Chinese "已登录,请重试" even
|
package/src/client/index.tsx
CHANGED
|
@@ -21,6 +21,7 @@ import { createAuthClient, type AuthClient } from './client.js'
|
|
|
21
21
|
import { disposeAuth, registerAuth, registerAuthSync, registerHqEdgeGetter } from './auth-state.js'
|
|
22
22
|
import { HuaqiuToolView } from './ui/needs-auth-toolview.jsx'
|
|
23
23
|
import { HuaqiuAuthSidebarAction } from './ui/sidebar-action.jsx'
|
|
24
|
+
import { HuaqiuTurnErrorSubscribeAction } from './ui/turn-error-cta.jsx'
|
|
24
25
|
import { disposeUiEnv } from './ui-env.js'
|
|
25
26
|
|
|
26
27
|
/**
|
|
@@ -75,13 +76,17 @@ export function apply(ctx: ClientContext): () => void {
|
|
|
75
76
|
documentLike: document,
|
|
76
77
|
// Host mode subscription dialog: the EDA host owns the UI. Open its
|
|
77
78
|
// native window through the hqEdge service's DialogService.OpenUrl
|
|
78
|
-
// bridge
|
|
79
|
-
// subscribe button stays usable and reports the limitation.
|
|
79
|
+
// bridge, which acknowledges once the dialog is launched.
|
|
80
80
|
openHostDialog: async (url, options) => {
|
|
81
81
|
const hqEdge = ctx.get?.<{
|
|
82
82
|
openUrl?(url: string, opts?: Record<string, unknown>): Promise<unknown>
|
|
83
83
|
} | undefined>('hqEdge')
|
|
84
|
-
if (!hqEdge || typeof hqEdge.openUrl !== 'function')
|
|
84
|
+
if (!hqEdge || typeof hqEdge.openUrl !== 'function') {
|
|
85
|
+
// Without the EDA bridge no native dialog can ever appear. Reject so
|
|
86
|
+
// callers surface their fallback copy instead of reporting a launch
|
|
87
|
+
// that never happened.
|
|
88
|
+
throw new Error('hqEdge.openUrl is unavailable')
|
|
89
|
+
}
|
|
85
90
|
await hqEdge.openUrl(url, {
|
|
86
91
|
...(options.title ? { title: options.title } : {}),
|
|
87
92
|
...(options.size ? { size: options.size } : {}),
|
|
@@ -170,6 +175,13 @@ export function apply(ctx: ClientContext): () => void {
|
|
|
170
175
|
disposers.push(slots.inject('tool.call.toolview', () => slots.register({ name: 'tool.call.toolview', key: toolName }, HuaqiuToolView) as () => void))
|
|
171
176
|
}
|
|
172
177
|
disposers.push(slots.inject('sidebar.footer.action', () => slots.register({ name: 'sidebar.footer.action', id: 'huaqiu-auth' }, HuaqiuAuthSidebarAction) as () => void))
|
|
178
|
+
// The turn-error action slot only exists on harness versions that ship
|
|
179
|
+
// the seam; `inject` waits for its declaration, so an older harness
|
|
180
|
+
// simply never calls this and the shipped error card stays unchanged.
|
|
181
|
+
disposers.push(slots.inject('conversation.chat.turn-error.action', () => slots.register({
|
|
182
|
+
name: 'conversation.chat.turn-error.action',
|
|
183
|
+
id: 'huaqiu-turn-error-cta',
|
|
184
|
+
}, HuaqiuTurnErrorSubscribeAction) as () => void))
|
|
173
185
|
}
|
|
174
186
|
})
|
|
175
187
|
|
package/src/client/lib.ts
CHANGED
|
@@ -21,7 +21,11 @@
|
|
|
21
21
|
|
|
22
22
|
export const AUTH_ORIGIN = 'https://auth.eda.cn'
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
/** Standalone-DSH fallback for the「Go to profile」destination. Under HQ Edge the full URL is
|
|
25
|
+
* resolved ONCE from `HQ_AI_ENV` (PROD: `https://www.eda.cn/account/profile`,
|
|
26
|
+
* TEST: `http://www.fdatasheets.com/account`) and arrives via the node half's
|
|
27
|
+
* `/config` route; this constant is used only when nothing supplies one.
|
|
28
|
+
*/
|
|
25
29
|
export const PROFILE_URL = 'https://www.eda.cn/account/profile'
|
|
26
30
|
|
|
27
31
|
// ── Subscription (quota + upgrade dialog) ────────────────────────────────────
|
|
@@ -171,15 +175,21 @@ export async function fetchSubscriptionQuota(options: {
|
|
|
171
175
|
* in the query, mirroring `hq-eda-ai`'s `UserMenu`
|
|
172
176
|
* (`/account/profile?token=…&phone=…`).
|
|
173
177
|
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
178
|
+
* `profileUrl` comes from the node half (HQ Edge → `HQ_AI_ENV`); omitted in
|
|
179
|
+
* standalone DSH, which keeps the {@link PROFILE_URL} PROD fallback. The
|
|
180
|
+
* injected value is a FULL URL (path included), because PROD and TEST do not
|
|
181
|
+
* share it (`/account/profile` vs `/account`); any existing query string on it
|
|
182
|
+
* is preserved and `token`/`phone` are appended.
|
|
179
183
|
*/
|
|
180
|
-
export function buildProfileUrl(options: {
|
|
184
|
+
export function buildProfileUrl(options: {
|
|
185
|
+
token: string
|
|
186
|
+
phone?: string | number
|
|
187
|
+
profileUrl?: string
|
|
188
|
+
}): string {
|
|
181
189
|
const phone = options.phone === undefined || options.phone === null ? '' : String(options.phone)
|
|
182
|
-
|
|
190
|
+
const base = options.profileUrl?.length ? options.profileUrl : PROFILE_URL
|
|
191
|
+
const sep = base.includes('?') ? '&' : '?'
|
|
192
|
+
return `${base}${sep}token=${encodeURIComponent(options.token)}&phone=${encodeURIComponent(phone)}`
|
|
183
193
|
}
|
|
184
194
|
|
|
185
195
|
/**
|
package/src/client/transport.ts
CHANGED
|
@@ -16,7 +16,9 @@ export interface AuthTransport {
|
|
|
16
16
|
* - `subscriptionOrigin`: the SAME `HQ_AI_ENV`-resolved origin used for the
|
|
17
17
|
* subscription dialog (null → standalone PROD fallback);
|
|
18
18
|
* - `subscriptionApiBaseUrl`: origin of the resolved balances API, used for
|
|
19
|
-
* the GEN/ERC active-subscription calls (null → quota unavailable)
|
|
19
|
+
* the GEN/ERC active-subscription calls (null → quota unavailable);
|
|
20
|
+
* - `profileUrl`: full「Go to profile」URL from the same resolution (null →
|
|
21
|
+
* standalone keeps lib.ts's PROD default).
|
|
20
22
|
*/
|
|
21
23
|
fetchRuntimeConfig(): Promise<AuthRuntimeConfig>
|
|
22
24
|
/**
|
|
@@ -67,10 +69,12 @@ export interface AuthRuntimeConfig {
|
|
|
67
69
|
subscriptionOrigin: string | null
|
|
68
70
|
/** Origin for GEN/ERC quota calls; null = no environment configured. */
|
|
69
71
|
subscriptionApiBaseUrl: string | null
|
|
72
|
+
/** Full profile URL (path kept); null = standalone PROD fallback. */
|
|
73
|
+
profileUrl: string | null
|
|
70
74
|
}
|
|
71
75
|
|
|
72
|
-
/** Accept only an absolute http(s)
|
|
73
|
-
function
|
|
76
|
+
/** Accept only an absolute http(s) URL; anything else → null. A path is kept. */
|
|
77
|
+
function normalizeHttpUrl(value: unknown): string | null {
|
|
74
78
|
if (typeof value !== 'string') return null
|
|
75
79
|
const trimmed = value.trim().replace(/\/+$/, '')
|
|
76
80
|
if (!trimmed) return null
|
|
@@ -133,17 +137,20 @@ export function createWebServerAuthTransport(
|
|
|
133
137
|
hostMode: false,
|
|
134
138
|
subscriptionOrigin: null,
|
|
135
139
|
subscriptionApiBaseUrl: null,
|
|
140
|
+
profileUrl: null,
|
|
136
141
|
}
|
|
137
142
|
}
|
|
138
143
|
const body = await res.json() as {
|
|
139
144
|
hostMode?: unknown
|
|
140
145
|
subscriptionOrigin?: unknown
|
|
141
146
|
subscriptionApiBaseUrl?: unknown
|
|
147
|
+
profileUrl?: unknown
|
|
142
148
|
}
|
|
143
149
|
return {
|
|
144
150
|
hostMode: body.hostMode === true,
|
|
145
|
-
subscriptionOrigin:
|
|
146
|
-
subscriptionApiBaseUrl:
|
|
151
|
+
subscriptionOrigin: normalizeHttpUrl(body.subscriptionOrigin),
|
|
152
|
+
subscriptionApiBaseUrl: normalizeHttpUrl(body.subscriptionApiBaseUrl),
|
|
153
|
+
profileUrl: normalizeHttpUrl(body.profileUrl),
|
|
147
154
|
}
|
|
148
155
|
} catch {
|
|
149
156
|
// Offline/same-origin failure: fall back to standalone (show the login
|
|
@@ -153,6 +160,7 @@ export function createWebServerAuthTransport(
|
|
|
153
160
|
hostMode: false,
|
|
154
161
|
subscriptionOrigin: null,
|
|
155
162
|
subscriptionApiBaseUrl: null,
|
|
163
|
+
profileUrl: null,
|
|
156
164
|
}
|
|
157
165
|
}
|
|
158
166
|
},
|
|
@@ -859,7 +859,14 @@ export const HuaqiuAuthSidebarAction = memo(function HuaqiuAuthSidebarAction({ w
|
|
|
859
859
|
.then((i) => (i ? { token: i.token, phone: i.phone } : null))
|
|
860
860
|
.catch(() => null)
|
|
861
861
|
if (!info?.token) return
|
|
862
|
-
|
|
862
|
+
// `profileUrl` is the HQ Edge-resolved full URL (`HQ_AI_ENV`); null in
|
|
863
|
+
// standalone, where buildProfileUrl keeps its PROD fallback.
|
|
864
|
+
const profileUrl = auth.getProfileUrl()
|
|
865
|
+
window.open(
|
|
866
|
+
buildProfileUrl({ ...info, ...(profileUrl ? { profileUrl } : {}) }),
|
|
867
|
+
'_blank',
|
|
868
|
+
'noopener,noreferrer',
|
|
869
|
+
)
|
|
863
870
|
})()
|
|
864
871
|
}
|
|
865
872
|
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `conversation.chat.turn-error.action` entry: a subscription button inside
|
|
3
|
+
* the Chat terminal-failure card.
|
|
4
|
+
*
|
|
5
|
+
* The huaqiu gateway reports an exhausted token balance as a provider error
|
|
6
|
+
* whose own payload code is `insufficient_user_quota`. The harness keeps that
|
|
7
|
+
* code on the durable failure as `providerErrorCode` and attributes the
|
|
8
|
+
* failure to the routed `provider`; this entry gates on those structured
|
|
9
|
+
* fields only, so a wording change or an unrelated error simply renders no
|
|
10
|
+
* button. Clicking it opens the subscription dialog owned by this package's
|
|
11
|
+
* auth client (`openSubscriptionDialog`), which is the same path the sidebar
|
|
12
|
+
* upgrade entry uses.
|
|
13
|
+
*
|
|
14
|
+
* Fallbacks stay inside the card: no host dialog service → the existing
|
|
15
|
+
* "host provides no dialog service" copy; transport failure → retry copy; no
|
|
16
|
+
* credential → the auth client's login surface. The entry never throws, so a
|
|
17
|
+
* missing or older slot harness degrades to the shipped card.
|
|
18
|
+
*/
|
|
19
|
+
import { memo, useCallback, useState, useSyncExternalStore, type CSSProperties } from 'react'
|
|
20
|
+
import { getAuth, getAuthState, subscribeAuth } from '../auth-state.js'
|
|
21
|
+
import { useT } from '../i18n.js'
|
|
22
|
+
import { useIsDark, useLocale } from '../ui-env.js'
|
|
23
|
+
|
|
24
|
+
/** Structural view of the durable turn-error node this entry receives. */
|
|
25
|
+
export interface TurnErrorCtaNode {
|
|
26
|
+
readonly code?: string
|
|
27
|
+
readonly message: string
|
|
28
|
+
readonly provider?: string
|
|
29
|
+
readonly providerErrorCode?: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Gateway provider whose balance signature this entry understands. */
|
|
33
|
+
const QUOTA_PROVIDER = 'huaqiu'
|
|
34
|
+
/** The gateway's own code for a token balance that can be topped up. */
|
|
35
|
+
const HUAQIU_QUOTA_SIGNATURE = 'insufficient_user_quota'
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Whether a durable failure is the huaqiu gateway's exhausted token balance.
|
|
39
|
+
*
|
|
40
|
+
* Two structured shapes qualify:
|
|
41
|
+
* - the adapter extracted the gateway's own `insufficient_user_quota` code, or
|
|
42
|
+
* - the routed provider is huaqiu and the harness classified the failure as
|
|
43
|
+
* `QUOTA` (covers classifier fixes that stop carrying the payload code).
|
|
44
|
+
*
|
|
45
|
+
* Never gate on `message`: unrelated failures can quote provider text, and a
|
|
46
|
+
* payload/wording change must degrade to "no button", not to a false positive.
|
|
47
|
+
* @param node - durable turn-error node, possibly absent at runtime boundaries.
|
|
48
|
+
* @returns true only for the huaqiu token-quota signature.
|
|
49
|
+
*/
|
|
50
|
+
export function isHuaqiuQuotaFailure(node: TurnErrorCtaNode | null | undefined): boolean {
|
|
51
|
+
if (node === null || node === undefined) return false
|
|
52
|
+
if (node.code !== 'PI_AI_ERROR' && node.code !== 'QUOTA') return false
|
|
53
|
+
if (node.providerErrorCode === HUAQIU_QUOTA_SIGNATURE) return true
|
|
54
|
+
return node.provider === QUOTA_PROVIDER && node.code === 'QUOTA'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const ctaStyle: CSSProperties = {
|
|
58
|
+
display: 'block',
|
|
59
|
+
width: 'fit-content',
|
|
60
|
+
marginTop: 4,
|
|
61
|
+
padding: '1px 10px',
|
|
62
|
+
border: '1px solid var(--dsw-alias-brand-primary, #e62e2d)',
|
|
63
|
+
borderRadius: 999,
|
|
64
|
+
background: 'transparent',
|
|
65
|
+
color: 'var(--dsw-alias-brand-primary, #e62e2d)',
|
|
66
|
+
font: 'inherit',
|
|
67
|
+
fontSize: 13,
|
|
68
|
+
lineHeight: '20px',
|
|
69
|
+
cursor: 'pointer',
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const noticeStyle: CSSProperties = {
|
|
73
|
+
display: 'block',
|
|
74
|
+
marginTop: 2,
|
|
75
|
+
color: 'var(--dsw-alias-state-error-primary, #d4380d)',
|
|
76
|
+
fontSize: 12,
|
|
77
|
+
lineHeight: '18px',
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const stopStyle: CSSProperties = { borderRadius: 999, opacity: 0.6, cursor: 'default' }
|
|
81
|
+
|
|
82
|
+
/** Turn-error recovery affordance for the huaqiu token quota. */
|
|
83
|
+
export const HuaqiuTurnErrorSubscribeAction = memo(function HuaqiuTurnErrorSubscribeAction(
|
|
84
|
+
{ node }: { node: TurnErrorCtaNode },
|
|
85
|
+
) {
|
|
86
|
+
const auth = getAuth()
|
|
87
|
+
const authState = useSyncExternalStore(subscribeAuth, getAuthState, getAuthState)
|
|
88
|
+
const locale = useLocale()
|
|
89
|
+
const dark = useIsDark()
|
|
90
|
+
const t = useT()
|
|
91
|
+
const [busy, setBusy] = useState(false)
|
|
92
|
+
const [hovered, setHovered] = useState(false)
|
|
93
|
+
const [notice, setNotice] = useState<{ readonly key: string; readonly text: string } | null>(null)
|
|
94
|
+
|
|
95
|
+
// A projection may hand a fresh node object to the same mounted instance;
|
|
96
|
+
// scope the notice to the failure it describes so a later turn never inherits
|
|
97
|
+
// stale copy.
|
|
98
|
+
const nodeKey = node.message
|
|
99
|
+
const visibleNotice = notice !== null && notice.key === nodeKey ? notice.text : null
|
|
100
|
+
|
|
101
|
+
const open = useCallback(() => {
|
|
102
|
+
if (busy) return
|
|
103
|
+
const fail = (): void => {
|
|
104
|
+
setNotice({ key: nodeKey, text: t('turnError.aiQuotaOpenFailed') })
|
|
105
|
+
}
|
|
106
|
+
if (!auth) {
|
|
107
|
+
fail()
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
// Host mode's login blocks until its dialog completes, so it is never
|
|
111
|
+
// awaited here; `busy` belongs to the subscription request only.
|
|
112
|
+
const requestLogin = (): void => {
|
|
113
|
+
try {
|
|
114
|
+
void auth.login({ lang: locale, theme: dark ? 'dark' : 'light' }).catch(fail)
|
|
115
|
+
} catch {
|
|
116
|
+
fail()
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (!authState.authenticated) {
|
|
120
|
+
requestLogin()
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
setNotice(null)
|
|
124
|
+
setBusy(true)
|
|
125
|
+
void auth.openSubscriptionDialog()
|
|
126
|
+
.then((outcome) => {
|
|
127
|
+
if (outcome.status === 'host-unavailable') {
|
|
128
|
+
setNotice({ key: nodeKey, text: t('subscription.hostUnavailable') })
|
|
129
|
+
} else if (outcome.status === 'signed-out') {
|
|
130
|
+
requestLogin()
|
|
131
|
+
}
|
|
132
|
+
})
|
|
133
|
+
.catch(fail)
|
|
134
|
+
.finally(() => setBusy(false))
|
|
135
|
+
}, [auth, authState.authenticated, busy, dark, locale, nodeKey, t])
|
|
136
|
+
|
|
137
|
+
if (!isHuaqiuQuotaFailure(node)) return null
|
|
138
|
+
|
|
139
|
+
const label = busy
|
|
140
|
+
? t('subscription.subscribing')
|
|
141
|
+
: authState.authenticated
|
|
142
|
+
? t('turnError.aiQuotaCta')
|
|
143
|
+
: t('turnError.aiQuotaSignIn')
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<>
|
|
147
|
+
<button
|
|
148
|
+
type="button"
|
|
149
|
+
style={{
|
|
150
|
+
...ctaStyle,
|
|
151
|
+
...(hovered && !busy ? { background: 'var(--dsw-alias-interactive-bg-hover, rgba(230, 46, 45, 0.08))' } : {}),
|
|
152
|
+
...(busy ? stopStyle : {}),
|
|
153
|
+
}}
|
|
154
|
+
disabled={busy}
|
|
155
|
+
aria-busy={busy}
|
|
156
|
+
onClick={open}
|
|
157
|
+
onMouseEnter={() => setHovered(true)}
|
|
158
|
+
onMouseLeave={() => setHovered(false)}
|
|
159
|
+
>
|
|
160
|
+
{label}
|
|
161
|
+
</button>
|
|
162
|
+
{visibleNotice === null ? null : <span style={noticeStyle} aria-live="polite">{visibleNotice}</span>}
|
|
163
|
+
</>
|
|
164
|
+
)
|
|
165
|
+
})
|
package/src/host.ts
CHANGED
|
@@ -56,6 +56,11 @@ export interface HuaqiuAuthConfig {
|
|
|
56
56
|
* `HQ_AI_ENV`. Absent → the browser half keeps its standalone PROD default.
|
|
57
57
|
*/
|
|
58
58
|
subscriptionOrigin?: string
|
|
59
|
+
/**
|
|
60
|
+
* Full「Go to profile」URL resolved by HQ Edge from the SAME `HQ_AI_ENV`.
|
|
61
|
+
* Absent → the browser half keeps its standalone PROD default.
|
|
62
|
+
*/
|
|
63
|
+
profileUrl?: string
|
|
59
64
|
}
|
|
60
65
|
|
|
61
66
|
export const DEFAULT_HOST_AUTH_PATH = '/api/v1/auth/token'
|
|
@@ -112,6 +117,7 @@ export function resolveHostConfig(
|
|
|
112
117
|
const subscriptionOrigin = (config?.subscriptionOrigin ?? env.HQ_AI_SUBSCRIPTION_ORIGIN ?? '')
|
|
113
118
|
.trim()
|
|
114
119
|
.replace(/\/+$/, '')
|
|
120
|
+
const profileUrl = (config?.profileUrl ?? env.HQ_AI_PROFILE_URL ?? '').trim()
|
|
115
121
|
return {
|
|
116
122
|
hqEdgeBaseUrl: baseUrl,
|
|
117
123
|
hostAuthPath,
|
|
@@ -121,6 +127,7 @@ export function resolveHostConfig(
|
|
|
121
127
|
validationTtlSeconds,
|
|
122
128
|
subscriptionBalancesUrl,
|
|
123
129
|
subscriptionOrigin,
|
|
130
|
+
profileUrl,
|
|
124
131
|
}
|
|
125
132
|
}
|
|
126
133
|
|
package/src/routes.ts
CHANGED
|
@@ -240,6 +240,9 @@ export function createAuthHandler(service: HuaqiuAuthService): AuthHandler {
|
|
|
240
240
|
// Origin of the resolved balances API — lets the browser build the
|
|
241
241
|
// GEN/ERC active-subscription calls without hardcoding eda.cn.
|
|
242
242
|
subscriptionApiBaseUrl: service.subscriptionApiBaseUrl,
|
|
243
|
+
// Full profile URL (path included) resolved from the same HQ_AI_ENV;
|
|
244
|
+
// null in standalone → the browser keeps lib.ts's PROD default.
|
|
245
|
+
profileUrl: service.profileUrl,
|
|
243
246
|
})
|
|
244
247
|
return
|
|
245
248
|
}
|