@lobehub/chat 0.158.2 → 0.159.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +4 -0
- package/CHANGELOG.md +58 -0
- package/Dockerfile +3 -0
- package/README.md +1 -0
- package/README.zh-CN.md +1 -0
- package/docs/self-hosting/advanced/authentication.mdx +9 -0
- package/docs/self-hosting/advanced/authentication.zh-CN.mdx +9 -0
- package/docs/self-hosting/environment-variables/model-provider.mdx +16 -7
- package/docs/self-hosting/environment-variables/model-provider.zh-CN.mdx +16 -7
- package/docs/usage/features/multi-ai-providers.mdx +1 -0
- package/docs/usage/features/multi-ai-providers.zh-CN.mdx +1 -0
- package/locales/ar/error.json +2 -0
- package/locales/ar/modelProvider.json +12 -0
- package/locales/bg-BG/error.json +2 -0
- package/locales/bg-BG/modelProvider.json +12 -0
- package/locales/de-DE/error.json +2 -0
- package/locales/de-DE/modelProvider.json +12 -0
- package/locales/en-US/error.json +2 -0
- package/locales/en-US/modelProvider.json +12 -0
- package/locales/es-ES/error.json +2 -0
- package/locales/es-ES/modelProvider.json +12 -0
- package/locales/fr-FR/error.json +2 -0
- package/locales/fr-FR/modelProvider.json +12 -0
- package/locales/it-IT/error.json +2 -0
- package/locales/it-IT/modelProvider.json +12 -0
- package/locales/ja-JP/error.json +2 -0
- package/locales/ja-JP/modelProvider.json +12 -0
- package/locales/ko-KR/error.json +2 -0
- package/locales/ko-KR/modelProvider.json +12 -0
- package/locales/nl-NL/error.json +2 -0
- package/locales/nl-NL/modelProvider.json +12 -0
- package/locales/pl-PL/error.json +2 -0
- package/locales/pl-PL/modelProvider.json +12 -0
- package/locales/pt-BR/error.json +2 -0
- package/locales/pt-BR/modelProvider.json +12 -0
- package/locales/ru-RU/error.json +2 -0
- package/locales/ru-RU/modelProvider.json +12 -0
- package/locales/tr-TR/error.json +2 -0
- package/locales/tr-TR/modelProvider.json +12 -0
- package/locales/vi-VN/error.json +2 -0
- package/locales/vi-VN/modelProvider.json +12 -0
- package/locales/zh-CN/error.json +2 -0
- package/locales/zh-CN/modelProvider.json +12 -0
- package/locales/zh-TW/error.json +2 -0
- package/locales/zh-TW/modelProvider.json +12 -0
- package/package.json +2 -2
- package/src/app/(main)/chat/(workspace)/_layout/Desktop/ChatHeader/Main.tsx +13 -15
- package/src/app/(main)/settings/common/features/Common.tsx +11 -8
- package/src/app/(main)/settings/common/index.tsx +1 -9
- package/src/app/(main)/settings/llm/DeepSeek/index.tsx +21 -0
- package/src/app/(main)/settings/llm/index.tsx +2 -0
- package/src/app/api/chat/agentRuntime.test.ts +17 -0
- package/src/app/api/chat/agentRuntime.ts +5 -0
- package/src/app/api/errorResponse.test.ts +6 -0
- package/src/app/api/errorResponse.ts +3 -0
- package/src/components/ModelIcon/index.tsx +2 -0
- package/src/components/ModelProviderIcon/index.tsx +5 -0
- package/src/components/ModelTag/ModelIcon.tsx +2 -0
- package/src/config/modelProviders/deepseek.ts +23 -0
- package/src/config/modelProviders/index.ts +4 -0
- package/src/config/server/provider.ts +10 -0
- package/src/const/settings/index.ts +6 -0
- package/src/features/Conversation/Error/APIKeyForm/ProviderAvatar.tsx +5 -0
- package/src/features/Conversation/Error/APIKeyForm/index.tsx +4 -0
- package/src/features/Conversation/Error/OAuthForm.tsx +6 -4
- package/src/features/Conversation/Error/index.tsx +1 -0
- package/src/features/User/__tests__/UserAvatar.test.tsx +8 -3
- package/src/features/User/__tests__/useMenu.test.tsx +3 -3
- package/src/libs/agent-runtime/AgentRuntime.ts +7 -0
- package/src/libs/agent-runtime/deepseek/index.test.ts +254 -0
- package/src/libs/agent-runtime/deepseek/index.ts +15 -0
- package/src/libs/agent-runtime/error.ts +3 -0
- package/src/libs/agent-runtime/index.ts +1 -0
- package/src/libs/agent-runtime/types/type.ts +1 -0
- package/src/libs/next-auth/index.ts +0 -7
- package/src/locales/default/error.ts +3 -0
- package/src/locales/default/modelProvider.ts +12 -0
- package/src/migrations/FromV3ToV4/types/v3.ts +0 -8
- package/src/server/globalConfig/index.ts +4 -0
- package/src/services/__tests__/chat.test.ts +16 -0
- package/src/services/chat.ts +3 -0
- package/src/store/serverConfig/selectors.ts +1 -0
- package/src/store/user/slices/auth/action.test.ts +61 -0
- package/src/store/user/slices/auth/action.ts +17 -15
- package/src/store/user/slices/auth/selectors.test.ts +18 -2
- package/src/store/user/slices/auth/selectors.ts +4 -4
- package/src/store/user/slices/settings/actions/llm.ts +2 -0
- package/src/types/next-auth.d.ts +23 -0
- package/src/types/serverConfig.ts +1 -0
- package/src/types/settings/modelProvider.ts +1 -0
- package/src/hooks/useOAuthSession.ts +0 -24
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
Claude,
|
|
10
10
|
Cohere,
|
|
11
11
|
Dbrx,
|
|
12
|
+
DeepSeek,
|
|
12
13
|
FishAudio,
|
|
13
14
|
Gemini,
|
|
14
15
|
Gemma,
|
|
@@ -43,6 +44,7 @@ const ModelIcon = memo<ModelIconProps>(({ model, size = 12 }) => {
|
|
|
43
44
|
if (model.startsWith('gpt')) return <OpenAI size={size} />;
|
|
44
45
|
if (model.startsWith('glm') || model.includes('chatglm')) return <ChatGLM size={size} />;
|
|
45
46
|
if (model.includes('claude')) return <Claude size={size} />;
|
|
47
|
+
if (model.includes('deepseek')) return <DeepSeek size={size} />;
|
|
46
48
|
if (model.includes('titan')) return <Aws size={size} />;
|
|
47
49
|
if (model.includes('llama')) return <Meta size={size} />;
|
|
48
50
|
if (model.includes('llava')) return <LLaVA size={size} />;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ModelProviderCard } from '@/types/llm';
|
|
2
|
+
|
|
3
|
+
const DeepSeek: ModelProviderCard = {
|
|
4
|
+
chatModels: [
|
|
5
|
+
{
|
|
6
|
+
description: '擅长通用对话任务',
|
|
7
|
+
displayName: 'DeepSeek-V2',
|
|
8
|
+
enabled: true,
|
|
9
|
+
id: 'deepseek-chat',
|
|
10
|
+
tokens: 32_768,
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
description: '擅长编程场景',
|
|
14
|
+
displayName: 'DeepSeek-coder',
|
|
15
|
+
enabled: true,
|
|
16
|
+
id: 'deepseek-coder',
|
|
17
|
+
tokens: 16_384,
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
id: 'deepseek',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default DeepSeek;
|
|
@@ -3,6 +3,7 @@ import { ChatModelCard, ModelProviderCard } from '@/types/llm';
|
|
|
3
3
|
import AnthropicProvider from './anthropic';
|
|
4
4
|
import AzureProvider from './azure';
|
|
5
5
|
import BedrockProvider from './bedrock';
|
|
6
|
+
import DeepSeekProvider from './deepseek';
|
|
6
7
|
import GoogleProvider from './google';
|
|
7
8
|
import GroqProvider from './groq';
|
|
8
9
|
import MinimaxProvider from './minimax';
|
|
@@ -20,6 +21,7 @@ export const LOBE_DEFAULT_MODEL_LIST: ChatModelCard[] = [
|
|
|
20
21
|
OpenAIProvider.chatModels,
|
|
21
22
|
ZhiPuProvider.chatModels,
|
|
22
23
|
BedrockProvider.chatModels,
|
|
24
|
+
DeepSeekProvider.chatModels,
|
|
23
25
|
GoogleProvider.chatModels,
|
|
24
26
|
GroqProvider.chatModels,
|
|
25
27
|
MinimaxProvider.chatModels,
|
|
@@ -38,6 +40,7 @@ export const DEFAULT_MODEL_PROVIDER_LIST = [
|
|
|
38
40
|
{ ...AzureProvider, chatModels: [] },
|
|
39
41
|
OllamaProvider,
|
|
40
42
|
AnthropicProvider,
|
|
43
|
+
DeepSeekProvider,
|
|
41
44
|
GoogleProvider,
|
|
42
45
|
OpenRouterProvider,
|
|
43
46
|
TogetherAIProvider,
|
|
@@ -58,6 +61,7 @@ export const filterEnabledModels = (provider: ModelProviderCard) => {
|
|
|
58
61
|
export { default as AnthropicProviderCard } from './anthropic';
|
|
59
62
|
export { default as AzureProviderCard } from './azure';
|
|
60
63
|
export { default as BedrockProviderCard } from './bedrock';
|
|
64
|
+
export { default as DeepSeekProviderCard } from './deepseek';
|
|
61
65
|
export { default as GoogleProviderCard } from './google';
|
|
62
66
|
export { default as GroqProviderCard } from './groq';
|
|
63
67
|
export { default as MinimaxProviderCard } from './minimax';
|
|
@@ -19,6 +19,10 @@ declare global {
|
|
|
19
19
|
AZURE_ENDPOINT?: string;
|
|
20
20
|
AZURE_API_VERSION?: string;
|
|
21
21
|
|
|
22
|
+
// DeepSeek Provider
|
|
23
|
+
ENABLED_DEEPSEEK?: string;
|
|
24
|
+
DEEPSEEK_API_KEY?: string;
|
|
25
|
+
|
|
22
26
|
// ZhiPu Provider
|
|
23
27
|
ENABLED_ZHIPU?: string;
|
|
24
28
|
ZHIPU_API_KEY?: string;
|
|
@@ -105,8 +109,11 @@ export const getProviderConfig = () => {
|
|
|
105
109
|
const AZURE_API_KEY = process.env.AZURE_API_KEY || '';
|
|
106
110
|
|
|
107
111
|
const ZHIPU_API_KEY = process.env.ZHIPU_API_KEY || '';
|
|
112
|
+
|
|
108
113
|
const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID || '';
|
|
109
114
|
|
|
115
|
+
const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY || '';
|
|
116
|
+
|
|
110
117
|
const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY || '';
|
|
111
118
|
|
|
112
119
|
const MOONSHOT_API_KEY = process.env.MOONSHOT_API_KEY || '';
|
|
@@ -168,6 +175,9 @@ export const getProviderConfig = () => {
|
|
|
168
175
|
ENABLED_ZHIPU: !!ZHIPU_API_KEY,
|
|
169
176
|
ZHIPU_API_KEY,
|
|
170
177
|
|
|
178
|
+
ENABLED_DEEPSEEK: !!DEEPSEEK_API_KEY,
|
|
179
|
+
DEEPSEEK_API_KEY,
|
|
180
|
+
|
|
171
181
|
ENABLED_GOOGLE: !!GOOGLE_API_KEY,
|
|
172
182
|
GOOGLE_API_KEY,
|
|
173
183
|
GOOGLE_PROXY_URL: process.env.GOOGLE_PROXY_URL,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AnthropicProviderCard,
|
|
3
3
|
BedrockProviderCard,
|
|
4
|
+
DeepSeekProviderCard,
|
|
4
5
|
GoogleProviderCard,
|
|
5
6
|
GroqProviderCard,
|
|
6
7
|
MinimaxProviderCard,
|
|
@@ -81,6 +82,11 @@ export const DEFAULT_LLM_CONFIG: GlobalLLMConfig = {
|
|
|
81
82
|
region: 'us-east-1',
|
|
82
83
|
secretAccessKey: '',
|
|
83
84
|
},
|
|
85
|
+
deepseek: {
|
|
86
|
+
apiKey: '',
|
|
87
|
+
enabled: false,
|
|
88
|
+
enabledModels: filterEnabledModels(DeepSeekProviderCard),
|
|
89
|
+
},
|
|
84
90
|
google: {
|
|
85
91
|
apiKey: '',
|
|
86
92
|
enabled: false,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Anthropic,
|
|
3
|
+
DeepSeek,
|
|
3
4
|
Google,
|
|
4
5
|
Groq,
|
|
5
6
|
Minimax,
|
|
@@ -53,6 +54,10 @@ const ProviderAvatar = memo<ProviderAvatarProps>(({ provider }) => {
|
|
|
53
54
|
return <Anthropic color={Anthropic.colorPrimary} size={52} />;
|
|
54
55
|
}
|
|
55
56
|
|
|
57
|
+
case ModelProvider.DeepSeek: {
|
|
58
|
+
return <DeepSeek color={DeepSeek.colorPrimary} size={56} />;
|
|
59
|
+
}
|
|
60
|
+
|
|
56
61
|
case ModelProvider.Groq: {
|
|
57
62
|
return <Groq color={Groq.colorPrimary} size={56} />;
|
|
58
63
|
}
|
|
@@ -43,6 +43,10 @@ const APIKeyForm = memo<APIKeyFormProps>(({ id, provider }) => {
|
|
|
43
43
|
return 'gsk_*****************************';
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
case ModelProvider.DeepSeek: {
|
|
47
|
+
return 'sk_******************************';
|
|
48
|
+
}
|
|
49
|
+
|
|
46
50
|
default: {
|
|
47
51
|
return '*********************************';
|
|
48
52
|
}
|
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
import { Icon } from '@lobehub/ui';
|
|
2
2
|
import { App, Button } from 'antd';
|
|
3
3
|
import { ScanFace } from 'lucide-react';
|
|
4
|
-
import { signIn, signOut } from 'next-auth/react';
|
|
5
4
|
import { memo, useCallback } from 'react';
|
|
6
5
|
import { useTranslation } from 'react-i18next';
|
|
7
6
|
import { Center, Flexbox } from 'react-layout-kit';
|
|
8
7
|
|
|
9
|
-
import { useOAuthSession } from '@/hooks/useOAuthSession';
|
|
10
8
|
import { useChatStore } from '@/store/chat';
|
|
9
|
+
import { useUserStore } from '@/store/user';
|
|
10
|
+
import { authSelectors, userProfileSelectors } from '@/store/user/selectors';
|
|
11
11
|
|
|
12
12
|
import { FormAction } from './style';
|
|
13
13
|
|
|
14
14
|
const OAuthForm = memo<{ id: string }>(({ id }) => {
|
|
15
15
|
const { t } = useTranslation('error');
|
|
16
16
|
|
|
17
|
-
const
|
|
17
|
+
const [signIn, signOut] = useUserStore((s) => [s.openLogin, s.logout]);
|
|
18
|
+
const user = useUserStore(userProfileSelectors.userProfile);
|
|
19
|
+
const isOAuthLoggedIn = useUserStore(authSelectors.isLoginWithAuth);
|
|
18
20
|
|
|
19
21
|
const [resend, deleteMessage] = useChatStore((s) => [s.regenerateMessage, s.deleteMessage]);
|
|
20
22
|
|
|
@@ -38,7 +40,7 @@ const OAuthForm = memo<{ id: string }>(({ id }) => {
|
|
|
38
40
|
avatar={isOAuthLoggedIn ? '✅' : '🕵️♂️'}
|
|
39
41
|
description={
|
|
40
42
|
isOAuthLoggedIn
|
|
41
|
-
? `${t('unlock.oauth.welcome')} ${user?.
|
|
43
|
+
? `${t('unlock.oauth.welcome')} ${user?.fullName || ''}`
|
|
42
44
|
: t('unlock.oauth.description')
|
|
43
45
|
}
|
|
44
46
|
title={isOAuthLoggedIn ? t('unlock.oauth.success') : t('unlock.oauth.title')}
|
|
@@ -73,6 +73,7 @@ const ErrorMessageExtra = memo<{ data: ChatMessage }>(({ data }) => {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
case AgentRuntimeErrorType.InvalidBedrockCredentials:
|
|
76
|
+
case AgentRuntimeErrorType.InvalidDeepSeekAPIKey:
|
|
76
77
|
case AgentRuntimeErrorType.InvalidZhipuAPIKey:
|
|
77
78
|
case AgentRuntimeErrorType.InvalidMinimaxAPIKey:
|
|
78
79
|
case AgentRuntimeErrorType.InvalidMistralAPIKey:
|
|
@@ -30,6 +30,7 @@ describe('UserAvatar', () => {
|
|
|
30
30
|
|
|
31
31
|
act(() => {
|
|
32
32
|
useUserStore.setState({
|
|
33
|
+
enableAuth: () => true,
|
|
33
34
|
isSignedIn: true,
|
|
34
35
|
user: { avatar: mockAvatar, id: 'abc', username: mockUsername },
|
|
35
36
|
});
|
|
@@ -45,7 +46,11 @@ describe('UserAvatar', () => {
|
|
|
45
46
|
const mockUsername = 'testuser';
|
|
46
47
|
|
|
47
48
|
act(() => {
|
|
48
|
-
useUserStore.setState({
|
|
49
|
+
useUserStore.setState({
|
|
50
|
+
enableAuth: () => true,
|
|
51
|
+
isSignedIn: true,
|
|
52
|
+
user: { id: 'bbb', username: mockUsername },
|
|
53
|
+
});
|
|
49
54
|
});
|
|
50
55
|
|
|
51
56
|
render(<UserAvatar />);
|
|
@@ -54,7 +59,7 @@ describe('UserAvatar', () => {
|
|
|
54
59
|
|
|
55
60
|
it('should show LobeChat and default avatar when the user is not logged in and enable auth', () => {
|
|
56
61
|
act(() => {
|
|
57
|
-
useUserStore.setState({ isSignedIn: false, user: undefined });
|
|
62
|
+
useUserStore.setState({ enableAuth: () => true, isSignedIn: false, user: undefined });
|
|
58
63
|
});
|
|
59
64
|
|
|
60
65
|
render(<UserAvatar />);
|
|
@@ -67,7 +72,7 @@ describe('UserAvatar', () => {
|
|
|
67
72
|
it('should show LobeChat and default avatar when the user is not logged in and disabled auth', () => {
|
|
68
73
|
enableAuth = false;
|
|
69
74
|
act(() => {
|
|
70
|
-
useUserStore.setState({ isSignedIn: false, user: undefined });
|
|
75
|
+
useUserStore.setState({ enableAuth: () => false, isSignedIn: false, user: undefined });
|
|
71
76
|
});
|
|
72
77
|
|
|
73
78
|
render(<UserAvatar />);
|
|
@@ -64,7 +64,7 @@ afterEach(() => {
|
|
|
64
64
|
describe('useMenu', () => {
|
|
65
65
|
it('should provide correct menu items when user is logged in with auth', () => {
|
|
66
66
|
act(() => {
|
|
67
|
-
useUserStore.setState({ isSignedIn: true });
|
|
67
|
+
useUserStore.setState({ isSignedIn: true, enableAuth: () => true });
|
|
68
68
|
});
|
|
69
69
|
enableAuth = true;
|
|
70
70
|
enableClerk = false;
|
|
@@ -104,7 +104,7 @@ describe('useMenu', () => {
|
|
|
104
104
|
|
|
105
105
|
it('should provide correct menu items when user is logged in without auth', () => {
|
|
106
106
|
act(() => {
|
|
107
|
-
useUserStore.setState({ isSignedIn: false });
|
|
107
|
+
useUserStore.setState({ isSignedIn: false, enableAuth: () => false });
|
|
108
108
|
});
|
|
109
109
|
enableAuth = false;
|
|
110
110
|
|
|
@@ -123,7 +123,7 @@ describe('useMenu', () => {
|
|
|
123
123
|
|
|
124
124
|
it('should provide correct menu items when user is not logged in', () => {
|
|
125
125
|
act(() => {
|
|
126
|
-
useUserStore.setState({ isSignedIn: false });
|
|
126
|
+
useUserStore.setState({ isSignedIn: false, enableAuth: () => true });
|
|
127
127
|
});
|
|
128
128
|
enableAuth = true;
|
|
129
129
|
|
|
@@ -6,6 +6,7 @@ import { LobeRuntimeAI } from './BaseAI';
|
|
|
6
6
|
import { LobeAnthropicAI } from './anthropic';
|
|
7
7
|
import { LobeAzureOpenAI } from './azureOpenai';
|
|
8
8
|
import { LobeBedrockAI, LobeBedrockAIParams } from './bedrock';
|
|
9
|
+
import { LobeDeepSeekAI } from './deepseek';
|
|
9
10
|
import { LobeGoogleAI } from './google';
|
|
10
11
|
import { LobeGroq } from './groq';
|
|
11
12
|
import { LobeMinimaxAI } from './minimax';
|
|
@@ -101,6 +102,7 @@ class AgentRuntime {
|
|
|
101
102
|
anthropic: Partial<ClientOptions>;
|
|
102
103
|
azure: { apiVersion?: string; apikey?: string; endpoint?: string };
|
|
103
104
|
bedrock: Partial<LobeBedrockAIParams>;
|
|
105
|
+
deepseek: Partial<ClientOptions>;
|
|
104
106
|
google: { apiKey?: string; baseURL?: string };
|
|
105
107
|
groq: Partial<ClientOptions>;
|
|
106
108
|
minimax: Partial<ClientOptions>;
|
|
@@ -169,6 +171,11 @@ class AgentRuntime {
|
|
|
169
171
|
break;
|
|
170
172
|
}
|
|
171
173
|
|
|
174
|
+
case ModelProvider.DeepSeek: {
|
|
175
|
+
runtimeModel = new LobeDeepSeekAI(params.deepseek ?? {});
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
|
|
172
179
|
case ModelProvider.Minimax: {
|
|
173
180
|
runtimeModel = new LobeMinimaxAI(params.minimax ?? {});
|
|
174
181
|
break;
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import OpenAI from 'openai';
|
|
3
|
+
import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
ChatStreamCallbacks,
|
|
7
|
+
LobeOpenAICompatibleRuntime,
|
|
8
|
+
ModelProvider,
|
|
9
|
+
} from '@/libs/agent-runtime';
|
|
10
|
+
|
|
11
|
+
import * as debugStreamModule from '../utils/debugStream';
|
|
12
|
+
import { LobeDeepSeekAI } from './index';
|
|
13
|
+
|
|
14
|
+
const provider = ModelProvider.DeepSeek;
|
|
15
|
+
const defaultBaseURL = 'https://api.deepseek.com/v1';
|
|
16
|
+
const bizErrorType = 'DeepSeekBizError';
|
|
17
|
+
const invalidErrorType = 'InvalidDeepSeekAPIKey';
|
|
18
|
+
|
|
19
|
+
// Mock the console.error to avoid polluting test output
|
|
20
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
21
|
+
|
|
22
|
+
let instance: LobeOpenAICompatibleRuntime;
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
instance = new LobeDeepSeekAI({ apiKey: 'test' });
|
|
26
|
+
|
|
27
|
+
// 使用 vi.spyOn 来模拟 chat.completions.create 方法
|
|
28
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(
|
|
29
|
+
new ReadableStream() as any,
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterEach(() => {
|
|
34
|
+
vi.clearAllMocks();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('LobeDeepSeekAI', () => {
|
|
38
|
+
describe('init', () => {
|
|
39
|
+
it('should correctly initialize with an API key', async () => {
|
|
40
|
+
const instance = new LobeDeepSeekAI({ apiKey: 'test_api_key' });
|
|
41
|
+
expect(instance).toBeInstanceOf(LobeDeepSeekAI);
|
|
42
|
+
expect(instance.baseURL).toEqual(defaultBaseURL);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('chat', () => {
|
|
47
|
+
describe('Error', () => {
|
|
48
|
+
it('should return OpenAIBizError with an openai error response when OpenAI.APIError is thrown', async () => {
|
|
49
|
+
// Arrange
|
|
50
|
+
const apiError = new OpenAI.APIError(
|
|
51
|
+
400,
|
|
52
|
+
{
|
|
53
|
+
status: 400,
|
|
54
|
+
error: {
|
|
55
|
+
message: 'Bad Request',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
'Error message',
|
|
59
|
+
{},
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
|
63
|
+
|
|
64
|
+
// Act
|
|
65
|
+
try {
|
|
66
|
+
await instance.chat({
|
|
67
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
68
|
+
model: 'deepseek-chat',
|
|
69
|
+
temperature: 0,
|
|
70
|
+
});
|
|
71
|
+
} catch (e) {
|
|
72
|
+
expect(e).toEqual({
|
|
73
|
+
endpoint: defaultBaseURL,
|
|
74
|
+
error: {
|
|
75
|
+
error: { message: 'Bad Request' },
|
|
76
|
+
status: 400,
|
|
77
|
+
},
|
|
78
|
+
errorType: bizErrorType,
|
|
79
|
+
provider,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should throw AgentRuntimeError with NoOpenAIAPIKey if no apiKey is provided', async () => {
|
|
85
|
+
try {
|
|
86
|
+
new LobeDeepSeekAI({});
|
|
87
|
+
} catch (e) {
|
|
88
|
+
expect(e).toEqual({ errorType: invalidErrorType });
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should return OpenAIBizError with the cause when OpenAI.APIError is thrown with cause', async () => {
|
|
93
|
+
// Arrange
|
|
94
|
+
const errorInfo = {
|
|
95
|
+
stack: 'abc',
|
|
96
|
+
cause: {
|
|
97
|
+
message: 'api is undefined',
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
|
101
|
+
|
|
102
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
|
103
|
+
|
|
104
|
+
// Act
|
|
105
|
+
try {
|
|
106
|
+
await instance.chat({
|
|
107
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
108
|
+
model: 'deepseek-chat',
|
|
109
|
+
temperature: 0,
|
|
110
|
+
});
|
|
111
|
+
} catch (e) {
|
|
112
|
+
expect(e).toEqual({
|
|
113
|
+
endpoint: defaultBaseURL,
|
|
114
|
+
error: {
|
|
115
|
+
cause: { message: 'api is undefined' },
|
|
116
|
+
stack: 'abc',
|
|
117
|
+
},
|
|
118
|
+
errorType: bizErrorType,
|
|
119
|
+
provider,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should return OpenAIBizError with an cause response with desensitize Url', async () => {
|
|
125
|
+
// Arrange
|
|
126
|
+
const errorInfo = {
|
|
127
|
+
stack: 'abc',
|
|
128
|
+
cause: { message: 'api is undefined' },
|
|
129
|
+
};
|
|
130
|
+
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
|
131
|
+
|
|
132
|
+
instance = new LobeDeepSeekAI({
|
|
133
|
+
apiKey: 'test',
|
|
134
|
+
|
|
135
|
+
baseURL: 'https://api.abc.com/v1',
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
|
139
|
+
|
|
140
|
+
// Act
|
|
141
|
+
try {
|
|
142
|
+
await instance.chat({
|
|
143
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
144
|
+
model: 'deepseek-chat',
|
|
145
|
+
temperature: 0,
|
|
146
|
+
});
|
|
147
|
+
} catch (e) {
|
|
148
|
+
expect(e).toEqual({
|
|
149
|
+
endpoint: 'https://api.***.com/v1',
|
|
150
|
+
error: {
|
|
151
|
+
cause: { message: 'api is undefined' },
|
|
152
|
+
stack: 'abc',
|
|
153
|
+
},
|
|
154
|
+
errorType: bizErrorType,
|
|
155
|
+
provider,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should throw an InvalidDeepSeekAPIKey error type on 401 status code', async () => {
|
|
161
|
+
// Mock the API call to simulate a 401 error
|
|
162
|
+
const error = new Error('Unauthorized') as any;
|
|
163
|
+
error.status = 401;
|
|
164
|
+
vi.mocked(instance['client'].chat.completions.create).mockRejectedValue(error);
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
await instance.chat({
|
|
168
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
169
|
+
model: 'deepseek-chat',
|
|
170
|
+
temperature: 0,
|
|
171
|
+
});
|
|
172
|
+
} catch (e) {
|
|
173
|
+
// Expect the chat method to throw an error with InvalidDeepSeekAPIKey
|
|
174
|
+
expect(e).toEqual({
|
|
175
|
+
endpoint: defaultBaseURL,
|
|
176
|
+
error: new Error('Unauthorized'),
|
|
177
|
+
errorType: invalidErrorType,
|
|
178
|
+
provider,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('should return AgentRuntimeError for non-OpenAI errors', async () => {
|
|
184
|
+
// Arrange
|
|
185
|
+
const genericError = new Error('Generic Error');
|
|
186
|
+
|
|
187
|
+
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(genericError);
|
|
188
|
+
|
|
189
|
+
// Act
|
|
190
|
+
try {
|
|
191
|
+
await instance.chat({
|
|
192
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
193
|
+
model: 'deepseek-chat',
|
|
194
|
+
temperature: 0,
|
|
195
|
+
});
|
|
196
|
+
} catch (e) {
|
|
197
|
+
expect(e).toEqual({
|
|
198
|
+
endpoint: defaultBaseURL,
|
|
199
|
+
errorType: 'AgentRuntimeError',
|
|
200
|
+
provider,
|
|
201
|
+
error: {
|
|
202
|
+
name: genericError.name,
|
|
203
|
+
cause: genericError.cause,
|
|
204
|
+
message: genericError.message,
|
|
205
|
+
stack: genericError.stack,
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('DEBUG', () => {
|
|
213
|
+
it('should call debugStream and return StreamingTextResponse when DEBUG_DEEPSEEK_CHAT_COMPLETION is 1', async () => {
|
|
214
|
+
// Arrange
|
|
215
|
+
const mockProdStream = new ReadableStream() as any; // 模拟的 prod 流
|
|
216
|
+
const mockDebugStream = new ReadableStream({
|
|
217
|
+
start(controller) {
|
|
218
|
+
controller.enqueue('Debug stream content');
|
|
219
|
+
controller.close();
|
|
220
|
+
},
|
|
221
|
+
}) as any;
|
|
222
|
+
mockDebugStream.toReadableStream = () => mockDebugStream; // 添加 toReadableStream 方法
|
|
223
|
+
|
|
224
|
+
// 模拟 chat.completions.create 返回值,包括模拟的 tee 方法
|
|
225
|
+
(instance['client'].chat.completions.create as Mock).mockResolvedValue({
|
|
226
|
+
tee: () => [mockProdStream, { toReadableStream: () => mockDebugStream }],
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// 保存原始环境变量值
|
|
230
|
+
const originalDebugValue = process.env.DEBUG_DEEPSEEK_CHAT_COMPLETION;
|
|
231
|
+
|
|
232
|
+
// 模拟环境变量
|
|
233
|
+
process.env.DEBUG_DEEPSEEK_CHAT_COMPLETION = '1';
|
|
234
|
+
vi.spyOn(debugStreamModule, 'debugStream').mockImplementation(() => Promise.resolve());
|
|
235
|
+
|
|
236
|
+
// 执行测试
|
|
237
|
+
// 运行你的测试函数,确保它会在条件满足时调用 debugStream
|
|
238
|
+
// 假设的测试函数调用,你可能需要根据实际情况调整
|
|
239
|
+
await instance.chat({
|
|
240
|
+
messages: [{ content: 'Hello', role: 'user' }],
|
|
241
|
+
model: 'deepseek-chat',
|
|
242
|
+
stream: true,
|
|
243
|
+
temperature: 0,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// 验证 debugStream 被调用
|
|
247
|
+
expect(debugStreamModule.debugStream).toHaveBeenCalled();
|
|
248
|
+
|
|
249
|
+
// 恢复原始环境变量值
|
|
250
|
+
process.env.DEBUG_DEEPSEEK_CHAT_COMPLETION = originalDebugValue;
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AgentRuntimeErrorType } from '../error';
|
|
2
|
+
import { ModelProvider } from '../types';
|
|
3
|
+
import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory';
|
|
4
|
+
|
|
5
|
+
export const LobeDeepSeekAI = LobeOpenAICompatibleFactory({
|
|
6
|
+
baseURL: 'https://api.deepseek.com/v1',
|
|
7
|
+
debug: {
|
|
8
|
+
chatCompletion: () => process.env.DEBUG_DEEPSEEK_CHAT_COMPLETION === '1',
|
|
9
|
+
},
|
|
10
|
+
errorType: {
|
|
11
|
+
bizError: AgentRuntimeErrorType.DeepSeekBizError,
|
|
12
|
+
invalidAPIKey: AgentRuntimeErrorType.InvalidDeepSeekAPIKey,
|
|
13
|
+
},
|
|
14
|
+
provider: ModelProvider.DeepSeek,
|
|
15
|
+
});
|
|
@@ -49,6 +49,9 @@ export const AgentRuntimeErrorType = {
|
|
|
49
49
|
|
|
50
50
|
InvalidMinimaxAPIKey: 'InvalidMinimaxAPIKey',
|
|
51
51
|
MinimaxBizError: 'MinimaxBizError',
|
|
52
|
+
|
|
53
|
+
InvalidDeepSeekAPIKey: 'InvalidDeepSeekAPIKey',
|
|
54
|
+
DeepSeekBizError: 'DeepSeekBizError',
|
|
52
55
|
} as const;
|
|
53
56
|
|
|
54
57
|
export type ILobeAgentRuntimeErrorType =
|
|
@@ -3,6 +3,7 @@ export { LobeAnthropicAI } from './anthropic';
|
|
|
3
3
|
export { LobeAzureOpenAI } from './azureOpenai';
|
|
4
4
|
export * from './BaseAI';
|
|
5
5
|
export { LobeBedrockAI } from './bedrock';
|
|
6
|
+
export { LobeDeepSeekAI } from './deepseek';
|
|
6
7
|
export * from './error';
|
|
7
8
|
export { LobeGoogleAI } from './google';
|
|
8
9
|
export { LobeGroq } from './groq';
|
|
@@ -92,6 +92,9 @@ export default {
|
|
|
92
92
|
InvalidBedrockCredentials: 'Bedrock 鉴权未通过,请检查 AccessKeyId/SecretAccessKey 后重试',
|
|
93
93
|
BedrockBizError: '请求 Bedrock 服务出错,请根据以下信息排查或重试',
|
|
94
94
|
|
|
95
|
+
InvalidDeepSeekAPIKey: 'DeepSeek API Key 不正确或为空,请检查 DeepSeek API Key 后重试',
|
|
96
|
+
DeepSeekBizError: '请求 DeepSeek 服务出错,请根据以下信息排查或重试',
|
|
97
|
+
|
|
95
98
|
InvalidAzureAPIKey: 'Azure API Key 不正确或为空,请检查 Azure API Key 后重试',
|
|
96
99
|
AzureBizError: '请求 Azure AI 服务出错,请根据以下信息排查或重试',
|
|
97
100
|
|
|
@@ -62,6 +62,18 @@ export default {
|
|
|
62
62
|
title: '使用自定义 Bedrock 鉴权信息',
|
|
63
63
|
},
|
|
64
64
|
},
|
|
65
|
+
deepseek: {
|
|
66
|
+
title: 'DeepSeek',
|
|
67
|
+
token: {
|
|
68
|
+
desc: '填入来自 DeepSeek 的 API Key',
|
|
69
|
+
placeholder: 'DeepSeek API Key',
|
|
70
|
+
title: 'API Key',
|
|
71
|
+
},
|
|
72
|
+
unlock: {
|
|
73
|
+
description: '输入你的 DeepSeek API Key 即可开始会话。应用不会记录你的 API Key',
|
|
74
|
+
title: '使用自定义 DeepSeek API Key',
|
|
75
|
+
},
|
|
76
|
+
},
|
|
65
77
|
google: {
|
|
66
78
|
title: 'Google',
|
|
67
79
|
token: {
|
|
@@ -22,20 +22,12 @@ export interface V3LegacyConfig {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export interface V3LLMConfig {
|
|
25
|
-
anthropic: V3GeneralConfig;
|
|
26
25
|
bedrock: any;
|
|
27
26
|
google: V3GeneralConfig;
|
|
28
|
-
groq: V3GeneralConfig;
|
|
29
|
-
minimax: V3GeneralConfig;
|
|
30
|
-
mistral: V3GeneralConfig;
|
|
31
|
-
moonshot: V3GeneralConfig;
|
|
32
27
|
ollama: V3LegacyConfig;
|
|
33
28
|
openAI: V3OpenAIConfig;
|
|
34
29
|
openrouter: V3LegacyConfig;
|
|
35
|
-
perplexity: V3GeneralConfig;
|
|
36
30
|
togetherai: V3LegacyConfig;
|
|
37
|
-
zeroone: V3GeneralConfig;
|
|
38
|
-
zhipu: V3GeneralConfig;
|
|
39
31
|
}
|
|
40
32
|
|
|
41
33
|
/**
|