Skip to content

Commit

Permalink
✨ feat: ChatList 支持操作行为
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jul 11, 2023
1 parent 3db700a commit 30da537
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 69 deletions.
1 change: 1 addition & 0 deletions src/pages/chat/Config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Config = () => {
mode={'float'}
pin
resize={{ left: false }}
minWidth={400}
expand={showAgentSettings}
className={styles.drawer}
>
Expand Down
20 changes: 19 additions & 1 deletion src/pages/chat/Conversation/ChatList.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import { ChatList } from '@lobehub/ui';
import isEqual from 'fast-deep-equal';
import { memo } from 'react';
import { shallow } from 'zustand/shallow';

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

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

return <ChatList data={data} />;
return (
<ChatList
data={data}
onActionClick={(key, id) => {
switch (key) {
case 'delete':
deleteMessage(id);
break;

case 'regenerate':
resendMessage(id);
break;
}
}}
style={{ marginTop: 24 }}
/>
);
};

export default memo(List);
141 changes: 73 additions & 68 deletions src/store/session/slices/chat/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ export interface ChatAction {
* @param index - 消息索引
* @returns Promise<void>
*/
// resendMessage: (id: string) => Promise<void>;
resendMessage: (id: string) => Promise<void>;

/**
* @title 发送消息
* @returns Promise<void>
*/
sendMessage: (text: string) => Promise<void>;
deleteMessage: (id: string) => void;
}

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

// resendMessage: async (id) => {
// const {
// sendMessage,
// dispatchMessage,
// // generateMessage
// } = get();
//
// const session = sessionSelectors.currentSession(get());
//
// if (!session) return;
//
// const index = session.chats.findIndex((s) => s.id === id);
// if (index < 0) return;
//
// const message = session.chats[index];
//
// // 用户通过手动删除,造成了他的问题是最后一条消息
// // 这种情况下,相当于用户重新发送消息
// if (session.chats.length === index && message.role === 'user') {
// // 发送消息的时候会把传入的消息 message 新建一条,因此在发送前先把这条消息在记录中删除
// dispatchMessage({ id: message.id, type: 'deleteMessage' });
// await sendMessage(message.content);
// return;
// }
//
// // 上下文消息就是当前消息之前的消息
// const contextMessages = session.chats.slice(0, index);
//
// // 上下文消息中最后一条消息
// const userMessage = contextMessages.at(-1)?.content;
// if (!userMessage) return;
//
// const targetMessage = session.chats[index];
//
// // 如果不是 assistant 的消息,那么需要额外插入一条消息
// if (targetMessage.role === 'assistant') {
// // 保存之前的消息为历史消息
// // dispatchMessage({ type: 'updateMessage', message: botPrevMsg, index });
// // dispatchMessage({ type: 'updateMessage', message: LOADING_FLAT, index });
// } else {
// // dispatchMessage({
// // type: 'insertMessage',
// // index,
// // message: { role: 'assistant', content: LOADING_FLAT },
// // });
// }
//
// // 重置错误信息
// dispatchMessage({
// id: targetMessage.id,
// key: 'error',
// type: 'updateMessage',
// value: undefined,
// });
//
// // 开始更新消息
//
// // await generateMessage(userMessage, contextMessages, {
// // onMessageHandle: (text) => {
// // currentResponse = [...currentResponse, text];
// // dispatchMessage({ type: 'updateMessage', message: currentResponse.join(''), index });
// // },
// // onErrorHandle: (error) => {
// // dispatchMessage({ type: 'updateMessage' });
// // },
// // });
// },
resendMessage: async () => {
// const {
// sendMessage,
// dispatchMessage,
// // generateMessage
// } = get();
//
// const session = sessionSelectors.currentSession(get());
//
// if (!session) return;
//
// const index = session.chats.findIndex((s) => s.id === id);
// if (index < 0) return;
//
// const message = session.chats[index];
//
// // 用户通过手动删除,造成了他的问题是最后一条消息
// // 这种情况下,相当于用户重新发送消息
// if (session.chats.length === index && message.role === 'user') {
// // 发送消息的时候会把传入的消息 message 新建一条,因此在发送前先把这条消息在记录中删除
// dispatchMessage({ id: message.id, type: 'deleteMessage' });
// await sendMessage(message.content);
// return;
// }
//
// // 上下文消息就是当前消息之前的消息
// const contextMessages = session.chats.slice(0, index);
//
// // 上下文消息中最后一条消息
// const userMessage = contextMessages.at(-1)?.content;
// if (!userMessage) return;
//
// const targetMessage = session.chats[index];
//
// // 如果不是 assistant 的消息,那么需要额外插入一条消息
// if (targetMessage.role === 'assistant') {
// // 保存之前的消息为历史消息
// // dispatchMessage({ type: 'updateMessage', message: botPrevMsg, index });
// // dispatchMessage({ type: 'updateMessage', message: LOADING_FLAT, index });
// } else {
// // dispatchMessage({
// // type: 'insertMessage',
// // index,
// // message: { role: 'assistant', content: LOADING_FLAT },
// // });
// }
//
// // 重置错误信息
// dispatchMessage({
// id: targetMessage.id,
// key: 'error',
// type: 'updateMessage',
// value: undefined,
// });
//
// // 开始更新消息
//
// // await generateMessage(userMessage, contextMessages, {
// // onMessageHandle: (text) => {
// // currentResponse = [...currentResponse, text];
// // dispatchMessage({ type: 'updateMessage', message: currentResponse.join(''), index });
// // },
// // onErrorHandle: (error) => {
// // dispatchMessage({ type: 'updateMessage' });
// // },
// // });
},

sendMessage: async (message) => {
const { dispatchMessage, generateMessage } = get();
Expand Down Expand Up @@ -182,4 +183,8 @@ export const createChatSlice: StateCreator<SessionStore, [['zustand/devtools', n
},
});
},

deleteMessage: (id) => {
get().dispatchMessage({ type: 'deleteMessage', id });
},
});

0 comments on commit 30da537

Please sign in to comment.