Skip to content

Commit 30da537

Browse files
committed
✨ feat: ChatList 支持操作行为
1 parent 3db700a commit 30da537

File tree

3 files changed

+93
-69
lines changed

3 files changed

+93
-69
lines changed

src/pages/chat/Config/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const Config = () => {
2727
mode={'float'}
2828
pin
2929
resize={{ left: false }}
30+
minWidth={400}
3031
expand={showAgentSettings}
3132
className={styles.drawer}
3233
>
+19-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
import { ChatList } from '@lobehub/ui';
22
import isEqual from 'fast-deep-equal';
33
import { memo } from 'react';
4+
import { shallow } from 'zustand/shallow';
45

56
import { chatSelectors, useChatStore } from '@/store/session';
67

78
const List = () => {
89
const data = useChatStore(chatSelectors.currentChats, isEqual);
10+
const [deleteMessage, resendMessage] = useChatStore((s) => [s.deleteMessage, s.resendMessage], shallow);
911

10-
return <ChatList data={data} />;
12+
return (
13+
<ChatList
14+
data={data}
15+
onActionClick={(key, id) => {
16+
switch (key) {
17+
case 'delete':
18+
deleteMessage(id);
19+
break;
20+
21+
case 'regenerate':
22+
resendMessage(id);
23+
break;
24+
}
25+
}}
26+
style={{ marginTop: 24 }}
27+
/>
28+
);
1129
};
1230

1331
export default memo(List);

src/store/session/slices/chat/action.ts

+73-68
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ export interface ChatAction {
3232
* @param index - 消息索引
3333
* @returns Promise<void>
3434
*/
35-
// resendMessage: (id: string) => Promise<void>;
35+
resendMessage: (id: string) => Promise<void>;
3636

3737
/**
3838
* @title 发送消息
3939
* @returns Promise<void>
4040
*/
4141
sendMessage: (text: string) => Promise<void>;
42+
deleteMessage: (id: string) => void;
4243
}
4344

4445
export const createChatSlice: StateCreator<SessionStore, [['zustand/devtools', never]], [], ChatAction> = (
@@ -73,73 +74,73 @@ export const createChatSlice: StateCreator<SessionStore, [['zustand/devtools', n
7374
set({ editingMessageId: messageId });
7475
},
7576

76-
// resendMessage: async (id) => {
77-
// const {
78-
// sendMessage,
79-
// dispatchMessage,
80-
// // generateMessage
81-
// } = get();
82-
//
83-
// const session = sessionSelectors.currentSession(get());
84-
//
85-
// if (!session) return;
86-
//
87-
// const index = session.chats.findIndex((s) => s.id === id);
88-
// if (index < 0) return;
89-
//
90-
// const message = session.chats[index];
91-
//
92-
// // 用户通过手动删除,造成了他的问题是最后一条消息
93-
// // 这种情况下,相当于用户重新发送消息
94-
// if (session.chats.length === index && message.role === 'user') {
95-
// // 发送消息的时候会把传入的消息 message 新建一条,因此在发送前先把这条消息在记录中删除
96-
// dispatchMessage({ id: message.id, type: 'deleteMessage' });
97-
// await sendMessage(message.content);
98-
// return;
99-
// }
100-
//
101-
// // 上下文消息就是当前消息之前的消息
102-
// const contextMessages = session.chats.slice(0, index);
103-
//
104-
// // 上下文消息中最后一条消息
105-
// const userMessage = contextMessages.at(-1)?.content;
106-
// if (!userMessage) return;
107-
//
108-
// const targetMessage = session.chats[index];
109-
//
110-
// // 如果不是 assistant 的消息,那么需要额外插入一条消息
111-
// if (targetMessage.role === 'assistant') {
112-
// // 保存之前的消息为历史消息
113-
// // dispatchMessage({ type: 'updateMessage', message: botPrevMsg, index });
114-
// // dispatchMessage({ type: 'updateMessage', message: LOADING_FLAT, index });
115-
// } else {
116-
// // dispatchMessage({
117-
// // type: 'insertMessage',
118-
// // index,
119-
// // message: { role: 'assistant', content: LOADING_FLAT },
120-
// // });
121-
// }
122-
//
123-
// // 重置错误信息
124-
// dispatchMessage({
125-
// id: targetMessage.id,
126-
// key: 'error',
127-
// type: 'updateMessage',
128-
// value: undefined,
129-
// });
130-
//
131-
// // 开始更新消息
132-
//
133-
// // await generateMessage(userMessage, contextMessages, {
134-
// // onMessageHandle: (text) => {
135-
// // currentResponse = [...currentResponse, text];
136-
// // dispatchMessage({ type: 'updateMessage', message: currentResponse.join(''), index });
137-
// // },
138-
// // onErrorHandle: (error) => {
139-
// // dispatchMessage({ type: 'updateMessage' });
140-
// // },
141-
// // });
142-
// },
77+
resendMessage: async () => {
78+
// const {
79+
// sendMessage,
80+
// dispatchMessage,
81+
// // generateMessage
82+
// } = get();
83+
//
84+
// const session = sessionSelectors.currentSession(get());
85+
//
86+
// if (!session) return;
87+
//
88+
// const index = session.chats.findIndex((s) => s.id === id);
89+
// if (index < 0) return;
90+
//
91+
// const message = session.chats[index];
92+
//
93+
// // 用户通过手动删除,造成了他的问题是最后一条消息
94+
// // 这种情况下,相当于用户重新发送消息
95+
// if (session.chats.length === index && message.role === 'user') {
96+
// // 发送消息的时候会把传入的消息 message 新建一条,因此在发送前先把这条消息在记录中删除
97+
// dispatchMessage({ id: message.id, type: 'deleteMessage' });
98+
// await sendMessage(message.content);
99+
// return;
100+
// }
101+
//
102+
// // 上下文消息就是当前消息之前的消息
103+
// const contextMessages = session.chats.slice(0, index);
104+
//
105+
// // 上下文消息中最后一条消息
106+
// const userMessage = contextMessages.at(-1)?.content;
107+
// if (!userMessage) return;
108+
//
109+
// const targetMessage = session.chats[index];
110+
//
111+
// // 如果不是 assistant 的消息,那么需要额外插入一条消息
112+
// if (targetMessage.role === 'assistant') {
113+
// // 保存之前的消息为历史消息
114+
// // dispatchMessage({ type: 'updateMessage', message: botPrevMsg, index });
115+
// // dispatchMessage({ type: 'updateMessage', message: LOADING_FLAT, index });
116+
// } else {
117+
// // dispatchMessage({
118+
// // type: 'insertMessage',
119+
// // index,
120+
// // message: { role: 'assistant', content: LOADING_FLAT },
121+
// // });
122+
// }
123+
//
124+
// // 重置错误信息
125+
// dispatchMessage({
126+
// id: targetMessage.id,
127+
// key: 'error',
128+
// type: 'updateMessage',
129+
// value: undefined,
130+
// });
131+
//
132+
// // 开始更新消息
133+
//
134+
// // await generateMessage(userMessage, contextMessages, {
135+
// // onMessageHandle: (text) => {
136+
// // currentResponse = [...currentResponse, text];
137+
// // dispatchMessage({ type: 'updateMessage', message: currentResponse.join(''), index });
138+
// // },
139+
// // onErrorHandle: (error) => {
140+
// // dispatchMessage({ type: 'updateMessage' });
141+
// // },
142+
// // });
143+
},
143144

144145
sendMessage: async (message) => {
145146
const { dispatchMessage, generateMessage } = get();
@@ -182,4 +183,8 @@ export const createChatSlice: StateCreator<SessionStore, [['zustand/devtools', n
182183
},
183184
});
184185
},
186+
187+
deleteMessage: (id) => {
188+
get().dispatchMessage({ type: 'deleteMessage', id });
189+
},
185190
});

0 commit comments

Comments
 (0)