@lobehub/chat 0.109.0 → 0.110.0

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.
@@ -0,0 +1,228 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { DEFAULT_AVATAR } from '@/const/meta';
4
+ import { DEFAULT_AGENT_CONFIG, DEFAUTT_AGENT_TTS_CONFIG } from '@/const/settings';
5
+ import { SessionStore } from '@/store/session';
6
+ import { MetaData } from '@/types/meta';
7
+ import { LobeAgentSession, LobeSessionType } from '@/types/session';
8
+
9
+ import { agentSelectors } from '../agent';
10
+
11
+ vi.mock('i18next', () => ({
12
+ t: vi.fn((key) => key), // Simplified mock return value
13
+ }));
14
+
15
+ const mockSessionStore = {
16
+ activeId: '1',
17
+ sessions: [
18
+ {
19
+ id: '1',
20
+ config: DEFAULT_AGENT_CONFIG,
21
+ meta: {
22
+ title: 'title1',
23
+ description: 'description1',
24
+ },
25
+ type: LobeSessionType.Agent,
26
+ } as LobeAgentSession,
27
+ {
28
+ id: '2',
29
+ meta: {
30
+ title: 'title2',
31
+ description: 'description2',
32
+ },
33
+ config: DEFAULT_AGENT_CONFIG,
34
+ type: LobeSessionType.Agent,
35
+ } as LobeAgentSession,
36
+ ],
37
+ } as unknown as SessionStore;
38
+
39
+ describe('agentSelectors', () => {
40
+ describe('currentAgentConfig', () => {
41
+ it('should return the merged default and session-specific agent config', () => {
42
+ const config = agentSelectors.currentAgentConfig(mockSessionStore);
43
+ expect(config).toEqual(expect.objectContaining(mockSessionStore.sessions[0].config));
44
+ });
45
+ });
46
+
47
+ describe('currentAgentModel', () => {
48
+ it('should return the model from the agent config', () => {
49
+ const model = agentSelectors.currentAgentModel(mockSessionStore);
50
+ expect(model).toBe(mockSessionStore.sessions[0].config.model);
51
+ });
52
+ });
53
+
54
+ describe('currentAgentMeta', () => {
55
+ it('should return the merged default and session-specific meta data', () => {
56
+ const meta = agentSelectors.currentAgentMeta(mockSessionStore);
57
+ expect(meta).toEqual(expect.objectContaining(mockSessionStore.sessions[0].meta));
58
+ });
59
+
60
+ it('should return inbox defaults if it is an inbox session', () => {
61
+ // Assume sessionSelectors.isInboxSession() is mocked to return true for this test
62
+ const meta = agentSelectors.currentAgentMeta(mockSessionStore);
63
+ expect(meta.avatar).toBe(DEFAULT_AVATAR);
64
+ });
65
+ });
66
+
67
+ describe('currentAgentTitle', () => {
68
+ it('should return the title from the session meta data', () => {
69
+ const title = agentSelectors.currentAgentTitle(mockSessionStore);
70
+ expect(title).toBe(mockSessionStore.sessions[0].meta.title);
71
+ });
72
+ });
73
+
74
+ describe('currentAgentDescription', () => {
75
+ it('should return the description from the session meta data', () => {
76
+ const description = agentSelectors.currentAgentDescription(mockSessionStore);
77
+ expect(description).toBe(mockSessionStore.sessions[0].meta.description);
78
+ });
79
+ });
80
+
81
+ // ... More tests for other selectors
82
+
83
+ describe('hasSystemRole', () => {
84
+ it('should return true if the system role is defined in the agent config', () => {
85
+ const hasRole = agentSelectors.hasSystemRole(mockSessionStore);
86
+ expect(hasRole).toBe(false);
87
+ });
88
+
89
+ it('should return false if the system role is not defined in the agent config', () => {
90
+ const modifiedSessionStore = {
91
+ ...mockSessionStore,
92
+ sessions: [
93
+ {
94
+ ...mockSessionStore.sessions[0],
95
+ config: {
96
+ ...mockSessionStore.sessions[0].config,
97
+ systemRole: 'test',
98
+ },
99
+ },
100
+ ],
101
+ };
102
+ const hasRole = agentSelectors.hasSystemRole(modifiedSessionStore);
103
+ expect(hasRole).toBe(true);
104
+ });
105
+ });
106
+
107
+ describe('currentAgentTTS', () => {
108
+ it('should return the TTS config from the agent config', () => {
109
+ const ttsConfig = agentSelectors.currentAgentTTS(mockSessionStore);
110
+ expect(ttsConfig).toEqual(mockSessionStore.sessions[0].config.tts);
111
+ });
112
+
113
+ it('should return the default TTS config if none is defined in the agent config', () => {
114
+ const modifiedSessionStore = {
115
+ ...mockSessionStore,
116
+ sessions: [
117
+ {
118
+ ...mockSessionStore.sessions[0],
119
+ config: {
120
+ ...mockSessionStore.sessions[0].config,
121
+ tts: DEFAUTT_AGENT_TTS_CONFIG,
122
+ },
123
+ },
124
+ ],
125
+ };
126
+ const ttsConfig = agentSelectors.currentAgentTTS(modifiedSessionStore);
127
+ expect(ttsConfig).toEqual(DEFAUTT_AGENT_TTS_CONFIG);
128
+ });
129
+ });
130
+
131
+ describe('currentAgentTTSVoice', () => {
132
+ it('should return the appropriate TTS voice based on the service and language', () => {
133
+ const lang = 'en';
134
+ const ttsVoice = agentSelectors.currentAgentTTSVoice(lang)(mockSessionStore);
135
+ expect(ttsVoice).toBe(mockSessionStore.sessions[0].config.tts.voice.openai);
136
+ });
137
+ });
138
+
139
+ describe('getAvatar', () => {
140
+ it('should return the avatar from the meta data', () => {
141
+ const meta: MetaData = { avatar: 'custom-avatar.png' };
142
+ const avatar = agentSelectors.getAvatar(meta);
143
+ expect(avatar).toBe(meta.avatar);
144
+ });
145
+
146
+ it('should return the default avatar if none is defined in the meta data', () => {
147
+ const meta: MetaData = {};
148
+ const avatar = agentSelectors.getAvatar(meta);
149
+ expect(avatar).toBe(DEFAULT_AVATAR);
150
+ });
151
+ });
152
+
153
+ describe('getTitle', () => {
154
+ it('should return the title from the meta data', () => {
155
+ const meta: MetaData = { title: 'Custom Title' };
156
+ const title = agentSelectors.getTitle(meta);
157
+ expect(title).toBe(meta.title);
158
+ });
159
+
160
+ it('should return the default title if none is defined in the meta data', () => {
161
+ const meta: MetaData = {};
162
+ const title = agentSelectors.getTitle(meta);
163
+ expect(title).toBe('defaultSession'); // Assuming translation returns this key
164
+ });
165
+ });
166
+
167
+ describe('getDescription', () => {
168
+ it('should return the description from the meta data', () => {
169
+ const meta: MetaData = { description: 'Custom Description' };
170
+ const description = agentSelectors.getDescription(meta);
171
+ expect(description).toBe(meta.description);
172
+ });
173
+
174
+ it('should return the default description if none is defined in the meta data', () => {
175
+ const meta: MetaData = {};
176
+ const description = agentSelectors.getDescription(meta);
177
+ expect(description).toBe('noDescription'); // Assuming translation returns this key
178
+ });
179
+ });
180
+
181
+ describe('showTokenTag', () => {
182
+ it('should return true if the model is in the list of models that show tokens', () => {
183
+ const show = agentSelectors.showTokenTag(mockSessionStore);
184
+ expect(show).toBe(true);
185
+ });
186
+
187
+ it('should return false if the model is not in the list of models that show tokens', () => {
188
+ const modifiedSessionStore = {
189
+ ...mockSessionStore,
190
+ sessions: [
191
+ {
192
+ ...mockSessionStore.sessions[0],
193
+ config: {
194
+ ...mockSessionStore.sessions[0].config,
195
+ model: 'some-other-model',
196
+ },
197
+ },
198
+ ],
199
+ };
200
+ const show = agentSelectors.showTokenTag(modifiedSessionStore);
201
+ expect(show).toBe(false);
202
+ });
203
+ });
204
+
205
+ describe('modelHasVisionAbility', () => {
206
+ it('should return true if the model has vision ability', () => {
207
+ const hasAbility = agentSelectors.modelHasVisionAbility(mockSessionStore);
208
+ expect(hasAbility).toBe(false);
209
+ });
210
+
211
+ it('should return false if the model does not have vision ability', () => {
212
+ const modifiedSessionStore = {
213
+ ...mockSessionStore,
214
+ sessions: [
215
+ {
216
+ ...mockSessionStore.sessions[0],
217
+ config: {
218
+ ...mockSessionStore.sessions[0].config,
219
+ model: 'some-other-model',
220
+ },
221
+ },
222
+ ],
223
+ };
224
+ const hasAbility = agentSelectors.modelHasVisionAbility(modifiedSessionStore);
225
+ expect(hasAbility).toBe(false);
226
+ });
227
+ });
228
+ });
@@ -1,3 +1,4 @@
1
+ import { VoiceList } from '@lobehub/tts';
1
2
  import { t } from 'i18next';
