| 1 | import { useMemo, useRef } from "react"; |
| 2 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 3 | import { saveCurrentSession } from "../../redux/thunks/session"; |
| 4 | import { useCompactConversation } from "../../util/compactConversation"; |
| 5 | import { ToolTip } from "../gui/Tooltip"; |
| 6 | |
| 7 | const ContextStatus = () => { |
| 8 | const dispatch = useAppDispatch(); |
| 9 | const contextPercentage = useAppSelector( |
| 10 | (state) => state.session.contextPercentage, |
| 11 | ); |
| 12 | const selectedChatModel = useAppSelector( |
| 13 | (state) => state.config.config.selectedModelByRole.chat?.model, |
| 14 | ); |
| 15 | const previousHistoryLength = useRef<number | null>(null); |
| 16 | const previousSelectedChatModel = useRef<string | null>(null); |
| 17 | const history = useAppSelector((state) => state.session.history); |
| 18 | const percent = Math.round((contextPercentage ?? 0) * 100); |
| 19 | const isPruned = useAppSelector((state) => state.session.isPruned); |
| 20 | |
| 21 | const isDifferentModelAndSameHistory = useMemo(() => { |
| 22 | if (!selectedChatModel) return false; |
| 23 | // only reset if history changes |
| 24 | if (previousHistoryLength.current !== history.length) { |
| 25 | previousHistoryLength.current = history.length; |
| 26 | previousSelectedChatModel.current = selectedChatModel; |
| 27 | return false; |
| 28 | } |
| 29 | return previousSelectedChatModel.current !== selectedChatModel; |
| 30 | }, [history.length, selectedChatModel]); |
| 31 | |
| 32 | const compactConversation = useCompactConversation(); |
| 33 | if (!isPruned && percent < 60) { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | // if user changed to a different model, we shouldn't show the context status until the user sends a new message |
| 38 | if (isDifferentModelAndSameHistory) { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | const barColorClass = isPruned ? "bg-error" : "bg-description"; |
| 43 | |
| 44 | return ( |
| 45 | <div> |
| 46 | <ToolTip |
| 47 | closeEvents={{ |
| 48 | // blur: false, |
| 49 | mouseleave: true, |
| 50 | click: true, |
| 51 | mouseup: false, |
| 52 | }} |
| 53 | clickable |
| 54 | content={ |
| 55 | <div className="flex flex-col gap-0 text-left text-xs"> |
| 56 | <span className="inline-block"> |
| 57 | {`${percent}% of context filled.`} |
| 58 | </span> |
| 59 | {isPruned && ( |
| 60 | <span className="inline-block"> |
| 61 | {`Oldest messages are being removed.`} |
| 62 | </span> |
| 63 | )} |
| 64 | {history.length > 0 && ( |
| 65 | <div className="flex flex-col gap-1 whitespace-pre"> |
| 66 | <div> |
| 67 | <span |
| 68 | className="hover:text-link inline-block cursor-pointer underline" |
| 69 | onClick={() => compactConversation(history.length - 1)} |
| 70 | > |
| 71 | Compact conversation |
| 72 | </span> |
| 73 | {"\n"} |
| 74 | <span |
| 75 | className="hover:text-link inline-block cursor-pointer underline" |
| 76 | onClick={() => { |
| 77 | void dispatch( |
| 78 | saveCurrentSession({ |
| 79 | openNewSession: true, |
| 80 | generateTitle: false, |
| 81 | }), |
| 82 | ); |
| 83 | }} |
| 84 | > |
| 85 | Start a new session |
| 86 | </span> |
| 87 | </div> |
| 88 | </div> |
| 89 | )} |
| 90 | </div> |
| 91 | } |
| 92 | > |
| 93 | <div className="border-command-border relative h-[14px] w-[7px] rounded-[1px] border-[0.5px] border-solid md:h-[10px] md:w-[5px]"> |
| 94 | <div |
| 95 | className={`transition-height absolute bottom-0 left-0 w-full duration-300 ease-in-out ${barColorClass}`} |
| 96 | style={{ height: `${percent}%` }} |
| 97 | /> |
| 98 | </div> |
| 99 | </ToolTip> |
| 100 | </div> |
| 101 | ); |
| 102 | }; |
| 103 | |
| 104 | export default ContextStatus; |