Merge pull request #6709 from continuedev/tomasz/compaction-button-ui
Tomasz/compaction button UI
This commit is contained in:
commit
2e11347e25
5 changed files with 151 additions and 61 deletions
|
|
@ -1,9 +1,12 @@
|
|||
import { ChevronDownIcon, ChevronUpIcon } from "@heroicons/react/24/solid";
|
||||
import { TrashIcon } from "@heroicons/react/24/outline";
|
||||
import { ChatHistoryItem } from "core";
|
||||
import { useState } from "react";
|
||||
import { AnimatedEllipsis } from "../";
|
||||
import { useAppSelector } from "../../redux/hooks";
|
||||
import StyledMarkdownPreview from "../StyledMarkdownPreview";
|
||||
import { useDeleteCompaction } from "../../util/compactConversation";
|
||||
import HeaderButtonWithToolTip from "../gui/HeaderButtonWithToolTip";
|
||||
|
||||
interface ConversationSummaryProps {
|
||||
item: ChatHistoryItem;
|
||||
|
|
@ -15,6 +18,7 @@ export default function ConversationSummary(props: ConversationSummaryProps) {
|
|||
const isLoading = useAppSelector(
|
||||
(state) => state.session.compactionLoading[props.index] || false,
|
||||
);
|
||||
const deleteCompaction = useDeleteCompaction();
|
||||
|
||||
if (!props.item.conversationSummary && !isLoading) {
|
||||
return null;
|
||||
|
|
@ -47,7 +51,16 @@ export default function ConversationSummary(props: ConversationSummaryProps) {
|
|||
) : (
|
||||
<ChevronDownIcon className="h-3 w-3" />
|
||||
)}
|
||||
<span>Conversation Summary</span>
|
||||
<span className="flex-1">Conversation Summary</span>
|
||||
<HeaderButtonWithToolTip
|
||||
text="Delete summary"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
deleteCompaction(props.index);
|
||||
}}
|
||||
>
|
||||
<TrashIcon className="text-description-muted h-3 w-3" />
|
||||
</HeaderButtonWithToolTip>
|
||||
</div>
|
||||
{open && (
|
||||
<>
|
||||
|
|
|
|||
|
|
@ -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,49 +35,56 @@ 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,
|
||||
);
|
||||
const isPruned = useAppSelector((state) => state.session.isPruned);
|
||||
const ruleGenerationSupported = useMemo(() => {
|
||||
return selectedModel && modelSupportsTools(selectedModel);
|
||||
}, [selectedModel]);
|
||||
|
||||
const percent = Math.round((contextPercentage ?? 0) * 100);
|
||||
const buttonColorClass =
|
||||
isLast && (isPruned || percent > 80)
|
||||
? "text-warning"
|
||||
: "text-description-muted";
|
||||
|
||||
const showLabel = isLast && (isPruned || percent >= 60);
|
||||
|
||||
const compactConversation = useCompactConversation();
|
||||
|
||||
const onGenerateRule = () => {
|
||||
dispatch(setShowDialog(true));
|
||||
dispatch(setDialogMessage(<GenerateRuleDialog />));
|
||||
};
|
||||
|
||||
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 (
|
||||
<div className="text-description-muted mx-2 flex cursor-default items-center justify-end space-x-1 bg-transparent pb-0 text-xs">
|
||||
<HeaderButtonWithToolTip
|
||||
testId={`compact-button-${index}`}
|
||||
text={
|
||||
showLabel
|
||||
? "Summarize conversation to reduce context length"
|
||||
: "Compact conversation"
|
||||
}
|
||||
tabIndex={-1}
|
||||
onClick={() => compactConversation(index)}
|
||||
>
|
||||
<div className="flex items-center space-x-1">
|
||||
<ArrowsPointingInIcon
|
||||
className={`h-3.5 w-3.5 ${buttonColorClass || "text-description-muted"}`}
|
||||
/>
|
||||
{showLabel && (
|
||||
<span
|
||||
className={`text-xs ${buttonColorClass || "text-description-muted"}`}
|
||||
>
|
||||
Compact conversation
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</HeaderButtonWithToolTip>
|
||||
|
||||
{isLast && ruleGenerationSupported && (
|
||||
<HeaderButtonWithToolTip
|
||||
tabIndex={-1}
|
||||
|
|
@ -100,15 +105,6 @@ export default function ResponseActions({
|
|||
</HeaderButtonWithToolTip>
|
||||
)}
|
||||
|
||||
<HeaderButtonWithToolTip
|
||||
testId={`compact-button-${index}`}
|
||||
text="Compact conversation"
|
||||
tabIndex={-1}
|
||||
onClick={onCompactConversation}
|
||||
>
|
||||
<ArrowsPointingInIcon className="text-description-muted h-3.5 w-3.5" />
|
||||
</HeaderButtonWithToolTip>
|
||||
|
||||
<HeaderButtonWithToolTip
|
||||
testId={`delete-button-${index}`}
|
||||
text="Delete"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { useAppDispatch, useAppSelector } from "../../redux/hooks";
|
||||
import { saveCurrentSession } from "../../redux/thunks/session";
|
||||
import { useCompactConversation } from "../../util/compactConversation";
|
||||
import { ToolTip } from "../gui/Tooltip";
|
||||
|
||||
const ContextStatus = () => {
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -32,7 +35,7 @@ const ContextStatus = () => {
|
|||
}}
|
||||
clickable
|
||||
>
|
||||
<div className="flex flex-col gap-0">
|
||||
<div className="flex flex-col gap-0 text-xs">
|
||||
<span className="inline-block">
|
||||
{`${percent}% of context filled`}
|
||||
</span>
|
||||
|
|
@ -42,21 +45,30 @@ const ContextStatus = () => {
|
|||
</span>
|
||||
)}
|
||||
{history.length > 0 && (
|
||||
<div>
|
||||
<span className="inline-block">Start a</span>{" "}
|
||||
<span
|
||||
className="inline-block cursor-pointer underline"
|
||||
onClick={() => {
|
||||
void dispatch(
|
||||
saveCurrentSession({
|
||||
openNewSession: true,
|
||||
generateTitle: false,
|
||||
}),
|
||||
);
|
||||
}}
|
||||
>
|
||||
New Session
|
||||
</span>
|
||||
<div className="flex flex-col gap-1 whitespace-pre">
|
||||
<div>
|
||||
<span
|
||||
className="inline-block cursor-pointer underline"
|
||||
onClick={() => compactConversation(history.length - 1)}
|
||||
>
|
||||
Compact conversation
|
||||
</span>
|
||||
{"\n"}
|
||||
<span className="inline-block">or</span> {"\n"}
|
||||
<span
|
||||
className="inline-block cursor-pointer underline"
|
||||
onClick={() => {
|
||||
dispatch(
|
||||
saveCurrentSession({
|
||||
openNewSession: true,
|
||||
generateTitle: false,
|
||||
}),
|
||||
);
|
||||
}}
|
||||
>
|
||||
Start a new session
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -445,6 +445,16 @@ export const sessionSlice = createSlice({
|
|||
state.isPruned = false;
|
||||
state.contextPercentage = undefined;
|
||||
},
|
||||
deleteCompaction: (state, action: PayloadAction<number>) => {
|
||||
// Removes the conversation summary from the specified message
|
||||
const historyItem = state.history[action.payload];
|
||||
if (historyItem?.conversationSummary) {
|
||||
state.history[action.payload] = {
|
||||
...historyItem,
|
||||
conversationSummary: undefined,
|
||||
};
|
||||
}
|
||||
},
|
||||
updateHistoryItemAtIndex: (
|
||||
state,
|
||||
{
|
||||
|
|
@ -1000,6 +1010,7 @@ export const {
|
|||
clearDanglingMessages,
|
||||
setMainEditorContentTrigger,
|
||||
deleteMessage,
|
||||
deleteCompaction,
|
||||
setIsGatheringContext,
|
||||
resetNextCodeBlockToApplyIndex,
|
||||
updateApplyState,
|
||||
|
|
|
|||
58
gui/src/util/compactConversation.ts
Normal file
58
gui/src/util/compactConversation.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import { useContext } from "react";
|
||||
import { IdeMessengerContext } from "../context/IdeMessenger";
|
||||
import { useAppDispatch, useAppSelector } from "../redux/hooks";
|
||||
import {
|
||||
setCompactionLoading,
|
||||
deleteCompaction,
|
||||
} from "../redux/slices/sessionSlice";
|
||||
import { loadSession, saveCurrentSession } 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 }));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const useDeleteCompaction = () => {
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return (index: number) => {
|
||||
// Update local state and save to persistence
|
||||
dispatch(deleteCompaction(index));
|
||||
dispatch(
|
||||
saveCurrentSession({
|
||||
openNewSession: false,
|
||||
generateTitle: false,
|
||||
}),
|
||||
);
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue