-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
feat: add DeepChat deeplink support #4668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+9
to
+20
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify there are no remaining hardcoded string literals in throw-new-Error calls in this hook.
rg -n "throw new Error\\(['\"][^'\"]+['\"]\\)" web/default/src/features/chat/hooks/use-active-chat-key.tsRepository: QuantumNous/new-api Length of output: 144 🏁 Script executed: cat -n web/default/src/features/chat/hooks/use-active-chat-key.tsRepository: QuantumNous/new-api Length of output: 1564 🏁 Script executed: # Verify the file follows the expected pattern for non-React utility functions
head -5 web/default/src/features/chat/hooks/use-active-chat-key.ts | grep -E "(React|useTranslation|i18next)"Repository: QuantumNous/new-api Length of output: 45 Use Lines 9, 15, and 20 contain hardcoded error strings that violate the i18n requirement for Suggested refactor+import { t } from 'i18next'
import { useQuery } from '@tanstack/react-query'
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')
+ throw new Error(result.message || t('chat.errors.failedToLoadApiKeys'))
}
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.')
+ throw new Error(t('chat.errors.noEnabledApiKeys'))
}
const keyResult = await fetchTokenKey(active.id)
if (!keyResult.success || !keyResult.data?.key) {
- throw new Error(keyResult.message || 'Failed to load API key')
+ throw new Error(keyResult.message || t('chat.errors.failedToLoadApiKey'))
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enabled-key detection is limited to page 1.
Line 7 fetches only the first 50 keys, and Line 13 only searches that slice. If the first enabled key is on a later page, Line 15 throws a false “No enabled API keys found” error.
💡 Suggested fix (scan pages until an enabled key is found)
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) + const pageSize = 50 + let page = 1 + let active: { id: string | number } | undefined + + while (!active) { + const result = await getApiKeys({ p: page, size: pageSize }) + if (!result.success) { + throw new Error(result.message || 'Failed to load API keys') + } + + const items = result.data?.items ?? [] + active = items.find((item) => item.status === API_KEY_STATUS.ENABLED) + if (active || items.length < pageSize) break + page += 1 + } + if (!active) { throw new Error('No enabled API keys found. Create or enable one first.') }📝 Committable suggestion
🤖 Prompt for AI Agents