Skip to content

Commit

Permalink
✨ feat: 使用最后一条消息作为会话列表的描述
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmclin2 committed May 5, 2024
1 parent f4a2a8c commit c143621
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/constants/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Agent, CategoryEnum } from '@/types/agent';

export const LOBE_VIDOL_DEFAULT_AGENT_ID = 'lobe-vidol-default-agent';

const OFFICIAL_ROBOT_NAME = 'V';
const OFFICIAL_ROBOT_NAME = 'Elsa';

export const DEFAULT_VIDOL_AGENT: Agent = {
agentId: LOBE_VIDOL_DEFAULT_AGENT_ID,
Expand Down
25 changes: 25 additions & 0 deletions src/features/Actions/UnSubscribeButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Button, Popconfirm } from 'antd';
import React from 'react';

import { agentListSelectors, useAgentStore } from '@/store/agent';

export default () => {
const currentAgent = useAgentStore((s) => agentListSelectors.currentAgentItem(s));
const [unsubscribe] = useAgentStore((s) => [s.unsubscribe]);

return (
<Popconfirm
cancelText="取消"
description={`确定删除角色 ${currentAgent?.meta.name} 以及相关联的会话消息吗?删除后无法恢复, 请谨慎操作!`}
key="delete"
okText="确定"
onConfirm={() => {
if (!currentAgent) return;
unsubscribe(currentAgent.agentId);
}}
title="取消订阅?"
>
<Button danger>删除角色</Button>
</Popconfirm>
);
};
26 changes: 8 additions & 18 deletions src/features/RoleInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use client';

import { DraggablePanel } from '@lobehub/ui';
import { Button, Popconfirm } from 'antd';
import { Button } from 'antd';
import { createStyles } from 'antd-style';
import { useRouter } from 'next/navigation';
import React, { memo, useState } from 'react';

