Skip to content

Commit cce9a6a

Browse files
authored
feat: add Authorization header support (#170)
1 parent b5d07fd commit cce9a6a

File tree

7 files changed

+51
-17
lines changed

7 files changed

+51
-17
lines changed

.changeset/metal-adults-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@alauda/doom": patch
3+
---
4+
5+
feat: add Authorization header support

packages/doom/src/cli/load-config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ const getCommonConfig = async ({
344344
server: {
345345
open,
346346
proxy: {
347-
'/api/v1': 'https://cloud.alauda.cn',
348347
'/smart/api': 'https://docs-dev.alauda.cn',
349348
},
350349
},

packages/doom/src/global/Intelligence/AIAssistant/Preamble/LoginForm/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ export const LoginForm = ({ onSubmit, ...props }: LoginFormProps) => {
147147
{CLOUD_AUTH_ORIGINS.map(({ name, value }) => (
148148
<Radio
149149
key={name}
150-
label={name === 'local' ? 'Local' : t(`customer_portal_${name}`)}
150+
label={t(`customer_portal_${name}`)}
151151
value={value}
152-
></Radio>
152+
/>
153153
))}
154154
</RadioGroup>
155155
</FormItem>

packages/doom/src/global/Intelligence/AIAssistant/index.tsx

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { clsx } from 'clsx'
2-
import { useRef, useState } from 'react'
2+
import { useEffect, useRef, useState } from 'react'
33
import { Tooltip } from 'react-tooltip'
4-
import { ApiMethod, xfetch } from 'x-fetch'
4+
import {
5+
ApiMethod,
6+
interceptors,
7+
ResponseError,
8+
xfetch,
9+
type ApiInterceptor,
10+
} from 'x-fetch'
511

612
import { type CloudAuth, useCloudAuth } from '../context.tsx'
713
import type { AuthInfo } from '../types.ts'
@@ -54,6 +60,36 @@ export const AIAssistant = ({ open, onOpenChange }: AIAssistantProps) => {
5460
onNewChat()
5561
})
5662

63+
useEffect(() => {
64+
const interceptor: ApiInterceptor = async (req, next) => {
65+
if (!req.url.startsWith('/smart/')) {
66+
return next(req)
67+
}
68+
if (!req.headers.has('Authorization')) {
69+
req.headers.set('Authorization', `Bearer ${authInfo!.token}`)
70+
}
71+
if (!req.headers.has('CLOUD_AUTH_ORIGIN')) {
72+
req.headers.set('CLOUD_AUTH_ORIGIN', authInfo!.origin)
73+
}
74+
try {
75+
return await next(req)
76+
} catch (err) {
77+
if (
78+
err instanceof ResponseError &&
79+
// type-coverage:ignore-next-line -- no idea
80+
err.response.status === 401
81+
) {
82+
onLogout()
83+
}
84+
throw err
85+
}
86+
}
87+
interceptors.use(interceptor)
88+
return () => {
89+
interceptors.eject(interceptor)
90+
}
91+
}, [authInfo, onLogout])
92+
5793
const onClose = useMemoizedFn(() => {
5894
onOpenChange(false)
5995
})
@@ -93,7 +129,6 @@ export const AIAssistant = ({ open, onOpenChange }: AIAssistantProps) => {
93129
'/smart/api/new_session',
94130
{
95131
method: ApiMethod.POST,
96-
headers: { username: authInfo!.detail!.user.name },
97132
},
98133
)
99134
sessionIdRef.current = session_id
@@ -152,6 +187,9 @@ export const AIAssistant = ({ open, onOpenChange }: AIAssistantProps) => {
152187
try {
153188
await onSend_(content)
154189
} catch {
190+
if (!sessionIdRef.current) {
191+
return
192+
}
155193
flushMessages((messages) => {
156194
const index = assistantMessageIndexRef.current
157195
return [

packages/doom/src/global/Intelligence/constants.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { isProduction } from '@rspress/shared'
2-
31
import type { CloudAuthRegion } from './types.js'
42

53
export const CLOUD_AUTH_ORIGIN_KEY = '__CLOUD_AUTH_ORIGIN__'
@@ -16,13 +14,6 @@ const CLOUD_AUTH_ORIGINS: CloudAuthRegion[] = [
1614
},
1715
]
1816

19-
if (!isProduction()) {
20-
CLOUD_AUTH_ORIGINS.unshift({
21-
name: 'local',
22-
value: '',
23-
})
24-
}
25-
2617
export const CLOUD_AUTH_ORIGIN_VALUES = CLOUD_AUTH_ORIGINS.map(
2718
({ value }) => value,
2819
)

packages/doom/src/global/Intelligence/context.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getAuthInfoFromToken, setLocalStorage } from './utils.js'
66

77
export interface CloudAuth {
88
origin: string
9+
token?: string
910
detail?: AuthInfo
1011
}
1112

@@ -30,7 +31,7 @@ const getCloudAuth = (): CloudAuth | null => {
3031
return { origin }
3132
}
3233

33-
return { origin, detail: getAuthInfoFromToken(token) }
34+
return { origin, token, detail: getAuthInfoFromToken(token) }
3435
}
3536

3637
export const CloudAuthProvider = ({ children }: { children: ReactNode }) => {

packages/doom/src/global/Intelligence/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ export interface AuthInfo {
3434
}
3535

3636
export interface CloudAuthRegion {
37-
name: 'global' | 'china' | 'local'
37+
name: 'global' | 'china'
3838
value: string
3939
}

0 commit comments

Comments
 (0)