@huaqiu/dsh-auth 0.6.19 → 0.6.21
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 +181 -131
- package/lib/client.js.map +1 -1
- package/lib/index.d.mts +88 -1
- package/lib/index.mjs +167 -21
- package/package.json +2 -2
- package/src/apiKeySync.ts +157 -0
- package/src/client/client.ts +14 -2
- package/src/client/i18n.ts +10 -10
- package/src/client/lib.ts +21 -14
- package/src/client/transport.ts +41 -14
- package/src/client/ui/sidebar-action.tsx +138 -73
- package/src/host.ts +20 -0
- package/src/index.ts +53 -1
- package/src/routes.ts +17 -11
- package/src/service.ts +31 -0
package/src/routes.ts
CHANGED
|
@@ -85,11 +85,10 @@ export const AUTH_ROUTE_PREFIX = '/api/v1/huaqiu/auth'
|
|
|
85
85
|
* could not be loaded because the App Transport Security policy requires the
|
|
86
86
|
* use of a secure connection") and, independently, by CORS (the response sends
|
|
87
87
|
* no `Access-Control-Allow-Origin`). Node's fetch is subject to neither, and
|
|
88
|
-
* this process already talks to
|
|
89
|
-
*
|
|
90
|
-
* `
|
|
88
|
+
* this process already talks to the same AI host for the Huaqiu gateway — so
|
|
89
|
+
* the call is known to work from here. The endpoint is injected as resolved
|
|
90
|
+
* config by HQ Edge (`HQ_AI_ENV`); this module has no hardcoded env URL.
|
|
91
91
|
*/
|
|
92
|
-
export const AI_USAGE_URL = 'http://www.fdatasheets.com/user-rest/api/ai/usage'
|
|
93
92
|
|
|
94
93
|
/**
|
|
95
94
|
* Normalized AI token usage — the `/user-rest/api/ai/usage` result.
|
|
@@ -121,10 +120,11 @@ export interface AiTokenUsage {
|
|
|
121
120
|
*/
|
|
122
121
|
export async function fetchAiTokenUsage(
|
|
123
122
|
token: string,
|
|
123
|
+
usageUrl: string,
|
|
124
124
|
doFetch: typeof fetch = globalThis.fetch.bind(globalThis),
|
|
125
125
|
): Promise<AiTokenUsage | null> {
|
|
126
126
|
try {
|
|
127
|
-
const res = await doFetch(
|
|
127
|
+
const res = await doFetch(usageUrl, {
|
|
128
128
|
method: 'GET',
|
|
129
129
|
headers: {
|
|
130
130
|
Accept: 'application/json, text/plain, */*',
|
|
@@ -228,7 +228,13 @@ export function createAuthHandler(service: HuaqiuAuthService): AuthHandler {
|
|
|
228
228
|
}
|
|
229
229
|
|
|
230
230
|
if (req.method === 'GET' && pathname === `${AUTH_ROUTE_PREFIX}/config`) {
|
|
231
|
-
|
|
231
|
+
// `subscriptionOrigin` is the SAME resolved HQ_AI_ENV value as the
|
|
232
|
+
// usage endpoint; the browser half uses it to build the host-dialog
|
|
233
|
+
// URL instead of hardcoding gen.eda.cn.
|
|
234
|
+
sendJson(res, 200, {
|
|
235
|
+
hostMode: service.hostMode,
|
|
236
|
+
subscriptionOrigin: service.subscriptionOrigin,
|
|
237
|
+
})
|
|
232
238
|
return
|
|
233
239
|
}
|
|
234
240
|
|
|
@@ -248,15 +254,15 @@ export function createAuthHandler(service: HuaqiuAuthService): AuthHandler {
|
|
|
248
254
|
|
|
249
255
|
if (req.method === 'GET' && pathname === `${AUTH_ROUTE_PREFIX}/ai-usage`) {
|
|
250
256
|
// The WebView CANNOT reach this endpoint itself (plain http → ATS +
|
|
251
|
-
// CORS
|
|
252
|
-
//
|
|
253
|
-
//
|
|
257
|
+
// CORS), so the node half performs the call with the credential IT
|
|
258
|
+
// resolved (host → pushed → persisted) — the same shape as /user-info
|
|
259
|
+
// above. The URL itself is resolved by HQ Edge; absent → no call.
|
|
254
260
|
const token = await service.auth.getAccessToken()
|
|
255
|
-
if (!token) {
|
|
261
|
+
if (!token || !service.aiUsageUrl) {
|
|
256
262
|
sendJson(res, 200, { usage: null })
|
|
257
263
|
return
|
|
258
264
|
}
|
|
259
|
-
sendJson(res, 200, { usage: await fetchAiTokenUsage(token) })
|
|
265
|
+
sendJson(res, 200, { usage: await fetchAiTokenUsage(token, service.aiUsageUrl) })
|
|
260
266
|
return
|
|
261
267
|
}
|
|
262
268
|
|
package/src/service.ts
CHANGED
|
@@ -95,11 +95,31 @@ export interface HuaqiuAuthService {
|
|
|
95
95
|
* hq-edge, so the auth plugin's own login UI is suppressed.
|
|
96
96
|
*/
|
|
97
97
|
readonly hostMode: boolean
|
|
98
|
+
/**
|
|
99
|
+
* Resolved AI token-usage endpoint injected by HQ Edge (`config.aiUsageUrl`),
|
|
100
|
+
* or null when none was configured. `/ai-usage` never falls back to a stale
|
|
101
|
+
* hardcoded environment endpoint.
|
|
102
|
+
*/
|
|
103
|
+
readonly aiUsageUrl: string | null
|
|
104
|
+
/**
|
|
105
|
+
* Resolved subscription origin injected by HQ Edge (`config.subscriptionOrigin`),
|
|
106
|
+
* or null when none was configured (standalone keeps its PROD default).
|
|
107
|
+
*/
|
|
108
|
+
readonly subscriptionOrigin: string | null
|
|
98
109
|
}
|
|
99
110
|
|
|
100
111
|
const PERSIST_FILE = 'session.json'
|
|
101
112
|
const PERSIST_DIR = () => dshHomePath('auth')
|
|
102
113
|
|
|
114
|
+
/** Host-only label for diagnostics (never log a complete AI usage URL). */
|
|
115
|
+
function safeHost(url: string): string {
|
|
116
|
+
try {
|
|
117
|
+
return new URL(url).host
|
|
118
|
+
} catch {
|
|
119
|
+
return '<invalid-url>'
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
103
123
|
/**
|
|
104
124
|
* Bounded wait for the host to confirm a logout. Shorter than the login wait
|
|
105
125
|
* (which must cover a human completing the EDA dialog): logout is a host-side
|
|
@@ -202,15 +222,26 @@ export class InMemoryHuaqiuAuthService implements HuaqiuAuthService {
|
|
|
202
222
|
fetchImpl: this.doFetch,
|
|
203
223
|
})
|
|
204
224
|
this.hostMode = this.host.enabled
|
|
225
|
+
this.aiUsageUrl = resolved.aiUsageUrl?.length ? resolved.aiUsageUrl : null
|
|
226
|
+
this.subscriptionOrigin = resolved.subscriptionOrigin?.length
|
|
227
|
+
? resolved.subscriptionOrigin
|
|
228
|
+
: null
|
|
205
229
|
this.log.info('auth service started', {
|
|
206
230
|
hostMode: this.hostMode,
|
|
207
231
|
hostAuthPath: resolved.hostAuthPath,
|
|
208
232
|
hostSessionTtlSeconds: resolved.hostSessionTtlSeconds,
|
|
233
|
+
// Hosts only — never a complete URL that could carry a credential.
|
|
234
|
+
aiUsageHost: this.aiUsageUrl ? safeHost(this.aiUsageUrl) : null,
|
|
235
|
+
subscriptionOrigin: this.subscriptionOrigin,
|
|
209
236
|
})
|
|
210
237
|
}
|
|
211
238
|
|
|
212
239
|
/** Host mode is active iff a host base URL was configured (see HostSessionResolver.enabled). */
|
|
213
240
|
readonly hostMode: boolean
|
|
241
|
+
/** AI usage endpoint resolved by HQ Edge (`HQ_AI_ENV`); null → no call. */
|
|
242
|
+
readonly aiUsageUrl: string | null
|
|
243
|
+
/** Subscription origin resolved by HQ Edge (`HQ_AI_ENV`); null → standalone. */
|
|
244
|
+
readonly subscriptionOrigin: string | null
|
|
214
245
|
|
|
215
246
|
readonly auth: HuaqiuAuthApi = {
|
|
216
247
|
isAuthenticated: async () => {
|