@lobehub/chat 0.119.10 → 0.119.11

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.
Files changed (37) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/locales/de-DE/chat.json +9 -4
  3. package/locales/en-US/chat.json +9 -4
  4. package/locales/es-ES/chat.json +9 -4
  5. package/locales/fr-FR/chat.json +9 -4
  6. package/locales/it-IT/chat.json +9 -4
  7. package/locales/ja-JP/chat.json +9 -4
  8. package/locales/ko-KR/chat.json +9 -4
  9. package/locales/nl-NL/chat.json +9 -4
  10. package/locales/pl-PL/chat.json +9 -4
  11. package/locales/pt-BR/chat.json +9 -4
  12. package/locales/ru-RU/chat.json +9 -4
  13. package/locales/tr-TR/chat.json +9 -4
  14. package/locales/vi-VN/chat.json +9 -4
  15. package/locales/zh-CN/chat.json +9 -4
  16. package/locales/zh-TW/chat.json +9 -4
  17. package/package.json +1 -1
  18. package/src/app/chat/(desktop)/features/ChatInput/Footer/index.tsx +164 -0
  19. package/src/app/chat/(desktop)/features/ChatInput/Header/index.tsx +33 -0
  20. package/src/app/chat/(desktop)/features/ChatInput/TextArea.test.tsx +316 -0
  21. package/src/app/chat/(desktop)/features/ChatInput/TextArea.tsx +118 -0
  22. package/src/app/chat/(desktop)/features/ChatInput/index.tsx +41 -110
  23. package/src/app/chat/(mobile)/features/ChatHeaderTitle.tsx +1 -2
  24. package/src/app/chat/features/ChatHeader/SettingButton.tsx +1 -1
  25. package/src/app/chat/features/ChatHeader/ShareButton/useScreenshot.ts +1 -1
  26. package/src/features/ChatInput/useChatInput.ts +2 -8
  27. package/src/features/ChatInput/useSend.ts +9 -2
  28. package/src/locales/default/chat.ts +8 -3
  29. package/src/store/chat/slices/message/action.test.ts +7 -7
  30. package/src/store/chat/slices/message/action.ts +11 -2
  31. package/src/store/chat/slices/message/initialState.ts +0 -3
  32. package/src/store/global/slices/common/initialState.ts +5 -0
  33. package/src/store/global/slices/common/selectors.ts +4 -1
  34. package/src/utils/keyboard.ts +13 -0
  35. package/src/utils/platform.ts +4 -0
  36. /package/src/app/chat/(desktop)/features/ChatInput/{LocalFiles.tsx → Footer/LocalFiles.tsx} +0 -0
  37. /package/src/app/chat/(desktop)/features/ChatInput/{DragUpload.tsx → Header/DragUpload.tsx} +0 -0
@@ -1,124 +1,55 @@
1
- import { ActionIcon, ChatInputArea, ChatSendButton } from '@lobehub/ui';
2
- import { Maximize2, Minimize2 } from 'lucide-react';
3
- import { memo, useCallback, useEffect, useMemo } from 'react';
4
- import { useTranslation } from 'react-i18next';
1
+ import { DraggablePanel } from '@lobehub/ui';
2
+ import { memo, useState } from 'react';
3
+ import { Flexbox } from 'react-layout-kit';
5
4
 
6
5
  import {
7
6
  CHAT_TEXTAREA_HEIGHT,
8
7
  CHAT_TEXTAREA_MAX_HEIGHT,
9
8
  HEADER_HEIGHT,
10
9
  } from '@/const/layoutTokens';
11
- import ActionBar from '@/features/ChatInput/ActionBar';
12
- import SaveTopic from '@/features/ChatInput/Topic';
13
- import { useChatInput } from '@/features/ChatInput/useChatInput';
10
+ import { useGlobalStore } from '@/store/global';
14
11
 
15
- import DragUpload from './DragUpload';
16
- import { LocalFiles } from './LocalFiles';
17
- import { useAutoFocus } from './useAutoFocus';
12
+ import Footer from './Footer';
13
+ import Head from './Header';
14
+ import TextArea from './TextArea';
18
15
 
