| 1 | import { ChatHistoryItem } from "core"; |
| 2 | import { useEffect, useState } from "react"; |
| 3 | import { useAppSelector } from "../../redux/hooks"; |
| 4 | import { selectSelectedChatModel } from "../../redux/slices/configSlice"; |
| 5 | |
| 6 | interface ThinkingIndicatorProps { |
| 7 | historyItem: ChatHistoryItem; |
| 8 | } |
| 9 | /* |
| 10 | Thinking animation |
| 11 | Only for reasoning (long load time) models for now |
| 12 | */ |
| 13 | const ThinkingIndicator = ({ historyItem }: ThinkingIndicatorProps) => { |
| 14 | // Animation for thinking ellipses |
| 15 | const [animation, setAnimation] = useState(2); |
| 16 | useEffect(() => { |
| 17 | const interval = setInterval(() => { |
| 18 | setAnimation((prevState) => (prevState === 2 ? 0 : prevState + 1)); |
| 19 | }, 600); |
| 20 | return () => { |
| 21 | clearInterval(interval); |
| 22 | }; |
| 23 | }, []); |
| 24 | |
| 25 | const selectedModel = useAppSelector(selectSelectedChatModel); |
| 26 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 27 | |
| 28 | const hasContent = Array.isArray(historyItem.message.content) |
| 29 | ? !!historyItem.message.content.length |
| 30 | : !!historyItem.message.content; |
| 31 | const isO1 = selectedModel?.model.startsWith("o1"); |
| 32 | const isThinking = |
| 33 | isStreaming && !historyItem.isGatheringContext && !hasContent; |
| 34 | if (!isThinking || !isO1) { |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | return ( |
| 39 | <div className="px-2 py-2"> |
| 40 | <span className="text-lightgray">{`Thinking.${".".repeat(animation)}`}</span> |
| 41 | </div> |
| 42 | ); |
| 43 | }; |
| 44 | |
| 45 | export default ThinkingIndicator; |