-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from lobehub/history-count
限制历史记录条数
- Loading branch information
Showing
12 changed files
with
150 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ActionIcon } from '@lobehub/ui'; | ||
import { Popconfirm } from 'antd'; | ||
import { Eraser } from 'lucide-react'; | ||
import { memo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { shallow } from 'zustand/shallow'; | ||
|
||
import { useSessionStore } from '@/store/session'; | ||
|
||
const ActionsRight = memo(() => { | ||
const { t } = useTranslation('setting'); | ||
const [clearMessage] = useSessionStore((s) => [s.clearMessage, s.updateAgentConfig], shallow); | ||
|
||
return ( | ||
<Popconfirm | ||
cancelText={t('cancel', { ns: 'common' })} | ||
okButtonProps={{ danger: true }} | ||
okText={t('ok', { ns: 'common' })} | ||
onConfirm={() => clearMessage()} | ||
title={t('confirmClearCurrentMessages', { ns: 'common' })} | ||
> | ||
<ActionIcon | ||
icon={Eraser} | ||
placement={'bottom'} | ||
title={t('clearCurrentMessages', { ns: 'common' })} | ||
/> | ||
</Popconfirm> | ||
); | ||
}); | ||
|
||
export default ActionsRight; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,40 @@ | ||
import { DEFAULT_USER_AVATAR } from '@/const/meta'; | ||
import { agentSelectors } from '@/store/session'; | ||
import { useSettings } from '@/store/settings'; | ||
import { ChatMessage } from '@/types/chatMessage'; | ||
|
||
import type { SessionStore } from '../../../store'; | ||
import { agentSelectors } from '../../agentConfig'; | ||
import { sessionSelectors } from '../../session'; | ||
import { getSlicedMessagesWithConfig } from '../utils'; | ||
import { organizeChats } from './utils'; | ||
|
||
// 展示在聊天框中的消息 | ||
export const getChatsById = | ||
(id: string) => | ||
(s: SessionStore): ChatMessage[] => { | ||
const session = sessionSelectors.getSessionById(id)(s); | ||
|
||
if (!session) return []; | ||
|
||
return organizeChats( | ||
session, | ||
{ | ||
assistant: agentSelectors.currentAgentAvatar(s), | ||
user: useSettings.getState().settings.avatar || DEFAULT_USER_AVATAR, | ||
}, | ||
s.activeTopicId, | ||
); | ||
}; | ||
|
||
// 当前激活的消息列表 | ||
export const currentChats = (s: SessionStore): ChatMessage[] => { | ||
const session = sessionSelectors.currentSession(s); | ||
if (!session) return []; | ||
|
||
return organizeChats( | ||
session, | ||
{ | ||
assistant: agentSelectors.currentAgentAvatar(s), | ||
user: useSettings.getState().settings.avatar || DEFAULT_USER_AVATAR, | ||
}, | ||
s.activeTopicId, | ||
); | ||
if (!s.activeId) return []; | ||
|
||
return getChatsById(s.activeId)(s); | ||
}; | ||
|
||
export const systemRoleSel = (s: SessionStore): string => { | ||
export const currentChatsWithHistoryConfig = (s: SessionStore): ChatMessage[] => { | ||
const chats = currentChats(s); | ||
const config = agentSelectors.currentAgentConfig(s); | ||
|
||
return config.systemRole; | ||
return getSlicedMessagesWithConfig(chats, config); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ChatMessage } from '@/types/chatMessage'; | ||
import { LobeAgentConfig } from '@/types/session'; | ||
|
||
export const getSlicedMessagesWithConfig = ( | ||
messages: ChatMessage[], | ||
config: LobeAgentConfig, | ||
): ChatMessage[] => { | ||
// 如果没有开启历史消息数限制,或者限制为 0,则直接返回 | ||
if (!config.enableHistoryCount || !config.historyCount) return messages; | ||
|
||
// 如果开启了,则返回尾部的N条消息 | ||
return messages.reverse().slice(0, config.historyCount).reverse(); | ||
}; |