mirror of
https://github.com/NovaOSS/nova-betterchat.git
synced 2024-11-25 21:43:59 +01:00
parent
2d1139e3d2
commit
15f21b23fb
|
@ -26,6 +26,7 @@ import useSubmit from '@hooks/useSubmit';
|
||||||
import { ChatInterface } from '@type/chat';
|
import { ChatInterface } from '@type/chat';
|
||||||
|
|
||||||
import PopupModal from '@components/PopupModal';
|
import PopupModal from '@components/PopupModal';
|
||||||
|
import TokenCount from '@components/TokenCount';
|
||||||
import CommandPrompt from './CommandPrompt';
|
import CommandPrompt from './CommandPrompt';
|
||||||
import CodeBlock from './CodeBlock';
|
import CodeBlock from './CodeBlock';
|
||||||
import { codeLanguageSubset } from '@constants/chat';
|
import { codeLanguageSubset } from '@constants/chat';
|
||||||
|
@ -512,6 +513,7 @@ const EditViewButtons = React.memo(
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
{sticky && <TokenCount />}
|
||||||
<CommandPrompt _setContent={_setContent} />
|
<CommandPrompt _setContent={_setContent} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
29
src/components/TokenCount/TokenCount.tsx
Normal file
29
src/components/TokenCount/TokenCount.tsx
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import React, { useEffect, useState } from 'react';
|
||||||
|
import useStore from '@store/store';
|
||||||
|
import { shallow } from 'zustand/shallow';
|
||||||
|
|
||||||
|
import { countMessagesToken } from '@utils/messageUtils';
|
||||||
|
|
||||||
|
const TokenCount = React.memo(() => {
|
||||||
|
const [tokenCount, setTokenCount] = useState<number>(0);
|
||||||
|
const generating = useStore((state) => state.generating);
|
||||||
|
const messages = useStore(
|
||||||
|
(state) =>
|
||||||
|
state.chats ? state.chats[state.currentChatIndex].messages : [],
|
||||||
|
shallow
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!generating) setTokenCount(countMessagesToken(messages));
|
||||||
|
}, [messages, generating]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className='absolute top-[-16px] right-0'>
|
||||||
|
<div className='text-xs italic text-gray-900 dark:text-gray-300'>
|
||||||
|
Tokens: {tokenCount}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default TokenCount;
|
1
src/components/TokenCount/index.ts
Normal file
1
src/components/TokenCount/index.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export { default } from './TokenCount';
|
|
@ -17,3 +17,10 @@ export const limitMessageTokens = (
|
||||||
|
|
||||||
return limitedMessages;
|
return limitedMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const countMessagesToken = (messages: MessageInterface[]) => {
|
||||||
|
return messages.reduce(
|
||||||
|
(tokenCount, message) => (tokenCount += countTokens(message.content)),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue