Skip to content

Commit

Permalink
✨ feat: 支持 inbox 的会话功能
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 12, 2023
1 parent 1f89066 commit 9b713b8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/pages/chat/features/Conversation/Input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ChatInput = () => {
s.updatePreference,
]);
const [sendMessage, hasTopic, saveToTopic] = useSessionStore((s) => [
s.createOrSendMsg,
s.sendMessage,
!!s.activeTopicId,
s.saveToTopic,
]);
Expand Down
11 changes: 1 addition & 10 deletions src/pages/chat/features/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,7 @@ const Header = memo<{ settings?: boolean }>(({ settings = true }) => {
</Flexbox>
) : (
<Flexbox align={'flex-start'} gap={12} horizontal>
<Avatar
avatar={avatar}
background={backgroundColor}
onClick={() => {
Router.push(`/chat/${id}/setting`);
}}
size={40}
style={{ cursor: 'pointer', flex: 'none' }}
title={title}
/>
<Avatar avatar={avatar} background={backgroundColor} size={40} title={title} />
<HeaderTitle
desc={description}
tag={
Expand Down
20 changes: 10 additions & 10 deletions src/store/session/slices/agentConfig/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ const currentAutocomplete = (s: SessionStore): Autocomplete => ({

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);
Expand All @@ -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);

Expand Down
26 changes: 8 additions & 18 deletions src/store/session/slices/chat/actions/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ export interface ChatMessageAction {
* 清除消息
*/
clearMessage: () => void;
/**
* 创建或发送消息
* @param text - 消息文本
*/
createOrSendMsg: (text: string) => Promise<void>;
/**
* 删除消息
* @param id - 消息 ID
Expand Down Expand Up @@ -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' });
},
Expand Down Expand Up @@ -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);
// },
});
29 changes: 17 additions & 12 deletions src/store/session/slices/session/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand All @@ -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;

Expand Down
24 changes: 14 additions & 10 deletions src/store/session/slices/session/initialState.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { merge } from 'lodash-es';

import { DEFAULT_AGENT_META } from '@/const/meta';
import { LobeAgentSession, LobeSessionType } from '@/types/session';

Expand Down Expand Up @@ -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<LobeAgentSession>);

export const initialSessionState: SessionState = {
activeId: null,
inbox: initInbox,
searchKeywords: '',
sessions: {},
};

0 comments on commit 9b713b8

Please sign in to comment.