2
3
 
3
4
  import { DEFAULT_OPENAI_MODEL_LIST, VISION_MODEL_WHITE_LIST } from '@/const/llm';
@@ -46,6 +47,29 @@ const currentAgentTTS = (s: SessionStore): LobeAgentTTSConfig => {
46
47
  return config?.tts || DEFAUTT_AGENT_TTS_CONFIG;
47
48
  };
48
49
 
50
+ const currentAgentTTSVoice =
51
+ (lang: string) =>
52
+ (s: SessionStore): string => {
53
+ const { voice, ttsService } = currentAgentTTS(s);
54
+ const voiceList = new VoiceList(lang);
55
+ let currentVoice;
56
+ switch (ttsService) {
57
+ case 'openai': {
58
+ currentVoice = voice.openai || (VoiceList.openaiVoiceOptions?.[0].value as string);
59
+ break;
60
+ }
61
+ case 'edge': {
62
+ currentVoice = voice.edge || (voiceList.edgeVoiceOptions?.[0].value as string);
63
+ break;
64
+ }
65
+ case 'microsoft': {
66
+ currentVoice = voice.microsoft || (voiceList.microsoftVoiceOptions?.[0].value as string);
67
+ break;
68
+ }
69
+ }
70
+ return currentVoice || 'alloy';
71
+ };
72
+
49
73
  // ========== Meta ============== //
