Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💄 style: Auto refresh TokenTag count #7011

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/features/ChatInput/ActionBar/Token/TokenTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TokenTag, Tooltip } from '@lobehub/ui';
import { Popover } from 'antd';
import { useTheme } from 'antd-style';
import numeral from 'numeral';
import { memo } from 'react';
import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { Center, Flexbox } from 'react-layout-kit';

Expand All @@ -12,7 +12,7 @@ import { useTokenCount } from '@/hooks/useTokenCount';
import { useAgentStore } from '@/store/agent';
import { agentChatConfigSelectors, agentSelectors } from '@/store/agent/selectors';
import { useChatStore } from '@/store/chat';
import { topicSelectors } from '@/store/chat/selectors';
import { chatSelectors, topicSelectors } from '@/store/chat/selectors';
import { useToolStore } from '@/store/tool';
import { toolSelectors } from '@/store/tool/selectors';

Expand Down Expand Up @@ -41,6 +41,11 @@ const Token = memo<TokenTagProps>(({ total: messageString }) => {
];
});

const [historyCount, enableHistoryCount] = useAgentStore((s) => [
agentChatConfigSelectors.historyCount(s),
agentChatConfigSelectors.enableHistoryCount(s),
]);

const maxTokens = useModelContextWindowTokens(model, provider);

// Tool usage token
Expand All @@ -60,7 +65,12 @@ const Token = memo<TokenTagProps>(({ total: messageString }) => {
// Chat usage token
const inputTokenCount = useTokenCount(input);

const chatsToken = useTokenCount(messageString) + inputTokenCount;
const chatsString = useMemo(() => {
const chats = chatSelectors.mainAIChatsWithHistoryConfig(useChatStore.getState());
return chats.map(chat => chat.content).join('');
}, [messageString, historyCount, enableHistoryCount]);

const chatsToken = useTokenCount(chatsString) + inputTokenCount;

// SystemRole token
const systemRoleToken = useTokenCount(systemRole);
Expand Down