feat: performance optimization (#22810)

This commit is contained in:
Wu Tianwei
2025-07-23 16:04:46 +08:00
committed by GitHub
parent cf07189bd2
commit a366de26c4
30 changed files with 138 additions and 83 deletions

View File

@@ -83,28 +83,51 @@ const Panel: FC = () => {
const hasConfiguredTracing = !!(langSmithConfig || langFuseConfig || opikConfig || weaveConfig || arizeConfig || phoenixConfig || aliyunConfig) const hasConfiguredTracing = !!(langSmithConfig || langFuseConfig || opikConfig || weaveConfig || arizeConfig || phoenixConfig || aliyunConfig)
const fetchTracingConfig = async () => { const fetchTracingConfig = async () => {
const getArizeConfig = async () => {
const { tracing_config: arizeConfig, has_not_configured: arizeHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.arize }) const { tracing_config: arizeConfig, has_not_configured: arizeHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.arize })
if (!arizeHasNotConfig) if (!arizeHasNotConfig)
setArizeConfig(arizeConfig as ArizeConfig) setArizeConfig(arizeConfig as ArizeConfig)
}
const getPhoenixConfig = async () => {
const { tracing_config: phoenixConfig, has_not_configured: phoenixHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.phoenix }) const { tracing_config: phoenixConfig, has_not_configured: phoenixHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.phoenix })
if (!phoenixHasNotConfig) if (!phoenixHasNotConfig)
setPhoenixConfig(phoenixConfig as PhoenixConfig) setPhoenixConfig(phoenixConfig as PhoenixConfig)
}
const getLangSmithConfig = async () => {
const { tracing_config: langSmithConfig, has_not_configured: langSmithHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.langSmith }) const { tracing_config: langSmithConfig, has_not_configured: langSmithHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.langSmith })
if (!langSmithHasNotConfig) if (!langSmithHasNotConfig)
setLangSmithConfig(langSmithConfig as LangSmithConfig) setLangSmithConfig(langSmithConfig as LangSmithConfig)
}
const getLangFuseConfig = async () => {
const { tracing_config: langFuseConfig, has_not_configured: langFuseHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.langfuse }) const { tracing_config: langFuseConfig, has_not_configured: langFuseHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.langfuse })
if (!langFuseHasNotConfig) if (!langFuseHasNotConfig)
setLangFuseConfig(langFuseConfig as LangFuseConfig) setLangFuseConfig(langFuseConfig as LangFuseConfig)
}
const getOpikConfig = async () => {
const { tracing_config: opikConfig, has_not_configured: OpikHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.opik }) const { tracing_config: opikConfig, has_not_configured: OpikHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.opik })
if (!OpikHasNotConfig) if (!OpikHasNotConfig)
setOpikConfig(opikConfig as OpikConfig) setOpikConfig(opikConfig as OpikConfig)
}
const getWeaveConfig = async () => {
const { tracing_config: weaveConfig, has_not_configured: weaveHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.weave }) const { tracing_config: weaveConfig, has_not_configured: weaveHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.weave })
if (!weaveHasNotConfig) if (!weaveHasNotConfig)
setWeaveConfig(weaveConfig as WeaveConfig) setWeaveConfig(weaveConfig as WeaveConfig)
}
const getAliyunConfig = async () => {
const { tracing_config: aliyunConfig, has_not_configured: aliyunHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.aliyun }) const { tracing_config: aliyunConfig, has_not_configured: aliyunHasNotConfig } = await doFetchTracingConfig({ appId, provider: TracingProvider.aliyun })
if (!aliyunHasNotConfig) if (!aliyunHasNotConfig)
setAliyunConfig(aliyunConfig as AliyunConfig) setAliyunConfig(aliyunConfig as AliyunConfig)
} }
Promise.all([
getArizeConfig(),
getPhoenixConfig(),
getLangSmithConfig(),
getLangFuseConfig(),
getOpikConfig(),
getWeaveConfig(),
getAliyunConfig(),
])
}
const handleTracingConfigUpdated = async (provider: TracingProvider) => { const handleTracingConfigUpdated = async (provider: TracingProvider) => {
// call api to hide secret key value // call api to hide secret key value
@@ -155,7 +178,6 @@ const Panel: FC = () => {
await fetchTracingConfig() await fetchTracingConfig()
setLoaded() setLoaded()
})() })()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []) }, [])
const [controlShowPopup, setControlShowPopup] = useState<number>(0) const [controlShowPopup, setControlShowPopup] = useState<number>(0)

View File

