Elffuss BraidHub

kikocis/continue public

Merge pull request #6709 from continuedev/tomasz/compaction-button-ui

Tomasz/compaction button UI
Changesetf7ae11f353f9e83d6bd2e387e52fb8e753dbc9bea6f3cea690678458a258d1a1
AuthorDallin Romney <dallinromney@gmail.com> · 2025-07-18 21:01 +02:00
CommitterGitHub <noreply@github.com> · 2025-07-18 21:01 +02:00
Parents7668a8715766, 5bd6280cc559

Browse the files at this changeset

5 files changed

modified gui/src/components/StepContainer/ConversationSummary.tsx +14 −1
@@ -1,9 +1,12 @@
11 import { ChevronDownIcon, ChevronUpIcon } from "@heroicons/react/24/solid";
2+import { TrashIcon } from "@heroicons/react/24/outline";
23 import { ChatHistoryItem } from "core";
34 import { useState } from "react";
45 import { AnimatedEllipsis } from "../";
56 import { useAppSelector } from "../../redux/hooks";
67 import StyledMarkdownPreview from "../StyledMarkdownPreview";
8+import { useDeleteCompaction } from "../../util/compactConversation";
9+import HeaderButtonWithToolTip from "../gui/HeaderButtonWithToolTip";
710
811 interface ConversationSummaryProps {
912 item: ChatHistoryItem;
@@ -15,6 +18,7 @@
1518 const isLoading = useAppSelector(
1619 (state) => state.session.compactionLoading[props.index] || false,
1720 );
21+ const deleteCompaction = useDeleteCompaction();
1822
1923 if (!props.item.conversationSummary && !isLoading) {
2024 return null;
@@ -47,7 +51,16 @@
4751 ) : (
4852 <ChevronDownIcon className="h-3 w-3" />
4953 )}
50- <span>Conversation Summary</span>
54+ <span className="flex-1">Conversation Summary</span>
55+ <HeaderButtonWithToolTip
56+ text="Delete summary"
57+ onClick={(e) => {
58+ e.stopPropagation();
59+ deleteCompaction(props.index);
60+ }}
61+ >
62+ <TrashIcon className="text-description-muted h-3 w-3" />
63+ </HeaderButtonWithToolTip>
5164 </div>
5265 {open && (
5366 <>
modified gui/src/components/StepContainer/ResponseActions.tsx +40 −44
@@ -7,13 +7,11 @@
77 import { ChatHistoryItem } from "core";
88 import { modelSupportsTools } from "core/llm/autodetect";
99 import { renderChatMessage } from "core/util/messageContent";
10-import { useContext, useMemo } from "react";
11-import { IdeMessengerContext } from "../../context/IdeMessenger";
10+import { useMemo } from "react";
1211 import { useAppDispatch, useAppSelector } from "../../redux/hooks";
1312 import { selectSelectedChatModel } from "../../redux/slices/configSlice";
14-import { setCompactionLoading } from "../../redux/slices/sessionSlice";
1513 import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice";
16-import { loadSession } from "../../redux/thunks/session";
14+import { useCompactConversation } from "../../util/compactConversation";
1715 import { FeedbackButtons } from "../FeedbackButtons";
1816 import { GenerateRuleDialog } from "../GenerateRuleDialog";
1917 import { CopyIconButton } from "../gui/CopyIconButton";
@@ -37,49 +35,56 @@
3735 isLast,
3836 }: ResponseActionsProps) {
3937 const dispatch = useAppDispatch();
40- const ideMessenger = useContext(IdeMessengerContext);
41- const currentSessionId = useAppSelector((state) => state.session.id);
4238 const selectedModel = useAppSelector(selectSelectedChatModel);
39+ const contextPercentage = useAppSelector(
40+ (state) => state.session.contextPercentage,
41+ );
42+ const isPruned = useAppSelector((state) => state.session.isPruned);
4343 const ruleGenerationSupported = useMemo(() => {
4444 return selectedModel && modelSupportsTools(selectedModel);
4545 }, [selectedModel]);
4646
47+ const percent = Math.round((contextPercentage ?? 0) * 100);
48+ const buttonColorClass =
49+ isLast && (isPruned || percent > 80)
50+ ? "text-warning"
51+ : "text-description-muted";
52+
53+ const showLabel = isLast && (isPruned || percent >= 60);
54+
55+ const compactConversation = useCompactConversation();
56+
4757 const onGenerateRule = () => {
4858 dispatch(setShowDialog(true));
4959 dispatch(setDialogMessage(<GenerateRuleDialog />));
5060 };
5161
52- const onCompactConversation = async () => {
53- if (!currentSessionId) {
54- return;
55- }
56-
57- try {
58- // Set loading state
59- dispatch(setCompactionLoading({ index, loading: true }));
60-
61- await ideMessenger.request("conversation/compact", {
62- index,
63- sessionId: currentSessionId,
64- });
65-
66- // Reload the current session to refresh the conversation state
67- dispatch(
68- loadSession({
69- sessionId: currentSessionId,
70- saveCurrentSession: false,
71- }),
72- );
73- } catch (error) {
74- console.error("Error compacting conversation:", error);
75- } finally {
76- // Clear loading state
77- dispatch(setCompactionLoading({ index, loading: false }));
78- }
79- };
80-
8162 return (
8263 <div className="text-description-muted mx-2 flex cursor-default items-center justify-end space-x-1 bg-transparent pb-0 text-xs">
64+ <HeaderButtonWithToolTip
65+ testId={`compact-button-${index}`}
66+ text={
67+ showLabel
68+ ? "Summarize conversation to reduce context length"
69+ : "Compact conversation"
70+ }
71+ tabIndex={-1}
72+ onClick={() => compactConversation(index)}
73+ >
74+ <div className="flex items-center space-x-1">
75+ <ArrowsPointingInIcon
76+ className={`h-3.5 w-3.5 ${buttonColorClass || "text-description-muted"}`}
77+ />
78+ {showLabel && (
79+ <span
80+ className={`text-xs ${buttonColorClass || "text-description-muted"}`}
81+ >
82+ Compact conversation
83+ </span>
84+ )}
85+ </div>
86+ </HeaderButtonWithToolTip>
87+
8388 {isLast && ruleGenerationSupported && (
8489 <HeaderButtonWithToolTip
8590 tabIndex={-1}
@@ -100,15 +105,6 @@
100105 </HeaderButtonWithToolTip>
101106 )}
102107
103- <HeaderButtonWithToolTip
104- testId={`compact-button-${index}`}
105- text="Compact conversation"
106- tabIndex={-1}
107- onClick={onCompactConversation}
108- >
109- <ArrowsPointingInIcon className="text-description-muted h-3.5 w-3.5" />
110- </HeaderButtonWithToolTip>
111-
112108 <HeaderButtonWithToolTip
113109 testId={`delete-button-${index}`}
114110 text="Delete"
modified gui/src/components/mainInput/ContextStatus.tsx +28 −16
@@ -1,5 +1,6 @@
11 import { useAppDispatch, useAppSelector } from "../../redux/hooks";
22 import { saveCurrentSession } from "../../redux/thunks/session";
3+import { useCompactConversation } from "../../util/compactConversation";
34 import { ToolTip } from "../gui/Tooltip";
45
56 const ContextStatus = () => {
@@ -10,6 +11,8 @@
1011 const history = useAppSelector((state) => state.session.history);
1112 const percent = Math.round((contextPercentage ?? 0) * 100);
1213 const isPruned = useAppSelector((state) => state.session.isPruned);
14+
15+ const compactConversation = useCompactConversation();
1316 if (!isPruned && percent < 60) {
1417 return null;
1518 }
@@ -32,7 +35,7 @@
3235 }}
3336 clickable
3437 >
35- <div className="flex flex-col gap-0">
38+ <div className="flex flex-col gap-0 text-xs">
3639 <span className="inline-block">
3740 {`${percent}% of context filled`}
3841 </span>
@@ -42,21 +45,30 @@
4245 </span>
4346 )}
4447 {history.length > 0 && (
45- <div>
46- <span className="inline-block">Start a</span>{" "}
47- <span
48- className="inline-block cursor-pointer underline"
49- onClick={() => {
50- void dispatch(
51- saveCurrentSession({
52- openNewSession: true,
53- generateTitle: false,
54- }),
55- );
56- }}
57- >
58- New Session
59- </span>
48+ <div className="flex flex-col gap-1 whitespace-pre">
49+ <div>
50+ <span
51+ className="inline-block cursor-pointer underline"
52+ onClick={() => compactConversation(history.length - 1)}
53+ >
54+ Compact conversation
55+ </span>
56+ {"\n"}
57+ <span className="inline-block">or</span> {"\n"}
58+ <span
59+ className="inline-block cursor-pointer underline"
60+ onClick={() => {
61+ dispatch(
62+ saveCurrentSession({
63+ openNewSession: true,
64+ generateTitle: false,
65+ }),
66+ );
67+ }}
68+ >
69+ Start a new session
70+ </span>
71+ </div>
6072 </div>
6173 )}
6274 </div>
modified gui/src/redux/slices/sessionSlice.ts +11 −0
@@ -445,6 +445,16 @@
445445 state.isPruned = false;
446446 state.contextPercentage = undefined;
447447 },
448+ deleteCompaction: (state, action: PayloadAction<number>) => {
449+ // Removes the conversation summary from the specified message
450+ const historyItem = state.history[action.payload];
451+ if (historyItem?.conversationSummary) {
452+ state.history[action.payload] = {
453+ ...historyItem,
454+ conversationSummary: undefined,
455+ };
456+ }
457+ },
448458 updateHistoryItemAtIndex: (
449459 state,
450460 {
@@ -1000,6 +1010,7 @@
10001010 clearDanglingMessages,
10011011 setMainEditorContentTrigger,
10021012 deleteMessage,
1013+ deleteCompaction,
10031014 setIsGatheringContext,
10041015 resetNextCodeBlockToApplyIndex,
10051016 updateApplyState,
added gui/src/util/compactConversation.ts +58 −0
@@ -0,0 +1,58 @@
1+import { useContext } from "react";
2+import { IdeMessengerContext } from "../context/IdeMessenger";
3+import { useAppDispatch, useAppSelector } from "../redux/hooks";
4+import {
5+ setCompactionLoading,
6+ deleteCompaction,
7+} from "../redux/slices/sessionSlice";
8+import { loadSession, saveCurrentSession } from "../redux/thunks/session";
9+
10+export const useCompactConversation = () => {
11+ const dispatch = useAppDispatch();
12+ const ideMessenger = useContext(IdeMessengerContext);
13+ const currentSessionId = useAppSelector((state) => state.session.id);
14+
15+ return async (index: number) => {
16+ if (!currentSessionId) {
17+ return;
18+ }
19+
20+ try {
21+ // Set loading state
22+ dispatch(setCompactionLoading({ index, loading: true }));
23+
24+ await ideMessenger.request("conversation/compact", {
25+ index,
26+ sessionId: currentSessionId,
27+ });
28+
29+ // Reload the current session to refresh the conversation state
30+ dispatch(
31+ loadSession({
32+ sessionId: currentSessionId,
33+ saveCurrentSession: false,
34+ }),
35+ );
36+ } catch (error) {
37+ console.error("Error compacting conversation:", error);
38+ } finally {
39+ // Clear loading state
40+ dispatch(setCompactionLoading({ index, loading: false }));
41+ }
42+ };
43+};
44+
45+export const useDeleteCompaction = () => {
46+ const dispatch = useAppDispatch();
47+
48+ return (index: number) => {
49+ // Update local state and save to persistence
50+ dispatch(deleteCompaction(index));
51+ dispatch(
52+ saveCurrentSession({
53+ openNewSession: false,
54+ generateTitle: false,
55+ }),
56+ );
57+ };
58+};