@lobehub/chat 0.91.0 → 0.93.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.
Files changed (82) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/locales/en_US/chat.json +55 -0
  3. package/locales/en_US/common.json +17 -51
  4. package/locales/ja_JP/chat.json +55 -0
  5. package/locales/ja_JP/common.json +14 -48
  6. package/locales/ko_KR/chat.json +55 -0
  7. package/locales/ko_KR/common.json +25 -59
  8. package/locales/ru_RU/chat.json +55 -0
  9. package/locales/ru_RU/common.json +11 -45
  10. package/locales/zh_CN/chat.json +55 -0
  11. package/locales/zh_CN/common.json +11 -45
  12. package/locales/zh_TW/chat.json +55 -0
  13. package/locales/zh_TW/common.json +30 -64
  14. package/package.json +2 -1
  15. package/src/app/chat/(desktop)/features/ChatHeader.tsx +1 -1
  16. package/src/app/chat/(desktop)/features/SessionHeader.tsx +1 -1
  17. package/src/app/chat/(mobile)/features/ChatHeader.tsx +1 -1
  18. package/src/app/chat/(mobile)/features/TopicList.tsx +1 -1
  19. package/src/app/chat/features/ChatHeader/ShareButton/Inner.tsx +1 -1
  20. package/src/app/chat/features/ChatHeader/ShareButton/Preview.tsx +1 -1
  21. package/src/app/chat/features/ChatInputContent/ActionBar/ActionRight.tsx +3 -3
  22. package/src/app/chat/features/ChatInputContent/ActionBar/Token.tsx +1 -1
  23. package/src/app/chat/features/ChatInputContent/Footer/SaveTopic.tsx +1 -1
  24. package/src/app/chat/features/ChatInputContent/Footer/index.tsx +1 -1
  25. package/src/app/chat/features/ChatInputContent/index.tsx +1 -1
  26. package/src/app/chat/features/Conversation/ChatList/Actions/Assistant.tsx +22 -0
  27. package/src/app/chat/features/Conversation/ChatList/Actions/Error.tsx +15 -0
  28. package/src/app/chat/features/Conversation/ChatList/{Messages/System.tsx → Actions/Fallback.tsx} +1 -1
  29. package/src/app/chat/features/Conversation/ChatList/Actions/Function.tsx +14 -0
  30. package/src/app/chat/features/Conversation/ChatList/{Messages → Actions}/User.tsx +6 -1
  31. package/src/app/chat/features/Conversation/ChatList/Actions/customAction.ts +23 -0
  32. package/src/app/chat/features/Conversation/ChatList/Actions/index.ts +13 -0
  33. package/src/app/chat/features/Conversation/ChatList/Extras/Assistant.tsx +39 -0
  34. package/src/app/chat/features/Conversation/ChatList/Extras/Translate.tsx +75 -0
  35. package/src/app/chat/features/Conversation/ChatList/Extras/User.tsx +24 -0
  36. package/src/app/chat/features/Conversation/ChatList/Extras/index.ts +9 -0
  37. package/src/app/chat/features/Conversation/ChatList/Loading.tsx +53 -0
  38. package/src/app/chat/features/Conversation/ChatList/Messages/Assistant.tsx +5 -58
  39. package/src/app/chat/features/Conversation/ChatList/Messages/Default.tsx +7 -1
  40. package/src/app/chat/features/Conversation/ChatList/Messages/Function.tsx +4 -23
  41. package/src/app/chat/features/Conversation/ChatList/Messages/index.ts +4 -17
  42. package/src/app/chat/features/Conversation/ChatList/Plugins/FunctionCall.tsx +0 -2
  43. package/src/app/chat/features/Conversation/ChatList/Plugins/PluginMessage.tsx +3 -4
  44. package/src/app/chat/features/Conversation/ChatList/index.tsx +27 -5
  45. package/src/app/chat/features/Conversation/index.tsx +1 -1
  46. package/src/app/chat/features/SessionListContent/Inbox/index.tsx +1 -1
  47. package/src/app/chat/features/SessionListContent/List/AddButton.tsx +1 -1
  48. package/src/app/chat/features/SessionSearchBar/index.tsx +1 -1
  49. package/src/app/chat/features/TopicListContent/Topic/DefaultContent.tsx +1 -1
  50. package/src/app/chat/features/TopicListContent/Topic/TopicContent.tsx +1 -1
  51. package/src/app/chat/features/TopicListContent/Topic/index.tsx +1 -1
  52. package/src/app/chat/features/TopicListContent/TopicSearchBar/index.tsx +1 -1
  53. package/src/app/home/index.tsx +2 -2
  54. package/src/chains/langDetect.ts +31 -0
  55. package/src/chains/pickEmoji.ts +34 -0
  56. package/src/{prompts/agent.ts → chains/summaryAgentName.ts} +5 -46
  57. package/src/chains/summaryDescription.ts +15 -0
  58. package/src/{prompts/chat.ts → chains/summaryTitle.ts} +2 -2
  59. package/src/chains/translate.ts +17 -0
  60. package/src/features/AgentSetting/AgentPrompt/index.tsx +3 -3
  61. package/src/features/AgentSetting/store/action.ts +6 -4
  62. package/src/layout/ResponsiveLayout.client.tsx +4 -2
  63. package/src/locales/create.ts +2 -1
  64. package/src/locales/default/chat.ts +60 -0
  65. package/src/locales/default/common.ts +10 -45
  66. package/src/locales/options.ts +10 -0
  67. package/src/locales/resources/en_US.ts +2 -0
  68. package/src/locales/resources/index.ts +1 -1
  69. package/src/locales/resources/ja_JP.ts +2 -0
  70. package/src/locales/resources/ko_KR.ts +2 -0
  71. package/src/locales/resources/ru_RU.ts +2 -0
  72. package/src/locales/resources/zh_CN.ts +2 -0
  73. package/src/locales/resources/zh_TW.ts +2 -0
  74. package/src/store/global/selectors/settings.ts +4 -0
  75. package/src/store/session/slices/chat/actions/index.ts +7 -1
  76. package/src/store/session/slices/chat/actions/message.ts +2 -2
  77. package/src/store/session/slices/chat/actions/topic.ts +2 -2
  78. package/src/store/session/slices/chat/actions/translate.ts +88 -0
  79. package/src/store/session/slices/chat/selectors/chat.ts +23 -17
  80. package/src/store/session/slices/chat/selectors/index.ts +2 -2
  81. package/src/types/chatMessage.ts +5 -4
  82. package/src/types/translate.ts +4 -0
