Skip to content

Commit

Permalink
🐛 fix: fix agents market locale fallback to english (#382)
Browse files Browse the repository at this point in the history
* 🐛 fix: fix market locale url
  • Loading branch information
arvinxx authored Oct 29, 2023
1 parent 267e2e7 commit 3814523
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
19 changes: 19 additions & 0 deletions src/app/api/market/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -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;
};
18 changes: 18 additions & 0 deletions src/app/api/market/route.ts
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions src/services/_url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const URLS = {
market: '/api/market',
plugins: '/api/plugins',
};

Expand Down
21 changes: 6 additions & 15 deletions src/services/agentMarket.ts
Original file line number Diff line number Diff line change
@@ -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();
};
21 changes: 13 additions & 8 deletions src/store/market/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
},
Expand Down

0 comments on commit 3814523

Please sign in to comment.