mirror of
https://github.com/lobehub/lobe-chat.git
synced 2026-06-14 03:30:19 +00:00
🐛 fix: fix agents market locale fallback to english (#382)
* 🐛 fix: fix market locale url
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { DEFAULT_LANG } from '@/const/locale';
|
||||
import { getAgentJSON } from '@/const/url';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
export const GET = async (req: Request, { params }: { params: { id: string } }) => {
|
||||
const { searchParams } = new URL(req.url);
|
||||
|
||||
const locale = searchParams.get('locale');
|
||||
|
||||
let res: Response;
|
||||
|
||||
res = await fetch(getAgentJSON(params.id, locale as any));
|
||||
if (res.status === 404) {
|
||||
res = await fetch(getAgentJSON(params.id, DEFAULT_LANG));
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
import { DEFAULT_LANG } from '@/const/locale';
|
||||
import { getAgentIndexJSON } from '@/const/url';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
export const GET = async (req: Request) => {
|
||||
const locale = new URL(req.url).searchParams.get('locale');
|
||||
|
||||
let res: Response;
|
||||
|
||||
res = await fetch(getAgentIndexJSON(locale as any));
|
||||
|
||||
if (res.status === 404) {
|
||||
res = await fetch(getAgentIndexJSON(DEFAULT_LANG));
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
export const URLS = {
|
||||
market: '/api/market',
|
||||
plugins: '/api/plugins',
|
||||
};
|
||||
|
||||
|
||||
@@ -1,30 +1,21 @@
|
||||
import { getAgentIndexJSON, getAgentJSON } from '@/const/url';
|
||||
import { getCurrentLanguage } from '@/store/global/helpers';
|
||||
import { URLS } from '@/services/_url';
|
||||
import { LobeChatAgentsMarketIndex } from '@/types/market';
|
||||
|
||||
/**
|
||||
* 请求助手列表
|
||||
*/
|
||||
export const getAgentList = async () => {
|
||||
let res: Response;
|
||||
export const getAgentList = async (locale: string): Promise<LobeChatAgentsMarketIndex> => {
|
||||
const res = await fetch(`${URLS.market}?locale=${locale}`);
|
||||
|
||||
res = await fetch(getAgentIndexJSON(getCurrentLanguage()));
|
||||
|
||||
if (res.status === 404) {
|
||||
res = await fetch(getAgentIndexJSON('en-US'));
|
||||
}
|
||||
|
||||
const data: LobeChatAgentsMarketIndex = await res.json();
|
||||
|
||||
return data;
|
||||
return res.json();
|
||||
};
|
||||
|
||||
/**
|
||||
* 请求助手 manifest
|
||||
*/
|
||||
export const getAgentManifest = async (identifier?: string) => {
|
||||
export const getAgentManifest = async (identifier: string, locale: string) => {
|
||||
if (!identifier) return;
|
||||
const res = await fetch(getAgentJSON(identifier, getCurrentLanguage()));
|
||||
const res = await fetch(`${URLS.market}/${identifier}?locale=${locale}`);
|
||||
|
||||
return res.json();
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ import useSWR, { SWRResponse } from 'swr';
|
||||
import type { StateCreator } from 'zustand/vanilla';
|
||||
|
||||
import { getAgentList, getAgentManifest } from '@/services/agentMarket';
|
||||
import { getCurrentLanguage } from '@/store/global/helpers';
|
||||
import { AgentsMarketItem, LobeChatAgentsMarketIndex } from '@/types/market';
|
||||
|
||||
import type { Store } from './store';
|
||||
@@ -40,16 +41,20 @@ export const createMarketAction: StateCreator<
|
||||
set({ agentMap: nextAgentMap }, false, `setAgentMap/${key}`);
|
||||
},
|
||||
useFetchAgent: (identifier) =>
|
||||
useSWR<AgentsMarketItem>(identifier, getAgentManifest, {
|
||||
onError: () => {
|
||||
get().deactivateAgent();
|
||||
useSWR<AgentsMarketItem>(
|
||||
[identifier, getCurrentLanguage()],
|
||||
([id, locale]) => getAgentManifest(id, locale as string),
|
||||
{
|
||||
onError: () => {
|
||||
get().deactivateAgent();
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
get().updateAgentMap(identifier, data);
|
||||
},
|
||||
},
|
||||
onSuccess: (data, key) => {
|
||||
get().updateAgentMap(key, data);
|
||||
},
|
||||
}),
|
||||
),
|
||||
useFetchAgentList: () =>
|
||||
useSWR<LobeChatAgentsMarketIndex>('fetchAgentList', getAgentList, {
|
||||
useSWR<LobeChatAgentsMarketIndex>(getCurrentLanguage(), getAgentList, {
|
||||
onSuccess: (agentMarketIndex) => {
|
||||
set({ agentList: agentMarketIndex.agents }, false, 'useFetchAgentList');
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user