fix: Token estimate count cannot be updated on the UI when typing quickly.

This commit is contained in:
Konjac-XZ 2025-03-03 09:04:41 +08:00 committed by kangfenmao
parent 012e79a7e2
commit d62ff69351

View File

@ -94,11 +94,24 @@ const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic }) => {
const showKnowledgeIcon = useSidebarIconShow('knowledge') const showKnowledgeIcon = useSidebarIconShow('knowledge')
const estimateTextTokens = useCallback(debounce(estimateTxtTokens, 1000), []) const [tokenCount, setTokenCount] = useState(0)
const inputTokenCount = useMemo(
() => (showInputEstimatedTokens ? estimateTextTokens(text) || 0 : 0), const debouncedEstimate = useCallback(
[estimateTextTokens, showInputEstimatedTokens, text] debounce((newText) => {
if (showInputEstimatedTokens) {
const count = estimateTxtTokens(newText) || 0
setTokenCount(count)
}
}, 500),
[showInputEstimatedTokens]
) )
useEffect(() => {
debouncedEstimate(text)
}, [text, debouncedEstimate])
const inputTokenCount = showInputEstimatedTokens ? tokenCount : 0
const newTopicShortcut = useShortcutDisplay('new_topic') const newTopicShortcut = useShortcutDisplay('new_topic')
const newContextShortcut = useShortcutDisplay('toggle_new_context') const newContextShortcut = useShortcutDisplay('toggle_new_context')
const cleanTopicShortcut = useShortcutDisplay('clear_topic') const cleanTopicShortcut = useShortcutDisplay('clear_topic')