From 9b713b8c83e7de9e9d48700d93b016b0a8f2efc3 Mon Sep 17 00:00:00 2001 From: arvinxx Date: Sat, 12 Aug 2023 21:10:17 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=94=AF=E6=8C=81=20inbox?= =?UTF-8?q?=20=E7=9A=84=E4=BC=9A=E8=AF=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../features/Conversation/Input/index.tsx | 2 +- src/pages/chat/features/Header/index.tsx | 11 +------ .../session/slices/agentConfig/selectors.ts | 20 ++++++------- .../session/slices/chat/actions/message.ts | 26 +++++------------ src/store/session/slices/session/action.ts | 29 +++++++++++-------- .../session/slices/session/initialState.ts | 24 ++++++++------- 6 files changed, 51 insertions(+), 61 deletions(-) diff --git a/src/pages/chat/features/Conversation/Input/index.tsx b/src/pages/chat/features/Conversation/Input/index.tsx index e7f5838ff3e1..ff29ae46fee6 100644 --- a/src/pages/chat/features/Conversation/Input/index.tsx +++ b/src/pages/chat/features/Conversation/Input/index.tsx @@ -22,7 +22,7 @@ const ChatInput = () => { s.updatePreference, ]); const [sendMessage, hasTopic, saveToTopic] = useSessionStore((s) => [ - s.createOrSendMsg, + s.sendMessage, !!s.activeTopicId, s.saveToTopic, ]); diff --git a/src/pages/chat/features/Header/index.tsx b/src/pages/chat/features/Header/index.tsx index b36b2d9fc2b7..530bb503cc52 100644 --- a/src/pages/chat/features/Header/index.tsx +++ b/src/pages/chat/features/Header/index.tsx @@ -48,16 +48,7 @@ const Header = memo<{ settings?: boolean }>(({ settings = true }) => { ) : ( - { - Router.push(`/chat/${id}/setting`); - }} - size={40} - style={{ cursor: 'pointer', flex: 'none' }} - title={title} - /> + ({ const currentAgentTitle = (s: SessionStore) => currentAgentMeta(s)?.title || t('defaultSession'); +const currentAgentConfig = (s: SessionStore) => { + const session = sessionSelectors.currentSession(s); + return merge({}, initialLobeAgentConfig, session?.config); +}; + +const currentAgentSystemRole = (s: SessionStore) => { + return currentAgentConfig(s).systemRole; +}; + const currentAgentDescription = (s: SessionStore) => - currentAgentMeta(s)?.description || t('noDescription'); + currentAgentMeta(s)?.description || currentAgentSystemRole(s) || t('noDescription'); const currentAgentBackgroundColor = (s: SessionStore) => { const session = sessionSelectors.currentSession(s); @@ -41,15 +50,6 @@ const currentAgentAvatar = (s: SessionStore) => { return session.meta.avatar || DEFAULT_AVATAR; }; -const currentAgentConfig = (s: SessionStore) => { - const session = sessionSelectors.currentSession(s); - return merge({}, initialLobeAgentConfig, session?.config); -}; - -const currentAgentSystemRole = (s: SessionStore) => { - return currentAgentConfig(s).systemRole; -}; - const currentAgentModel = (s: SessionStore): LanguageModel => { const config = currentAgentConfig(s); diff --git a/src/store/session/slices/chat/actions/message.ts b/src/store/session/slices/chat/actions/message.ts index 7d47989f5938..a78a27bf303d 100644 --- a/src/store/session/slices/chat/actions/message.ts +++ b/src/store/session/slices/chat/actions/message.ts @@ -27,11 +27,6 @@ export interface ChatMessageAction { * 清除消息 */ clearMessage: () => void; - /** - * 创建或发送消息 - * @param text - 消息文本 - */ - createOrSendMsg: (text: string) => Promise; /** * 删除消息 * @param id - 消息 ID @@ -89,19 +84,6 @@ export const chatMessage: StateCreator< } }, - createOrSendMsg: async (message) => { - if (!message) return; - - const { sendMessage, createSession } = get(); - const session = sessionSelectors.currentSession(get()); - - if (!session) { - await createSession(); - } - - sendMessage(message); - }, - deleteMessage: (id) => { get().dispatchMessage({ id, type: 'deleteMessage' }); }, @@ -328,4 +310,12 @@ export const chatMessage: StateCreator< await realFetchAIResponse(chats, message.id); }, + + // genShareUrl: () => { + // const session = sessionSelectors.currentSession(get()); + // if (!session) return ''; + // + // const agent = session.config; + // return genShareMessagesUrl(session.chats, agent.systemRole); + // }, }); diff --git a/src/store/session/slices/session/action.ts b/src/store/session/slices/session/action.ts index 2089063c6bd5..4ff02dc11513 100644 --- a/src/store/session/slices/session/action.ts +++ b/src/store/session/slices/session/action.ts @@ -13,6 +13,7 @@ import { initLobeSession } from './initialState'; import { SessionDispatch, sessionsReducer } from './reducers/session'; const t = setNamespace('session'); + export interface SessionAction { activeSession: (sessionId: string) => void; clearSessions: () => void; @@ -96,11 +97,21 @@ export const createSessionSlice: StateCreator< dispatchSession: (payload) => { const { type, ...res } = payload; - set( - { sessions: sessionsReducer(get().sessions, payload) }, - false, - t(`dispatchChat/${type}`, res), - ); + + // 如果是 inbox 类型的 session + if ('id' in res && res.id === 'inbox') { + const nextInbox = sessionsReducer({ inbox: get().inbox }, payload) as { + inbox: LobeAgentSession; + }; + set({ inbox: nextInbox.inbox }, false, t(`dispatchInbox/${type}`, res)); + } else { + // 常规类型的session + set( + { sessions: sessionsReducer(get().sessions, payload) }, + false, + t(`dispatchSessions/${type}`, res), + ); + } }, importSessions: (importSessions) => { @@ -120,13 +131,7 @@ export const createSessionSlice: StateCreator< t('importSessions', importSessions), ); }, - // genShareUrl: () => { - // const session = sessionSelectors.currentSession(get()); - // if (!session) return ''; - // - // const agent = session.config; - // return genShareMessagesUrl(session.chats, agent.systemRole); - // }, + pinSession: (sessionId, pinned) => { const nextValue = typeof pinned === 'boolean' ? pinned : !get().sessions[sessionId].pinned; diff --git a/src/store/session/slices/session/initialState.ts b/src/store/session/slices/session/initialState.ts index d4648d314542..8f2bc43116f0 100644 --- a/src/store/session/slices/session/initialState.ts +++ b/src/store/session/slices/session/initialState.ts @@ -1,3 +1,5 @@ +import { merge } from 'lodash-es'; + import { DEFAULT_AGENT_META } from '@/const/meta'; import { LobeAgentSession, LobeSessionType } from '@/types/session'; @@ -26,18 +28,20 @@ export const initLobeSession: LobeAgentSession = { updateAt: Date.now(), }; -export const initialSessionState: SessionState = { - activeId: null, - - inbox: { - ...initLobeSession, - id: 'inbox', - meta: { - ...DEFAULT_AGENT_META, - title: 'Inbox', - }, +export const initInbox = merge({}, initLobeSession, { + config: { + systemRole: '你是一名 AI 助理', }, + id: 'inbox', + meta: { + avatar: '🗳', + title: 'Inbox', + }, +} as Partial); +export const initialSessionState: SessionState = { + activeId: null, + inbox: initInbox, searchKeywords: '', sessions: {}, };