Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/metal-adults-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@alauda/doom": patch
---

feat: add Authorization header support
1 change: 0 additions & 1 deletion packages/doom/src/cli/load-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ const getCommonConfig = async ({
server: {
open,
proxy: {
'/api/v1': 'https://cloud.alauda.cn',
'/smart/api': 'https://docs-dev.alauda.cn',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ export const LoginForm = ({ onSubmit, ...props }: LoginFormProps) => {
{CLOUD_AUTH_ORIGINS.map(({ name, value }) => (
<Radio
key={name}
label={name === 'local' ? 'Local' : t(`customer_portal_${name}`)}
label={t(`customer_portal_${name}`)}
value={value}
></Radio>
/>
))}
</RadioGroup>
</FormItem>
Expand Down
44 changes: 41 additions & 3 deletions packages/doom/src/global/Intelligence/AIAssistant/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { clsx } from 'clsx'
import { useRef, useState } from 'react'
import { useEffect, useRef, useState } from 'react'
import { Tooltip } from 'react-tooltip'
import { ApiMethod, xfetch } from 'x-fetch'
import {
ApiMethod,
interceptors,
ResponseError,
xfetch,
type ApiInterceptor,
} from 'x-fetch'

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

useEffect(() => {
const interceptor: ApiInterceptor = async (req, next) => {
if (!req.url.startsWith('/smart/')) {
return next(req)
}
if (!req.headers.has('Authorization')) {
req.headers.set('Authorization', `Bearer ${authInfo!.token}`)
}
if (!req.headers.has('CLOUD_AUTH_ORIGIN')) {
req.headers.set('CLOUD_AUTH_ORIGIN', authInfo!.origin)
}
try {
return await next(req)
} catch (err) {
if (
err instanceof ResponseError &&
// type-coverage:ignore-next-line -- no idea
err.response.status === 401
) {
onLogout()
}
throw err
}
}
interceptors.use(interceptor)
return () => {
interceptors.eject(interceptor)
}
}, [authInfo, onLogout])

const onClose = useMemoizedFn(() => {
onOpenChange(false)
})
Expand Down Expand Up @@ -93,7 +129,6 @@ export const AIAssistant = ({ open, onOpenChange }: AIAssistantProps) => {
'/smart/api/new_session',
{
method: ApiMethod.POST,
headers: { username: authInfo!.detail!.user.name },
},
)
sessionIdRef.current = session_id
Expand Down Expand Up @@ -152,6 +187,9 @@ export const AIAssistant = ({ open, onOpenChange }: AIAssistantProps) => {
try {
await onSend_(content)
} catch {
if (!sessionIdRef.current) {
return
}
flushMessages((messages) => {
const index = assistantMessageIndexRef.current
return [
Expand Down
9 changes: 0 additions & 9 deletions packages/doom/src/global/Intelligence/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isProduction } from '@rspress/shared'

import type { CloudAuthRegion } from './types.js'

export const CLOUD_AUTH_ORIGIN_KEY = '__CLOUD_AUTH_ORIGIN__'
Expand All @@ -16,13 +14,6 @@ const CLOUD_AUTH_ORIGINS: CloudAuthRegion[] = [
},
]

if (!isProduction()) {
CLOUD_AUTH_ORIGINS.unshift({
name: 'local',
value: '',
})
}

export const CLOUD_AUTH_ORIGIN_VALUES = CLOUD_AUTH_ORIGINS.map(
({ value }) => value,
)
Expand Down
3 changes: 2 additions & 1 deletion packages/doom/src/global/Intelligence/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getAuthInfoFromToken, setLocalStorage } from './utils.js'

export interface CloudAuth {
origin: string
token?: string
detail?: AuthInfo
}

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

return { origin, detail: getAuthInfoFromToken(token) }
return { origin, token, detail: getAuthInfoFromToken(token) }
}

export const CloudAuthProvider = ({ children }: { children: ReactNode }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/doom/src/global/Intelligence/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export interface AuthInfo {
}

export interface CloudAuthRegion {
name: 'global' | 'china' | 'local'
name: 'global' | 'china'
value: string
}
Loading