feat: add DeepChat deeplink support (#4668)
This commit is contained in:
+27
-19
@@ -1,30 +1,38 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { getApiKeys } from '@/features/keys/api'
|
||||
import { fetchTokenKey, getApiKeys } from '@/features/keys/api'
|
||||
import { API_KEY_STATUS } from '@/features/keys/constants'
|
||||
import { useAuthStore } from '@/stores/auth-store'
|
||||
|
||||
export async function fetchActiveChatKey() {
|
||||
const result = await getApiKeys({ p: 1, size: 50 })
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || 'Failed to load API keys')
|
||||
}
|
||||
|
||||
const items = result.data?.items ?? []
|
||||
const active = items.find((item) => item.status === API_KEY_STATUS.ENABLED)
|
||||
if (!active) {
|
||||
throw new Error('No enabled API keys found. Create or enable one first.')
|
||||
}
|
||||
|
||||
const keyResult = await fetchTokenKey(active.id)
|
||||
if (!keyResult.success || !keyResult.data?.key) {
|
||||
throw new Error(keyResult.message || 'Failed to load API key')
|
||||
}
|
||||
|
||||
return `sk-${keyResult.data.key}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the currently active API key for chat links
|
||||
*/
|
||||
export function useActiveChatKey(enabled: boolean) {
|
||||
const userId = useAuthStore((state) => state.auth.user?.id)
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['chat-active-key'],
|
||||
queryFn: async () => {
|
||||
const result = await getApiKeys({ p: 1, size: 50 })
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || 'Failed to load API keys')
|
||||
}
|
||||
const items = result.data?.items ?? []
|
||||
const active = items.find(
|
||||
(item) => item.status === API_KEY_STATUS.ENABLED
|
||||
)
|
||||
if (!active) {
|
||||
throw new Error(
|
||||
'No enabled API keys found. Create or enable one first.'
|
||||
)
|
||||
}
|
||||
return active.key
|
||||
},
|
||||
enabled,
|
||||
queryKey: ['chat-active-key', userId],
|
||||
queryFn: fetchActiveChatKey,
|
||||
enabled: enabled && Boolean(userId),
|
||||
staleTime: 5 * 60 * 1000,
|
||||
gcTime: 10 * 60 * 1000,
|
||||
})
|
||||
|
||||
+19
@@ -66,6 +66,15 @@ export function detectChatLinkType(url: string): ChatLinkType {
|
||||
return 'custom-protocol'
|
||||
}
|
||||
|
||||
export function chatLinkRequiresApiKey(url: string): boolean {
|
||||
return (
|
||||
url.includes('{key}') ||
|
||||
url.includes('{cherryConfig}') ||
|
||||
url.includes('{aionuiConfig}') ||
|
||||
url.includes('{deepchatConfig}')
|
||||
)
|
||||
}
|
||||
|
||||
export function parseChatConfig(raw: RawChatConfig): ChatPreset[] {
|
||||
let parsed: unknown = raw
|
||||
|
||||
@@ -146,6 +155,16 @@ export function resolveChatUrl({
|
||||
return replaceToken(url, '{aionuiConfig}', encoded)
|
||||
}
|
||||
|
||||
if (url.includes('{deepchatConfig}')) {
|
||||
const payload = {
|
||||
id: 'new-api',
|
||||
baseUrl: safeServerAddress,
|
||||
apiKey: safeApiKey,
|
||||
}
|
||||
const encoded = encodeURIComponent(toBase64(JSON.stringify(payload)))
|
||||
return replaceToken(url, '{deepchatConfig}', encoded)
|
||||
}
|
||||
|
||||
if (safeServerAddress) {
|
||||
const encodedAddress = encodeURIComponent(safeServerAddress)
|
||||
url = replaceToken(url, '{address}', encodedAddress)
|
||||
|
||||
Reference in New Issue
Block a user