Skip to content

Commit

Permalink
🐛 fix: fix agent market index not work with docker
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Dec 23, 2023
1 parent 07f3c85 commit c29f116
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 103 deletions.
4 changes: 0 additions & 4 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ const withPWA = nextPWA({
/** @type {import('next').NextConfig} */
const nextConfig = {
compress: isProd,
env: {
AGENTS_INDEX_URL: process.env.AGENTS_INDEX_URL ?? '',
PLUGINS_INDEX_URL: process.env.PLUGINS_INDEX_URL ?? '',
},
experimental: {
forceSwcTransforms: true,
optimizePackageImports: [
Expand Down
25 changes: 25 additions & 0 deletions src/app/api/market/AgentMarket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import urlJoin from 'url-join';

import { getServerConfig } from '@/config/server';
import { DEFAULT_LANG, checkLang } from '@/const/locale';
import { Locales } from '@/locales/resources';

export class AgentMarket {
private readonly baseUrl: string;

constructor(baseUrl?: string) {
this.baseUrl = baseUrl || getServerConfig().AGENTS_INDEX_URL;
}

getAgentIndexUrl = (lang: Locales = DEFAULT_LANG) => {
if (checkLang(lang)) return this.baseUrl;

return urlJoin(this.baseUrl, `index.${lang}.json`);
};

getAgentUrl = (identifier: string, lang: Locales = DEFAULT_LANG) => {
if (checkLang(lang)) return urlJoin(this.baseUrl, `${identifier}.json`);

return urlJoin(this.baseUrl, `${identifier}.${lang}.json`);
};
}
9 changes: 6 additions & 3 deletions src/app/api/market/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DEFAULT_LANG } from '@/const/locale';
import { getAgentJSON } from '@/const/url';

import { AgentMarket } from '../AgentMarket';

export const runtime = 'edge';

Expand All @@ -8,11 +9,13 @@ export const GET = async (req: Request, { params }: { params: { id: string } })

const locale = searchParams.get('locale');

const market = new AgentMarket();

let res: Response;

res = await fetch(getAgentJSON(params.id, locale as any));
res = await fetch(market.getAgentUrl(params.id, locale as any));
if (res.status === 404) {
res = await fetch(getAgentJSON(params.id, DEFAULT_LANG));
res = await fetch(market.getAgentUrl(params.id, DEFAULT_LANG));
}

return res;
Expand Down
9 changes: 6 additions & 3 deletions src/app/api/market/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { DEFAULT_LANG } from '@/const/locale';
import { getAgentIndexJSON } from '@/const/url';

import { AgentMarket } from './AgentMarket';

export const runtime = 'edge';

export const GET = async (req: Request) => {
const locale = new URL(req.url).searchParams.get('locale');

const market = new AgentMarket();

let res: Response;

res = await fetch(getAgentIndexJSON(locale as any));
res = await fetch(market.getAgentIndexUrl(locale as any));

if (res.status === 404) {
res = await fetch(getAgentIndexJSON(DEFAULT_LANG));
res = await fetch(market.getAgentIndexUrl(DEFAULT_LANG));
}

return res;
Expand Down
23 changes: 0 additions & 23 deletions src/config/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,6 @@ vi.stubGlobal('process', {
});

describe('getClientConfig', () => {
it('should return default URLs when no environment variables are set', () => {
const config = getClientConfig();
expect(config.AGENTS_INDEX_URL).toBe('https://chat-agents.lobehub.com');
expect(config.PLUGINS_INDEX_URL).toBe('https://chat-plugins.lobehub.com');
});

it('should return custom URLs when environment variables are set', () => {
process.env.AGENTS_INDEX_URL = 'https://custom-agents-url.com';
process.env.PLUGINS_INDEX_URL = 'https://custom-plugins-url.com';
const config = getClientConfig();
expect(config.AGENTS_INDEX_URL).toBe('https://custom-agents-url.com');
expect(config.PLUGINS_INDEX_URL).toBe('https://custom-plugins-url.com');
});

it('should return default URLs when environment variables are empty string', () => {
process.env.AGENTS_INDEX_URL = '';
process.env.PLUGINS_INDEX_URL = '';

const config = getClientConfig();
expect(config.AGENTS_INDEX_URL).toBe('https://chat-agents.lobehub.com');
expect(config.PLUGINS_INDEX_URL).toBe('https://chat-plugins.lobehub.com');
});

it('should correctly reflect boolean values for analytics flags', () => {
process.env.NEXT_PUBLIC_ANALYTICS_VERCEL = '1';
process.env.NEXT_PUBLIC_VERCEL_DEBUG = '1';
Expand Down
27 changes: 26 additions & 1 deletion src/config/__tests__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('getServerConfig', () => {
global.process = undefined;

expect(() => getServerConfig()).toThrow(
'[Server Config] you are importing a nodejs-only module outside of nodejs',
'[Server Config] you are importing a server-only module outside of server',
);

global.process = originalProcess; // Restore the original process object
Expand Down Expand Up @@ -55,4 +55,29 @@ describe('getServerConfig', () => {
const config = getServerConfig();
expect(config.IMGUR_CLIENT_ID).toBe('custom-client-id');
});

describe('index url', () => {
it('should return default URLs when no environment variables are set', () => {
const config = getServerConfig();
expect(config.AGENTS_INDEX_URL).toBe('https://chat-agents.lobehub.com');
expect(config.PLUGINS_INDEX_URL).toBe('https://chat-plugins.lobehub.com');
});

it('should return custom URLs when environment variables are set', () => {
process.env.AGENTS_INDEX_URL = 'https://custom-agents-url.com';
process.env.PLUGINS_INDEX_URL = 'https://custom-plugins-url.com';
const config = getServerConfig();
expect(config.AGENTS_INDEX_URL).toBe('https://custom-agents-url.com');
expect(config.PLUGINS_INDEX_URL).toBe('https://custom-plugins-url.com');
});

it('should return default URLs when environment variables are empty string', () => {
process.env.AGENTS_INDEX_URL = '';
process.env.PLUGINS_INDEX_URL = '';

const config = getServerConfig();
expect(config.AGENTS_INDEX_URL).toBe('https://chat-agents.lobehub.com');
expect(config.PLUGINS_INDEX_URL).toBe('https://chat-plugins.lobehub.com');
});
});
});
19 changes: 4 additions & 15 deletions src/config/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/**
* the client config is only used in Vercel deployment
*/

/* eslint-disable sort-keys-fix/sort-keys-fix , typescript-sort-keys/interface */

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
interface ProcessEnv {
AGENTS_INDEX_URL?: string;
PLUGINS_INDEX_URL?: string;

NEXT_PUBLIC_CUSTOM_MODELS?: string;

NEXT_PUBLIC_ANALYTICS_VERCEL?: string;
NEXT_PUBLIC_VERCEL_DEBUG?: string;

Expand All @@ -35,16 +34,6 @@ declare global {
}

export const getClientConfig = () => ({
AGENTS_INDEX_URL: !!process.env.AGENTS_INDEX_URL
? process.env.AGENTS_INDEX_URL
: 'https://chat-agents.lobehub.com',
PLUGINS_INDEX_URL: !!process.env.PLUGINS_INDEX_URL
? process.env.PLUGINS_INDEX_URL
: 'https://chat-plugins.lobehub.com',

// custom model names
CUSTOM_MODELS: process.env.NEXT_PUBLIC_CUSTOM_MODELS,

// Vercel Analytics
ANALYTICS_VERCEL: process.env.NEXT_PUBLIC_ANALYTICS_VERCEL === '1',
VERCEL_DEBUG: process.env.NEXT_PUBLIC_VERCEL_DEBUG === '1',
Expand Down
15 changes: 14 additions & 1 deletion src/config/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare global {
namespace NodeJS {
interface ProcessEnv {
ACCESS_CODE?: string;
CUSTOM_MODELS?: string;

OPENAI_API_KEY?: string;
OPENAI_PROXY_URL?: string;
Expand All @@ -14,6 +15,9 @@ declare global {
USE_AZURE_OPENAI?: string;

IMGUR_CLIENT_ID?: string;

AGENTS_INDEX_URL?: string;
PLUGINS_INDEX_URL?: string;
}
}
}
Expand All @@ -24,7 +28,7 @@ const DEFAULT_IMAGUR_CLIENT_ID = 'e415f320d6e24f9';

export const getServerConfig = () => {
if (typeof process === 'undefined') {
throw new Error('[Server Config] you are importing a nodejs-only module outside of nodejs');
throw new Error('[Server Config] you are importing a server-only module outside of server');
}

// region format: iad1,sfo1
Expand All @@ -37,6 +41,7 @@ export const getServerConfig = () => {

return {
ACCESS_CODES,
CUSTOM_MODELS: process.env.CUSTOM_MODELS,

SHOW_ACCESS_CODE_CONFIG: !!ACCESS_CODES.length,

Expand All @@ -49,5 +54,13 @@ export const getServerConfig = () => {
USE_AZURE_OPENAI: process.env.USE_AZURE_OPENAI === '1',

IMGUR_CLIENT_ID: process.env.IMGUR_CLIENT_ID || DEFAULT_IMAGUR_CLIENT_ID,

AGENTS_INDEX_URL: !!process.env.AGENTS_INDEX_URL
? process.env.AGENTS_INDEX_URL
: 'https://chat-agents.lobehub.com',

PLUGINS_INDEX_URL: !!process.env.PLUGINS_INDEX_URL
? process.env.PLUGINS_INDEX_URL
: 'https://chat-plugins.lobehub.com',
};
};
3 changes: 0 additions & 3 deletions src/const/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getClientConfig } from '@/config/client';
import { DEFAULT_OPENAI_MODEL_LIST } from '@/const/llm';
import { DEFAULT_AGENT_META } from '@/const/meta';
import { LobeAgentConfig, LobeAgentTTSConfig } from '@/types/agent';
Expand Down Expand Up @@ -52,8 +51,6 @@ export const DEFAULT_AGENT_CONFIG: LobeAgentConfig = {
export const DEFAULT_LLM_CONFIG: GlobalLLMConfig = {
openAI: {
OPENAI_API_KEY: '',
// support user custom model names with env var
customModelName: getClientConfig().CUSTOM_MODELS,
models: DEFAULT_OPENAI_MODEL_LIST,
},
};
Expand Down
19 changes: 1 addition & 18 deletions src/const/url.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import urlJoin from 'url-join';

import { getClientConfig } from '@/config/client';
import { Locales } from '@/locales/resources';

import pkg from '../../package.json';
Expand All @@ -15,30 +14,14 @@ export const ABOUT = pkg.homepage;
export const FEEDBACK = pkg.bugs.url;
export const DISCORD = 'https://discord.gg/AYFPHvv2jT';

export const { PLUGINS_INDEX_URL, AGENTS_INDEX_URL } = getClientConfig();
export const PLUGINS_INDEX_URL = 'https://chat-plugins.lobehub.com';

export const getPluginIndexJSON = (lang: Locales = DEFAULT_LANG, baseUrl = PLUGINS_INDEX_URL) => {
if (checkLang(lang)) return baseUrl;

return urlJoin(baseUrl, `index.${lang}.json`);
};

export const getAgentIndexJSON = (lang: Locales = DEFAULT_LANG, baseUrl = AGENTS_INDEX_URL) => {
if (checkLang(lang)) return baseUrl;

return urlJoin(baseUrl, `index.${lang}.json`);
};

export const getAgentJSON = (
identifier: string,
lang: Locales = DEFAULT_LANG,
baseUrl = AGENTS_INDEX_URL,
) => {
if (checkLang(lang)) return urlJoin(baseUrl, `${identifier}.json`);

return urlJoin(baseUrl, `${identifier}.${lang}.json`);
};

export const AGENTS_INDEX_GITHUB = 'https://github.com/lobehub/lobe-chat-agents';
export const AGENTS_INDEX_GITHUB_ISSUE = urlJoin(AGENTS_INDEX_GITHUB, 'issues/new');

Expand Down
21 changes: 0 additions & 21 deletions src/services/agentMarket.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/services/market.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { URLS } from '@/services/_url';
import { LobeChatAgentsMarketIndex } from '@/types/market';

class MarketService {
getAgentList = async (locale: string): Promise<LobeChatAgentsMarketIndex> => {
const res = await fetch(`${URLS.market}?locale=${locale}`);

return res.json();
};

/**
* 请求助手 manifest
*/
getAgentManifest = async (identifier: string, locale: string) => {
if (!identifier) return;
const res = await fetch(`${URLS.market}/${identifier}?locale=${locale}`);

return res.json();
};
}
export const marketService = new MarketService();
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ exports[`settingsSelectors > currentSettings > should merge DEFAULT_SETTINGS and
"languageModel": {
"openAI": {
"OPENAI_API_KEY": "openai-api-key",
"customModelName": undefined,
"endpoint": "https://openai-endpoint.com",
"models": [
"gpt-3.5-turbo",
Expand Down
24 changes: 14 additions & 10 deletions src/store/market/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { produce } from 'immer';
import useSWR, { SWRResponse } from 'swr';
import type { StateCreator } from 'zustand/vanilla';

import { getAgentList, getAgentManifest } from '@/services/agentMarket';
import { marketService } from '@/services/market';
import { globalHelpers } from '@/store/global/helpers';
import { AgentsMarketItem, LobeChatAgentsMarketIndex } from '@/types/market';

Expand Down Expand Up @@ -47,7 +47,7 @@ export const createMarketAction: StateCreator<
useFetchAgent: (identifier) =>
useSWR<AgentsMarketItem>(
[identifier, globalHelpers.getCurrentLanguage()],
([id, locale]) => getAgentManifest(id, locale as string),
([id, locale]) => marketService.getAgentManifest(id, locale as string),
{
onError: () => {
get().deactivateAgent();
Expand All @@ -58,13 +58,17 @@ export const createMarketAction: StateCreator<
},
),
useFetchAgentList: () =>
useSWR<LobeChatAgentsMarketIndex>(globalHelpers.getCurrentLanguage(), getAgentList, {
onSuccess: (agentMarketIndex) => {
set(
{ agentList: agentMarketIndex.agents, tagList: agentMarketIndex.tags },
false,
'useFetchAgentList',
);
useSWR<LobeChatAgentsMarketIndex>(
globalHelpers.getCurrentLanguage(),
marketService.getAgentList,
{
onSuccess: (agentMarketIndex) => {
set(
{ agentList: agentMarketIndex.agents, tagList: agentMarketIndex.tags },
false,
'useFetchAgentList',
);
},
},
}),
),
});

0 comments on commit c29f116

Please sign in to comment.