@lobehub/chat 0.94.5 → 0.95.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 (37) hide show
  1. package/.github/workflows/pr-welcome.yml +1 -1
  2. package/CHANGELOG.md +35 -0
  3. package/package.json +4 -4
  4. package/src/app/chat/(desktop)/features/ChatInput.tsx +0 -1
  5. package/src/app/chat/features/Conversation/ChatList/Error/Plugin/PluginSettings.tsx +1 -1
  6. package/src/app/chat/features/Conversation/ChatList/Messages/Assistant.tsx +12 -15
  7. package/src/app/chat/features/Conversation/ChatList/Messages/Function.tsx +26 -15
  8. package/src/app/chat/features/Conversation/ChatList/Plugins/Inspector/Settings.tsx +36 -0
  9. package/src/app/chat/features/Conversation/ChatList/Plugins/{FunctionCall.tsx → Inspector/index.tsx} +53 -25
  10. package/src/app/chat/features/Conversation/ChatList/Plugins/{IFrameRender → Render/DefaultType/IFrameRender}/index.tsx +5 -16
  11. package/src/app/chat/features/Conversation/ChatList/Plugins/{PluginMessage.tsx → Render/DefaultType/index.tsx} +3 -3
  12. package/src/app/chat/features/Conversation/ChatList/Plugins/Render/StandaloneType/Iframe.tsx +136 -0
  13. package/src/app/chat/features/Conversation/ChatList/Plugins/Render/StandaloneType/index.tsx +28 -0
  14. package/src/app/chat/features/Conversation/ChatList/Plugins/Render/index.tsx +28 -0
  15. package/src/app/chat/features/Conversation/ChatList/Plugins/Render/utils/iframeOnReady.ts +23 -0
  16. package/src/app/chat/features/Conversation/ChatList/Plugins/Render/utils/listenToPlugin.ts +65 -0
  17. package/src/app/chat/features/Conversation/ChatList/Plugins/Render/utils/pluginSettings.ts +17 -0
  18. package/src/app/chat/features/Conversation/ChatList/Plugins/Render/utils/pluginState.ts +20 -0
  19. package/src/app/chat/features/Conversation/ChatList/Plugins/Render/utils/postMessage.ts +17 -0
  20. package/src/const/plugin.ts +1 -1
  21. package/src/features/PluginSettings/index.tsx +5 -3
  22. package/src/features/PluginSettingsModal/index.tsx +45 -0
  23. package/src/store/plugin/selectors.ts +10 -4
  24. package/src/store/session/slices/chat/actions/index.ts +4 -1
  25. package/src/store/session/slices/chat/actions/message.ts +102 -117
  26. package/src/store/session/slices/chat/actions/plugin.ts +109 -0
  27. package/src/store/session/slices/chat/reducers/message.ts +31 -1
  28. package/src/store/session/slices/chat/selectors/chat.ts +17 -20
  29. package/src/store/session/slices/chat/selectors/index.ts +4 -2
  30. package/src/types/chatMessage.ts +1 -0
  31. package/src/utils/message.ts +8 -1
  32. package/src/app/chat/features/Conversation/ChatList/Plugins/IFrameRender/hooks.ts +0 -29
  33. package/src/app/chat/features/Conversation/ChatList/Plugins/IFrameRender/utils.ts +0 -17
  34. /package/src/app/chat/features/Conversation/ChatList/Plugins/{PluginResultRender.tsx → Inspector/PluginResultJSON.tsx} +0 -0
  35. /package/src/app/chat/features/Conversation/ChatList/Plugins/{style.ts → Inspector/style.ts} +0 -0
  36. /package/src/app/chat/features/Conversation/ChatList/Plugins/{SystemJsRender → Render/DefaultType/SystemJsRender}/index.tsx +0 -0
  37. /package/src/app/chat/features/Conversation/ChatList/Plugins/{SystemJsRender → Render/DefaultType/SystemJsRender}/utils.ts +0 -0
@@ -1,8 +1,10 @@
1
+ import isEqual from 'fast-deep-equal';
1
2
  import { produce } from 'immer';
2
3
 
3
4
  import { ChatMessage, ChatMessageMap } from '@/types/chatMessage';
4
5
  import { LLMRoleType } from '@/types/llm';
5
6
  import { MetaData } from '@/types/meta';
7
+ import { merge } from '@/utils/merge';
6
8
  import { nanoid } from '@/utils/uuid';
7
9
 
8
10
  interface AddMessage {
@@ -38,12 +40,20 @@ interface UpdateMessageExtra {
38
40
  value: any;
39
41
  }
40
42
 
43
+ interface UpdatePluginState {
44
+ id: string;
45
+ key: string;
46
+ type: 'updatePluginState';
47
+ value: any;
48
+ }
49
+
41
50
  export type MessageDispatch =
42
51
  | AddMessage
43
52
  | DeleteMessage
44
53
  | ResetMessages
45
54
  | UpdateMessage
46
- | UpdateMessageExtra;
55
+ | UpdateMessageExtra
56
+ | UpdatePluginState;
47
57
 
48
58
  export const messagesReducer = (
49
59
  state: ChatMessageMap,
@@ -101,6 +111,26 @@ export const messagesReducer = (
101
111
  });
102
112
  }
103
113
 