@@ -0,0 +1,88 @@
1
+ import { produce } from 'immer';
2
+ import { StateCreator } from 'zustand/vanilla';
3
+
4
+ import { chainLangDetect } from '@/chains/langDetect';
5
+ import { chainTranslate } from '@/chains/translate';
6
+ import { supportLocales } from '@/locales/options';
7
+ import { SessionStore } from '@/store/session';
8
+ import { fetchPresetTaskResult } from '@/utils/fetch';
9
+ import { setNamespace } from '@/utils/storeDebug';
10
+
11
+ import { sessionSelectors } from '../../session/selectors';
12
+
13
+ const t = setNamespace('chat/translate');
14
+
15
+ /**
16
+ * 翻译事件
17
+ */
18
+ export interface ChatTranslateAction {
19
+ clearTranslate: (id: string) => void;
20
+
21
+ /**
22
+ * 翻译消息
23
+ * @param id
24
+ */
25
+ translateMessage: (id: string, targetLang: string) => Promise<void>;
26
+ }
27
+
28
+ export const chatTranslate: StateCreator<
29
+ SessionStore,
30
+ [['zustand/devtools', never]],
31
+ [],
32
+ ChatTranslateAction
33
+ > = (set, get) => ({
34
+ clearTranslate: (id) => {
35
+ get().dispatchMessage({
36
+ id,
37
+ key: 'translate',
38
+ type: 'updateMessageExtra',
39
+ value: null,
40
+ });
41
+ },
42
+
43
+ translateMessage: async (id, targetLang) => {
44
+ const { toggleChatLoading, dispatchMessage } = get();
45
+ const session = sessionSelectors.currentSession(get());
46
+ if (!session || !id) return;
47
+
48
+ const message = session.chats[id];
49
+ if (!message) return;
50
+
51
+ let content = '';
52
+ let from = '';
53
+
54
+ dispatchMessage({
55
+ id,
56
+ key: 'translate',
57
+ type: 'updateMessageExtra',
58
+ value: { content: '', to: targetLang },
59
+ });
60
+
61
+ toggleChatLoading(true, id, t('translateMessage(start)', { id }) as string);
62
+
63
+ // detect from language
64
+ fetchPresetTaskResult({
65
+ params: chainLangDetect(message.content),
66
+ }).then((data) => {
67
+ if (data && supportLocales.includes(data)) from = data;
68
+ });
69
+
70
+ // translate to target language
71
+ await fetchPresetTaskResult({
72
+ onMessageHandle: (text) => {
73
+ dispatchMessage({
74
+ id,
75
+ key: 'translate',
76
+ type: 'updateMessageExtra',
77
+ value: produce({ content: '', from, to: targetLang }, (draft) => {
78
+ content += text;
79
+ draft.content += content;
80
+ }),
81
+ });
82
+ },
83
+ params: chainTranslate(message.content, targetLang),
84
+ });
85
+
86
+ toggleChatLoading(false);
87
+ },
88
+ });
@@ -52,15 +52,21 @@ export const currentChatsWithGuideMessage = (s: SessionStore): ChatMessage[] =>
52
52
 
