Elffuss BraidHub

kikocis/continue public

main · 1.6 KiBRawPermalink
1import { useContext } from "react";
2import { IdeMessengerContext } from "../context/IdeMessenger";
3import { useAppDispatch, useAppSelector } from "../redux/hooks";
4import {
5 setCompactionLoading,
6 deleteCompaction,
7} from "../redux/slices/sessionSlice";
8import { loadSession, saveCurrentSession } from "../redux/thunks/session";
9
10export 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
45export 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};