114
+ case 'updatePluginState': {
115
+ return produce(state, (draftState) => {
116
+ const { id, key, value } = payload;
117
+ const message = draftState[id];
118
+ if (!message) return;
119
+
120
+ let newState;
121
+ if (!message.pluginState) {
122
+ newState = { [key]: value } as any;
123
+ } else {
124
+ newState = merge(message.pluginState, { [key]: value });
125
+ }
126
+
127
+ if (isEqual(message.pluginState, newState)) return;
128
+
129
+ message.pluginState = newState;
130
+ message.updateAt = Date.now();
131
+ });
132
+ }
133
+
104
134
  case 'resetMessages': {
105
135
  return produce(state, (draftState) => {
106
136
  const { topicId } = payload;
@@ -1,6 +1,6 @@
1
+ import { LobePluginType } from '@lobehub/chat-plugin-sdk';
1
2
  import { t } from 'i18next';
2
3
 
3
- import { FunctionCallProps } from '@/app/chat/features/Conversation/ChatList/Plugins/FunctionCall';
4
4
  import { DEFAULT_INBOX_AVATAR, DEFAULT_USER_AVATAR } from '@/const/meta';
5
5
  import { INBOX_SESSION_ID } from '@/const/session';
6
6
  import { useGlobalStore } from '@/store/global';
@@ -92,22 +92,19 @@ export const chatsMessageString = (s: SessionStore): string => {
92
92
  return chats.map((m) => m.content).join('');
93
93
  };
94
94
 
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 }) => {
102
- const itemId = plugin?.identifier || function_call?.name;
103
- const command = plugin ?? function_call;
104
- const args = command?.arguments;
105
-
106
- return {
107
- arguments: args,
108
- command,
109
- content,
110
- id: itemId,
111
- loading: id === s.chatLoadingId,
112
- };
113
- };
95
+ export const getFunctionMessageProps =
96
+ ({ plugin, content, id }: Pick<ChatMessage, 'plugin' | 'content' | 'id'>) =>
97
+ (s: SessionStore) => ({
98
+ arguments: plugin?.arguments,
99
+ command: plugin,
100
+ content,
101
+ id: plugin?.identifier,
102
+ loading: id === s.chatLoadingId,
103
+ type: plugin?.type as LobePluginType,
104
+ });
105
+
106
+ export const getMessageById = (id: string) => (s: SessionStore) => {
107
+ for (const e of Object.values(s.sessions)) {
108
+ if (e.chats[id]) return e.chats[id];
109
+ }
110
+ };
@@ -4,7 +4,8 @@ import {
4
4
  currentChatsWithGuideMessage,
5
5
  currentChatsWithHistoryConfig,
6
6
  getChatsById,
7
- getFunctionMessageParams,
7
+ getFunctionMessageProps,
8
+ getMessageById,
8
9
  } from './chat';
9
10
  import { currentTopics, getTopicMessages } from './topic';
10
11
 
@@ -14,7 +15,8 @@ export const chatSelectors = {
14
15
  currentChatsWithGuideMessage,
15
16
  currentChatsWithHistoryConfig,
16
17
  getChatsById,
17
- getFunctionMessageParams,
18
+ getFunctionMessageProps,
19
+ getMessageById,
18
20
  };
19
21
 
20
22
  export const topicSelectors = {
@@ -46,6 +46,7 @@ export interface ChatMessage extends BaseDataModel {
46
46
  parentId?: string;
47
47
 
48
48
  plugin?: PluginRequestPayload;
49
+ pluginState?: any;
49
50
 
50
51
  // 引用
51
52
  quotaId?: string;
@@ -1,9 +1,16 @@
1
1
  import { FUNCTION_MESSAGE_FLAG } from '@/const/message';
2
2
 
3
- export const isFunctionMessage = (content: string) => {
3
+ export const isFunctionMessageAtStart = (content: string) => {
4
4
  return content.startsWith(FUNCTION_MESSAGE_FLAG);
5
5
  };
6
6
 
7
+ export const testFunctionMessageAtEnd = (content: string) => {
8
+ const regExp = /{"function_call":.*?}}/;
9
+ const match = content.match(regExp);
10
+
11
+ return { content: match ? match[0] : '', valid: match };
12
+ };
13
+
7
14
  // export const createFunctionCallMessage = () => {
8
15
  // return [
9
16
  // {
@@ -1,29 +0,0 @@
1
- import { useEffect } from 'react';
2
-
3
- import { onPluginFetchMessage, onPluginReady } from './utils';
4
-
5
- export const useOnPluginReady = (onReady: () => void) => {
6
- useEffect(() => {
7
- const fn = (e: MessageEvent) => {
8
- onPluginReady(e, onReady);
9
- };
10
-
11
- window.addEventListener('message', fn);
12
- return () => {
13
- window.removeEventListener('message', fn);
14
- };
15
- }, []);
16
- };
17
-
18
- export const useOnPluginFetchMessage = (onRequest: (data: any) => void, deps: any[] = []) => {
19
- useEffect(() => {
20
- const fn = (e: MessageEvent) => {
21
- onPluginFetchMessage(e, onRequest);
22
- };
23
-
24
- window.addEventListener('message', fn);
25
- return () => {
26
- window.removeEventListener('message', fn);
27
- };
28
- }, deps);
29
- };
@@ -1,17 +0,0 @@
1
- import { PluginChannel } from '@lobehub/chat-plugin-sdk/client';
2
-
3
- export const onPluginReady = (e: MessageEvent, onReady: () => void) => {
4
- if (e.data.type === PluginChannel.pluginReadyForRender) {
5
- onReady();
6
- }
7
- };
8
-
9
- export const onPluginFetchMessage = (e: MessageEvent, onRequest: (data: any) => void) => {
10
- if (e.data.type === PluginChannel.fetchPluginMessage) {
11
- onRequest(e.data);
12
- }
13
- };
14
-
15
- export const sendMessageToPlugin = (window: Window, props: any) => {
16
- window.postMessage({ props, type: PluginChannel.renderPlugin }, '*');
17
- };