feat: add /api/v1/language endpoint for Go/Python runtime detection (#16952)

Both backends serve GET /api/v1/language. Frontend calls it once and
caches. By this way, front end can know the backend is go or python and
thus can determine which part of logic to load.

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Jack
2026-07-16 15:13:43 +08:00
committed by GitHub
parent 92ae4f6769
commit fd5487e064
8 changed files with 111 additions and 1 deletions

View File

@@ -10,6 +10,11 @@ import {
} from '@/interfaces/database/user-setting';
import { ISetLangfuseConfigRequestBody } from '@/interfaces/request/system';
import { DEFAULT_LANGUAGE_CODE, supportedLanguages } from '@/locales/config';
import kbService from '@/services/knowledge-service';
import {
getBackendLanguage,
subscribeBackendLanguage,
} from '@/utils/backend-runtime';
import userService, {
addTenantUser,
agreeTenant,
@@ -19,6 +24,7 @@ import userService, {
} from '@/services/user-service';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useCallback, useMemo, useState } from 'react';
import { useSyncExternalStore } from 'react';
import { useTranslation } from 'react-i18next';
import { useWarnEmptyModel } from './use-warn-empty-model';
@@ -38,6 +44,7 @@ export const enum UserSettingApiAction {
AgreeTenant = 'agreeTenant',
SetLangfuseConfig = 'setLangfuseConfig',
DeleteLangfuseConfig = 'deleteLangfuseConfig',
ListPipelines = 'listPipelines',
FetchLangfuseConfig = 'fetchLangfuseConfig',
}
@@ -102,6 +109,27 @@ export const useSelectParserList = (): Array<{
const { data: tenantInfo } = useFetchTenantInfo(true);
const { t } = useTranslation();
// Detect backend runtime language (Go vs Python) so we can choose
// the matching parser-list code path at runtime.
// fetchBackendLanguage / getBackendLanguage handle their own caching
// internally; no need for an extra useQuery layer.
const backendLang = useSyncExternalStore(
subscribeBackendLanguage,
getBackendLanguage,
getBackendLanguage,
);
// Go backend: fetch pipeline catalog dynamically.
const { data: pipelineListData } = useQuery({
queryKey: [UserSettingApiAction.ListPipelines],
queryFn: async () => {
const { data } = await kbService.listPipelines();
return data;
},
staleTime: Infinity,
enabled: backendLang === 'go',
});
const defaultParsers = useMemo(
() => [
{ value: 'naive', label: t('knowledgeConfiguration.parserLabel.naive') },
@@ -135,6 +163,25 @@ export const useSelectParserList = (): Array<{
);
const parserList = useMemo(() => {
// Go backend: prefer the dynamic pipeline catalog from the API.
if (backendLang === 'go') {
const pipelineList: Array<{ parser_id: string; title: string; dsl: Record<string, any> }> =
pipelineListData?.data ?? [];
if (pipelineList.length > 0) {
const labelFromAPI = (parserId: string, title: string) => {
const key = `knowledgeConfiguration.parserLabel.${parserId}`;
const translated = t(key);
return translated !== key ? translated : title;
};
return pipelineList.map((item) => ({
value: item.parser_id,
label: labelFromAPI(item.parser_id, item.title),
}));
}
}
// Python backend (or fallback): use tenant-level parser_ids or
// the hardcoded default parsers.
const parserArray: Array<string> = tenantInfo?.parser_ids?.split(',') ?? [];
const filteredArray = parserArray.filter((x) => x.trim() !== '');
@@ -146,7 +193,7 @@ export const useSelectParserList = (): Array<{
const arr = x.split(':');
return { value: arr[0], label: arr[1] };
});
}, [tenantInfo, defaultParsers]);
}, [tenantInfo, defaultParsers, backendLang, pipelineListData, t]);
return parserList;
};