19
- const ChatInputDesktopLayout = memo(() => {
20
- const { t } = useTranslation('chat');
21
- const {
22
- ref,
23
- onStop,
24
- onSend,
25
- loading,
26
- value,
27
- onInput,
28
- expand,
29
- setExpand,
30
- inputHeight,
31
- updatePreference,
32
- canUpload,
33
- } = useChatInput();
16
+ const ChatInput = memo(() => {
17
+ const [expand, setExpand] = useState<boolean>(false);
34
18
 
35
- useAutoFocus(ref);
36
-
37
- const handleSizeChange = useCallback(
38
- (_: any, size: any) => {
39
- if (!size) return;
40
- updatePreference({
41
- inputHeight: typeof size.height === 'string' ? Number.parseInt(size.height) : size.height,
42
- });
43
- },
44
- [updatePreference],
45
- );
46
-
47
- const buttonAddons = useMemo(
48
- () => (
49
- <ChatSendButton
50
- leftAddons={canUpload && <LocalFiles />}
51
- loading={loading}
52
- onSend={onSend}
53
- onStop={onStop}
54
- rightAddons={<SaveTopic />}
55
- texts={{
56
- send: t('send'),
57
- stop: t('stop'),
58
- warp: t('warp'),
59
- }}
60
- />
61
- ),
62
- [canUpload, loading, onSend, onStop, t],
63
- );
64
-
65
- const topAddons = useMemo(
66
- () => (
67
- <ActionBar
68
- rightAreaEndRender={
69
- <ActionIcon
70
- icon={expand ? Minimize2 : Maximize2}
71
- onClick={() => {
72
- setExpand(!expand);
73
- }}
74
- />
75
- }
76
- />
77
- ),
78
- [expand],
79
- );
80
-
81
- const hasValue = !!value;
82
-
83
- useEffect(() => {
84
- const fn = (e: BeforeUnloadEvent) => {
85
- if (hasValue) {
86
- // set returnValue to trigger alert modal
87
- // Note: No matter what value is set, the browser will display the standard text
88
- e.returnValue = '你有正在输入中的内容,确定要离开吗?';
89
- }
90
- };
91
-
92
- window.addEventListener('beforeunload', fn);
93
- return () => {
94
- window.removeEventListener('beforeunload', fn);
95
- };
96
- }, [hasValue]);
19
+ const [inputHeight, updatePreference] = useGlobalStore((s) => [
20
+ s.preference.inputHeight,
21
+ s.updatePreference,
22
+ ]);
97
23
 
98
24
  return (
99
- <>
100
- {canUpload && <DragUpload />}
101
- <ChatInputArea
102
- autoFocus
103
- bottomAddons={buttonAddons}
104
- expand={expand}
105
- heights={{
106
- headerHeight: HEADER_HEIGHT,
107
- inputHeight,
108
- maxHeight: CHAT_TEXTAREA_MAX_HEIGHT,
109
- minHeight: CHAT_TEXTAREA_HEIGHT,
110
- }}
111
- loading={loading}
112
- onInput={onInput}
113
- onSend={onSend}
114
- onSizeChange={handleSizeChange}
115
- placeholder={t('sendPlaceholder')}
116
- ref={ref}
117
- topAddons={topAddons}
118
- value={value}
119
- />
120
- </>
25
+ <DraggablePanel
26
+ fullscreen={expand}
27
+ headerHeight={HEADER_HEIGHT}
28
+ maxHeight={CHAT_TEXTAREA_MAX_HEIGHT}
29
+ minHeight={CHAT_TEXTAREA_HEIGHT}
30
+ onSizeChange={(_, size) => {
31
+ if (!size) return;
32
+
33
+ updatePreference({
34
+ inputHeight: typeof size.height === 'string' ? Number.parseInt(size.height) : size.height,
35
+ });
36
+ }}
37
+ placement="bottom"
38
+ size={{ height: inputHeight, width: '100%' }}
39
+ style={{ zIndex: 10 }}
40
+ >
41
+ <Flexbox
42
+ gap={8}
43
+ height={'100%'}
44
+ padding={'12px 0 16px'}
45
+ style={{ minHeight: CHAT_TEXTAREA_HEIGHT, position: 'relative' }}
46
+ >
47
+ <Head expand={expand} setExpand={setExpand} />
48
+ <TextArea setExpand={setExpand} />
49
+ <Footer />
50
+ </Flexbox>
51
+ </DraggablePanel>
121
52
  );
122
53
  });
123
54
 
124
- export default ChatInputDesktopLayout;
55
+ export default ChatInput;
@@ -9,8 +9,7 @@ import { useChatStore } from '@/store/chat';
9
9
  import { topicSelectors } from '@/store/chat/selectors';
10
10
  import { useGlobalStore } from '@/store/global';
11
11
  import { useSessionStore } from '@/store/session';
12
- import { agentSelectors } from '@/store/session/slices/agent';
13
- import { sessionSelectors } from '@/store/session/slices/session/selectors';
12
+ import { agentSelectors, sessionSelectors } from '@/store/session/selectors';
14
13
 
15
14
  const ChatHeaderTitle = memo(() => {
16
15
  const { t } = useTranslation('chat');
@@ -8,7 +8,7 @@ import { DESKTOP_HEADER_ICON_SIZE, MOBILE_HEADER_ICON_SIZE } from '@/const/layou
8
8
  import { useGlobalStore } from '@/store/global';
9
9
  import { SettingsTabs, SidebarTabKey } from '@/store/global/slices/common/initialState';
10
10
  import { useSessionStore } from '@/store/session';
11
- import { sessionSelectors } from '@/store/session/slices/session/selectors';
11
+ import { sessionSelectors } from '@/store/session/selectors';
12
12
  import { pathString } from '@/utils/url';
13
13
 
14
14
  const SettingButton = memo<{ mobile?: boolean }>(({ mobile }) => {
@@ -3,7 +3,7 @@ import { domToJpeg, domToPng, domToSvg, domToWebp } from 'modern-screenshot';
3
3
  import { useCallback, useState } from 'react';
4
4
 
5
5
  import { useSessionStore } from '@/store/session';
6
- import { agentSelectors } from '@/store/session/slices/agent';
6
+ import { agentSelectors } from '@/store/session/selectors';
7
7
 
8
8
  import { ImageType } from './type';
9
9
 
@@ -3,9 +3,8 @@ import { useCallback, useRef, useState } from 'react';
3
3
 
4
4
  import { useIsMobile } from '@/hooks/useIsMobile';
5
5
  import { useChatStore } from '@/store/chat';
6
- import { useGlobalStore } from '@/store/global';
7
6
  import { useSessionStore } from '@/store/session';
8
- import { agentSelectors } from '@/store/session/slices/agent';
7
+ import { agentSelectors } from '@/store/session/selectors';
9
8
 
10
9
  import { useSendMessage } from './useSend';
11
10
 
@@ -13,10 +12,7 @@ export const useChatInput = () => {
13
12
  const ref = useRef<TextAreaRef>(null);
14
13
  const [expand, setExpand] = useState<boolean>(false);
15
14
  const onSend = useSendMessage();
16
- const [inputHeight, updatePreference] = useGlobalStore((s) => [
17
- s.preference.inputHeight,
18
- s.updatePreference,
19
- ]);
15
+
20
16
  const canUpload = useSessionStore(agentSelectors.modelHasVisionAbility);
21
17
  const [loading, value, onInput, onStop] = useChatStore((s) => [
22
18
  !!s.chatLoadingId,
@@ -37,14 +33,12 @@ export const useChatInput = () => {
37
33
  return {
38
34
  canUpload,
39
35
  expand,
40
- inputHeight,
41
36
  loading,
42
37
  onInput,
43
38
  onSend: handleSend,
44
39
  onStop,
45
40
  ref,
46
41
  setExpand,
47
- updatePreference,
48
42
  value,
49
43
  };
50
44
  };
@@ -9,12 +9,19 @@ export const useSendMessage = () => {
9
9
  s.updateInputMessage,
10
10
  ]);
11
11
 
12
- return useCallback(() => {
12
+ return useCallback((onlyAddUserMessage?: boolean) => {
13
13
  const store = useChatStore.getState();
14
14
  if (!!store.chatLoadingId) return;
15
+ if (!store.inputMessage) return;
16
+
15
17
  const imageList = filesSelectors.imageUrlOrBase64List(useFileStore.getState());
16
18
 
17
- sendMessage(store.inputMessage, imageList);
19
+ sendMessage({
20
+ files: imageList,
21
+ message: store.inputMessage,
22
+ onlyAddUserMessage: onlyAddUserMessage,
23
+ });
24
+
18
25
  updateInputMessage('');
19
26
  useFileStore.getState().clearImageList();
20
27
  }, []);
@@ -16,6 +16,14 @@ export default {
16
16
  desc: '开启大脑集群,激发思维火花。你的智能助理,在这里与你交流一切',
17
17
  title: '随便聊聊',
18
18
  },
19
+ input: {
20
+ onlyAdd: '仅添加消息',
21
+ send: '发送',
22
+ sendWithCmdEnter: '按 {{meta}} + Enter 键发送',
23
+ sendWithEnter: '按 Enter 键发送',
24
+ stop: '停止',
25
+ warp: '换行',
26
+ },
19
27
  messageAction: {
20
28
  delAndRegenerate: '删除并重新生成',
21
29
  regenerate: '重新生成',
@@ -27,7 +35,6 @@ export default {
27
35
  regenerate: '重新生成',
28
36
  roleAndArchive: '角色与记录',
29
37
  searchAgentPlaceholder: '搜索助手和对话...',
30
- send: '发送',
31
38
  sendPlaceholder: '输入聊天内容...',
32
39
  sessionList: '助手列表',
33
40
  shareModal: {
@@ -41,7 +48,6 @@ export default {
41
48
  withPluginInfo: '包含插件信息',
42
49
  withSystemRole: '包含助手角色设定',
43
50
  },
44
- stop: '停止',
45
51
  stt: {
46
52
  action: '语音输入',
47
53
  loading: '识别中...',
@@ -90,5 +96,4 @@ export default {
90
96
  dragDesc: '拖拽文件到这里,支持上传多个图片。按住 Shift 直接发送图片',
91
97
  dragTitle: '上传图片',
92
98
  },
93
- warp: '换行',
94
99
  };
@@ -179,7 +179,7 @@ describe('chatMessage actions', () => {
179
179
  const message = 'Test message';
180
180
 
181
181
  await act(async () => {
182
- await result.current.sendMessage(message);
182
+ await result.current.sendMessage({ message });
183
183
  });
184
184
 
185
185
  expect(messageService.create).not.toHaveBeenCalled();
@@ -192,7 +192,7 @@ describe('chatMessage actions', () => {
192
192
  const message = '';
193
193
 
194
194
  await act(async () => {
195
- await result.current.sendMessage(message);
195
+ await result.current.sendMessage({ message });
196
196
  });
197
197
 
198
198
  expect(messageService.create).not.toHaveBeenCalled();
@@ -205,7 +205,7 @@ describe('chatMessage actions', () => {
205
205
  const message = '';
206
206
 
207
207
  await act(async () => {
208
- await result.current.sendMessage(message, []);
208
+ await result.current.sendMessage({ message, files: [] });
209
209
  });
210
210
 
211
211
  expect(messageService.create).not.toHaveBeenCalled();
@@ -222,7 +222,7 @@ describe('chatMessage actions', () => {
222
222
  (messageService.create as Mock).mockResolvedValue('new-message-id');
223
223
 
224
224
  await act(async () => {
225
- await result.current.sendMessage(message, files);
225
+ await result.current.sendMessage({ message, files });
226
226
  });
227
227
 
228
228
  expect(messageService.create).toHaveBeenCalledWith({
@@ -269,7 +269,7 @@ describe('chatMessage actions', () => {
269
269
  switchTopic: switchTopicMock,
270
270
  });
271
271
 
272
- await result.current.sendMessage(message);
272
+ await result.current.sendMessage({ message });
273
273
  });
274
274
 
275
275
  expect(saveToTopicMock).not.toHaveBeenCalled();
@@ -310,7 +310,7 @@ describe('chatMessage actions', () => {
310
310
  });
311
311
 
312
312
  await act(async () => {
313
- await result.current.sendMessage(message);
313
+ await result.current.sendMessage({ message });
314
314
  });
315
315
 
316
316
  expect(saveToTopicMock).toHaveBeenCalled();
@@ -349,7 +349,7 @@ describe('chatMessage actions', () => {
349
349
  switchTopic: switchTopicMock,
350
350
  });
351
351
 
352
- await result.current.sendMessage(message);
352
+ await result.current.sendMessage({ message });
353
353
  });
354
354
 
355
355
  expect(saveToTopicMock).not.toHaveBeenCalled();
@@ -24,10 +24,16 @@ import { MessageDispatch, messagesReducer } from './reducer';
24
24
 
25
25
  const n = setNamespace('message');
26
26
 
27
+ interface SendMessageParams {
28
+ message: string;
29
+ files?: { id: string; url: string }[];
30
+ onlyAddUserMessage?: boolean;
31
+ }
32
+
27
33
  export interface ChatMessageAction {
28
34
  // create
29
35
  resendMessage: (id: string) => Promise<void>;
30
- sendMessage: (text: string, images?: { id: string; url: string }[]) => Promise<void>;
36
+ sendMessage: (params: SendMessageParams) => Promise<void>;
31
37
  // delete
32
38
  /**
33
39
  * clear message on the active session
@@ -153,7 +159,7 @@ export const chatMessage: StateCreator<
153
159
 
154
160
  await coreProcessMessage(contextMessages, latestMsg.id);
155
161
  },
156
- sendMessage: async (message, files) => {
162
+ sendMessage: async ({ message, files, onlyAddUserMessage }) => {
157
163
  const { coreProcessMessage, activeTopicId, activeId } = get();
158
164
  if (!activeId) return;
159
165
 
@@ -175,6 +181,9 @@ export const chatMessage: StateCreator<
175
181
  const id = await messageService.create(newMessage);
176
182
  await get().refreshMessages();
177
183
 
184
+ // if only add user message, then stop
185
+ if (onlyAddUserMessage) return;
186
+
178
187
  // Get the current messages to generate AI response
179
188
  const messages = chatSelectors.currentChats(get());
180
189
 
@@ -7,9 +7,6 @@ export interface ChatMessageState {
7
7
  * @description 当前正在编辑或查看的会话
8
8
  */
9
9
  activeId: string;
10
- /**
11
- * @deprecated
12
- */
13
10
  chatLoadingId?: string;
14
11
  inputMessage: string;
15
12
  messageLoadingIds: [];
@@ -25,6 +25,10 @@ export interface GlobalPreference {
25
25
  showChatSideBar?: boolean;
26
26
  showSessionPanel?: boolean;
27
27
  showSystemRole?: boolean;
28
+ /**
29
+ * whether to use cmd + enter to send message
30
+ */
31
+ useCmdEnterToSend?: boolean;
28
32
  }
29
33
 
30
34
  export interface GlobalCommonState {
@@ -49,6 +53,7 @@ export const initialCommonState: GlobalCommonState = {
49
53
  showChatSideBar: true,
50
54
  showSessionPanel: true,
51
55
  showSystemRole: false,
56
+ useCmdEnterToSend: false,
52
57
  },
53
58
  settingsTab: SettingsTabs.Common,
54
59
  sidebarKey: SidebarTabKey.Chat,
@@ -5,6 +5,9 @@ import { initialState } from '../../initialState';
5
5
  const sessionGroupKeys = (s: GlobalStore): string[] =>
6
6
  s.preference.sessionGroupKeys || initialState.preference.sessionGroupKeys;
7
7
 
8
+ const useCmdEnterToSend = (s: GlobalStore): boolean => s.preference.useCmdEnterToSend || false;
9
+
8
10
  export const preferenceSelectors = {
9
- sessionGroupKeys: sessionGroupKeys,
11
+ sessionGroupKeys,
12
+ useCmdEnterToSend,
10
13
  };
@@ -0,0 +1,13 @@
1
+ import { KeyboardEvent } from 'react';
2
+
3
+ import { isMacOS } from './platform';
4
+
5
+ export const isCommandPressed = (event: KeyboardEvent) => {
6
+ const isMac = isMacOS();
7
+
8
+ if (isMac) {
9
+ return event.metaKey; // Use metaKey (Command key) on macOS
10
+ } else {
11
+ return event.ctrlKey; // Use ctrlKey on Windows/Linux
12
+ }
13
+ };
@@ -1,6 +1,8 @@
1
1
  import UAParser from 'ua-parser-js';
2
2
 
3
3
  const getPaser = () => {
4
+ if (typeof window === 'undefined') return new UAParser('Node');
5
+
4
6
  let ua = navigator.userAgent;
5
7
  return new UAParser(ua);
6
8
  };
@@ -12,3 +14,5 @@ export const getPlatform = () => {
12
14
  export const getBrowser = () => {
13
15
  return getPaser().getResult().browser.name;
14
16
  };
17
+
18
+ export const isMacOS = () => getPlatform() === 'Mac OS';