@@ -1,10 +1,13 @@
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React, { useEffect } from 'react' import React, { useEffect, useState } from 'react'
import I18NContext from '@/context/i18n' import I18NContext from '@/context/i18n'
import type { Locale } from '@/i18n' import type { Locale } from '@/i18n'
import { setLocaleOnClient } from '@/i18n' import { setLocaleOnClient } from '@/i18n'
import Loading from './base/loading'
import { usePrefetchQuery } from '@tanstack/react-query'
import { getSystemFeatures } from '@/service/common'
export type II18nProps = { export type II18nProps = {
locale: Locale locale: Locale
@@ -14,10 +17,22 @@ const I18n: FC<II18nProps> = ({
locale, locale,
children, children,
}) => { }) => {
const [loading, setLoading] = useState(true)
usePrefetchQuery({
queryKey: ['systemFeatures'],
queryFn: getSystemFeatures,
})
useEffect(() => { useEffect(() => {
setLocaleOnClient(locale, false) setLocaleOnClient(locale, false).then(() => {
setLoading(false)
})
}, [locale]) }, [locale])
if (loading)
return <div className='flex h-screen w-screen items-center justify-center'><Loading type='app' /></div>
return ( return (
<I18NContext.Provider value={{ <I18NContext.Provider value={{
locale, locale,

View File

@@ -4,8 +4,8 @@ import Link from 'next/link'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import { RiAlertFill } from '@remixicon/react' import { RiAlertFill } from '@remixicon/react'
import { Trans } from 'react-i18next' import { Trans } from 'react-i18next'
import { snakeCase2CamelCase } from '@/utils/format'
import { useMixedTranslation } from '../marketplace/hooks' import { useMixedTranslation } from '../marketplace/hooks'
import { camelCase } from 'lodash-es'
type DeprecationNoticeProps = { type DeprecationNoticeProps = {
status: 'deleted' | 'active' status: 'deleted' | 'active'
@@ -36,7 +36,7 @@ const DeprecationNotice: FC<DeprecationNoticeProps> = ({
const deprecatedReasonKey = useMemo(() => { const deprecatedReasonKey = useMemo(() => {
if (!deprecatedReason) return '' if (!deprecatedReason) return ''
return snakeCase2CamelCase(deprecatedReason) return camelCase(deprecatedReason)
}, [deprecatedReason]) }, [deprecatedReason])
// Check if the deprecatedReasonKey exists in i18n // Check if the deprecatedReasonKey exists in i18n

View File

@@ -1,13 +1,19 @@
'use client' 'use client'
import { resources } from '@/i18n/i18next-config' import { loadLangResources } from '@/i18n/i18next-config'
import { useEffect, useState } from 'react' import { useCallback, useEffect, useState } from 'react'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import { LanguagesSupported } from '@/i18n/language'
export default function I18nTest() { export default function I18nTest() {
const [langs, setLangs] = useState<Lang[]>([]) const [langs, setLangs] = useState<Lang[]>([])
const getLangs = useCallback(async () => {
const langs = await genLangs()
setLangs(langs)
}, [])
useEffect(() => { useEffect(() => {
setLangs(genLangs()) getLangs()
}, []) }, [])
return ( return (
@@ -107,10 +113,15 @@ export default function I18nTest() {
) )
} }
function genLangs() { async function genLangs() {
const langs_: Lang[] = [] const langs_: Lang[] = []
let en!: Lang let en!: Lang
const resources: Record<string, any> = {}
// Initialize empty resource object
for (const lang of LanguagesSupported)
resources[lang] = await loadLangResources(lang)
for (const [key, value] of Object.entries(resources)) { for (const [key, value] of Object.entries(resources)) {
const keys = getNestedKeys(value.translation) const keys = getNestedKeys(value.translation)
const lang: Lang = { const lang: Lang = {

View File

@@ -57,7 +57,7 @@ export default function InviteSettingsPage() {
if (res.result === 'success') { if (res.result === 'success') {
localStorage.setItem('console_token', res.data.access_token) localStorage.setItem('console_token', res.data.access_token)
localStorage.setItem('refresh_token', res.data.refresh_token) localStorage.setItem('refresh_token', res.data.refresh_token)
setLocaleOnClient(language, false) await setLocaleOnClient(language, false)
router.replace('/apps') router.replace('/apps')
} }
} }

View File

@@ -9,13 +9,15 @@ import { noop } from 'lodash-es'
type II18NContext = { type II18NContext = {
locale: Locale locale: Locale
i18n: Record<string, any> i18n: Record<string, any>
setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => void setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => Promise<void>
} }
const I18NContext = createContext<II18NContext>({ const I18NContext = createContext<II18NContext>({
locale: 'en-US', locale: 'en-US',
i18n: {}, i18n: {},
setLocaleOnClient: noop, setLocaleOnClient: async (_lang: Locale, _reloadPage?: boolean) => {
noop()
},
}) })
export const useI18N = () => useContext(I18NContext) export const useI18N = () => useContext(I18NContext)

View File

@@ -28,7 +28,7 @@ This directory contains the internationalization (i18n) files for this project.
│   ├── [ 52] layout.ts │   ├── [ 52] layout.ts
│   ├── [2.3K] login.ts │   ├── [2.3K] login.ts
│   ├── [ 52] register.ts │   ├── [ 52] register.ts
│   ├── [2.5K] share-app.ts │   ├── [2.5K] share.ts
│   └── [2.8K] tools.ts │   └── [2.8K] tools.ts
├── [1.6K] i18next-config.ts ├── [1.6K] i18next-config.ts
├── [ 634] index.ts ├── [ 634] index.ts

View File

@@ -1,65 +1,74 @@
'use client' 'use client'
import i18n from 'i18next' import i18n from 'i18next'
import { camelCase } from 'lodash-es'
import { initReactI18next } from 'react-i18next' import { initReactI18next } from 'react-i18next'
import { LanguagesSupported } from '@/i18n/language' const requireSilent = async (lang: string, namespace: string) => {
const requireSilent = (lang: string) => {
let res let res
try { try {
res = require(`./${lang}/education`).default res = (await import(`./${lang}/${namespace}`)).default
} }
catch { catch {
res = require('./en-US/education').default res = (await import(`./en-US/${namespace}`)).default
} }
return res return res
} }
const loadLangResources = (lang: string) => ({ const NAMESPACES = [
translation: { 'app-annotation',
common: require(`./${lang}/common`).default, 'app-api',
layout: require(`./${lang}/layout`).default, 'app-debug',
login: require(`./${lang}/login`).default, 'app-log',
register: require(`./${lang}/register`).default, 'app-overview',
app: require(`./${lang}/app`).default, 'app',
appOverview: require(`./${lang}/app-overview`).default, 'billing',
appDebug: require(`./${lang}/app-debug`).default, 'common',
appApi: require(`./${lang}/app-api`).default, 'custom',
appLog: require(`./${lang}/app-log`).default, 'dataset-creation',
appAnnotation: require(`./${lang}/app-annotation`).default, 'dataset-documents',
share: require(`./${lang}/share-app`).default, 'dataset-hit-testing',
dataset: require(`./${lang}/dataset`).default, 'dataset-settings',
datasetDocuments: require(`./${lang}/dataset-documents`).default, 'dataset',
datasetHitTesting: require(`./${lang}/dataset-hit-testing`).default, 'education',
datasetSettings: require(`./${lang}/dataset-settings`).default, 'explore',
datasetCreation: require(`./${lang}/dataset-creation`).default, 'layout',
explore: require(`./${lang}/explore`).default, 'login',
billing: require(`./${lang}/billing`).default, 'plugin-tags',
custom: require(`./${lang}/custom`).default, 'plugin',
tools: require(`./${lang}/tools`).default, 'register',
workflow: require(`./${lang}/workflow`).default, 'run-log',
runLog: require(`./${lang}/run-log`).default, 'share',
plugin: require(`./${lang}/plugin`).default, 'time',
pluginTags: require(`./${lang}/plugin-tags`).default, 'tools',
time: require(`./${lang}/time`).default, 'workflow',
education: requireSilent(lang), ]
},
})
type Resource = Record<string, ReturnType<typeof loadLangResources>> export const loadLangResources = async (lang: string) => {
// Automatically generate the resources object const modules = await Promise.all(NAMESPACES.map(ns => requireSilent(lang, ns)))
export const resources = LanguagesSupported.reduce<Resource>((acc, lang) => { const resources = modules.reduce((acc, mod, index) => {
acc[lang] = loadLangResources(lang) acc[camelCase(NAMESPACES[index])] = mod
return acc return acc
}, {}) }, {} as Record<string, any>)
return {
translation: resources,
}
}
i18n.use(initReactI18next) i18n.use(initReactI18next)
.init({ .init({
lng: undefined, lng: undefined,
fallbackLng: 'en-US', fallbackLng: 'en-US',
resources,
}) })
export const changeLanguage = i18n.changeLanguage export const changeLanguage = async (lng?: string) => {
const resolvedLng = lng ?? 'en-US'
const resources = {
[resolvedLng]: await loadLangResources(resolvedLng),
}
if (!i18n.hasResourceBundle(resolvedLng, 'translation'))
i18n.addResourceBundle(resolvedLng, 'translation', resources[resolvedLng].translation, true, true)
await i18n.changeLanguage(resolvedLng)
}
export default i18n export default i18n

View File

@@ -11,9 +11,9 @@ export const i18n = {
export type Locale = typeof i18n['locales'][number] export type Locale = typeof i18n['locales'][number]
export const setLocaleOnClient = (locale: Locale, reloadPage = true) => { export const setLocaleOnClient = async (locale: Locale, reloadPage = true) => {
Cookies.set(LOCALE_COOKIE_NAME, locale, { expires: 365 }) Cookies.set(LOCALE_COOKIE_NAME, locale, { expires: 365 })
changeLanguage(locale) await changeLanguage(locale)
reloadPage && location.reload() reloadPage && location.reload()
} }

View File

@@ -56,7 +56,3 @@ export const downloadFile = ({ data, fileName }: { data: Blob; fileName: string
a.remove() a.remove()
window.URL.revokeObjectURL(url) window.URL.revokeObjectURL(url)
} }
export const snakeCase2CamelCase = (input: string): string => {
return input.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase())
}