50
74
  const currentAgentMeta = (s: SessionStore): MetaData => {
51
75
  const isInbox = sessionSelectors.isInboxSession(s);
@@ -97,6 +121,7 @@ export const agentSelectors = {
97
121
  currentAgentPlugins,
98
122
  currentAgentSystemRole,
99
123
  currentAgentTTS,
124
+ currentAgentTTSVoice,
100
125
  currentAgentTitle,
101
126
  getAvatar,
102
127
  getDescription,
@@ -7,7 +7,7 @@ const getPluginFormList = (pluginList: LobeChatPluginMeta[], id: string) =>
7
7
 
8
8
  const getPluginTitle = (meta?: LobeChatPluginMeta['meta']) => meta?.title;
9
9
  const getPluginDesc = (meta?: LobeChatPluginMeta['meta']) => meta?.description;
10
- const getPluginAvatar = (meta?: LobeChatPluginMeta['meta']) => meta?.avatar;
10
+ const getPluginAvatar = (meta?: LobeChatPluginMeta['meta']) => meta?.avatar || '🧩';
11
11
 
12
12
  const isCustomPlugin = (id: string, pluginList: CustomPlugin[]) =>
13
13
  pluginList.some((i) => i.identifier === id);
@@ -18,7 +18,9 @@ export interface ChatTranslate extends Translate {
18
18
  }
19
19
 
20
20
  export interface ChatTTS {
21
- init?: boolean;
21
+ contentMd5?: string;
22
+ file?: string;
23
+ voice?: string;
22
24
  }
23
25
 
24
26
  export interface ChatPluginPayload {
package/vitest.config.ts CHANGED
@@ -2,6 +2,10 @@ import { resolve } from 'node:path';
2
2
  import { defineConfig } from 'vitest/config';
3
3
 
4
4
  export default defineConfig({
5
+ optimizeDeps: {
6
+ exclude: ['crypto', 'util', 'tty'],
7
+ include: ['@lobehub/tts'],
8
+ },
5
9
  test: {
6
10
  alias: {
7
11
  '@': resolve(__dirname, './src'),
@@ -1,122 +0,0 @@
1
- import { AudioPlayer } from '@lobehub/tts/react';
2
- import { ActionIcon, Alert, Highlighter } from '@lobehub/ui';
3
- import { Button } from 'antd';
4
- import { TrashIcon } from 'lucide-react';
5
- import { memo, useCallback, useEffect, useState } from 'react';
6
- import { useTranslation } from 'react-i18next';
7
- import { Flexbox } from 'react-layout-kit';
8
-
9
- import { useTTS } from '@/hooks/useTTS';
10
- import { useChatStore } from '@/store/chat';
11
- import { ChatMessageError, ChatTTS } from '@/types/chatMessage';
12
- import { getMessageError } from '@/utils/fetch';
13
-
14
- interface TTSProps extends ChatTTS {
15
- content: string;
16
- id: string;
17
- loading?: boolean;
18
- }
19
-
20
- const TTS = memo<TTSProps>(({ id, init, content }) => {
21
- const [isStart, setIsStart] = useState(false);
22
- const [error, setError] = useState<ChatMessageError>();
23
- const { t } = useTranslation('chat');
24
-
25
- const [ttsMessage, clearTTS] = useChatStore((s) => [s.ttsMessage, s.clearTTS]);
26
-
27
- const setDefaultError = useCallback(
28
- (err?: any) => {
29
- setError({ body: err, message: t('tts.responseError', { ns: 'error' }), type: 500 });
30
- },
31
- [t],
32
- );
33
-
34
- const { isGlobalLoading, audio, start, stop, response } = useTTS(content, {
35
- onError: (err) => {
36
- stop();
37
- setDefaultError(err);
38
- },
39
- onErrorRetry: (err) => {
40
- stop();
41
- setDefaultError(err);
42
- },
43
- onSuccess: async () => {
44
- if (!response) return;
45
- if (response.status === 200) return ttsMessage(id, true);
46
- const message = await getMessageError(response);
47
- if (message) {
48
- setError(message);
49
- } else {
50
- setDefaultError();
51
- }
52
- stop();
53
- },
54
- });
55
-
56
- const handleInitStart = useCallback(() => {
57
- if (isStart) return;
58
- start();
59
- setIsStart(true);
60
- }, [isStart]);
61
-
62
- const handleDelete = useCallback(() => {
63
- stop();
64
- clearTTS(id);
65
- }, [stop, id]);
66
-
67
- const handleRetry = useCallback(() => {
68
- setError(undefined);
69
- start();
70
- }, [start]);
71
-
72
- useEffect(() => {
73
- if (init) return;
74
- handleInitStart();
75
- }, [init]);
76
-
77
- return (
78
- <Flexbox align={'center'} horizontal style={{ minWidth: 160, width: '100%' }}>
79
- {error ? (
80
- <Alert
81
- action={
82
- <Button onClick={handleRetry} size={'small'} type={'primary'}>
83
- {t('retry', { ns: 'common' })}
84
- </Button>
85
- }
86
- closable
87
- extra={
88
- error.body && (
89
- <Highlighter copyButtonSize={'small'} language={'json'} type={'pure'}>
90
- {JSON.stringify(error.body, null, 2)}
91
- </Highlighter>
92
- )
93
- }
94
- message={error.message}
95
- onClose={handleDelete}
96
- style={{ alignItems: 'center', width: '100%' }}
97
- type="error"
98
- />
99
- ) : (
100
- <>
101
- <AudioPlayer
102
- audio={audio}
103
- buttonSize={'small'}
104
- isLoading={isGlobalLoading}
105
- onInitPlay={handleInitStart}
106
- onLoadingStop={stop}
107
- timeRender={'tag'}
108
- timeStyle={{ margin: 0 }}
109
- />
110
- <ActionIcon
111
- icon={TrashIcon}
112
- onClick={handleDelete}
113
- size={'small'}
114
- title={t('tts.clear')}
115
- />
116
- </>
117
- )}
118
- </Flexbox>
119
- );
120
- });
121
-
122
- export default TTS;