feat: compaction

This commit is contained in:
Tomasz Stefaniak 2025-07-18 09:42:59 -07:00
parent 3edb844d93
commit 5b854675ea
3 changed files with 72 additions and 50 deletions

View file

@ -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(<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">
@ -100,7 +70,7 @@ export default function ResponseActions({
: "Compact conversation"
}
tabIndex={-1}
onClick={onCompactConversation}
onClick={() => compactConversation(index)}
>
<div className="flex items-center space-x-1">
<ArrowsPointingInIcon

View file

@ -1,6 +1,7 @@
import { Tooltip } from "react-tooltip";
import { useAppDispatch, useAppSelector } from "../../redux/hooks";
import { saveCurrentSession } from "../../redux/thunks/session";
import { useCompactConversation } from "../../util/compactConversation";
const ContextStatus = () => {
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 = () => {
</span>
)}
{history.length > 0 && (
<div>
<span className="inline-block">Start a</span>{" "}
<span
className="inline-block cursor-pointer underline"
onClick={() => {
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>

View file

@ -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 }));
}
};
};