File size: 1,042 Bytes
3d700dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { useMutation, useQueryClient } from "@tanstack/react-query";
import AgentServerConversationService from "#/api/conversation-service/agent-server-conversation-service.api";

interface CondenseConversationVariables {
  conversationId: string;
  conversationUrl: string | null;
  sessionApiKey: string | null;
}

/**
 * Manually compact ("condense") a conversation's context via
 * `POST /api/conversations/{id}/condense`. Refreshes the usage metrics
 * afterwards so the context meter reflects the shrunken history.
 */
export const useCondenseConversation = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (variables: CondenseConversationVariables) =>
      AgentServerConversationService.condenseConversation(
        variables.conversationId,
        variables.conversationUrl,
        variables.sessionApiKey,
      ),
    onSettled: (_, __, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["conversation-metrics", variables.conversationId],
      });
    },
  });
};