diff --git a/gui/src/components/StepContainer/ResponseActions.tsx b/gui/src/components/StepContainer/ResponseActions.tsx index 673e05d9a..b507a2307 100644 --- a/gui/src/components/StepContainer/ResponseActions.tsx +++ b/gui/src/components/StepContainer/ResponseActions.tsx @@ -7,13 +7,11 @@ import { import { ChatHistoryItem } from "core"; import { modelSupportsTools } from "core/llm/autodetect"; import { renderChatMessage } from "core/util/messageContent"; -import { useContext, useMemo } from "react"; -import { IdeMessengerContext } from "../../context/IdeMessenger"; +import { useMemo } from "react"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; import { selectSelectedChatModel } from "../../redux/slices/configSlice"; -import { setCompactionLoading } from "../../redux/slices/sessionSlice"; import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; -import { loadSession } from "../../redux/thunks/session"; +import { useCompactConversation } from "../../util/compactConversation"; import { FeedbackButtons } from "../FeedbackButtons"; import { GenerateRuleDialog } from "../GenerateRuleDialog"; import { CopyIconButton } from "../gui/CopyIconButton"; @@ -37,8 +35,6 @@ export default function ResponseActions({ isLast, }: ResponseActionsProps) { const dispatch = useAppDispatch(); - const ideMessenger = useContext(IdeMessengerContext); - const currentSessionId = useAppSelector((state) => state.session.id); const selectedModel = useAppSelector(selectSelectedChatModel); const contextPercentage = useAppSelector( (state) => state.session.contextPercentage, @@ -56,39 +52,13 @@ export default function ResponseActions({ const showLabel = isLast && (isPruned || percent >= 60); + const compactConversation = useCompactConversation(); + const onGenerateRule = () => { dispatch(setShowDialog(true)); dispatch(setDialogMessage()); }; - const onCompactConversation = async () => { - if (!currentSessionId) { - return; - } - - try { - // Set loading state - dispatch(setCompactionLoading({ index, loading: true })); - - await ideMessenger.request("conversation/compact", { - index, - sessionId: currentSessionId, - }); - - // Reload the current session to refresh the conversation state - dispatch( - loadSession({ - sessionId: currentSessionId, - saveCurrentSession: false, - }), - ); - } catch (error) { - console.error("Error compacting conversation:", error); - } finally { - // Clear loading state - dispatch(setCompactionLoading({ index, loading: false })); - } - }; return (
@@ -100,7 +70,7 @@ export default function ResponseActions({ : "Compact conversation" } tabIndex={-1} - onClick={onCompactConversation} + onClick={() => compactConversation(index)} >
{ const dispatch = useAppDispatch(); @@ -10,6 +11,8 @@ const ContextStatus = () => { const history = useAppSelector((state) => state.session.history); const percent = Math.round((contextPercentage ?? 0) * 100); const isPruned = useAppSelector((state) => state.session.isPruned); + + const compactConversation = useCompactConversation(); if (!isPruned && percent < 60) { return null; } @@ -42,21 +45,30 @@ const ContextStatus = () => { )} {history.length > 0 && ( -
- Start a{" "} - { - dispatch( - saveCurrentSession({ - openNewSession: true, - generateTitle: false, - }), - ); - }} - > - New Session - +
+
+ compactConversation(history.length - 1)} + > + Compact conversation + + {"\n"} + or {"\n"} + { + dispatch( + saveCurrentSession({ + openNewSession: true, + generateTitle: false, + }), + ); + }} + > + Start a new session + +
)}
diff --git a/gui/src/util/compactConversation.ts b/gui/src/util/compactConversation.ts new file mode 100644 index 000000000..1908e8d62 --- /dev/null +++ b/gui/src/util/compactConversation.ts @@ -0,0 +1,40 @@ +import { useContext } from "react"; +import { IdeMessengerContext } from "../context/IdeMessenger"; +import { useAppDispatch, useAppSelector } from "../redux/hooks"; +import { setCompactionLoading } from "../redux/slices/sessionSlice"; +import { loadSession } from "../redux/thunks/session"; + +export const useCompactConversation = () => { + const dispatch = useAppDispatch(); + const ideMessenger = useContext(IdeMessengerContext); + const currentSessionId = useAppSelector((state) => state.session.id); + + return async (index: number) => { + if (!currentSessionId) { + return; + } + + try { + // Set loading state + dispatch(setCompactionLoading({ index, loading: true })); + + await ideMessenger.request("conversation/compact", { + index, + sessionId: currentSessionId, + }); + + // Reload the current session to refresh the conversation state + dispatch( + loadSession({ + sessionId: currentSessionId, + saveCurrentSession: false, + }), + ); + } catch (error) { + console.error("Error compacting conversation:", error); + } finally { + // Clear loading state + dispatch(setCompactionLoading({ index, loading: false })); + } + }; +}; \ No newline at end of file