diff --git a/src/features/ChatDialog/index.tsx b/src/features/ChatDialog/index.tsx index c428c0b1..9ba6a33b 100644 --- a/src/features/ChatDialog/index.tsx +++ b/src/features/ChatDialog/index.tsx @@ -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(null); const isHovered = useHover(ref); diff --git a/src/features/ChatItem/index.tsx b/src/features/ChatItem/index.tsx index b6f6b6a7..c17e0088 100644 --- a/src/features/ChatItem/index.tsx +++ b/src/features/ChatItem/index.tsx @@ -32,11 +32,11 @@ const Item = memo(({ 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) => [ diff --git a/src/features/ChatList/index.tsx b/src/features/ChatList/index.tsx index 5843af3f..0783e814 100644 --- a/src/features/ChatList/index.tsx +++ b/src/features/ChatList/index.tsx @@ -19,7 +19,10 @@ const VirtualizedList = memo(({ mobile }) => { const virtuosoRef = useRef(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(() => { diff --git a/src/store/session/selectors.ts b/src/store/session/selectors.ts index 353c1e6b..8586158a 100644 --- a/src/store/session/selectors.ts +++ b/src/store/session/selectors.ts @@ -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); @@ -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); @@ -98,9 +126,10 @@ const isDefaultAgent = (s: SessionStore) => { }; export const sessionSelectors = { + currentChatsWithGreetingMessage, currentAgent, currentAgentModel, - currentChatIDs, + currentChatIDsWithGreetingMessage, isDefaultAgent, currentChatMessage, currentChats,