53
53
  const [activeId, isInbox] = [s.activeId, s.activeId === INBOX_SESSION_ID];
54
54
  const meta = agentSelectors.currentAgentMeta(s);
55
+
56
+ const inboxMsg = t('inbox.defaultMessage', { ns: 'chat' });
57
+ const agentSystemRoleMsg = t('agentDefaultMessageWithSystemRole', {
58
+ name: meta.title || t('defaultAgent'),
59
+ ns: 'chat',
60
+ systemRole: meta.description,
61
+ });
62
+ const agentMsg = t('agentDefaultMessage', {
63
+ id: activeId,
64
+ name: meta.title || t('defaultAgent'),
65
+ ns: 'chat',
66
+ });
67
+
55
68
  const emptyInboxGuideMessage = {
56
- content: isInbox
57
- ? t('inbox.defaultMessage')
58
- : !!meta.description
59
- ? t('agentDefaultMessageWithSystemRole', {
60
- name: meta.title || t('defaultAgent'),
61
- systemRole: meta.description,
62
- })
63
- : t('agentDefaultMessage', { id: activeId, name: meta.title || t('defaultAgent') }),
69
+ content: isInbox ? inboxMsg : !!meta.description ? agentSystemRoleMsg : agentMsg,
64
70
  createAt: Date.now(),
65
71
  extra: {},
66
72
  id: 'default',
@@ -86,22 +92,22 @@ export const chatsMessageString = (s: SessionStore): string => {
86
92
  return chats.map((m) => m.content).join('');
87
93
  };
88
94
 
89
- export const currentFunctionCallProps = (
90
- s: SessionStore,
91
- ): ((
92
- props: Pick<ChatMessage, 'plugin' | 'function_call' | 'content' | 'id'>,
93
- ) => FunctionCallProps) => {
94
- const chatLoadingId = s.chatLoadingId;
95
- return ({ plugin, function_call, content, id }) => {
95
+ export const getFunctionMessageParams =
96
+ (
97
+ s: SessionStore,
98
+ ): ((
99
+ props: Pick<ChatMessage, 'plugin' | 'function_call' | 'content' | 'id'>,
100
+ ) => FunctionCallProps) =>
101
+ ({ plugin, function_call, content, id }) => {
96
102
  const itemId = plugin?.identifier || function_call?.name;
97
103
  const command = plugin ?? function_call;
98
104
  const args = command?.arguments;
105
+
99
106
  return {
100
107
  arguments: args,
101
108
  command,
102
109
  content,
103
110
  id: itemId,
104
- loading: id === chatLoadingId,
111
+ loading: id === s.chatLoadingId,
105
112
  };
106
113
  };
107
- };
@@ -3,8 +3,8 @@ import {
3
3
  currentChats,
4
4
  currentChatsWithGuideMessage,
5
5
  currentChatsWithHistoryConfig,
6
- currentFunctionCallProps,
7
6
  getChatsById,
7
+ getFunctionMessageParams,
8
8
  } from './chat';
9
9
  import { currentTopics, getTopicMessages } from './topic';
10
10
 
@@ -13,8 +13,8 @@ export const chatSelectors = {
13
13
  currentChats,
14
14
  currentChatsWithGuideMessage,
15
15
  currentChatsWithHistoryConfig,
16
- currentFunctionCallProps,
17
16
  getChatsById,
17
+ getFunctionMessageParams,
18
18
  };
19
19
 
20
20
  export const topicSelectors = {
@@ -1,6 +1,7 @@
1
1
  import { PluginRequestPayload } from '@lobehub/chat-plugin-sdk';
2
2
 
3
3
  import { ErrorType } from '@/types/fetch';
4
+ import { Translate } from '@/types/translate';
4
5
 
5
6
  import { LLMRoleType } from './llm';
6
7
  import { BaseDataModel } from './meta';
@@ -18,6 +19,9 @@ export interface OpenAIFunctionCall {
18
19
  name: string;
19
20
  }
20
21
 
22
+ export interface ChatTranslate extends Translate {
23
+ content?: string;
24
+ }
21
25
  export interface ChatMessage extends BaseDataModel {
22
26
  /**
23
27
  * @title 内容
@@ -29,10 +33,7 @@ export interface ChatMessage extends BaseDataModel {
29
33
  extra?: {
30
34
  fromModel?: string;
31
35
  // 翻译
32
- translate?: {
33
- target: string;
34
- to: string;
35
- };
36
+ translate?: ChatTranslate;
36
37
  } & Record<string, any>;
37
38
 
38
39
  /**
@@ -0,0 +1,4 @@
1
+ export interface Translate {
2
+ from: string;
3
+ to: string;
4
+ }