Skip to content

Commit

Permalink
✨ feat: 助手添加默认打招呼消息
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmclin2 committed May 3, 2024
1 parent 600cd70 commit 07eec41
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/features/ChatDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface DialogProps {
const Dialog = (props: DialogProps) => {
const { styles } = useStyles();
const { className, style, setOpen } = props;
const currentChats = useSessionStore((s) => sessionSelectors.currentChats(s));
const currentChats = useSessionStore((s) => sessionSelectors.currentChatsWithGreetingMessage(s));
const lastAgentChatIndex = currentChats.findLastIndex((item) => item.role === 'assistant');
const ref = React.useRef<HTMLDivElement>(null);
const isHovered = useHover(ref);
Expand Down
4 changes: 2 additions & 2 deletions src/features/ChatItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ const Item = memo<ChatListItemProps>(({ index, id, showTitle = false, type = 'bl
const [editing, setEditing] = useState(false);

const item = useSessionStore((s) => {
const chats = sessionSelectors.currentChats(s);
const chats = sessionSelectors.currentChatsWithGreetingMessage(s);

if (index >= chats.length) return;

return sessionSelectors.currentChats(s)[index];
return chats[index];
}, isEqual);

const [loading, updateMessageContent] = useSessionStore((s) => [
Expand Down
5 changes: 4 additions & 1 deletion src/features/ChatList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const VirtualizedList = memo<VirtualizedListProps>(({ mobile }) => {
const virtuosoRef = useRef<VirtuosoHandle>(null);
const [atBottom, setAtBottom] = useState(true);

const data = useSessionStore((s) => ['empty', ...sessionSelectors.currentChatIDs(s)], isEqual);
const data = useSessionStore(
(s) => ['empty', ...sessionSelectors.currentChatIDsWithGreetingMessage(s)],
isEqual,
);
const [id, chatLoading] = useSessionStore((s) => [s.activeId, !!s.chatLoadingId]);

useEffect(() => {
Expand Down
43 changes: 36 additions & 7 deletions src/store/session/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ const sessionListIds = (s: SessionStore): string[] => {
return sessionList.map((item) => item.agentId);
};

const currentChatIDs = (s: SessionStore): string[] => {
const session = currentSession(s);
if (!session) return [];
return session.messages.map((item) => item.id);
};

const currentAgent = (s: SessionStore): Agent | undefined => {
const { activeId, localAgentList } = s;
return localAgentList.find((item) => item.agentId === activeId);
Expand All @@ -47,6 +41,40 @@ const currentChats = (s: SessionStore): ChatMessage[] => {
});
};

const currentChatsWithGreetingMessage = (s: SessionStore): ChatMessage[] => {
const data = currentChats(s);

const isBrandNewChat = data.length === 0;

if (!isBrandNewChat) return data;

const agent = currentAgent(s);

const initTime = Date.now();

console.log('agent.greeting', agent?.greeting);

const emptyGuideMessage = {
content: agent?.greeting || '',
createdAt: initTime,
id: 'default',
meta: {
avatar: agent?.meta.avatar,
title: agent?.meta.name,
description: agent?.meta.description,
},
role: 'assistant',
updatedAt: initTime,
} as ChatMessage;

return [emptyGuideMessage];
};

const currentChatIDsWithGreetingMessage = (s: SessionStore): string[] => {
const currentChats = currentChatsWithGreetingMessage(s);
return currentChats.map((item) => item.id);
};

const previousChats = (s: SessionStore, id: string): ChatMessage[] => {
const chatList = currentChats(s);
const index = chatList.findIndex((item) => item.id === id);
Expand Down Expand Up @@ -98,9 +126,10 @@ const isDefaultAgent = (s: SessionStore) => {
};

export const sessionSelectors = {
currentChatsWithGreetingMessage,
currentAgent,
currentAgentModel,
currentChatIDs,
currentChatIDsWithGreetingMessage,
isDefaultAgent,
currentChatMessage,
currentChats,
Expand Down

0 comments on commit 07eec41

Please sign in to comment.