import AgentCard from '@/components/agent/AgentCard';
import SystemRole from '@/components/agent/SystemRole';
import { SIDEBAR_MAX_WIDTH, SIDEBAR_WIDTH } from '@/constants/common';
import UnSubscribeButton from '@/features/Actions/UnSubscribeButton';
import { agentListSelectors, useAgentStore } from '@/store/agent';
import { useConfigStore } from '@/store/config';
import { useSessionStore } from '@/store/session';
Expand All @@ -25,14 +27,14 @@ const useStyles = createStyles(({ css, token }) => ({

const Header = () => {
const { styles } = useStyles();
const router = useRouter();
const [tempId, setTempId] = useState<string>('');
const [showAgentSidebar, activateAgent, deactivateAgent, unsubscribe] = useAgentStore((s) => [
const [showAgentSidebar, activateAgent, deactivateAgent] = useAgentStore((s) => [
agentListSelectors.showSideBar(s),
s.activateAgent,
s.deactivateAgent,
s.unsubscribe,
]);
const [openPanel, closePanel] = useConfigStore((s) => [s.openPanel, s.closePanel]);
const [openPanel] = useConfigStore((s) => [s.openPanel, s.closePanel]);
const currentAgent = useAgentStore((s) => agentListSelectors.currentAgentItem(s));
const createSession = useSessionStore((s) => s.createSession);

Expand Down Expand Up @@ -61,7 +63,7 @@ const Header = () => {
onClick={() => {
if (!currentAgent) return;
createSession(currentAgent);
closePanel('agent');
router.push('/chat');
}}
type={'primary'}
>
Expand All @@ -77,19 +79,7 @@ const Header = () => {
>
编辑
</Button>,
<Popconfirm
cancelText="取消"
description={`确定取消角色 ${currentAgent?.meta.name} 的订阅吗?`}
key="delete"
okText="确定"
onConfirm={() => {
if (!currentAgent) return;
unsubscribe(currentAgent.agentId);
}}
title="取消订阅?"
>
<Button danger>取消订阅</Button>
</Popconfirm>,
<UnSubscribeButton key="unsubscribe" />,
]}
agent={currentAgent}
footer={<SystemRole systemRole={currentAgent?.systemRole} style={{ marginTop: 16 }} />}
Expand Down
2 changes: 2 additions & 0 deletions src/features/RoleList/List/Item/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export default (props: ActionsProps) => {
onOk: () => {
unsubscribe(id);
},
okText: '删除',
cancelText: '取消',
title: '确认删除角色以及相关联的会话消息吗?删除后无法恢复, 请谨慎操作!',
});
},
Expand Down
9 changes: 6 additions & 3 deletions src/features/SessionList/List/Item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ interface SessionItemProps {
const SessionItem = memo<SessionItemProps>(({ id, onClick }) => {
const [open, setOpen] = useState(false);
const [active] = useSessionStore((s) => [s.activeId === id]);
const [getAgentById, isDefaultAgent] = useSessionStore((s) => [
const [getAgentById, isDefaultAgent, getLastMessageByAgentId] = useSessionStore((s) => [
sessionSelectors.getAgentById(s),
sessionSelectors.isDefaultAgent(s),
sessionSelectors.getLastMessageByAgentId(s),
]);

const lastMessage = getLastMessageByAgentId(id);

const isDefault = isDefaultAgent(id);
const agent = getAgentById(id);
const { name, description, avatar } = agent?.meta || {};
const { name, avatar } = agent?.meta || {};

const actions = useMemo(() => <Actions id={id} setOpen={setOpen} />, [id]);

Expand All @@ -30,7 +33,7 @@ const SessionItem = memo<SessionItemProps>(({ id, onClick }) => {
actions={isDefault ? null : actions}
active={active}
avatar={avatar || ''}
description={description || agent?.systemRole}
description={lastMessage?.content || agent?.greeting || ''}
onClick={onClick}
showAction={open}
title={name}
Expand Down
9 changes: 7 additions & 2 deletions src/features/SessionList/V/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { Space, Tag } from 'antd';
import { memo } from 'react';

import { DEFAULT_VIDOL_AGENT, LOBE_VIDOL_DEFAULT_AGENT_ID } from '@/constants/agent';
import { useSessionStore } from '@/store/session';
import { sessionSelectors, useSessionStore } from '@/store/session';

import ListItem from '../ListItem';

const V = memo(() => {
const [activeId, switchSession] = useSessionStore((s) => [s.activeId, s.switchSession]);
const [getLastMessageByAgentId] = useSessionStore((s) => [
sessionSelectors.getLastMessageByAgentId(s),
]);

const lastMessage = getLastMessageByAgentId(LOBE_VIDOL_DEFAULT_AGENT_ID);

return (
<ListItem
Expand All @@ -22,7 +27,7 @@ const V = memo(() => {
<Tag color="geekblue">官方助手</Tag>
</Space>
}
description={DEFAULT_VIDOL_AGENT.meta.description}
description={lastMessage?.content || DEFAULT_VIDOL_AGENT.greeting || ''}
/>
);
});
Expand Down
13 changes: 13 additions & 0 deletions src/store/session/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ const getAgentById = (s: SessionStore) => {
};
};

const getLastMessageByAgentId = (s: SessionStore) => {
return (id: string): ChatMessage | undefined => {
const { sessionList, defaultSession } = s;
if (id === LOBE_VIDOL_DEFAULT_AGENT_ID) {
return defaultSession.messages.at(-1);
}
const session = sessionList.find((item) => item.agentId === id);
if (!session) return;
return session.messages.at(-1);
};
};

const isDefaultAgent = (s: SessionStore) => {
return (id: string): boolean => {
const agent = getAgentById(s)(id);
Expand All @@ -141,6 +153,7 @@ export const sessionSelectors = {
currentAgentModel,
currentChatIDsWithGreetingMessage,
isDefaultAgent,
getLastMessageByAgentId,
currentChatMessage,
currentChats,
currentChatsString,
Expand Down

0 comments on commit c143621

Please sign in to comment.