Files
ragflow/web/src/utils/backend-runtime.ts
Jack fd5487e064 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>
2026-07-16 15:13:43 +08:00

41 lines
1.2 KiB
TypeScript

/**
* Backend runtime language detection.
*
* Fetches /api/v1/language once at module load (app start) and caches the
* result. Components subscribe via React's useSyncExternalStore; there is no
* per-component fetch or useEffect.
*
* Pattern: module-level fetch + listener set, same subscription approach as
* enterprise billingStatus.ts.
*/
type Listener = () => void;
const listeners = new Set<Listener>();
let backendLanguage: string | null = null;
// Kick off the fetch at module load — app start, not component mount.
const promise: Promise<string> = fetch('/api/v1/language')
.then((r) => r.json())
.then((body: { data?: { language?: string } }) => {
backendLanguage = body.data?.language === 'go' ? 'go' : 'python';
listeners.forEach((fn) => fn());
return backendLanguage;
})
.catch(() => {
backendLanguage = 'python';
listeners.forEach((fn) => fn());
return 'python';
});
export const fetchBackendLanguage = (): Promise<string> => promise;
export const getBackendLanguage = (): string | null => backendLanguage;
export const subscribeBackendLanguage = (listener: Listener): (() => void) => {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
};